VariableT.h
1 /*
2 
3 MIT License
4 
5 Copyright (c) 2017 FMI Open Development / Markus Peura, first.last@fmi.fi
6 
7 Permission is hereby granted, free of charge, to any person obtaining a copy
8 of this software and associated documentation files (the "Software"), to deal
9 in the Software without restriction, including without limitation the rights
10 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11 copies of the Software, and to permit persons to whom the Software is
12 furnished to do so, subject to the following conditions:
13 
14 The above copyright notice and this permission notice shall be included in all
15 copies or substantial portions of the Software.
16 
17 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23 SOFTWARE.
24 
25 */
26 /*
27 Part of Rack development has been done in the BALTRAD projects part-financed
28 by the European Union (European Regional Development Fund and European
29 Neighbourhood 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 
40 namespace drain {
41 
42 //class Variable;
43 //class FlexibleVariable;
44 //class Reference;
45 
46 
48 
86 template <class T>
87 class VariableT : public T {
88 
89 public:
90 
91  typedef T varbase_t;
92 
93  typedef std::pair<const char *,const drain::VariableT<T> > init_pair_t;
94 
96 
99  template <class ...TT>
100  inline
101  VariableT(const TT & ...args) {
102  // std::cerr << __FILE__ << ':' << __LINE__ << ':' << __FUNCTION__ << "( variadics )" << '[' << typeid(VariableT<T>).name() << ']'<< std::endl;
103  // std::cerr << __FILE__ << ':' << __LINE__ << ':' << __FUNCTION__ << " " << value << std::endl;
104  this->init(args...); // invokes T::init(...)
105  }
106 
108  // VariableT(const VariableT<T> & v);
109 
110 
111 
113 
116  template <class D>
117  inline
118  VariableT(D & arg) {
119  this->init(arg); // invokes T::init(...)
120  }
121 
122 
123 
125 
127  virtual
128  bool requestType(const std::type_info & t);
129 
130 
132 
137  virtual
138  bool suggestType(const std::type_info & t);
139 
140 
142 
150  virtual
151  bool requestSize(size_t elementCount);
152 
154 
157  inline
158  VariableT & operator=(const VariableT<T> &x){ // VariableT<T>
159  this->assignCastable(x);
160  // this->append(7); // DEBUG
161  return *this;
162  }
163 
165 
168  template <class T2>
169  inline
170  VariableT & operator=(const VariableT<T2> &x){ // VariableT<T>
171  this->assignCastable(x);
172  // this->append(5); // DEBUG
173  return *this;
174  }
175 
177  // This will never be called?
178  inline
179  VariableT & operator=(const T &x){
180  this->assignCastable(x);
181  std::cerr << __FILE__ << ':' << __FUNCTION__ << ": ? for inner class: "<< TypeName<T>::str() << std::endl;
182  //this->append("Inner");
183  return *this;
184  }
185 
187 
190  inline
192  this->assignCastable(x);
193  return *this;
194  }
195 
196 
198  template <class T2>
199  inline
200  VariableT & operator=(const T2 &x){
201  this->assign(x);
202  return *this;
203  }
204 
206  template <class T2>
207  inline
208  VariableT & operator=(std::initializer_list<T2> l){
209  this->assignContainer(l, false);
210  return *this;
211  }
212 
214 
219  inline
220  VariableT & operator=(const char *x){
221  this->assign(x);
222  return *this;
223  }
224 
225  // Could be removed ? Handled by assign<T>() ?
226  template <class T2, size_t N>
227  inline
228  VariableT &operator=(const UniTuple<T2,N> & unituple){
229  this->assignContainer(unituple);
230  return *this;
231  }
232 
234  /*
235  template <class T2>
236  inline
237  T2 get() const {
238  //this->caster.get<double>();
239  return T2();
240  }
241  */
242 
243 
244  template <class T2>
245  inline
246  bool operator==(const VariableT<T2> & v) const {
247  return Castable::operator==((const Castable &) v);
248  }
249 
250  template <class T2>
251  inline
252  bool operator==(const T2 &x) const {
253  return Castable::operator==(x);
254  }
255 
256  inline
257  bool operator==(const char *s) const {
258  return Castable::operator==(s);
259  }
260 
261  template <class T2>
262  inline
263  bool operator!=(const VariableT<T2> & v) const{
264  return ! Castable::operator==((const Castable &) v);
265  }
266 
267  template <class T2>
268  inline
269  bool operator!=(const T2 &x) const {
270  return ! Castable::operator==(x);
271  }
272 
273  inline
274  bool operator!=(const char *x) const {
275  // std::cerr << __FILE__ << ':' << __LINE__ << ':' << __FUNCTION__ << " type: " << typeid(x).name() << std::endl;
276  return ! Castable::operator==(x);
277  }
278 
279 
280  // Default
281  inline
282  void info(std::ostream & ostr = std::cout) const {
283  Castable::info(ostr);
284  }
285 
286 protected:
287 
288  /*
289  static inline
290  bool compareByteByByte(const Castable & c1, const Castable & c2){
291 
292  if (c1.getType() != c2.getType())
293  return false;
294 
295  if (c1.getElementSize() != c2.getElementSize())
296  return false;
297 
298  if (c1.getElementCount() != c2.getElementCount())
299  return false;
300 
301  //const char *c1 = c1.getPtr();
302  for (size_t i = 0; i<c1.getElementCount(); ++i){
303  if (*c1.getPtr(i) != *c2.getPtr(i))
304  return false;
305  }
306 
307  return true;
308  }
309  */
310 
311 };
312 
313 
314 
315 } // drain
316 
317 #endif
Definition: Castable.h:76
virtual void info(std::ostream &ostr=std::cout) const
Print value, type and element count.
Definition: Castable.cpp:233
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:118
VariableT & operator=(const T &x)
Assignment of base classes (VariableLike or Castable)
Definition: VariableT.h:179
VariableT & operator=(const char *x)
Assignment of C strings. [Obligatory].
Definition: VariableT.h:220
VariableT & operator=(const VariableT< T2 > &x)
Assignment of objects of similar classes.
Definition: VariableT.h:170
void info(std::ostream &ostr=std::cout) const
Redefine for "ambivalent" FlexibleVariable: indicate if local or ref.
Definition: VariableT.h:282
virtual bool suggestType(const std::type_info &t)
Sets type, only if unset, and object is not a Reference.
Definition: FlexibleVariable.cpp:60
VariableT & operator=(const VariableT< T > &x)
Assignment of objects of the same class.
Definition: VariableT.h:158
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:48
VariableT & operator=(const Castable &x)
Assignment of objects of the same class.
Definition: VariableT.h:191
bool operator==(const VariableT< T2 > &v) const
debugging
Definition: VariableT.h:246
virtual bool requestSize(size_t elementCount)
Change the array size, if For Castable (and Reference) does nothing and returns false.
Definition: FlexibleVariable.cpp:70
VariableT & operator=(std::initializer_list< T2 > l)
General assignment operator.
Definition: VariableT.h:208
VariableT(const TT &...args)
Single constructor template, forwarded to init(args...) defined in base classes.
Definition: VariableT.h:101
VariableT & operator=(const T2 &x)
General assignment operator.
Definition: VariableT.h:200
Definition: DataSelector.cpp:1277
Definition: Type.h:542