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/Type.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
99
100 template<typename S>
101 inline
102 UniTuple(std::initializer_list<S> l) : start(this->arr), init(nullptr){ // start(this->arr),
103 this->set(l);
104 };
105
106
107
108 virtual inline
109 ~UniTuple(){};
110
111 tuple_t & operator=(const tuple_t &t){
112 if (&t != this){ // this check should be unneeded, as it is in assign().
113 this->assignSequence(t);
114 }
115 return *this;
116 }
117
118 tuple_t & operator=(const value_type & value){
119 this->assign(value);
120 return *this;
121 }
122
123
124 template<typename S>
125 tuple_t & operator=(std::initializer_list<S> l){
126 this->assignSequence(l);
127 return *this;
128 }
129
130 // Experimental
131 template<typename S>
132 inline
133 tuple_t & operator*=(S arg){
134 /*
135 for (iterator it = begin(); it != end(); ++it){
136 *it = i;
137 }
138 */
139 for (T & x: *this){
140 x *= arg;
141 }
142 return *this;
143 }
144
145
146
147 virtual inline
148 const_iterator begin() const override final {
149 return start; // this->arr
150 // return this->arr; //start;
151 }
152
153 virtual inline
154 const_iterator end() const override final {
155 return start + N; // this->arr
156 // return this->arr + N;
157 }
158
159 virtual inline
160 iterator begin() override final {
161 return start; // this->arr
162 // return this->arr; //start;
163 //return (iterator)this;
164 }
165
166 virtual inline
167 iterator end() override final {
168 return start + N; // this->arr
169 // return (iterator)this + N;
170 }
171
172
173 // Self-reference for casting
174 inline
175 const tuple_t & tuple() const{
176 return *this;
177 }
178
179
180 // Self-reference for casting
181 inline
182 tuple_t & tuple(){
183 return *this;
184 }
185
186 template<typename ... TT>
187 inline
188 tuple_t & tuple(const TT &... args){
189 // tuple_t & tuple(const T & arg, const TT &... rest){
190 // set(arg, rest);
191 this->set(args...);
192 return *this;
193 }
194
195
196
197
198 void debug(std::ostream & ostr) const {
199 ostr << "UniTuple<" << typeid(T).name() << sizeof(T) << ',' << N << ">: {" << *this << '}';
200 }
201
202
203protected:
204
205 const iterator start;
206
207
208private:
209
210 // Utility for initializing references is derived classes Point(x,y): x(init), y(++init),
211 iterator init;
212
213protected:
214
215 T & next(){
216 if (init==nullptr){
217 //std::cerr << __FILE__ << ':' << __FUNCTION__ << "warning: null ptr" << *this << std::endl;
218 return *(init=begin());
219 }
220 else if (init==end()){
221 std::cerr << __FILE__ << ':' << __FUNCTION__ << "warning: exceeded inits: " << *this << std::endl;
222 return *(init=begin());
223 }
224 return *(++init);
225 }
226
227
228 // Parasite
229
230 template <size_t N2>
231 UniTuple(UniTuple<T,N2> &tuple, size_t i): start(tuple.begin()+i), init(nullptr){ // 2023/04/24
232 if ((i+N)> N2){
233 throw std::runtime_error(drain::StringBuilder<>(drain::TypeName<UniTuple<T,N2> >::str(), "(", tuple, "): constructor index[", i,"] overflow with referenced tuple") );
234 }
235 };
236
237
238
239private:
240
242 T arr[N];
243
244};
245
246/*
247template <class T, size_t N>
248std::ostream & operator<<(std::ostream & ostr, const UniTuple<T,N> & tuple){
249 return tuple.toStream(ostr);
250}
251*/
252
253
254template <class T, size_t N>
255struct TypeName<UniTuple<T,N> > {
256
257 static const std::string & str(){
258 static const std::string name = drain::StringBuilder<>("UniTuple<", drain::TypeName<T>::str(), ',', N, ">");
259 return name;
260 }
261
262};
263
264
265/*
266 // NOTE: cannot use Sprinter, as long as SprinterLayout uses UniTuple...
267template <class T, size_t N>
268std::ostream & Sprinter::toStream(std::ostream & ostr, const UniTuple<T,N> & tuple, const SprinterLayout & layout) {
269 return Sprinter::sequenceToStream(ostr, tuple, layout);
270}
271*/
272
273} // drain
274
275
276#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 Type.h:541
static const std::string name
Default implementation: name returned by std::type_info::name()
Definition Type.h:549