GemaCoreLib
The GeMA Core library
Public Member Functions | Static Public Member Functions | Protected Member Functions | Protected Attributes | Private Attributes | Friends | List of all members
GmValueAccessorBase Class Reference

An implementation of basic attributes and functions that should be common for most value accessor implementations. More...

#include <gmValueAccessor.h>

Inheritance diagram for GmValueAccessorBase:
Inheritance graph
[legend]
Collaboration diagram for GmValueAccessorBase:
Collaboration graph
[legend]

Public Member Functions

 GmValueAccessorBase (GmValueInfo *info, const GmLogCategory &logger, UnitConverter *conv, QString desiredUnit)
 Constructor. Reveives infromation about the returned values and basic parameters needed for unit conversion, initializing the basic fields _info, _conv, _rconv and _convData that will be used by derived classes. Takes ownership of the converter.
 
virtual ~GmValueAccessorBase ()
 Destructor.
 
virtual GmValueInfoinfo () const
 Returns the information object describing the values returned by this accessor.
 
virtual Unit unit () const
 Returns the unit in which data returned/received by the accessor is expressed. More...
 
virtual bool hasUnitConversion () const
 Returns true if this accessor perfroms unit conversions, false if not.
 
virtual QString valueStr (int index, bool evalFunctions, bool printDefAsNil, int fieldWidth=0, char format='g', int precision=-1, const GmVector *coord=NULL) const
 Auxiliary function to return the value of an accessor converted to a string. Works for scalars, vectors and matrices. More...
 
virtual bool setFunctionFromName (int index, QString userFunctionId, QString &err)
 Sets a function value from the function name. More...
 
virtual bool setValueFromVariant (int index, const QVariant &v, bool acceptMissingDimension, QString &err)
 Sets a value with data read from a QVariant. See parameter descriptions in GmValueAccessor::setValueFromVariant() More...
 
virtual void setEvalContext (GmValueSetEvalContext *context, bool ownsContext)
 Sets the EvalContext object that will be used to translate function names / objects into function evaluators to allow for evaluating user functions. More...
 
virtual GmValueSetEvalContextevalContext () const
 Returns the internal evaluation context. Needed by GmCellAccessorProxy, GmBcAccessorProxy & state dump functions.
 
- Public Member Functions inherited from GmValueAccessor
virtual ~GmValueAccessor ()
 Virtual destructor.
 
virtual int size () const =0
 Returns the number of values in the set refered to by the accessor, meaning that valid indices will fall in the range [0..size()-1].
 
bool isScalar () const
 Returns true if the accessor returns scalar values (can be false even if valueSize() == 1 – think about a 1x1 matrix)
 
virtual bool isDefValue (int index) const =0
 Returns true if the value at the specified index is equal to the default value defined by info()->defValue()
 
virtual const double * defValue () const
 Returns the default value for the data converted to the accessor unit. The returned array contents is valid only UNTIL a new call to the accessor is done. Meaningfull only when the default value is NOT a function.
 
int valueSize () const
 Returns the size of the array returned by value(), i.e. 1 for scalar values and nlin * ncol for vector or matrix values.
 
virtual int adjustLinearIndex (int index, const GmMesh *m)
 Translates a linear index (from 0 to mesh->totalNumNodes()-1) into a valid index for this accessor or into -1 if the given index is out of range. More...
 
virtual int adjustLinearIndex (int index, int firstGhostIndex)
 Simmilar to adjustLinearIndex(int, const GmMesh*), this function overload gets as second parameter the index of the first ghost node in the mesh (equal to mesh->numNodes()).
 
virtual const double * valueAt (int index, const GmVector *coord) const =0
 Returns the value associated with the "index" position of the data set, evaluated at the requested position (when necessary). More...
 
const double * value (int index) const
 Similar to valueAt(), passing NULL as coordinates.
 
virtual double scalarValueAt (int index, const GmVector *coord) const =0
 Similar to valueAt() but returning a single double. Must be used for scalar data sets only.
 
double scalarValue (int index) const
 Similar to scalarValueAt(), passing NULL as coordinates.
 
void matrixValueAt (int index, const GmVector *coord, GmCRMatrix &mat) const
 Similar to valueAt(), but returning the result as a CONST matrix. Bear in mind that the resulting matrix is valid only UNTIL the next call to the accessor.
 
void matrixValue (int index, GmCRMatrix &mat) const
 Similar to matrixValueAt(), passing NULL as coordinates.
 
void vectorValueAt (int index, const GmVector *coord, GmCRVector &vec) const
 Similar to valueAt(), but returning the result as a CONST vector. Bear in mind that the resulting vector is valid only UNTIL the next call to the accessor.
 
void vectorValue (int index, GmCRVector &vec) const
 Similar to vectorValueAt(), passing NULL as coordinates.
 
virtual bool setValue (int index, const double *value)=0
 Allows for altering the value associated with entry "index" in the value set. This version of the function works for scalar, vector or matrix values. Values are copied to the data set. Matrix values should be organized in COLUMN MAJOR ORDER. More...
 
virtual bool setScalarValue (int index, double value)=0
 Similar to setValue() but passing a single double. Must be used for scalar data sets only.
 
virtual bool setScalarValueAtDim (int index, int dim, double val)
 Similar to setValue() but changing only the value at the given dimension. Can be used over scalar, vector and matrix sets. For matrices, dim refers to the position in the linearized vector data in COLUMN MAJOR FORMAT. Should be used only if the current value is NOT a function.
 
virtual bool setValueAsDef (int index)=0
 Similar to setValue(), filling the index entry with the default value.
 
bool setMatrixValue (int index, const GmMatrix &mat)
 Similar to setValue() but receiving the data as a matrix.
 
bool setVectorValue (int index, const GmVector &vec)
 Similar to setValue() but receiving the data as a vector.
 
bool addScalarToValue (int index, double value)
 Convenience function equivalent to calling setScalarValue(index, scalarValue(index) + value)
 
bool addToValue (int index, const double *val)
 Convenience function logically equivalent to calling setValue(index, value(index) + val) operating over the full vector. Should be used only if the current value is NOT a function.
 
virtual bool setFunctionValue (int index, GmUserFunction *functionDef)=0
 Overload of setValue() storing a user function definition. More...
 
virtual bool setFunctionValue (int index, GmUserFunctionEvaluator *functionEval)=0
 Overload of setValue() storing a user function evaluation context. More...
 
virtual bool isFunction (int index) const =0
 Returns true if the data in index is a reference to a function (in C or Lua). Used by valueStr() to mark function results.
 
virtual QString functionId (int index) const =0
 Returns a function id name for a function reference at the index or an empty string for non function indices. Used by valueStr().
 

Static Public Member Functions

static bool fillMatrixFromLuaTable (double *m, int nlin, int ncol, LuaTable &t, const GmValueInfo *info, const double *defVal, QString &err)
 An auxilliar function that given a Lua table storing either a list of doubles or a list of lists of doubles, fills the given matrix m, stored in the pre-allocated vector with size nlin * ncol and organized "per column". More...
 

Protected Member Functions

const double * evalValue (GmUserFunctionEvaluator *functionEval, int index, const GmVector *coord) const
 An auxiliary function tailored for evaluating functions identified by its evaluator. This function should be called by implementations of valueAt(), and scalarValueAt() for ValueData objects of type GM_VDT_FUNCTION_EVAL. More...
 
const double * evalValue (GmUserFunction *functionDef, int index, const GmVector *coord) const
 Overload of evalValue(GmUserFunctionEvaluator*, ...) receiving a user function object. Should be called for ValueData objects of type GM_VDT_FUNCTION.
 
const double * evalDefValue (int index, const GmVector *coord) const
 Simmilar to evalValue(), this function evaluates a default value function. Should be called only when _info->defIsFunction() returns true.
 
virtual double convertToAccessorUnit (double val) const
 An utility function that given a value, converts it to the accessor unit. If no conversion is needed, returns val.
 
virtual const double * convertToAccessorUnit (const double *p) const
 An utility function that given a pointer to the stored data, converts the value to the accessor unit and returns a pointer to the internal buffer used to store the converted value. If no conversion is needed, returns p.
 
virtual void convertFromAccessorUnit (double val, double *dst) const
 Copies the values given by val into the area pointed to by dst, converting units if needed.
 
virtual void convertFromAccessorUnit (const double *val, double *dst) const
 Copies the values given by val into the area pointed to by dst, converting units if needed.
 
double * convData () const
 

Protected Attributes

GmValueInfo_info
 The metadata describing the values returned by this accessor.
 
const GmLogCategory_logger
 The logger object used to report errors while accessing or setting data.
 
GmTLBuffer< double, true > * _convData
 Per thread local buffer used to store converted data. Null if _conv == NULL.
 
UnitConverter_conv
 Unit converter.
 
UnitConverter_rconv
 Unit converter in the reverse direction: from desired unit to data unit.
 
QString _unit
 The unit in which data will be returned. Needed for the case when _conv = NULL and _info->unit() is empty.
 

Private Attributes

GmValueSetEvalContext_evalContext
 The context used to parse / get evaluators for user functions.
 
bool _ownsEvalContext
 Do we own the _evalContext? If so we are expected to delete it on destruction.
 

Friends

class GmDiscontinuityAccessor
 

Detailed Description

An implementation of basic attributes and functions that should be common for most value accessor implementations.

To help the implementation of unit conversion on derived classes, this interface includes some common definitions regarding unit conversion needed by every derived class.

If an accessor can possibly need to evaluate a referenced function, the evaluation environment must have been set prior to data access. Failure to do so will lead to undefined behaviour. This is done by calling setEvalContext() with a complete context. Passing an incomplete context (one that isn't bound to a mesh) is supported only for accessors used to storing references to functions, without evaluating them).

This class provides some basic functions for function evaluation that should be used by the concrete classes inheriting from GmValueAccessor.

Member Function Documentation

◆ evalValue()

const double * GmValueAccessorBase::evalValue ( GmUserFunctionEvaluator functionEval,
int  index,
const GmVector coord 
) const
protected

An auxiliary function tailored for evaluating functions identified by its evaluator. This function should be called by implementations of valueAt(), and scalarValueAt() for ValueData objects of type GM_VDT_FUNCTION_EVAL.

The coord prameter should contain values passed for valueAt() / scalarValueAt() functions (which can be NULL).

The evaluated result is returned in an internal vector, valid until the next call to evalValue().

In case of errors evaluating the function, returns NULL and reports the error with the registered logger.

◆ fillMatrixFromLuaTable()

bool GmValueAccessorBase::fillMatrixFromLuaTable ( double *  m,
int  nlin,
int  ncol,
LuaTable t,
const GmValueInfo info,
const double *  defVal,
QString err 
)
static

An auxilliar function that given a Lua table storing either a list of doubles or a list of lists of doubles, fills the given matrix m, stored in the pre-allocated vector with size nlin * ncol and organized "per column".

If the Lua table is a simple list of doubles, it should be organized in a "per column" order.

If a default value is given, in a format compatible with the default value of an info object, matrix entries in the source table can be nil and will be filled with the corresponding value from the default. Otherwise, false will be returned.

If the Lua tables store more values than expected, also returns false, but that is only detected if the next element from the desired border is not empty.

Fills err with message when returning false.

IMPORTANT: Since the Lua tables are expected to have holes if defVal is not NULL, the standard table size operator can not be used!

◆ setEvalContext()

void GmValueAccessorBase::setEvalContext ( GmValueSetEvalContext context,
bool  ownsContext 
)
virtual

Sets the EvalContext object that will be used to translate function names / objects into function evaluators to allow for evaluating user functions.

For evaluating values, context should be a complete one. Passing an incomplete context (one that isn't bound to a mesh) is supported only for accessors used to storing references to functions, without evaluating them (usefull while parsing mesh definitions for property sets).

The second parameter, ownsContext, defines if the context should be deleted or not by the accessor when the accessor itself is deleted.

Implements GmValueAccessor.

◆ setFunctionFromName()

bool GmValueAccessorBase::setFunctionFromName ( int  index,
QString  userFunctionId,
QString err 
)
virtual

Sets a function value from the function name.

For function references to be parsed correctly, its imperative that an EvalContext has been set by a previous call to setEvalContext().

On errors, returns false and fills err with a description of the problem (errors in this case are not logged through the accessor logger).

Implements GmValueAccessor.

◆ setValueFromVariant()

bool GmValueAccessorBase::setValueFromVariant ( int  index,
const QVariant v,
bool  acceptMissingDimension,
QString err 
)
virtual

Sets a value with data read from a QVariant. See parameter descriptions in GmValueAccessor::setValueFromVariant()

For function references in v to be parsed correctly, its imperative that an EvalContext has been set by a previous call to setEvalContext().

Implements GmValueAccessor.

◆ unit()

virtual Unit GmValueAccessorBase::unit ( ) const
inlinevirtual

Returns the unit in which data returned/received by the accessor is expressed.

Keep in mind that this unit can be different from info()->unit() if the accessor was created with a different unit than the original data unit.

Implements GmValueAccessor.

◆ valueStr()

QString GmValueAccessorBase::valueStr ( int  index,
bool  evalFunctions,
bool  printDefAsNil,
int  fieldWidth = 0,
char  format = 'g',
int  precision = -1,
const GmVector coord = NULL 
) const
virtual

Auxiliary function to return the value of an accessor converted to a string. Works for scalars, vectors and matrices.

The evalFunctions parameter defines whether function based values will be evaluated or not. If printDefAsNil == true, default values will be printed as the 'nil' string.

The formatted value conforms to the standard QString::arg() parameters. Scalars are presented as simple formatted numbers. Vectors and matrices are surrounded by [...]. Matrix values are listed in COLUMN MAJOR ORDER. Values returned by function calls are surrounded by (), which means that a vector returned by a function call will be represented as ([...]).

Implements GmValueAccessor.

Reimplemented in StringListAccessor.


The documentation for this class was generated from the following files: