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:

## 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 >
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...
template<>
boost::any | getValue (const std::string &name) const

## Private Member Functions

| Accessible (const Accessible &other)
Accessible & | operator= (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

## DecoderType

typedef std::function<void(const YAML::Node*)> SurgSim::Framework::Accessible::DecoderType

## EncoderType

typedef std::function<YAML::Node(void)> SurgSim::Framework::Accessible::EncoderType

## GetterType

typedef std::function<boost::any(void)> SurgSim::Framework::Accessible::GetterType

## SetterType

typedef std::function<void (boost::any)> SurgSim::Framework::Accessible::SetterType

## Constructor & Destructor Documentation

## Accessible() [1/2]

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

Default Constructor.

## ~Accessible()

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

Destructor.

## Accessible() [2/2]

SurgSim::Framework::Accessible::Accessible | ( | const Accessible & | other | ) |

private

Prevent default copy construction and default assignment

## 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::AssertionFailure | if node is not of YAML::NodeType::Map.

Parameters

  node | The node that carries the data to be decoded, properties with names that don't match up with properties in the Accessible will be reported.
  ignoredProperties | Properties 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::AssertionFailure | if the target does not contain the property named in this call.

Parameters

  name | The name of the new property
  target | The instance that provides the actual property
  targetProperty | The name of the property that should be used.

## getValue() [1/4]

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

## getValue() [2/4]

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::AssertionFailure | If the conversion fails or the property cannot be found.

Template Parameters

  T | The requested type for the property.

Parameters

  name | The name of the property.

Returns
  The value of the property if the getter was found

## getValue() [3/4]

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::AssertionFailure | if the property cannot be found

Parameters

  name | The name of the property.

Returns
  The value of the property if the getter was found

## getValue() [4/4]

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

  T | the type of the property, usually can be deduced automatically

Parameters

  | name | The name of the property.
  [out] | value | If 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

  name | Name 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

  name | Name of the property to be checked.

Returns
  true if the property exists and has a setter

## operator=()

Accessible& SurgSim::Framework::Accessible::operator= | ( | const Accessible & | other | ) |

private

Prevent default copy construction and default assignment

## removeAccessors()

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

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

Parameters

  name | The 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::AssertionFailure | if either getter or setter is a nullptr.

Parameters

  name | The name of the property.
  getter | The getter.
  setter | The 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

  name | The name of the property.
  decoder | The 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::AssertionFailure | if func is a nullptr.

Parameters

  name | The name of the property.
  func | The 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

  name | The name of the property.
  encoder | The function to be used to put the property into the node.
  decoder | The 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::AssertionFailure | if func is a nullptr.

Parameters

  name | The name of the property.
  func | The 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::AssertionFailure | If the property cannot be found.

Parameters

  name | The name of the property.
  value | The value that it should be set to.

## Member Data Documentation

## m_functors

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

private

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

- SurgSim/Framework/Accessible.h
- SurgSim/Framework/Accessible-inl.h
- SurgSim/Framework/Accessible.cpp