SOFA API  ca70b0f0
Open source framework for multi-physics simuation
sofa::core::objectmodel::Data< T > Class Template Reference

#include <Data.h>

Container that holds a variable for a component. More...

Inheritance diagram for sofa::core::objectmodel::Data< T >:

Detailed Description

template<class T = void*>
class sofa::core::objectmodel::Data< T >

Container that holds a variable for a component.

This is a fundamental class template in Sofa. Data are used to encapsulated member variables of Sofa components (i.e. classes that somehow inherit from Base) in order to access them dynamically and generically: briefly, Data can be retrieved at run-time by their name, and they can be assigned a value from a string, or be printed as a string.

More concretely, from the perspective of XML scene files, each Data declared in a component corresponds to an attribute of this component.

Example

If a component Foo has a boolean parameter bar, it does not simply declares it as bool m_bar, but rather like this:

Data<bool> d_bar;
template class SOFA_CORE_API Data< bool >
Definition: Data.cpp:59

Then, this Data must be initialized to provide its name and default value. This is typically done in the initialization list of each constructor of the component, using the helper function Base::initData():

Foo::Foo(): d_bar(initData(&d_bar, true, "bar", "Here is a little description of this Data.")) {
// ...
}

And this Data can be assigned a value in XML scene files like so:

<Foo bar="false"/>

Classes

class  InitData
 This internal class is used by the initData() methods to store initialization parameters of a Data. More...
 

Public Attributes

int m_counter
 Number of changes since creation. More...
 
bool m_isSet
 True if this Data is set, i.e. its value is different from the default value. More...
 
- Public Attributes inherited from sofa::core::objectmodel::BaseData
std::string help {""}
 Help message. More...
 
std::string ownerClass {""}
 Owner class. More...
 
std::string group {""}
 group More...
 
std::string widget {""}
 widget More...
 
int m_counter
 Number of changes since creation. More...
 
bool m_isSet
 True if this Data is set, i.e. its value is different from the default value. More...
 
DataFlags m_dataFlags
 Flags indicating the purpose and behaviour of this Data. More...
 
Basem_owner {nullptr}
 Return the Base component owning this Data. More...
 
std::string m_name
 Data name within the Base component. More...
 
bool m_hasDefaultValue = false
 True if this Data has a default value. More...
 
DataLink< BaseDataparentData
 Parent Data. More...
 

Protected Attributes

m_defaultValue
 
ValueType m_value
 Value. More...
 
- Protected Attributes inherited from sofa::core::objectmodel::DDGNode
DDGLinkContainer inputs
 
DDGLinkContainer outputs
 

Public Member Functions

const sofa::defaulttype::AbstractTypeInfogetValueTypeInfo () const override
 Get info about the value type of the associated variable. More...
 
bool read (const std::string &s) override
 
void printValue (std::ostream &out) const override
 General case for printing default value. More...
 
std::string getValueString () const override
 General case for printing value. More...
 
std::string getDefaultValueString () const override
 General case for printing default value. More...
 
std::string getValueTypeString () const override
 Get the name of the type of the value held in this Data. More...
 
void operator= (const T &value)
 
bool copyValueFrom (const Data< T > *data)
 
 Data (const Data &)=delete
 
Dataoperator= (const Data &)=delete
 
bool SOFA_CORE_API read (const std::string &str)
 Specialization for reading strings. More...
 
bool SOFA_CORE_API read (const std::string &str)
 Specialization for reading booleans. More...
 
bool read (const std::string &str)
 Specialization for reading strings. More...
 
bool read (const std::string &str)
 Specialization for reading booleans. More...
 
Simple edition and retrieval API
virtual T * beginEdit ()
 
virtual T * beginWriteOnly ()
 
virtual void endEdit ()
 
void setValue (const T &value)
 
const T & getValue () const
 
- Public Member Functions inherited from sofa::core::objectmodel::BaseData
 BaseData (const BaseInitData &init)
 
 BaseData (const std::string &helpMsg, DataFlags flags=FLAG_DEFAULT)
 
 BaseData (const char *helpMsg, DataFlags flags=FLAG_DEFAULT)
 
 BaseData (const std::string &helpMsg, bool isDisplayed=true, bool isReadOnly=false)
 
 BaseData (const char *helpMsg, bool isDisplayed=true, bool isReadOnly=false)
 
 ~BaseData () override
 Destructor. More...
 
const void * getValueVoidPtr () const
 Get current value as a void pointer (use getValueTypeInfo to find how to access it) More...
 
void * beginEditVoidPtr ()
 Begin edit current value as a void pointer (use getValueTypeInfo to find how to access it) More...
 
void endEditVoidPtr ()
 Must be called after beginEditVoidPtr(), after you are finished modifying this Data. More...
 
const std::string & getHelp () const
 Get a help message that describes this Data. More...
 
void setHelp (const std::string &val)
 Set the help message. More...
 
const std::string & getGroup () const
 Get group. More...
 
void setGroup (const std::string &val)
 Set group. More...
 
const std::string & getWidget () const
 Get widget. More...
 
void setWidget (const char *val)
 Set widget. More...
 
virtual std::string getLinkPath () const
 If we use the Data as a link and not as value directly. More...
 
virtual bool canBeLinked () const
 
BasegetOwner () const
 Return the Base component owning this Data. More...
 
void setOwner (Base *o)
 Set the owner of this Data. More...
 
BaseDatagetData () const
 This method is needed by DDGNode. More...
 
const std::string & getName () const
 Return the name of this Data within the Base component. More...
 
void setName (const std::string &name)
 
bool hasDefaultValue () const
 Return whether the Data has a default value or not. More...
 
bool setParent (BaseData *parent, const std::string &path=std::string())
 Link to a parent data. The value of this data will automatically duplicate the value of the parent data. More...
 
bool setParent (const std::string &path)
 
virtual bool validParent (const BaseData *parent)
 Check if a given Data can be linked as a parent of this data. More...
 
BaseDatagetParent () const
 
void update () override
 Update the value of this Data. More...
 
bool copyValueFrom (const BaseData *data)
 Update this Data from the value of its parent. More...
 
bool updateValueFromLink (const BaseData *data)
 
void setFlag (DataFlagsEnum flag, bool b)
 Set one of the flags. More...
 
bool getFlag (DataFlagsEnum flag) const
 Get one of the flags. More...
 
bool isDisplayed () const
 Return whether this Data has to be displayed in GUIs. More...
 
bool isReadOnly () const
 Return whether this Data will be read-only in GUIs. More...
 
bool isPersistent () const
 Return whether this Data contains persistent information. More...
 
bool isAutoLink () const
 Return whether this Data should be autolinked when using the src="" syntax. More...
 
bool isRequired () const
 Return whether the Data has to be set by the user for the owner component to be valid. More...
 
void setDisplayed (bool b)
 Set whether this Data should be displayed in GUIs. More...
 
void setReadOnly (bool b)
 Set whether this Data is read-only. More...
 
void setPersistent (bool b)
 Set whether this Data contains persistent information. More...
 
void setAutoLink (bool b)
 Set whether this data should be autolinked when using the src="" syntax. More...
 
void setRequired (bool b)
 Set whether the Data has to be set by the user for the owner component to be valid. More...
 
bool isSet () const
 
void unset ()
 Reset the isSet flag to false, to indicate that the current value is the default for this Data. More...
 
void forceSet ()
 Reset the isSet flag to true, to indicate that the current value has been modified. More...
 
int getCounter () const
 
- Public Member Functions inherited from sofa::core::objectmodel::DDGNode
 DDGNode ()
 Constructor. More...
 
virtual ~DDGNode ()
 Destructor. Automatically remove remaining links. More...
 
void addInput (DDGNode *n)
 Add a new input to this node. More...
 
void delInput (DDGNode *n)
 Remove an input from this node. More...
 
void addOutput (DDGNode *n)
 Add a new output to this node. More...
 
void delOutput (DDGNode *n)
 Remove an output from this node. More...
 
const DDGLinkContainergetInputs ()
 Get the list of inputs for this DDGNode. More...
 
const DDGLinkContainergetOutputs ()
 Get the list of outputs for this DDGNode. More...
 
bool isDirty () const
 Returns true if the DDGNode needs to be updated. More...
 
virtual void setDirtyValue ()
 Indicate the value needs to be updated. More...
 
virtual void setDirtyOutputs ()
 Indicate the outputs needs to be updated. This method must be called after changing the value of this node. More...
 
void cleanDirty ()
 Set dirty flag to false. More...
 
virtual void notifyEndEdit ()
 Notify links that the DGNode has been modified. More...
 
void updateIfDirty () const
 Utility method to call update if necessary. This method should be called before reading of writing the value of this node. More...
 

Static Public Member Functions

static constexpr bool isCopyOnWrite ()
 
- Static Public Member Functions inherited from sofa::core::objectmodel::BaseData
static std::string decodeTypeName (const std::type_info &t)
 Helper method to decode the type name to a more readable form if possible. More...
 
template<class T >
static std::string typeName (const T *=nullptr)
 Helper method to get the type name of type T. More...
 

Protected Member Functions

std::istream & readValue (std::istream &in)
 
std::istream & readValue (std::istream &in)
 
SOFA_CORE_API std::istream & readValue (std::istream &in)
 
- Protected Member Functions inherited from sofa::core::objectmodel::BaseData
bool genericCopyValueFrom (const BaseData *parent)
 Try to update this Data from the value of its parent in "fast mode";. More...
 
- Protected Member Functions inherited from sofa::core::objectmodel::DDGNode
virtual void doAddInput (DDGNode *n)
 
virtual void doAddOutput (DDGNode *n)
 
virtual void doDelOutput (DDGNode *n)
 
void cleanDirtyOutputsOfInputs ()
 the dirtyOutputs flags of all the inputs will be set to false More...
 

Construction / destruction

static std::string templateName ()
 
BaseDatagetNewInstance () override
 
 Data (const BaseData::BaseInitData &init)
 
 Data (const InitData &init)
 
 Data (const char *helpMsg=nullptr, bool isDisplayed=true, bool isReadOnly=false)
 
 Data (const std::string &helpMsg, bool isDisplayed=true, bool isReadOnly=false)
 
 Data (const T &value, const char *helpMsg=nullptr, bool isDisplayed=true, bool isReadOnly=false)
 
 Data (const T &value, const std::string &helpMsg, bool isDisplayed=true, bool isReadOnly=false)
 
 ~Data () override
 Destructor. More...
 

Attribute details

◆ m_counter

template<class T = void*>
int sofa::core::objectmodel::BaseData::m_counter

Number of changes since creation.

◆ m_defaultValue

template<class T = void*>
T sofa::core::objectmodel::Data< T >::m_defaultValue
protected

◆ m_isSet

template<class T = void*>
bool sofa::core::objectmodel::BaseData::m_isSet

True if this Data is set, i.e. its value is different from the default value.

◆ m_value

template<class T = void*>
ValueType sofa::core::objectmodel::Data< T >::m_value
protected

Value.

Constructor details

◆ Data() [1/7]

template<class T = void*>
sofa::core::objectmodel::Data< T >::Data ( const BaseData< T >::BaseInitData< T > &  init)
inlineexplicit

Constructor used via the Base::initData() methods.

◆ Data() [2/7]

template<class T = void*>
sofa::core::objectmodel::Data< T >::Data ( const InitData< T > &  init)
inlineexplicit

Constructor used via the Base::initData() methods.

◆ Data() [3/7]

template<class T = void*>
sofa::core::objectmodel::Data< T >::Data ( const char helpMsg = nullptr,
bool  isDisplayed = true,
bool  isReadOnly = false 
)
inline

◆ Data() [4/7]

template<class T = void*>
sofa::core::objectmodel::Data< T >::Data ( const std::string &  helpMsg,
bool  isDisplayed = true,
bool  isReadOnly = false 
)
inline

Constructor.

Parameters
helpMsgA help message that describes this Data.
isDisplayedWhether this Data should be displayed in GUIs.
isReadOnlyWhether this Data should be modifiable in GUIs.

◆ Data() [5/7]

template<class T = void*>
sofa::core::objectmodel::Data< T >::Data ( const T &  value,
const char helpMsg = nullptr,
bool  isDisplayed = true,
bool  isReadOnly = false 
)
inline

Parameters
valueThe default value.

◆ Data() [6/7]

template<class T = void*>
sofa::core::objectmodel::Data< T >::Data ( const T &  value,
const std::string &  helpMsg,
bool  isDisplayed = true,
bool  isReadOnly = false 
)
inline

Parameters
valueThe default value.

◆ ~Data()

template<class T = void*>
sofa::core::objectmodel::Data< T >::~Data ( )
inlineoverride

Destructor.

◆ Data() [7/7]

template<class T = void*>
sofa::core::objectmodel::Data< T >::Data ( const Data< T > &  )
delete

Function details

◆ beginEdit()

template<class T = void*>
virtual T* sofa::core::objectmodel::Data< T >::beginEdit ( )
inlinevirtual

BeginEdit method if it is only to write the value checking that current value is up to date

◆ beginWriteOnly()

template<class T = void*>
virtual T* sofa::core::objectmodel::Data< T >::beginWriteOnly ( )
inlinevirtual

beginWriteOnly method if it is only to write the value regardless of the current status of this value: no dirtiness check

◆ copyValueFrom()

template<class T >
bool sofa::core::objectmodel::Data< T >::copyValueFrom ( const Data< T > *  data)

◆ endEdit()

template<class T = void*>
virtual void sofa::core::objectmodel::Data< T >::endEdit ( )
inlinevirtual

◆ getDefaultValueString()

template<class T >
std::string sofa::core::objectmodel::Data< T >::getDefaultValueString
overridevirtual

General case for printing default value.

Implements sofa::core::objectmodel::BaseData.

◆ getNewInstance()

template<class T = void*>
BaseData* sofa::core::objectmodel::Data< T >::getNewInstance ( )
inlineoverridevirtual

Reimplemented from sofa::core::objectmodel::BaseData.

◆ getValue()

template<class T = void*>
const T& sofa::core::objectmodel::Data< T >::getValue ( ) const
inline

◆ getValueString()

template<class T >
std::string sofa::core::objectmodel::Data< T >::getValueString
overridevirtual

General case for printing value.

Implements sofa::core::objectmodel::BaseData.

◆ getValueTypeInfo()

template<class T = void*>
const sofa::defaulttype::AbstractTypeInfo* sofa::core::objectmodel::Data< T >::getValueTypeInfo ( ) const
inlineoverridevirtual

Get info about the value type of the associated variable.

Implements sofa::core::objectmodel::BaseData.

◆ getValueTypeString()

template<class T >
std::string sofa::core::objectmodel::Data< T >::getValueTypeString ( ) const
overridevirtual

Get the name of the type of the value held in this Data.

Implements sofa::core::objectmodel::BaseData.

◆ isCopyOnWrite()

template<class T = void*>
static constexpr bool sofa::core::objectmodel::Data< T >::isCopyOnWrite ( )
inlinestaticconstexpr

◆ operator=() [1/2]

template<class T = void*>
Data& sofa::core::objectmodel::Data< T >::operator= ( const Data< T > &  )
delete

◆ operator=() [2/2]

template<class T = void*>
void sofa::core::objectmodel::Data< T >::operator= ( const T &  value)
inline

◆ printValue()

template<class T >
void sofa::core::objectmodel::Data< T >::printValue ( std::ostream &  out) const
overridevirtual

General case for printing default value.

Implements sofa::core::objectmodel::BaseData.

◆ read() [1/5]

template<class T >
bool sofa::core::objectmodel::Data< T >::read ( const std::string &  s)
overridevirtual

Try to read argument value from an input stream. Return false if failed

Implements sofa::core::objectmodel::BaseData.

◆ read() [2/5]

bool SOFA_CORE_API sofa::core::objectmodel::Data< std::string >::read ( const std::string &  str)
virtual

Specialization for reading strings.

Implements sofa::core::objectmodel::BaseData.

◆ read() [3/5]

bool SOFA_CORE_API sofa::core::objectmodel::Data< bool >::read ( const std::string &  str)
virtual

Specialization for reading booleans.

Implements sofa::core::objectmodel::BaseData.

◆ read() [4/5]

bool sofa::core::objectmodel::Data< std::string >::read ( const std::string &  str)
virtual

Specialization for reading strings.

Implements sofa::core::objectmodel::BaseData.

◆ read() [5/5]

bool sofa::core::objectmodel::Data< bool >::read ( const std::string &  str)
virtual

Specialization for reading booleans.

Implements sofa::core::objectmodel::BaseData.

◆ readValue() [1/3]

template<class T >
std::istream & sofa::core::objectmodel::Data< T >::readValue ( std::istream &  in)
protected

◆ readValue() [2/3]

std::istream & sofa::core::objectmodel::Data< sofa::core::visual::DisplayFlags >::readValue ( std::istream &  in)
protected

◆ readValue() [3/3]

SOFA_CORE_API std::istream & sofa::core::objectmodel::Data< sofa::core::visual::DisplayFlags >::readValue ( std::istream &  in)
protected

◆ setValue()

template<class T = void*>
void sofa::core::objectmodel::Data< T >::setValue ( const T &  value)
inline
Warning
writeOnly (the Data is not updated before being set)

◆ templateName()

template<class T = void*>
static std::string sofa::core::objectmodel::Data< T >::templateName ( )
inlinestatic