opensurgsim
Classes | Public Types | Public Member Functions | List of all members
SurgSim::Framework::Accessible Class Reference

Mixin class for enabling a property system on OSS classes, the instance still needs to initialize properties in the constructor by using either addSetter, addGetter, addAccessors or the macro for each member variable that should be made accessible. More...

#include <Accessible.h>

Inheritance diagram for SurgSim::Framework::Accessible:
SurgSim::Framework::Asset SurgSim::Framework::Component SurgSim::Framework::MultipleValuesClass SurgSim::Framework::Testable< T > SurgSim::Graphics::UniformBase SurgSim::Input::DeviceInterface SurgSim::Math::Shape SurgSim::Physics::RigidState

Public Types

typedef std::function< boost::any(void)> GetterType
 
typedef std::function< void(boost::any)> SetterType
 
typedef std::function< YAML::Node(void)> EncoderType
 
typedef std::function< void(const YAML::Node *)> DecoderType
 

Public Member Functions

 Accessible ()
 Default Constructor.
 
 ~Accessible ()
 Destructor.
 
template<class T >
getValue (const std::string &name) const
 Retrieves the value with the name by executing the getter if it is found and tries to convert it to the given type. More...
 
boost::any getValue (const std::string &name) const
 Retrieves the value with the name by executing the getter if it is found. More...
 
template<class T >
bool getValue (const std::string &name, T *value) const
 Retrieves the value with the name by executing the getter if it is found, and converts it to the type of the output parameter. More...
 
void setValue (const std::string &name, const boost::any &value)
 Sets a value of a property that has setter. More...
 
bool isReadable (const std::string &name) const
 Check whether a property is readable. More...
 
bool isWriteable (const std::string &name) const
 Check whether a property is writable. More...
 
void setGetter (const std::string &name, GetterType func)
 Sets a getter for a given property. More...
 
void setSetter (const std::string &name, SetterType func)
 Sets a setter for a given property. More...
 
void setAccessors (const std::string &name, GetterType getter, SetterType setter)
 Sets the accessors getter and setter in one function. More...
 
void removeAccessors (const std::string &name)
 Removes all the accessors (getter and setter) for a given property. More...
 
void forwardProperty (const std::string &name, const Accessible &target, const std::string &targetProperty)
 Adds a property with the given name that uses the targets accessors, in effect forwarding the value to the target. More...
 
void setSerializable (const std::string &name, EncoderType encoder, DecoderType decoder)
 Sets the functions used to convert data from and to a YAML::Node. More...
 
void setDecoder (const std::string &name, DecoderType decoder)
 Sets the functions used to convert data from a YAML::Node. More...
 
YAML::Node encode () const
 Encode this Accessible to a YAML::Node. More...
 
void decode (const YAML::Node &node, const std::vector< std::string > &ignoredProperties=std::vector< std::string >())
 Decode this Accessible from a YAML::Node, will throw an exception if the data type cannot be converted. More...
 
std::vector< std::string > getProperties ()
 
template<>
boost::any getValue (const std::string &name) const
 

Detailed Description

Mixin class for enabling a property system on OSS classes, the instance still needs to initialize properties in the constructor by using either addSetter, addGetter, addAccessors or the macro for each member variable that should be made accessible.

Member Function Documentation

§ decode()

void SurgSim::Framework::Accessible::decode ( const YAML::Node &  node,
const std::vector< std::string > &  ignoredProperties = std::vector<std::string>() 
)

Decode this Accessible from a YAML::Node, will throw an exception if the data type cannot be converted.

Exceptions
SurgSim::Framework::AssertionFailureif node is not of YAML::NodeType::Map.
Parameters
nodeThe node that carries the data to be decoded, properties with names that don't match up with properties in the Accessible will be reported.
ignoredPropertiesProperties that will be ignored.

§ encode()

YAML::Node SurgSim::Framework::Accessible::encode ( ) const

Encode this Accessible to a YAML::Node.

Returns
The encoded version of this instance.

§ forwardProperty()

void SurgSim::Framework::Accessible::forwardProperty ( const std::string &  name,
const Accessible target,
const std::string &  targetProperty 
)

Adds a property with the given name that uses the targets accessors, in effect forwarding the value to the target.

Note
This will copy the appropriate calls into the local function table of this accessible, in effect exposing a pointer to the target, if the target goes out of scope, the behavior is undefined
Exceptions
SurgSim::Framework::AssertionFailureif the target does not contain the property named in this call.
Parameters
nameThe name of the new property
targetThe instance that provides the actual property
targetPropertyThe name of the property that should be used.

§ getValue() [1/3]

template<class T >
T SurgSim::Framework::Accessible::getValue ( const std::string &  name) const

Retrieves the value with the name by executing the getter if it is found and tries to convert it to the given type.

Exceptions
SurgSim::Framework::AssertionFailureIf the conversion fails or the property cannot be found.
Template Parameters
TThe requested type for the property.
Parameters
nameThe name of the property.
Returns
The value of the property if the getter was found

§ getValue() [2/3]

boost::any SurgSim::Framework::Accessible::getValue ( const std::string &  name) const

Retrieves the value with the name by executing the getter if it is found.

Exceptions
SurgSim::Framework::AssertionFailureif the property cannot be found
Parameters
nameThe name of the property.
Returns
The value of the property if the getter was found

§ getValue() [3/3]

template<class T >
bool SurgSim::Framework::Accessible::getValue ( const std::string &  name,
T *  value 
) const

Retrieves the value with the name by executing the getter if it is found, and converts it to the type of the output parameter.

This does not throw.

Template Parameters
Tthe type of the property, usually can be deduced automatically
Parameters
nameThe name of the property.
[out]valueIf non-null, will receive the value of the given property.
Returns
true if value != nullptr and the getter can be found.

§ isReadable()

bool SurgSim::Framework::Accessible::isReadable ( const std::string &  name) const

Check whether a property is readable.

Parameters
nameName of the property to be checked.
Returns
true if the property exists and has a getter

§ isWriteable()

bool SurgSim::Framework::Accessible::isWriteable ( const std::string &  name) const

Check whether a property is writable.

Parameters
nameName of the property to be checked.
Returns
true if the property exists and has a setter

§ removeAccessors()

void SurgSim::Framework::Accessible::removeAccessors ( const std::string &  name)

Removes all the accessors (getter and setter) for a given property.

Parameters
nameThe name of the property

§ setAccessors()

void SurgSim::Framework::Accessible::setAccessors ( const std::string &  name,
GetterType  getter,
SetterType  setter 
)

Sets the accessors getter and setter in one function.

Exceptions
SurgSim::Framework::AssertionFailureif either getter or setter is a nullptr.
Parameters
nameThe name of the property.
getterThe getter.
setterThe setter.

§ setDecoder()

void SurgSim::Framework::Accessible::setDecoder ( const std::string &  name,
DecoderType  decoder 
)

Sets the functions used to convert data from a YAML::Node.

This leaves the encoder (class -> YAML) conversion empty, this can be used to let the user decide how to model the data in the data file, inside the class this should all result in one member to be created/changed.

Parameters
nameThe name of the property.
decoderThe function to be used to read the property from the node and set it in the instance.

§ setGetter()

void SurgSim::Framework::Accessible::setGetter ( const std::string &  name,
GetterType  func 
)

Sets a getter for a given property.

Exceptions
SurgSim::Framework::AssertionFailureif func is a nullptr.
Parameters
nameThe name of the property.
funcThe getter function.

§ setSerializable()

void SurgSim::Framework::Accessible::setSerializable ( const std::string &  name,
EncoderType  encoder,
DecoderType  decoder 
)

Sets the functions used to convert data from and to a YAML::Node.

Will throw an exception if the data type that is passed to YAML cannot be converted into a YAML::Node

Parameters
nameThe name of the property.
encoderThe function to be used to put the property into the node.
decoderThe function to be used to read the property from the node and set it in the instance.

§ setSetter()

void SurgSim::Framework::Accessible::setSetter ( const std::string &  name,
SetterType  func 
)

Sets a setter for a given property.

Exceptions
SurgSim::Framework::AssertionFailureif func is a nullptr.
Parameters
nameThe name of the property.
funcThe setter function.

§ setValue()

void SurgSim::Framework::Accessible::setValue ( const std::string &  name,
const boost::any &  value 
)

Sets a value of a property that has setter.

Exceptions
SurgSim::Framework::AssertionFailureIf the property cannot be found.
Parameters
nameThe name of the property.
valueThe value that it should be set to.

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