Loading...
Searching...
No Matches
PseudoTuple.h
1/*
2
3MIT License
4
5Copyright (c) 2017 FMI Open Development / Markus Peura, first.last@fmi.fi
6
7Permission is hereby granted, free of charge, to any person obtaining a copy
8of this software and associated documentation files (the "Software"), to deal
9in the Software without restriction, including without limitation the rights
10to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11copies of the Software, and to permit persons to whom the Software is
12furnished to do so, subject to the following conditions:
13
14The above copyright notice and this permission notice shall be included in all
15copies or substantial portions of the Software.
16
17THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23SOFTWARE.
24
25*/
26/*
27Part of Rack development has been done in the BALTRAD projects part-financed
28by the European Union (European Regional Development Fund and European
29Neighbourhood Partnership Instrument, Baltic Sea Region Programme 2007-2013)
30*/
31
32
33#ifndef DRAIN_UNITUPLE_WRAPPER
34#define DRAIN_UNITUPLE_WRAPPER
35
36#include <cstddef>
37#include <iostream>
38#include <sstream>
39#include <typeinfo>
40#include <stdexcept>
41#include <string>
42//#include <set>
43
44#include <drain/StringBuilder.h>
45#include <drain/Type.h> // Utils
46// lower == cannot use Sprinter, as long as SprinterLayouit uses UniTuple...
47// #include <drain/VariableT.h> lower
48//#include <drain/Sprinter.h>
49#include "TupleBase.h"
50
51namespace drain {
52
53
54/***
55 * \tparam C - base class
56 * \tparam S - storage class of members (int, double, char)
57 * \tparam N - number of the members included
58 */
59template <class C, typename T=typename C::value_t, size_t N=sizeof(C)/sizeof(T)>
60class PseudoTuple : public C, public TupleBase<T,N> {
61
62public:
63
64 typedef T* iterator;
65 typedef T const* const_iterator;
66
67 static const size_t baseTypeSize;
68
69 const size_t elementCount;
70
71
72 inline
73 PseudoTuple() : elementCount(N>0 ? N : baseTypeSize/TupleBase<T,N>::storageTypeSize) {
74 // std::cerr << N << '*' << TupleBase<T,N>::storageTypeSize << " ... " << baseTypeSize << '\n';
75 if (N*TupleBase<T,N>::storageTypeSize > baseTypeSize){
76 throw std::runtime_error(StringBuilder<>(__FUNCTION__, ": conflicting geometry ", N, 'x',
77 TupleBase<T,N>::storageTypeSize," in addressing base class ", TypeName<C>::str(), " size=", baseTypeSize));
78 }
79 };
80
81 virtual inline
82 const_iterator begin() const {
83 return static_cast<const_iterator>((void *)(const C *)this); //
84 }
85
86 virtual inline
87 const_iterator end() const {
88 return static_cast<const_iterator>((void *)(const C *)this) + N; //
89 }
90
91 virtual inline
92 iterator begin(){
93 return static_cast<iterator>((void *)(const C *)this);
94 }
95
96 virtual inline
97 iterator end(){
98 return static_cast<iterator>((void *)(const C *)this) + N;
99 }
100
101
102protected:
103
104};
105
106template <class C, typename T, size_t N>
107const size_t PseudoTuple<C,T,N>::baseTypeSize = sizeof(C);
108
109
110
111template <class C, typename T, size_t N>
112struct TypeName<PseudoTuple<C,T,N> > {
113
114 static const std::string & str(){
115 if (N == 0){
116 static const std::string name = drain::StringBuilder<>("PseudoTuple<", drain::TypeName<C>::str(),',' , drain::TypeName<T>::str(),">");
117 return name;
118 }
119 else {
120 static const std::string name = drain::StringBuilder<>("PseudoTuple<", drain::TypeName<C>::str(),',' , drain::TypeName<T>::str(),',' , N, ">");
121 return name;
122 }
123 }
124
125};
126
127/*
128template <class T, size_t N>
129std::ostream & operator<<(std::ostream & ostr, const UniTuple<T,N> & tuple){
130 return tuple.toStream(ostr);
131}
132*/
133
134
135
136
137/*
138 // NOTE: cannot use Sprinter, as long as SprinterLayout uses UniTuple...
139template <class T, size_t N>
140std::ostream & Sprinter::toStream(std::ostream & ostr, const UniTuple<T,N> & tuple, const SprinterLayout & layout) {
141 return Sprinter::sequenceToStream(ostr, tuple, layout);
142}
143*/
144
145} // drain
146
147
148#endif
Definition PseudoTuple.h:60
Definition StringBuilder.h:58
Definition TupleBase.h:75
Definition DataSelector.cpp:1277
Definition Type.h:542
static const std::string name
Default implementation: name returned by std::type_info::name()
Definition Type.h:558