Loading...
Searching...
No Matches
VariableBase.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_BASE
35#define DRAIN_VARIABLE_BASE
36
37#include "Castable.h"
38#include "CastableIterator.h"
39
40#include "VariableT.h"
41
42
43namespace drain {
44
45
47
56class VariableBase : public Castable {
57
58public:
59
60 // Like in images. Should be fixed.
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 /* Trying to raise to Castable
121 VariableBase & append(){
122 return *this;
123 }
124
125 template <class T, class ...TT>
126 VariableBase & append(const T &x, const TT& ...rest){
127 Castable::operator<<(x);
128 append(rest...);
129 return *this;
130 }
131 */
132
133 template <class T, class ...TT>
134 // VariableBase &
135 void set(const T &arg, const TT& ...args){
136 clear();
137 // (*this) = x; // 2025
138 assign(arg); // 2025
139 //return append(rest...);
140 append(args...);
141 }
142
144 /*
145 inline
146 void reset(){
147 caster.unsetType();
148 //resize(0);
149 updateSize(0);
150 setSeparator(',');
151 //this->separator = ','; // semantics ?
152 }
153 */
154
156
159 virtual
160 void setType(const std::type_info & t) override ;
161
163 template <class T>
164 inline
165 void setType(){
166 setType(typeid(T));
167 }
168
170 /*
171 * Note: if a std::string is given, does not split it like with Variable::operator=(const char *).
172 */
173 template <class T>
174 inline
176 //Castable::operator<<(x);
177 append(x);
178 return *this;
179 }
180
181
182
184
187 inline
188 const CastableIterator & begin() const { return dataBegin; };
189
191 /*
192 * Notice: in the case of std::strings, will iterate through characters.
193 */
194 inline
195 const CastableIterator & end() const { return dataEnd; };
196
197
199
202 virtual inline
203 bool setSize(size_t elementCount){
204 updateSize(elementCount);
205 return true;
206 }
207
209 virtual inline
210 void ensureSize(size_t elementCount){
211 if (this->elementCount < elementCount){
212 updateSize(elementCount);
213 }
214 // return true;
215 }
216
217protected:
218
219
220 /***
221 * This is the basic design pattern for all the VariableLikes
222 template <class ...TT>
223 inline
224 VariableBase(const TT & ...args) {
225 init(args...); //
226 }
227 */
228
229 virtual
230 void updateSize(size_t elementCount);
231
232private:
233
234 std::vector<char> data;
235
236 mutable CastableIterator dataBegin;
237 mutable CastableIterator dataEnd;
238
239
240private:
241
242 void updateIterators();
243
244};
245
246/*
247template <class T>
248class VariableT<T>;
249*/
250
254template <class V>
255class VariableInitializer : public V {
256
257protected:
258
260 /*
261 template <class T>
262 void init(const VariableInitializer<V> & value) {
263 std::cerr << __FILE__ << ':' << __LINE__ << ':' << __FUNCTION__ << " " << value << '[' << typeid(T).name() << ']'<< std::endl;
264 this->reset();
265 this->assignCastable(value);
266 }
267 */
268
269
271
277 template <class T>
278 void init(const VariableT<T> & value) {
279 // drain::Logger(__FILE__, __LINE__, __FUNCTION__).warn(drain::TypeName<VariableT<VariableInitializer<V> > >::str(),
280 // '(', drain::TypeName<VariableT<T> >::str(), ' ', value, ')');
281 this->reset();
282 this->assignCastable(value);
283 }
284
285 template <class T>
286 void init(VariableT<T> & value) {
287 // drain::Logger(__FILE__, __LINE__, __FUNCTION__).warn(drain::TypeName<VariableT<VariableInitializer<V> > >::str(),
288 // '(', drain::TypeName<VariableT<T> >::str(), ' ', value, ')');
289 this->reset();
290 if (value.isLinking() && this->isLinkable()){
291 this->relink(value);
292 // this->setPtr(value.getPtr(), value.getType(), value.getElementCount());
293 }
294 this->assignCastable(value);
295 }
296
297
298 /*
299 template <class D>
300 void init(const D & dst){
301 reset();
302 assign(dst);
303 }
304 */
306 inline
307 void init(const std::type_info &t = typeid(void)) {
308 this->reset();
309 this->setType(t);
310 };
311
312 // Initialisation, using type of argument or explicit type argument.
313 // Copies type, data and separator char. Fails with Reference?
314 template <class T>
315 void init(const T & value) {
316 // std::cerr << __FILE__ << ':' << __LINE__ << ':' << __FUNCTION__ << " " << value << '[' << typeid(T).name() << ']'<< std::endl;
317 // std::cerr << __LINE__ << " reset: " << std::endl;
318 this->reset();
319 // std::cerr << __LINE__ << " assign: " << std::endl;
320 this->assign(value); // Critical, direct assignment *this = value fails
321 // std::cerr << __LINE__ << " ...done " << std::endl;
322 }
323
324
325 template <class T>
326 void init(const T & value, const std::type_info &t) { // = typeid(void)
327 std::cerr << __FILE__ << ':' << __LINE__ << ':' << __FUNCTION__ << " " << value << '[' << t.name() << ']' << std::endl;
328
329 std::cerr << __LINE__ << " reset: " << std::endl;
330 this->reset();
331 std::cerr << __LINE__ << " setType: " << std::endl;
332 this->setType(t);
333 std::cerr << __LINE__ << " assign: " << std::endl;
334 this->assign(value); // Critical, direct assignment *this = value fails
335 std::cerr << __LINE__ << " ...done " << std::endl;
336 // ;
337 }
338
339
341 inline
342 void init(const VariableBase & v) {
343 std::cerr << __FILE__ << ':' << __LINE__ << __FUNCTION__ << " " << v << std::endl;
344 this->reset();
345 this->outputSeparator = v.outputSeparator;
346 this->inputSeparator = v.inputSeparator;
347 this->assignCastable(v);
348 };
349
351 inline
352 void init(const Castable & c) {
353 std::cerr << __FILE__ << ':' << __LINE__ << __FUNCTION__ << " " << c << std::endl;
354 this->reset();
355 //this->outputSeparator = c.outputSeparator;
356 //this->inputSeparator = c.inputSeparator;
357 this->assignCastable(c);
358 };
359
361 inline
362 void init(const char * s) {
363 // std::cerr << __FILE__ << ':' << __LINE__ << __FUNCTION__ << " " << s << std::endl;
364 this->reset();
365 this->assignString(s);
366 };
367
369 template<typename T>
370 inline
371 void init(std::initializer_list<T> l, const std::type_info &t = typeid(void)) {
372 // std::cerr << __FILE__ << ':' << __LINE__ << __FUNCTION__ << " " << std::endl;
373 this->reset();
374 this->setType(t);
375 this->assignContainer(l, true);
376 };
377
378 template <class ...TT>
379 void init(const TT& ...args){
380 // std::cerr << __FILE__ << ':' << __LINE__ << __FUNCTION__ << " (variadic args)" << std::endl;
381 this->reset();
382 this->append(args...);
383 }
384
385};
386
387}
388
389#endif
Definition CastableIterator.h:57
Definition Castable.h:76
size_t elementCount
Pointer to the data variable.
Definition Castable.h:1255
char outputSeparator
Element separator usein in writing and reading character strings (streams).
Definition Castable.h:1261
char inputSeparator
Element separator usein in reading a char sequence to an (numeric) array.
Definition Castable.h:1258
void clear()
Clears strings, or for scalars and arrays, sets all the values to zero. Does not change type.
Definition Castable.cpp:81
void append(const T &x)
Appends the string or appends the array by one element.
Definition Castable.h:410
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
virtual bool setSize(size_t elementCount)
Extends the array to include elementCount elements of current type.
Definition VariableBase.h:203
virtual bool isVariable() const
Tells if internal memory exists.
Definition VariableBase.h:105
const CastableIterator & begin() const
Like with std::iterator.
Definition VariableBase.h:188
virtual bool isLinkable() const
Tells if the internal pointer can point to an external variable.
Definition VariableBase.h:115
const CastableIterator & end() const
Like with std::iterator.
Definition VariableBase.h:195
VariableBase & operator<<(const T &x)
Extends the array by one element.
Definition VariableBase.h:175
void setType()
Overriding (shadowing) Castable::setType()
Definition VariableBase.h:165
Definition VariableBase.h:255
void init(const Castable &c)
Copies type, data and separator char.
Definition VariableBase.h:352
void init(const char *s)
Copies type, data and separator char.
Definition VariableBase.h:362
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:371
void init(const std::type_info &t=typeid(void))
Default constructor generates an empty array.
Definition VariableBase.h:307
void init(const VariableBase &v)
Copies type, data and separator char.
Definition VariableBase.h:342
void init(const VariableT< T > &value)
Copy constructor handler.
Definition VariableBase.h:278
VariableT is a final class applied through typedefs Variable, Reference and FlexibleVariable.
Definition VariableT.h:87
Definition DataSelector.cpp:1277