All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Classes | Public Types | Public Member Functions | Private Attributes | 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 <SurgSim/Framework/Accessible.h>

Inheritance diagram for SurgSim::Framework::Accessible:
Inheritance graph
[legend]
Collaboration diagram for SurgSim::Framework::Accessible:
Collaboration graph
[legend]

Classes

struct  Functors
 Private struct to keep the map under control. More...
 

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. More...
 
 ~Accessible ()
 Destructor. More...
 
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...
 
YAML::Node encode () const
 Encode this Accessible to a YAML::Node. More...
 
void decode (const YAML::Node &node)
 Decode this Accessible from a YAML::Node, will throw an exception if the data type cannot be converted. More...
 
template<>
boost::any getValue (const std::string &name) const
 

Private Member Functions

 Accessible (const Accessible &other)
 
Accessibleoperator= (const Accessible &other)
 

Private Attributes

std::unordered_map
< std::string, Functors
m_functors
 

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 Typedef Documentation

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

Constructor & Destructor Documentation

SurgSim::Framework::Accessible::Accessible ( )

Default Constructor.

SurgSim::Framework::Accessible::~Accessible ( )

Destructor.

SurgSim::Framework::Accessible::Accessible ( const Accessible other)
private

Prevent default copy construction and default assignment

Member Function Documentation

void SurgSim::Framework::Accessible::decode ( const YAML::Node &  node)

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, properties with names that don't match up with properties in the Accessible are ignored
YAML::Node SurgSim::Framework::Accessible::encode ( ) const

Encode this Accessible to a YAML::Node.

Returns
The encoded version of this instance.
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.
boost::any SurgSim::Framework::Accessible::getValue ( const std::string &  name) const
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
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
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.
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
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
Accessible& SurgSim::Framework::Accessible::operator= ( const Accessible other)
private

Prevent default copy construction and default assignment

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
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.
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.
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 and 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.
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.
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.

Member Data Documentation

std::unordered_map<std::string, Functors> SurgSim::Framework::Accessible::m_functors
private

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