Loading...
Searching...
No Matches
UniTuple.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
34#define DRAIN_UNITUPLE
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/TupleBase.h>
46#include <drain/TypeName.h> // Utils
47// #include <drain/Sprinter.h> lower == cannot use Sprinter, as long as SprinterLayouit uses UniTuple...
48// #include <drain/VariableT.h> lower
49
50namespace drain {
51
52
53
54//class VariableT<VariableInitializer<VariableBase> >;
55//class VariableT<VariableInitializer<ReferenceT<VariableBase> > >;
56//class VariableT<ReferenceT<Castable> >;
57
58
60
64template <class T, size_t N=2>
65class UniTuple : public TupleBase<T,N> {
66
67
68public:
69
70 typedef T value_type; // why value_type, not value_t (ok, STL also )
71 typedef UniTuple<T,N> tuple_t;
72 static const size_t tuple_size = N;
73
74 typedef T* iterator;
75 typedef const T* const_iterator;
76
77
78 inline
79 UniTuple() : start(this->arr), init(nullptr){ // start(this->arr),
81 };
82
83
84 template<typename ... TT>
85 inline
86 UniTuple(const TT &... args) : start(this->arr), init(nullptr){ //
88 this->set(args...);
89 }
90
91
93 inline
94 UniTuple(const UniTuple<T,N> & t) : start(this->arr), init(nullptr){ // start(this->arr),
95 this->set(t);
96 };
97
98 template<typename S>
99 inline
100 UniTuple(const std::vector<S> & v) : start(this->arr), init(nullptr){
101 this->assignSequence(v);
102 };
103
104
105 template<typename S>
106 inline
107 UniTuple(std::initializer_list<S> l) : start(this->arr), init(nullptr){ // start(this->arr),
108 this->set(l);
109 };
110
111
112
113 virtual inline
114 ~UniTuple(){};
115
116 tuple_t & operator=(const tuple_t &t){
117 if (&t != this){ // this check should be unneeded, as it is in assign().
118 this->assignSequence(t);
119 }
120 return *this;
121 }
122
123 tuple_t & operator=(const value_type & value){
124 this->assign(value);
125 return *this;
126 }
127
128
129 template<typename S>
130 tuple_t & operator=(std::initializer_list<S> l){
131 this->assignSequence(l);
132 return *this;
133 }
134
135 // Experimental
136 template<typename S>
137 inline
138 tuple_t & operator*=(S arg){
139 /*
140 for (iterator it = begin(); it != end(); ++it){
141 *it = i;
142 }
143 */
144 for (T & x: *this){
145 x *= arg;
146 }
147 return *this;
148 }
149
150
151
152 virtual inline
153 const_iterator begin() const override final {
154 return start; // this->arr
155 // return this->arr; //start;
156 }
157
158 virtual inline
159 const_iterator end() const override final {
160 return start + N; // this->arr
161 // return this->arr + N;
162 }
163
164 virtual inline
165 iterator begin() override final {
166 return start; // this->arr
167 // return this->arr; //start;
168 //return (iterator)this;
169 }
170
171 virtual inline
172 iterator end() override final {
173 return start + N; // this->arr
174 // return (iterator)this + N;
175 }
176
177
178 // Self-reference for casting
179 inline
180 const tuple_t & tuple() const{
181 return *this;
182 }
183
184
185 // Self-reference for casting
186 inline
187 tuple_t & tuple(){
188 return *this;
189 }
190
191 template<typename ... TT>
192 inline
193 tuple_t & tuple(const TT &... args){
194 // tuple_t & tuple(const T & arg, const TT &... rest){
195 // set(arg, rest);
196 this->set(args...);
197 return *this;
198 }
199
200
201
202
203 void debug(std::ostream & ostr) const {
204 ostr << "UniTuple<" << typeid(T).name() << sizeof(T) << ',' << N << ">: {" << *this << '}';
205 }
206
207
208protected:
209
210 const iterator start;
211
212
213private:
214
215 // Utility for initializing references is derived classes Point(x,y): x(init), y(++init),
216 iterator init;
217
218protected:
219
220 T & next(){
221 if (init==nullptr){
222 //std::cerr << __FILE__ << ':' << __FUNCTION__ << "warning: null ptr" << *this << std::endl;
223 return *(init=begin());
224 }
225 else if (init==end()){
226 std::cerr << __FILE__ << ':' << __FUNCTION__ << "warning: exceeded inits: " << *this << std::endl;
227 return *(init=begin());
228 }
229 return *(++init);
230 }
231
232
233 // Parasite
234
235 template <size_t N2>
236 UniTuple(UniTuple<T,N2> &tuple, size_t i): start(tuple.begin()+i), init(nullptr){ // 2023/04/24
237 if ((i+N)> N2){
238 throw std::runtime_error(drain::StringBuilder<>(drain::TypeName<UniTuple<T,N2> >::str(), "(", tuple, "): constructor index[", i,"] overflow with referenced tuple") );
239 }
240 };
241
242
243
244private:
245
247 T arr[N];
248
249};
250
251/*
252template <class T, size_t N>
253std::ostream & operator<<(std::ostream & ostr, const UniTuple<T,N> & tuple){
254 return tuple.toStream(ostr);
255}
256*/
257
258
259template <class T, size_t N>
260struct TypeName<UniTuple<T,N> > {
261
262 static const std::string & str(){
263 static const std::string name = drain::StringBuilder<>("UniTuple<", drain::TypeName<T>::str(), ',', N, ">");
264 return name;
265 }
266
267};
268
269
270/*
271 // NOTE: cannot use Sprinter, as long as SprinterLayout uses UniTuple...
272template <class T, size_t N>
273std::ostream & Sprinter::toStream(std::ostream & ostr, const UniTuple<T,N> & tuple, const SprinterLayout & layout) {
274 return Sprinter::sequenceToStream(ostr, tuple, layout);
275}
276*/
277
278} // drain
279
280
281#endif
Definition StringBuilder.h:58
Definition TupleBase.h:75
void fill(S i)
Set all the elements to i.
Definition TupleBase.h:315
tuplebase_t & assignSequence(T &sequence, bool LENIENT=false)
Proposed for tuples only; derived classes should not shadow this.
Definition TupleBase.h:287
Tuple of N elements of type T.
Definition UniTuple.h:65
UniTuple(const UniTuple< T, N > &t)
Copy constructor.
Definition UniTuple.h:94
Definition DataSelector.cpp:1277
Definition TupleBase.h:52
Default implementation.
Definition TypeName.h:57