Loading...
Searching...
No Matches
VariableT.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
34#ifndef DRAIN_VARIABLE_T
35#define DRAIN_VARIABLE_T
36
37#include <drain/Castable.h>
38#include <drain/UniTuple.h> // "Friend class"
39
40namespace drain {
41
42//class Variable;
43//class FlexibleVariable;
44//class Reference;
45
46
48
86template <class T>
87class VariableT : public T {
88
89public:
90
91 typedef T varbase_t;
92
93 typedef std::pair<const char *,const drain::VariableT<T> > init_pair_t;
94
96 inline
98
100 inline
101 VariableT(const VariableT<T> & v) : T(v) {
102 this->init(v);
103 };
104
106
109 template <class ...TT>
110 inline
111 VariableT(const TT & ...args) {
112 // std::cerr << __FILE__ << ':' << __LINE__ << ':' << __FUNCTION__ << "( variadics )" << '[' << typeid(VariableT<T>).name() << ']'<< std::endl;
113 // std::cerr << __FILE__ << ':' << __LINE__ << ':' << __FUNCTION__ << " " << value << std::endl;
114 this->init(args...); // invokes T::init(...)
115 }
116
117
119 // VariableT(const VariableT<T> & v);
120 // TODO: CHECK: -Wdeprecated-copy fires because operator=(const VariableT<T>&) below is
121 // user-declared but no copy constructor is; the implicit one is used instead (and is relied
122 // on, per the note above: an explicit copy constructor was tried and rejected because it
123 // intercepted calls meant for the variadic template constructor's init(args...) path).
124 // Confirm the implicit copy constructor's plain member/base copy is what copies actually need.
125
126
127
129
132 template <class D>
133 inline
134 VariableT(D & arg) {
135 this->init(arg); // invokes T::init(...)
136 }
137
138
139
141
143 virtual
144 bool requestType(const std::type_info & t);
145
146
148
153 virtual
154 bool suggestType(const std::type_info & t);
155
156
158
166 virtual
167 bool requestSize(size_t elementCount);
168
170
173 inline
174 VariableT & operator=(const VariableT<T> &x){ // VariableT<T>
175 this->assignCastable(x);
176 // this->append(7); // DEBUG
177 return *this;
178 }
179
181
184 template <class T2>
185 inline
186 VariableT & operator=(const VariableT<T2> &x){ // VariableT<T>
187 this->assignCastable(x);
188 // this->append(5); // DEBUG
189 return *this;
190 }
191
193 // This will never be called?
194 inline
195 VariableT & operator=(const T &x){
196 this->assignCastable(x);
197 std::cerr << __FILE__ << ':' << __FUNCTION__ << ": ? for inner class: "<< TypeName<T>::str() << std::endl;
198 //this->append("Inner");
199 return *this;
200 }
201
203
206 inline
208 this->assignCastable(x);
209 return *this;
210 }
211
212
214 template <class T2>
215 inline
216 VariableT & operator=(const T2 &x){
217 this->assign(x);
218 return *this;
219 }
220
222 template <class T2>
223 inline
224 VariableT & operator=(std::initializer_list<T2> l){
225 this->assignContainer(l, false);
226 return *this;
227 }
228
230
235 inline
236 VariableT & operator=(const char *x){
237 this->assign(x);
238 return *this;
239 }
240
241 // Could be removed ? Handled by assign<T>() ?
242 template <class T2, size_t N>
243 inline
244 VariableT &operator=(const UniTuple<T2,N> & unituple){
245 this->assignContainer(unituple);
246 return *this;
247 }
248
250 /*
251 template <class T2>
252 inline
253 T2 get() const {
254 //this->caster.get<double>();
255 return T2();
256 }
257 */
258
259
260 template <class T2>
261 inline
262 bool operator==(const VariableT<T2> & v) const {
263 return Castable::operator==((const Castable &) v);
264 }
265
266 template <class T2>
267 inline
268 bool operator==(const T2 &x) const {
269 return Castable::operator==(x);
270 }
271
272 inline
273 bool operator==(const char *s) const {
274 return Castable::operator==(s);
275 }
276
277 template <class T2>
278 inline
279 bool operator!=(const VariableT<T2> & v) const{
280 return ! Castable::operator==((const Castable &) v);
281 }
282
283 template <class T2>
284 inline
285 bool operator!=(const T2 &x) const {
286 return ! Castable::operator==(x);
287 }
288
289 inline
290 bool operator!=(const char *x) const {
291 // std::cerr << __FILE__ << ':' << __LINE__ << ':' << __FUNCTION__ << " type: " << typeid(x).name() << std::endl;
292 return ! Castable::operator==(x);
293 }
294
295
296 // Default
297 inline
298 void info(std::ostream & ostr = std::cout) const {
299 Castable::info(ostr);
300 }
301
302protected:
303
304 /*
305 static inline
306 bool compareByteByByte(const Castable & c1, const Castable & c2){
307
308 if (c1.getType() != c2.getType())
309 return false;
310
311 if (c1.getElementSize() != c2.getElementSize())
312 return false;
313
314 if (c1.getElementCount() != c2.getElementCount())
315 return false;
316
317 //const char *c1 = c1.getPtr();
318 for (size_t i = 0; i<c1.getElementCount(); ++i){
319 if (*c1.getPtr(i) != *c2.getPtr(i))
320 return false;
321 }
322
323 return true;
324 }
325 */
326
327};
328
329
330
331} // drain
332
333#endif
Definition Castable.h:76
virtual void info(std::ostream &ostr=std::cout) const
Print value, type and element count.
Definition Castable.cpp:244
Tuple of N elements of type T.
Definition UniTuple.h:65
VariableT is a final class applied through typedefs Variable, Reference and FlexibleVariable.
Definition VariableT.h:87
VariableT(D &arg)
C++ bug? : Copy constructor will not catch.
Definition VariableT.h:134
VariableT & operator=(const T2 &x)
General assignment operator.
Definition VariableT.h:216
VariableT()
Default constructor.
Definition VariableT.h:97
VariableT & operator=(const VariableT< T2 > &x)
Assignment of objects of similar classes.
Definition VariableT.h:186
void info(std::ostream &ostr=std::cout) const
Redefine for "ambivalent" FlexibleVariable: indicate if local or ref.
Definition VariableT.h:298
virtual bool suggestType(const std::type_info &t)
Sets type, only if unset, and object is not a Reference.
Definition FlexibleVariable.cpp:50
VariableT & operator=(const T &x)
Assignment of base classes (VariableLike or Castable)
Definition VariableT.h:195
virtual bool requestType(const std::type_info &t)
Sets or changes the type if possible - that is, object is not a Reference.
Definition FlexibleVariable.cpp:38
VariableT & operator=(const char *x)
Assignment of C strings. [Obligatory].
Definition VariableT.h:236
bool operator==(const VariableT< T2 > &v) const
debugging
Definition VariableT.h:262
virtual bool requestSize(size_t elementCount)
Change the array size, if For Castable (and Reference) does nothing and returns false.
Definition FlexibleVariable.cpp:60
VariableT & operator=(const Castable &x)
Assignment of objects of the same class.
Definition VariableT.h:207
VariableT & operator=(std::initializer_list< T2 > l)
General assignment operator.
Definition VariableT.h:224
VariableT & operator=(const VariableT< T > &x)
Assignment of objects of the same class.
Definition VariableT.h:174
VariableT(const VariableT< T > &v)
Copy constructor.
Definition VariableT.h:101
VariableT(const TT &...args)
Single constructor template, forwarded to init(args...) defined in base classes.
Definition VariableT.h:111
Definition Data.h:1473
static const std::string & str()
Default implementation: name returned by std::type_info::name()
Definition TypeName.h:68