GemaCoreLib
The GeMA Core library
gmValueSetData.h
Go to the documentation of this file.
1 /************************************************************************
2 **
3 ** Copyright (C) 2014 by Carlos Augusto Teixera Mendes
4 ** All rights reserved.
5 **
6 ** This file is part of the "GeMA" software. It's use should respect
7 ** the terms in the license agreement that can be found together
8 ** with this source code.
9 ** It is provided AS IS, with NO WARRANTY OF ANY KIND,
10 ** INCLUDING THE WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR
11 ** A PARTICULAR PURPOSE.
12 **
13 ************************************************************************/
14 
23 #ifndef _GEMA_VALUE_SET_DATA_H_
24 #define _GEMA_VALUE_SET_DATA_H_
25 
26 #include "gmCoreConfig.h"
27 #include "gmValueInfo.h"
28 
29 
30 #include <QVarLengthArray>
31 #include <QMutex>
32 #include <assert.h>
33 
34 
36 class GmValueSet;
37 class UnitConverter;
38 class GmLogCategory;
39 class GmUserFunction;
41 class GmNanBoxedValue;
42 
43 template <class T, bool Align> class GmTLBuffer;
44 
45 // Enable the define bellow to force validation of internal structures after value set initialization or growth
46 // #define ENABLE_VALUESETDATA_TESTS
47 
48 //----------------------------------------------------------------------
49 // GmValueSetData - Basic interface
50 //----------------------------------------------------------------------
51 
66 {
67 public:
70  {
74  };
75 
77  virtual ~GmValueSetData() {};
78 
80  virtual GmValueInfo* info() const = 0;
81 
83  virtual bool init(int numValues) = 0;
84 
86  virtual int numValues() const = 0;
87 
91  virtual bool addValues(int numAddedValues) = 0;
92 
94  virtual void restoreSize(int oldNumValues) = 0;
95 
97  virtual void removeValues(int index, int numValues) = 0;
98 
100  virtual void clear() = 0;
101 
106  virtual GmTrackedValueAccessor* accessor(const GmLogCategory& logger, UnitConverter* conv,
107  QString desiredUnit, bool atomic = false) = 0;
108 
114  virtual size_t usedMemory() const = 0;
115 
116 #if defined ENABLE_TESTS || defined ENABLE_VALUESETDATA_TESTS
117  virtual void validateInternalStructure() = 0;
118 #endif
119 
120 protected:
121  // Functions bellow are part of the cooperative interface between a ValueSetData and the
122  // ValueSetDataAccessor familly of classes. They are marked protected since they are not
123  // intended for beeing used by users of ValueSetData, who should always access and store
124  // values through accessors.
125  template <class T> friend class GmValueSetDataAccessorBase;
126  template <class T> friend class GmValueSetDataAccessor;
127  template <class T> friend class GmNoFunctionValueSetDataAccessor;
128  template <class T> friend class GmUnitConversionDataAccessor; // The unit conversion versions of the above classes
129  friend class GmDefValueValueSetData;
130  // The same logic applies for the state dumping and HDF5 saving classes that access the dump
131  // buffer interface functions for better performance
132  friend class GmStateDumpValueSetDataItem;
133  friend class GmHdf5;
134 
136  virtual bool isDefValue(int index) const = 0;
137 
139  virtual StoredValueType storedValueType(int index) const = 0;
140 
149  virtual const double* value(int index) const = 0;
150 
156  virtual bool setValue(int index, const double* values) = 0;
157 
163  virtual void* functionValue(int index) const = 0;
164 
173  virtual bool setFunctionValue(int index, StoredValueType type, void* functionObj) = 0;
174 
179  virtual char* dumpBuffer() { return NULL; }
180 
184  virtual int dumpBufferSize() const { return 0; }
185 };
186 
187 //----------------------------------------------------------------------
188 // GmVectorValueSetData<T> - Vector storage for non sparse, non function sets
189 //----------------------------------------------------------------------
190 
202 template <class T> class GmVectorValueSetData : public GmValueSetData
203 {
204 public:
206  virtual ~GmVectorValueSetData();
207 
208  // See comments on the base class
209  virtual GmValueInfo* info() const { return _info; }
210 
211  virtual bool init(int numValues);
212 
213  // See comments on the base class
214  virtual int numValues() const { return _numValues; }
215 
216  virtual bool addValues (int numAddedValues);
217  virtual void restoreSize(int oldNumValues);
218 
219  virtual void removeValues(int index, int numValues);
220 
221  // See comments on the base class
222  virtual void clear() { _data.clear(); _data.squeeze(); _numValues = 0; }
223 
224  virtual GmTrackedValueAccessor* accessor(const GmLogCategory& logger, UnitConverter* conv,
225  QString desiredUnit, bool atomic = false);
226 
227  // See comments on the base class
228  virtual size_t usedMemory() const { return _data.capacity() * sizeof(T); }
229 
230 #if defined ENABLE_TESTS || defined ENABLE_VALUESETDATA_TESTS
231  virtual void validateInternalStructure();
232 #endif
233 
234 protected:
236  const T* iptr(int index) const { assert(index >= 0 && index < _numValues); return _data.data() + index * _dim; }
237 
239  T* iptr(int index) { assert(index >= 0 && index < _numValues); return _data.data() + index * _dim; }
240 
241  virtual void initVector(int start, int n);
242 
243  // See comments on the base class
244  virtual bool isDefValue(int index) const { return !memcmp(value(index), _info->defValue(), _dim * sizeof(double)); }
245 
246  // See comments on the base class
247  virtual StoredValueType storedValueType(int index) const { Q_UNUSED(index); return GM_SVT_NUMBER; }
248 
249  virtual const double* value(int index) const;
250  virtual bool setValue(int index, const double* values);
251 
252  // See comments on the base class. Functions are not supported by this value set
253  virtual void* functionValue(int index) const { Q_UNUSED(index); assert(0); return NULL; }
254 
255  // See comments on the base class. Functions are not supported by this value set
256  virtual bool setFunctionValue(int index, StoredValueType type, void* functionObj)
257  {
258  Q_UNUSED(index); Q_UNUSED(type); Q_UNUSED(functionObj);
259  assert(0);
260  return false;
261  }
262 
263  // See comments on the base class
264  virtual char* dumpBuffer() { return (char*)_data.data(); }
265 
266  // See comments on the base class
267  virtual int dumpBufferSize() const { return _data.size() * sizeof(T); }
268 
270  int _dim;
274 
275 private:
276  Q_DISABLE_COPY(GmVectorValueSetData);
277 };
278 
279 //----------------------------------------------------------------------
280 // GmFVectorValueSetData - GmVectorValueSetData with support for functions
281 //----------------------------------------------------------------------
282 
298 {
299 public:
301 
302  virtual GmTrackedValueAccessor* accessor(const GmLogCategory& logger, UnitConverter* conv,
303  QString desiredUnit, bool atomic = false);
304 
305 protected:
307  const GmNanBoxedValue* nanBoxPtr(int index) const { return (const GmNanBoxedValue*)iptr(index); }
308 
310  GmNanBoxedValue* nanBoxPtr(int index) { return (GmNanBoxedValue*)iptr(index); }
311 
312  virtual void initVector(int start, int n);
313 
314  virtual bool isDefValue(int index) const;
315 
316  virtual StoredValueType storedValueType(int index) const;
317 
318  virtual const double* value (int index) const;
319  virtual bool setValue(int index, const double* values);
320 
321  virtual void* functionValue (int index) const;
322  virtual bool setFunctionValue(int index, StoredValueType type, void* functionObj);
323 
325 
326 private:
327  Q_DISABLE_COPY(GmFVectorValueSetData);
328 };
329 
330 
331 //----------------------------------------------------------------------
332 // GmDefValueValueSetData - ValueSetData returning always the default value
333 //----------------------------------------------------------------------
334 
351 {
352 public:
353  GmDefValueValueSetData(GmValueSetData* baseContainer, GmValueSet* vs);
354 
355  virtual ~GmDefValueValueSetData();
356 
357  // See comments on the base class
358  virtual GmValueInfo* info() const { return _bc->info(); }
359 
360  // See comments on the base class
361  virtual bool init(int numValues) { _numValues = numValues; return true; }
362 
363  // See comments on the base class
364  virtual int numValues() const { return _numValues; }
365 
366  // See comments on the base class
367  virtual bool addValues(int numAddedValues) { _numValues += numAddedValues; return true; }
368 
369  // See comments on the base class
370  virtual void restoreSize(int oldNumValues) { _numValues = oldNumValues; }
371 
372  // See comments on the base class
373  virtual void removeValues(int index, int numValues)
374  {
375  Q_UNUSED(index);
376  assert(index >= 0 && index + numValues <= _numValues);
377  _numValues -= numValues;
378  }
379 
380  // See comments on the base class
381  virtual void clear() { _numValues = 0; }
382 
383  virtual GmTrackedValueAccessor* accessor(const GmLogCategory& logger, UnitConverter* conv,
384  QString desiredUnit, bool atomic = false);
385 
386  // See comments on the base class
387  virtual size_t usedMemory() const { return 0; } // This value set uses a constant memory amount, not returned by usedMemory to be consistent with other ValueSetData classes
388 
389  static void cleanupDetachedData();
390 
391 #if defined ENABLE_TESTS || defined ENABLE_VALUESETDATA_TESTS
392  virtual void validateInternalStructure();
393 #endif
394 
395 protected:
396  // See comments on the base class
397  virtual bool isDefValue(int index) const { Q_UNUSED(index); return true; }
398 
399  // See comments on the base class
400  virtual StoredValueType storedValueType(int index) const { Q_UNUSED(index); return _defType; }
401 
402  // See comments on the base class
403  virtual const double* value(int index) const { Q_UNUSED(index); assert(_defType == GM_SVT_NUMBER); return (double*)_defValue; }
404 
405  virtual bool setValue(int index, const double* values);
406 
407  // See comments on the base class
408  virtual void* functionValue(int index) const { Q_UNUSED(index); assert(_defType != GM_SVT_NUMBER); return _defValue; }
409 
410  virtual bool setFunctionValue(int index, StoredValueType type, void* functionObj);
411 
416  void* _defValue;
418  bool _detached;
419 
427 
428 private:
429  Q_DISABLE_COPY(GmDefValueValueSetData);
430 };
431 
432 #endif
433 
434 
virtual StoredValueType storedValueType(int index) const
Returns the type (number, function definition/evaluator) of the stored value for the given index.
Definition: gmValueSetData.h:400
virtual StoredValueType storedValueType(int index) const
Returns the type (number, function definition/evaluator) of the stored value for the given index.
Definition: gmValueSetData.cpp:388
virtual void removeValues(int index, int numValues)
Removes numValues from the set, starting from (and including) index.
Definition: gmValueSetData.cpp:171
Auxiliar class used to store the definition of a value. It can be used to store informations about st...
Definition: gmValueInfo.h:126
int _dim
Value dimension. Equal to _info->size();.
Definition: gmValueSetData.h:270
virtual const double * value(int index) const
Returns the numeric value stored at the given index (converted to a double if needed) Must be called ...
Definition: gmValueSetData.cpp:396
int capacity() const const
virtual int numValues() const
Returns the number of values stored in this value set.
Definition: gmValueSetData.h:214
virtual const double * value(int index) const
Returns the numeric value stored at the given index (converted to a double if needed) Must be called ...
Definition: gmValueSetData.cpp:239
virtual size_t usedMemory() const
Returns an estimative of the memory used by the data set in bytes.
Definition: gmValueSetData.h:387
Basic class used to store sets of values, bound to a common definition, on behalf of another object (...
Definition: gmValueSet.h:53
StoredValueType _defType
The type of the default value.
Definition: gmValueSetData.h:415
An accessor implementation that can work with any kind of GmValueSetData, with or without functions,...
Definition: gmValueSetDataAccessor.h:195
virtual bool setValue(int index, const double *values)
Updates the value stored at the given index with a numeric value (converted to the correct type if ne...
Definition: gmValueSetData.cpp:404
virtual bool init(int numValues)
Initilizes the value set allocating space.
Definition: gmValueSetData.h:361
A class similar to GmTLS that creates a buffer for each possible thread in the GmThreadManager,...
Definition: gmValueSetData.h:43
A number.
Definition: gmValueSetData.h:71
virtual void initVector(int start, int n)
Initializes the data array from the given start position, with n copies of the default value.
Definition: gmValueSetData.cpp:349
Declaration of usefull configuration definitions for the Core library.
virtual void restoreSize(int oldNumValues)
Restores the size of the set to the previous size before addValues.
Definition: gmValueSetData.cpp:157
GmValueSet * _vs
The value set that owns us.
Definition: gmValueSetData.h:414
Declaration of the GmValueInfo class.
virtual GmTrackedValueAccessor * accessor(const GmLogCategory &logger, UnitConverter *conv, QString desiredUnit, bool atomic=false)=0
Returns an accessor for the stored data. The atomic flag can be used by the caller to request that a ...
A support class for saving data to a HDF5 file. Hdf5 files are self-describing but do not have a fixe...
Definition: gmHdf5.h:65
virtual bool setValue(int index, const double *values)=0
Updates the value stored at the given index with a numeric value (converted to the correct type if ne...
void * _defValue
The default value. Will be a double* or a pointer to one of the function object types depending on _d...
Definition: gmValueSetData.h:416
virtual int numValues() const =0
Returns the number of values stored in this value set.
An accessor implementation that can work with any kind of GmValueSetData, without support for functio...
Definition: gmValueSetDataAccessor.h:136
A GmMemoryDumpItem implementation loading / storing values from the GmValueSetData object associated ...
Definition: gmStateDumpItem.h:286
virtual size_t usedMemory() const
Returns an estimative of the memory used by the data set in bytes.
Definition: gmValueSetData.h:228
GmVectorValueSetData(GmValueInfo *info)
Constructor. The object will NOT take ownership of the given info object.
Definition: gmValueSetData.cpp:87
A GmValueSetData implementation that stores no data and returns the default value,...
Definition: gmValueSetData.h:350
virtual StoredValueType storedValueType(int index) const
Returns the type (number, function definition/evaluator) of the stored value for the given index.
Definition: gmValueSetData.h:247
virtual GmTrackedValueAccessor * accessor(const GmLogCategory &logger, UnitConverter *conv, QString desiredUnit, bool atomic=false)
Returns an accessor for the stored data. The atomic flag can be used by the caller to request that a ...
Definition: gmValueSetData.cpp:221
A GmValueSetData implementation that adds support for storing functions to GmVectorValueSetData.
Definition: gmValueSetData.h:297
virtual bool setFunctionValue(int index, StoredValueType type, void *functionObj)
Overload of setValue() storing a user function definition.
Definition: gmValueSetData.h:256
virtual void * functionValue(int index) const
Returns a pointer to the stored function object. Return will be either a GmUserFunction* or a GmUserF...
Definition: gmValueSetData.cpp:421
QMutex _setMutex
The mutex protecting setXxxx() operations.
Definition: gmValueSetData.h:417
bool _detached
Flag marking that this object should be no longer in use.
Definition: gmValueSetData.h:418
virtual void clear()
Clears the value set, restoring its size to 0.
Definition: gmValueSetData.h:381
virtual void clear()
Clears the value set, restoring its size to 0.
Definition: gmValueSetData.h:222
static QVector< GmValueSetData * > _detachedList
A global list with the set of detached GmDefValueValueSetData marked for delayed deletion by cleanupD...
Definition: gmValueSetData.h:426
virtual bool addValues(int numAddedValues)
Adds numAddedValues to the set, initializing them to the default value. Returns true on success,...
Definition: gmValueSetData.cpp:137
virtual void removeValues(int index, int numValues)
Removes numValues from the set, starting from (and including) index.
Definition: gmValueSetData.h:373
int _numValues
The number of entries in this data set.
Definition: gmValueSetData.h:412
QVarLengthArray< T, 1 > _data
The vector storing the data. Its size is equal to _dim * _numValues.
Definition: gmValueSetData.h:272
GmTLBuffer< double, true > * _convBuffer
The buffer used to store data returned by value() when T is not double.
Definition: gmValueSetData.h:273
virtual bool addValues(int numAddedValues)
Adds numAddedValues to the set, initializing them to the default value. Returns true on success,...
Definition: gmValueSetData.h:367
virtual void initVector(int start, int n)
Initializes the data array from the given start position, with n copies of the default value.
Definition: gmValueSetData.cpp:183
A GmValueSetData implementation that supports efficient storing for numeric values,...
Definition: gmValueSetData.h:202
A wrapper class around QtNanBoxedValue providing specific constructors to store user functions / user...
Definition: gmNanBoxedValue.h:40
virtual void restoreSize(int oldNumValues)
Restores the size of the set to the previous size before addValues.
Definition: gmValueSetData.h:370
#define GMC_API_EXPORT
Macro for controling if the class is being exported (GEMA_CORE_LIB defined) or imported (GEMA_CORE_LI...
Definition: gmCoreConfig.h:35
virtual bool isDefValue(int index) const
Returns true if the indexed value is the default value for the set.
Definition: gmValueSetData.cpp:368
const double * defValue() const
Returns the default value used for initializing/sparse values. If the default is a function,...
Definition: gmValueInfo.h:183
Auxiliary class responisble for allowing a value accessor to track changes to the referenced value se...
Definition: gmTrackedValueAccessor.h:37
The generic interface implemented by every GmValueSetData object. Its purpose is to provide a base cl...
Definition: gmValueSetData.h:65
Auxiliar class used to store the needed information to translate an user function id / user function ...
Definition: gmValueSetEvalContext.h:45
StoredValueType
Enumeration to detect the type of stored data for an index.
Definition: gmValueSetData.h:69
virtual bool isDefValue(int index) const
Returns true if the indexed value is the default value for the set.
Definition: gmValueSetData.h:397
Class representing a category with multiple logging levels.
Definition: gmLog.h:58
const T * iptr(int index) const
Returns a pointer to the index data inside _data.
Definition: gmValueSetData.h:236
A template class to add unit conversion support to the given base class.
Definition: gmValueSetDataAccessor.h:240
virtual char * dumpBuffer()
Returns the internal buffer that can be used for a direct data dumping/loading by the GmStateDump cla...
Definition: gmValueSetData.h:264
virtual const double * value(int index) const
Returns the numeric value stored at the given index (converted to a double if needed) Must be called ...
Definition: gmValueSetData.h:403
int _numValues
The number of values stored in _data. Equal to _data.size() / _dim.
Definition: gmValueSetData.h:271
Common code for both GmNoFunctionValueSetDataAccessor and GmValueSetDataAccessor.
Definition: gmValueSetDataAccessor.h:40
virtual GmValueInfo * info() const
Returns a pointer to the associated value info object.
Definition: gmValueSetData.h:358
virtual int dumpBufferSize() const
Returns the size in bytes of the internal buffer returned by dumpBuffer. Returns 0 if there is no suc...
Definition: gmValueSetData.h:267
GmValueInfo * _info
The info object describing this set contents.
Definition: gmValueSetData.h:269
virtual int numValues() const
Returns the number of values stored in this value set.
Definition: gmValueSetData.h:364
virtual bool setValue(int index, const double *values)
Updates the value stored at the given index with a numeric value (converted to the correct type if ne...
Definition: gmValueSetData.cpp:258
virtual void * functionValue(int index) const
Returns a pointer to the stored function object. Return will be either a GmUserFunction* or a GmUserF...
Definition: gmValueSetData.h:253
virtual int dumpBufferSize() const
Returns the size in bytes of the internal buffer returned by dumpBuffer. Returns 0 if there is no suc...
Definition: gmValueSetData.h:184
virtual ~GmValueSetData()
Virtual destructor.
Definition: gmValueSetData.h:77
const GmNanBoxedValue * nanBoxPtr(int index) const
Returns a pointer to the index data inside _data converted to a Nan Box.
Definition: gmValueSetData.h:307
virtual bool init(int numValues)
Initilizes the value set allocating space.
Definition: gmValueSetData.cpp:128
GmUserFunction * _defFunction
The default user function if the default value is a function, NULL otherwise.
Definition: gmValueSetData.h:324
Class used to store the definition of a user function and its parameters.
Definition: gmUserFunction.h:78
A pointer to a user function definition.
Definition: gmValueSetData.h:72
int size() const const
virtual char * dumpBuffer()
Returns the internal buffer that can be used for a direct data dumping/loading by the GmStateDump cla...
Definition: gmValueSetData.h:179
virtual bool setFunctionValue(int index, StoredValueType type, void *functionObj)=0
Overload of setValue() storing a user function definition.
virtual void * functionValue(int index) const
Returns a pointer to the stored function object. Return will be either a GmUserFunction* or a GmUserF...
Definition: gmValueSetData.h:408
virtual bool isDefValue(int index) const
Returns true if the indexed value is the default value for the set.
Definition: gmValueSetData.h:244
virtual bool setFunctionValue(int index, StoredValueType type, void *functionObj)
Overload of setValue() storing a user function definition.
Definition: gmValueSetData.cpp:429
GmValueSetData * _bc
The base container that we should initialize and revert to when a value is written to the data set.
Definition: gmValueSetData.h:413
GmFVectorValueSetData(GmValueInfo *info, GmValueSetEvalContext *evalContext)
Constructor. Needs a function evaluator as parameter when the default value is a function.
Definition: gmValueSetData.cpp:316
GmNanBoxedValue * nanBoxPtr(int index)
Non-const overload to return a pointer to the index data inside _data converted to a Nan Box.
Definition: gmValueSetData.h:310
A pointer to an evaluator for a user function definition.
Definition: gmValueSetData.h:73
virtual GmTrackedValueAccessor * accessor(const GmLogCategory &logger, UnitConverter *conv, QString desiredUnit, bool atomic=false)
Returns an accessor for the stored data. The atomic flag can be used by the caller to request that a ...
Definition: gmValueSetData.cpp:332
T * iptr(int index)
Non-const overload to return a pointer to the index data inside _data.
Definition: gmValueSetData.h:239
virtual GmValueInfo * info() const
Returns a pointer to the associated value info object.
Definition: gmValueSetData.h:209
virtual ~GmVectorValueSetData()
Destructor for the common case.
Definition: gmValueSetData.cpp:121