16 #ifndef SURGSIM_FRAMEWORK_ACCESSIBLE_H
17 #define SURGSIM_FRAMEWORK_ACCESSIBLE_H
21 #include <unordered_map>
23 #include <boost/any.hpp>
24 #include <yaml-cpp/yaml.h>
61 T
getValue(
const std::string& name)
const;
67 boost::any
getValue(
const std::string& name)
const;
77 bool getValue(
const std::string& name, T* value)
const;
83 void setValue(
const std::string& name,
const boost::any& value);
88 bool isReadable(
const std::string& name)
const;
99 void setGetter(
const std::string& name, GetterType func);
105 void setSetter(
const std::string& name, SetterType func);
112 void setAccessors(
const std::string& name, GetterType getter, SetterType setter);
134 void setSerializable(
const std::string& name, EncoderType encoder, DecoderType decoder);
138 YAML::Node
encode()
const;
146 void decode(
const YAML::Node& node,
const std::vector<std::string>& ignoredProperties = std::vector<std::string>());
199 #define SURGSIM_ADD_RW_PROPERTY(class, type, property, getter, setter) \
200 setAccessors(#property, \
201 std::bind(&class::getter, this),\
202 std::bind(&class::setter, this, std::bind(SurgSim::Framework::convert<type>,std::placeholders::_1)))
205 #define SURGSIM_ADD_RO_PROPERTY(class, type, property, getter) \
206 setGetter(#property, \
207 std::bind(&class::getter, this))
211 #define SURGSIM_ADD_SERIALIZABLE_PROPERTY(class, type, property, getter, setter) \
212 setAccessors(#property, \
213 std::bind(&class::getter, this),\
214 std::bind(&class::setter, this, std::bind(SurgSim::Framework::convert<type>,std::placeholders::_1)));\
215 setSerializable(#property,\
216 std::bind(&YAML::convert<type>::encode, std::bind(&class::getter, this)),\
217 std::bind(&class::setter, this, std::bind(&YAML::Node::as<type>,std::placeholders::_1)))
std::function< void(boost::any)> SetterType
Definition: Accessible.h:47
Definition: DriveElementFromInputBehavior.cpp:27
std::string name
Definition: Accessible.h:173
~Accessible()
Destructor.
Definition: Accessible.cpp:31
Private struct to keep the map under control.
Definition: Accessible.h:157
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 t...
Definition: Accessible-inl.h:42
std::weak_ptr< Accessible > accessible
Definition: Accessible.h:172
SetterType setter
Definition: Accessible.h:160
void setSerializable(const std::string &name, EncoderType encoder, DecoderType decoder)
Sets the functions used to convert data from and to a YAML::Node.
Definition: Accessible.cpp:118
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 converte...
Definition: Accessible.cpp:141
Accessible & operator=(const Accessible &other)
SurgSim::Math::Matrix44f convert(boost::any val)
Specialization for convert() to correctly cast Matrix44d to Matrix44f, will throw if the val is no...
Definition: Accessible.cpp:199
Public struct to pair an accessible with its appropriate property.
Definition: Accessible.h:170
bool isWriteable(const std::string &name) const
Check whether a property is writable.
Definition: Accessible.cpp:112
EncoderType encoder
Definition: Accessible.h:161
std::function< void(const YAML::Node *)> DecoderType
Definition: Accessible.h:50
void setValue(const std::string &name, const boost::any &value)
Sets a value of a property that has setter.
Definition: Accessible.cpp:58
Accessible()
Default Constructor.
Definition: Accessible.cpp:26
std::unordered_map< std::string, Functors > m_functors
Definition: Accessible.h:165
GetterType getter
Definition: Accessible.h:159
Eigen::Matrix< float, 4, 4, Eigen::RowMajor > Matrix44f
A 4x4 matrix of floats.
Definition: Matrix.h:43
void setSetter(const std::string &name, SetterType func)
Sets a setter for a given property.
Definition: Accessible.cpp:81
bool isReadable(const std::string &name) const
Check whether a property is readable.
Definition: Accessible.cpp:106
void setGetter(const std::string &name, GetterType func)
Sets a getter for a given property.
Definition: Accessible.cpp:74
void setAccessors(const std::string &name, GetterType getter, SetterType setter)
Sets the accessors getter and setter in one function.
Definition: Accessible.cpp:88
std::function< YAML::Node(void)> EncoderType
Definition: Accessible.h:49
Definitions of small fixed-size square matrix types.
std::function< boost::any(void)> GetterType
Definition: Accessible.h:46
YAML::Node encode() const
Encode this Accessible to a YAML::Node.
Definition: Accessible.cpp:127
DecoderType decoder
Definition: Accessible.h:162
Mixin class for enabling a property system on OSS classes, the instance still needs to initialize pro...
Definition: Accessible.h:36
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 t...
Definition: Accessible.cpp:182
void removeAccessors(const std::string &name)
Removes all the accessors (getter and setter) for a given property.
Definition: Accessible.cpp:95