VariableBase.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_BASE
35 #define DRAIN_VARIABLE_BASE
36 
37 #include "Castable.h"
38 #include "CastableIterator.h"
39 
40 #include "VariableT.h"
41 
42 
43 namespace drain {
44 
45 
47 
56 class VariableBase : public Castable {
57 
58 public:
59 
60  // Like in images. Should be fixed.
62  typedef CastableIterator iterator;
63 
64  //typedef std::pair<const char *,const drain::VariableBase> init_pair_t;
65 
66 
67  virtual inline
68  ~VariableBase(){};
69 
71 
74  virtual inline // true also if ptr == null
75  bool isLinking() const {
76  if (caster.ptr == nullptr){
77  return false;
78  }
79  else if (data.empty()){ // this is never true
80  return true;
81  }
82  else {
83  return (caster.ptr != (void *) &data[0]);
84  /*
85  if (caster.ptr != (void *) &data[0]){ // Debugging
86  std::cerr << __FILE__ << ':' << __LINE__ << ':' << __FUNCTION__ << " non-empty data [" << getTypeName() << "], but pointer discarding it " << std::endl;
87  std::cerr << __FILE__ << ':' << __LINE__ << ':' << __FUNCTION__ << " value: '" << *this << "'" << std::endl;
88  //info(std::cerr);
89  }
90  return false;
91  */
92  }
93 
94  }
95 
97 
104  virtual inline
105  bool isVariable() const {
106  return true;
107  }
108 
109 
111 
114  virtual inline
115  bool isLinkable() const {
116  return false;
117  }
118 
119 
120 
121 
122 
123  //template <class T>
124  VariableBase & append(){
125  return *this;
126  }
127 
128  template <class T, class ...TT>
129  VariableBase & append(const T &x, const TT& ...rest){
130  Castable::operator<<(x);
131  append(rest...);
132  return *this;
133  }
134 
135  template <class T, class ...TT>
136  VariableBase & set(const T &x, const TT& ...rest){
137  clear();
138  (*this) = x;
139  return append(rest...);
140  }
141 
143  /*
144  inline
145  void reset(){
146  caster.unsetType();
147  //resize(0);
148  updateSize(0);
149  setSeparator(',');
150  //this->separator = ','; // semantics ?
151  }
152  */
153 
155 
158  virtual
159  void setType(const std::type_info & t);
160 
161 
162 
164  /*
165  * Note: if a std::string is given, does not split it like with Variable::operator=(const char *).
166  */
167  template <class T>
168  inline
169  VariableBase &operator<<(const T &x){
170  //Castable::operator<<(x);
171  append(x);
172  return *this;
173  }
174 
175 
176 
178 
181  inline
182  const CastableIterator & begin() const { return dataBegin; };
183 
185  /*
186  * Notice: in the case of std::strings, will iterate through characters.
187  */
188  inline
189  const CastableIterator & end() const { return dataEnd; };
190 
191 
192 
193 //protected:
194 
195 
197 
200  virtual inline
201  bool setSize(size_t elementCount){
202  // TODO: if is reference?
203  //return updateSize(elementCount); // fix/check: always returns true?
204  updateSize(elementCount);
205  return true;
206  }
207 
208 protected:
209 
210 
211  /***
212  * This is the basic design pattern for all the VariableLikes
213  template <class ...TT>
214  inline
215  VariableBase(const TT & ...args) {
216  init(args...); //
217  }
218  */
219 
220  virtual
221  void updateSize(size_t elementCount);
222 
223 private:
224 
225  std::vector<char> data;
226 
227  mutable CastableIterator dataBegin;
228  mutable CastableIterator dataEnd;
229 
230 
231 private:
232 
233  void updateIterators();
234 
235 };
236 
237 /*
238 template <class T>
239 class VariableT<T>;
240 */
241 
245 template <class V>
246 class VariableInitializer : public V {
247 
248 protected:
249 
251  /*
252  template <class T>
253  void init(const VariableInitializer<V> & value) {
254  std::cerr << __FILE__ << ':' << __LINE__ << ':' << __FUNCTION__ << " " << value << '[' << typeid(T).name() << ']'<< std::endl;
255  this->reset();
256  this->assignCastable(value);
257  }
258  */
259 
260 
262 
268  template <class T>
269  void init(const VariableT<T> & value) {
270  // drain::Logger(__FILE__, __LINE__, __FUNCTION__).warn(drain::TypeName<VariableT<VariableInitializer<V> > >::str(),
271  // '(', drain::TypeName<VariableT<T> >::str(), ' ', value, ')');
272  this->reset();
273  this->assignCastable(value);
274  }
275 
276  template <class T>
277  void init(VariableT<T> & value) {
278  // drain::Logger(__FILE__, __LINE__, __FUNCTION__).warn(drain::TypeName<VariableT<VariableInitializer<V> > >::str(),
279  // '(', drain::TypeName<VariableT<T> >::str(), ' ', value, ')');
280  this->reset();
281  if (value.isLinking() && this->isLinkable()){
282  this->relink(value);
283  // this->setPtr(value.getPtr(), value.getType(), value.getElementCount());
284  }
285  this->assignCastable(value);
286  }
287 
288 
289  /*
290  template <class D>
291  void init(const D & dst){
292  reset();
293  assign(dst);
294  }
295  */
297  inline
298  void init(const std::type_info &t = typeid(void)) {
299  this->reset();
300  this->setType(t);
301  };
302 
303  // Initialisation, using type of argument or explicit type argument.
304  // Copies type, data and separator char. Fails with Reference?
305  template <class T>
306  void init(const T & value) {
307  // std::cerr << __FILE__ << ':' << __LINE__ << ':' << __FUNCTION__ << " " << value << '[' << typeid(T).name() << ']'<< std::endl;
308  // std::cerr << __LINE__ << " reset: " << std::endl;
309  this->reset();
310  // std::cerr << __LINE__ << " assign: " << std::endl;
311  this->assign(value); // Critical, direct assignment *this = value fails
312  // std::cerr << __LINE__ << " ...done " << std::endl;
313  }
314 
315 
316  template <class T>
317  void init(const T & value, const std::type_info &t) { // = typeid(void)
318  std::cerr << __FILE__ << ':' << __LINE__ << ':' << __FUNCTION__ << " " << value << '[' << t.name() << ']' << std::endl;
319 
320  std::cerr << __LINE__ << " reset: " << std::endl;
321  this->reset();
322  std::cerr << __LINE__ << " setType: " << std::endl;
323  this->setType(t);
324  std::cerr << __LINE__ << " assign: " << std::endl;
325  this->assign(value); // Critical, direct assignment *this = value fails
326  std::cerr << __LINE__ << " ...done " << std::endl;
327  // ;
328  }
329 
330 
332  inline
333  void init(const VariableBase & v) {
334  std::cerr << __FILE__ << ':' << __LINE__ << __FUNCTION__ << " " << v << std::endl;
335  this->reset();
336  this->outputSeparator = v.outputSeparator;
337  this->inputSeparator = v.inputSeparator;
338  this->assignCastable(v);
339  };
340 
342  inline
343  void init(const Castable & c) {
344  std::cerr << __FILE__ << ':' << __LINE__ << __FUNCTION__ << " " << c << std::endl;
345  this->reset();
346  //this->outputSeparator = c.outputSeparator;
347  //this->inputSeparator = c.inputSeparator;
348  this->assignCastable(c);
349  };
350 
352  inline
353  void init(const char * s) {
354  // std::cerr << __FILE__ << ':' << __LINE__ << __FUNCTION__ << " " << s << std::endl;
355  this->reset();
356  this->assignString(s);
357  };
358 
360  template<typename T>
361  inline
362  void init(std::initializer_list<T> l, const std::type_info &t = typeid(void)) {
363  // std::cerr << __FILE__ << ':' << __LINE__ << __FUNCTION__ << " " << std::endl;
364  this->reset();
365  this->setType(t);
366  this->assignContainer(l, true);
367  };
368 
369  template <class ...TT>
370  void init(const TT& ...args){
371  std::cerr << __FILE__ << ':' << __LINE__ << __FUNCTION__ << " (variadic args)" << std::endl;
372  this->reset();
373  this->append(args...);
374  }
375 
376 };
377 
378 }
379 
380 #endif
Definition: CastableIterator.h:57
Definition: Castable.h:76
size_t elementCount
Pointer to the data variable.
Definition: Castable.h:1209
char outputSeparator
Element separator usein in writing and reading character strings (streams).
Definition: Castable.h:1215
char inputSeparator
Element separator usein in reading a char sequence to an (numeric) array.
Definition: Castable.h:1212
void clear()
Clears strings, or for scalars and arrays, sets all the values to zero. Does not change type.
Definition: Castable.cpp:81
void setType()
Sets the storage type. If a target value is available, use setPtr() directly.
Definition: Castable.h:849
void * ptr
Future member: enables setting Caster type.
Definition: Caster.h:247
Base class for variables: Variable, Reference and FlexibleVariable.
Definition: VariableBase.h:56
virtual bool isLinking() const
Tells if the pointer points to an external variable.
Definition: VariableBase.h:75
const CastableIterator & end() const
Like with std::iterator.
Definition: VariableBase.h:189
virtual bool setSize(size_t elementCount)
Extends the array to include elementCount elements of current type.
Definition: VariableBase.h:201
virtual bool isVariable() const
Tells if internal memory exists.
Definition: VariableBase.h:105
VariableBase & operator<<(const T &x)
Extends the array by one element.
Definition: VariableBase.h:169
virtual bool isLinkable() const
Tells if the internal pointer can point to an external variable.
Definition: VariableBase.h:115
const CastableIterator & begin() const
Like with std::iterator.
Definition: VariableBase.h:182
Definition: VariableBase.h:246
void init(const Castable &c)
Copies type, data and separator char.
Definition: VariableBase.h:343
void init(const char *s)
Copies type, data and separator char.
Definition: VariableBase.h:353
void init(std::initializer_list< T > l, const std::type_info &t=typeid(void))
Initialisation with type of the first element or explicit type argument.
Definition: VariableBase.h:362
void init(const std::type_info &t=typeid(void))
Default constructor generates an empty array.
Definition: VariableBase.h:298
void init(const VariableBase &v)
Copies type, data and separator char.
Definition: VariableBase.h:333
void init(const VariableT< T > &value)
Copy constructor handler.
Definition: VariableBase.h:269
VariableT is a final class applied through typedefs Variable, Reference and FlexibleVariable.
Definition: VariableT.h:87
Definition: DataSelector.cpp:1277