Go to the documentation of this file.
16 #ifndef SURGSIM_FRAMEWORK_ACCESSIBLE_H
17 #define SURGSIM_FRAMEWORK_ACCESSIBLE_H
19 #include <boost/any.hpp>
20 #include <boost/preprocessor.hpp>
24 #include <unordered_map>
25 #include <yaml-cpp/yaml.h>
60 T
getValue(
const std::string& name)
const;
66 boost::any
getValue(
const std::string& name)
const;
75 bool getValue(
const std::string& name, T* value)
const;
81 void setValue(
const std::string& name,
const boost::any& value);
86 bool isReadable(
const std::string& name)
const;
145 YAML::Node
encode()
const;
153 void decode(
const YAML::Node& node,
const std::vector<std::string>& ignoredProperties = std::vector<std::string>());
208 std::string
convert(boost::any val);
213 #define SURGSIM_ADD_RW_PROPERTY(class, type, property, getter, setter) \
214 setAccessors(#property, \
215 std::bind(&class::getter, this),\
216 std::bind(&class::setter, this, std::bind(SurgSim::Framework::convert<type>,std::placeholders::_1)))
219 #define SURGSIM_ADD_RO_PROPERTY(class, type, property, getter) \
220 setGetter(#property, \
221 std::bind(&class::getter, this))
225 #define SURGSIM_ADD_SERIALIZABLE_PROPERTY(class, type, property, getter, setter) \
226 setAccessors(#property, \
227 std::bind(&class::getter, this),\
228 std::bind(&class::setter, this, std::bind(SurgSim::Framework::convert<type>,std::placeholders::_1)));\
229 setSerializable(#property,\
230 std::bind(&YAML::convert<type>::encode, std::bind(&class::getter, this)),\
231 std::bind(&class::setter, this, std::bind(&YAML::Node::as<type>,std::placeholders::_1)))
236 #define SURGSIM_ADD_SETTER(class, type, property, setter) \
238 setDecoder(#property, std::bind((void(class::*)(const type&))&class::setter, this,\
239 std::bind(&YAML::Node::as<type>,std::placeholders::_1))); \
240 setSetter(#property, std::bind((void(class::*)(const type&))&class::setter, this,\
241 std::bind(SurgSim::Framework::convert<type>,std::placeholders::_1)));\
248 #define SURGSIM_ENUM_TOSTRING(r, data, elem) \
250 result = BOOST_PP_STRINGIZE(elem); \
253 #define SURGSIM_ENUM_FROMSTRING(r, data, elem) \
254 if (value == BOOST_PP_STRINGIZE(elem)) \
261 #define SURGSIM_ENUM_TYPE int8_t
267 #define SURGSIM_SERIALIZABLE_ENUM(name, enumerators) \
268 enum name : SURGSIM_ENUM_TYPE\
270 BOOST_PP_SEQ_ENUM(enumerators) \
275 struct convert<name> \
277 static Node encode(const name& rhs) \
282 BOOST_PP_SEQ_FOR_EACH(SURGSIM_ENUM_TOSTRING, name, enumerators) \
284 SURGSIM_FAILURE() << "Can not find enum value in " << #name << ": " << rhs; \
288 static bool decode(const Node& node, name& rhs) \
290 std::string value = node.as<std::string>(); \
291 std::transform(value.begin(), value.end(), value.begin(), ::toupper); \
292 BOOST_PP_SEQ_FOR_EACH(SURGSIM_ENUM_FROMSTRING, name, enumerators) \
293 SURGSIM_FAILURE() << "Unknown " << #name << ": " << value; \
void setAccessors(const std::string &name, GetterType getter, SetterType setter)
Sets the accessors getter and setter in one function.
Definition: Accessible.cpp:86
Functors()
Definition: Accessible.h:166
std::function< void(const YAML::Node *)> DecoderType
Definition: Accessible.h:51
YAML::Node encode() const
Encode this Accessible to a YAML::Node.
Definition: Accessible.cpp:132
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:116
SetterType setter
Definition: Accessible.h:168
std::function< YAML::Node(void)> EncoderType
Definition: Accessible.h:50
void setGetter(const std::string &name, GetterType func)
Sets a getter for a given property.
Definition: Accessible.cpp:74
std::string name
Definition: Accessible.h:181
Definition: CompoundShapeToGraphics.cpp:29
std::unordered_map< std::string, Functors > m_functors
Definition: Accessible.h:173
Mixin class for enabling a property system on OSS classes, the instance still needs to initialize pro...
Definition: Accessible.h:37
std::weak_ptr< Accessible > accessible
Definition: Accessible.h:180
Public struct to pair an accessible with its appropriate property.
Definition: Accessible.h:178
EncoderType encoder
Definition: Accessible.h:169
~Accessible()
Destructor.
Definition: Accessible.cpp:31
void setDecoder(const std::string &name, DecoderType decoder)
Sets the functions used to convert data from a YAML::Node.
Definition: Accessible.cpp:125
std::function< boost::any(void)> GetterType
Definition: Accessible.h:47
GetterType getter
Definition: Accessible.h:167
Eigen::Matrix< float, 4, 4, Eigen::RowMajor > Matrix44f
A 4x4 matrix of floats.
Definition: Matrix.h:43
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
DecoderType decoder
Definition: Accessible.h:170
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:146
Private struct to keep the map under control.
Definition: Accessible.h:164
void removeAccessors(const std::string &name)
Removes all the accessors (getter and setter) for a given property.
Definition: Accessible.cpp:93
bool isWriteable(const std::string &name) const
Check whether a property is writable.
Definition: Accessible.cpp:110
Accessible()
Default Constructor.
Definition: Accessible.cpp:26
void setSetter(const std::string &name, SetterType func)
Sets a setter for a given property.
Definition: Accessible.cpp:80
std::function< void(boost::any)> SetterType
Definition: Accessible.h:48
bool isReadable(const std::string &name) const
Check whether a property is readable.
Definition: Accessible.cpp:104
Accessible & operator=(const Accessible &other)
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:191
void setValue(const std::string &name, const boost::any &value)
Sets a value of a property that has setter.
Definition: Accessible.cpp:58
SurgSim::Math::Matrix44f convert(boost::any val)
Specialization for convert<T>() to correctly cast Matrix44d to Matrix44f, will throw if the val is no...
Definition: Accessible.cpp:210