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> 47 typedef std::function<boost::any(void)> GetterType;
48 typedef std::function<void (boost::any)> SetterType;
50 typedef std::function<YAML::Node(void)> EncoderType;
51 typedef std::function<void(const YAML::Node*)> DecoderType;
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;
97 void setGetter(
const std::string& name, GetterType func);
103 void setSetter(
const std::string& name, SetterType func);
110 void setAccessors(
const std::string& name, GetterType getter, SetterType setter);
132 void setSerializable(
const std::string& name, EncoderType encoder, DecoderType decoder);
140 void setDecoder(
const std::string& name, DecoderType decoder);
145 YAML::Node
encode()
const;
153 void decode(
const YAML::Node& node,
const std::vector<std::string>& ignoredProperties = std::vector<std::string>());
155 std::vector<std::string> getProperties();
167 Functors() : getter(
nullptr), setter(
nullptr), encoder(
nullptr), decoder(
nullptr) {}
174 std::unordered_map<std::string, Functors> m_functors;
181 std::weak_ptr<Accessible> accessible;
195 T convert(boost::any val);
209 std::string convert(boost::any val);
214 #define SURGSIM_ADD_RW_PROPERTY(class, type, property, getter, setter) \ 215 setAccessors(#property, \ 216 std::bind(&class::getter, this),\ 217 std::bind(&class::setter, this, std::bind(SurgSim::Framework::convert<type>,std::placeholders::_1))) 220 #define SURGSIM_ADD_RO_PROPERTY(class, type, property, getter) \ 221 setGetter(#property, \ 222 std::bind(&class::getter, this)) 226 #define SURGSIM_ADD_SERIALIZABLE_PROPERTY(class, type, property, getter, setter) \ 227 setAccessors(#property, \ 228 std::bind(&class::getter, this),\ 229 std::bind(&class::setter, this, std::bind(SurgSim::Framework::convert<type>,std::placeholders::_1)));\ 230 setSerializable(#property,\ 231 std::bind(&YAML::convert<type>::encode, std::bind(&class::getter, this)),\ 232 std::bind(&class::setter, this, std::bind(&YAML::Node::as<type>,std::placeholders::_1))) 237 #define SURGSIM_ADD_SETTER(class, type, property, setter) \ 239 setDecoder(#property, std::bind((void(class::*)(const type&))&class::setter, this,\ 240 std::bind(&YAML::Node::as<type>,std::placeholders::_1))); \ 241 setSetter(#property, std::bind((void(class::*)(const type&))&class::setter, this,\ 242 std::bind(SurgSim::Framework::convert<type>,std::placeholders::_1)));\ 249 #define SURGSIM_ENUM_TOSTRING(r, data, elem) \ 251 result = BOOST_PP_STRINGIZE(elem); \ 254 #define SURGSIM_ENUM_FROMSTRING(r, data, elem) \ 255 if (value == BOOST_PP_STRINGIZE(elem)) \ 262 #define SURGSIM_ENUM_TYPE int8_t 268 #define SURGSIM_SERIALIZABLE_ENUM(name, enumerators) \ 269 enum name : SURGSIM_ENUM_TYPE\ 271 BOOST_PP_SEQ_ENUM(enumerators) \ 276 struct convert<name> \ 278 static Node encode(const name& rhs) \ 283 BOOST_PP_SEQ_FOR_EACH(SURGSIM_ENUM_TOSTRING, name, enumerators) \ 285 SURGSIM_FAILURE() << "Can not find enum value in " << #name << ": " << rhs; \ 289 static bool decode(const Node& node, name& rhs) \ 291 std::string value = node.as<std::string>(); \ 292 std::transform(value.begin(), value.end(), value.begin(), ::toupper); \ 293 BOOST_PP_SEQ_FOR_EACH(SURGSIM_ENUM_FROMSTRING, name, enumerators) \ 294 SURGSIM_FAILURE() << "Unknown " << #name << ": " << value; \ 300 #include "SurgSim/Framework/Accessible-inl.h" bool isReadable(const std::string &name) const
Check whether a property is readable.
Definition: Accessible.cpp:104
Wraps glewInit() to separate the glew opengl definitions from the osg opengl definitions only imgui n...
Definition: AddRandomSphereBehavior.cpp:36
~Accessible()
Destructor.
Definition: Accessible.cpp:31
bool isWriteable(const std::string &name) const
Check whether a property is writable.
Definition: Accessible.cpp:110
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
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
YAML::Node encode() const
Encode this Accessible to a YAML::Node.
Definition: Accessible.cpp:132
Public struct to pair an accessible with its appropriate property.
Definition: Accessible.h:179
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
void setSetter(const std::string &name, SetterType func)
Sets a setter for a given property.
Definition: Accessible.cpp:80
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:86
Definitions of small fixed-size square matrix types.
Eigen::Matrix< float, 4, 4, Eigen::RowMajor > Matrix44f
A 4x4 matrix of floats.
Definition: Matrix.h:43
void setDecoder(const std::string &name, DecoderType decoder)
Sets the functions used to convert data from a YAML::Node.
Definition: Accessible.cpp:125
Mixin class for enabling a property system on OSS classes, the instance still needs to initialize pro...
Definition: Accessible.h:37
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:204
void removeAccessors(const std::string &name)
Removes all the accessors (getter and setter) for a given property.
Definition: Accessible.cpp:93
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