Accessible.h
Go to the documentation of this file.
1 // This file is a part of the OpenSurgSim project.
2 // Copyright 2013-2015, SimQuest Solutions Inc.
3 //
4 // Licensed under the Apache License, Version 2.0 (the "License");
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at
7 //
8 // http://www.apache.org/licenses/LICENSE-2.0
9 //
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an "AS IS" BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
15 
16 #ifndef SURGSIM_FRAMEWORK_ACCESSIBLE_H
17 #define SURGSIM_FRAMEWORK_ACCESSIBLE_H
18 
19 #include <boost/any.hpp>
20 #include <boost/preprocessor.hpp>
21 #include <functional>
22 #include <memory>
23 #include <string>
24 #include <unordered_map>
25 #include <yaml-cpp/yaml.h>
26 
27 #include "SurgSim/Math/Matrix.h"
28 
29 namespace SurgSim
30 {
31 namespace Framework
32 {
33 
38 {
39 public:
40 
42  Accessible();
43 
45  ~Accessible();
46 
47  typedef std::function<boost::any(void)> GetterType;
48  typedef std::function<void (boost::any)> SetterType;
49 
50  typedef std::function<YAML::Node(void)> EncoderType;
51  typedef std::function<void(const YAML::Node*)> DecoderType;
52 
59  template <class T>
60  T getValue(const std::string& name) const;
61 
66  boost::any getValue(const std::string& name) const;
67 
74  template <class T>
75  bool getValue(const std::string& name, T* value) const;
76 
81  void setValue(const std::string& name, const boost::any& value);
82 
86  bool isReadable(const std::string& name) const;
87 
91  bool isWriteable(const std::string& name) const;
92 
97  void setGetter(const std::string& name, GetterType func);
98 
103  void setSetter(const std::string& name, SetterType func);
104 
110  void setAccessors(const std::string& name, GetterType getter, SetterType setter);
111 
114  void removeAccessors(const std::string& name);
115 
124  void forwardProperty(const std::string& name, const Accessible& target, const std::string& targetProperty);
125 
132  void setSerializable(const std::string& name, EncoderType encoder, DecoderType decoder);
133 
140  void setDecoder(const std::string& name, DecoderType decoder);
141 
142 
145  YAML::Node encode() const;
146 
153  void decode(const YAML::Node& node, const std::vector<std::string>& ignoredProperties = std::vector<std::string>());
154 
155 private:
156 
159  Accessible(const Accessible& other) /*= delete*/;
160  Accessible& operator=(const Accessible& other) /*= delete*/;
162 
164  struct Functors
165  {
166  Functors() : getter(nullptr), setter(nullptr), encoder(nullptr), decoder(nullptr) {}
171  };
172 
173  std::unordered_map<std::string, Functors> m_functors;
174 
175 };
176 
178 struct Property
179 {
180  std::weak_ptr<Accessible> accessible;
182 };
183 
184 template <>
185 boost::any Accessible::getValue(const std::string& name) const;
186 
187 
193 template <class T>
194 T convert(boost::any val);
195 
201 template <>
202 SurgSim::Math::Matrix44f convert(boost::any val);
203 
207 template <>
208 std::string convert(boost::any val);
209 
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)))
217 
219 #define SURGSIM_ADD_RO_PROPERTY(class, type, property, getter) \
220  setGetter(#property, \
221  std::bind(&class::getter, this))
222 
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)))
232 
236 #define SURGSIM_ADD_SETTER(class, type, property, setter) \
237  {\
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)));\
242  }
243 
244 
245 }; // Framework
246 }; // SurgSim
247 
248 #define SURGSIM_ENUM_TOSTRING(r, data, elem) \
249  case data::elem: \
250  result = BOOST_PP_STRINGIZE(elem); \
251  break;
252 
253 #define SURGSIM_ENUM_FROMSTRING(r, data, elem) \
254  if (value == BOOST_PP_STRINGIZE(elem)) \
255  { \
256  rhs = data::elem; \
257  return true; \
258  }
259 
261 #define SURGSIM_ENUM_TYPE int8_t
262 
267 #define SURGSIM_SERIALIZABLE_ENUM(name, enumerators) \
268  enum name : SURGSIM_ENUM_TYPE\
269  { \
270  BOOST_PP_SEQ_ENUM(enumerators) \
271  }; \
272  namespace YAML \
273  { \
274  template <> \
275  struct convert<name> \
276  { \
277  static Node encode(const name& rhs) \
278  { \
279  Node result; \
280  switch (rhs) \
281  { \
282  BOOST_PP_SEQ_FOR_EACH(SURGSIM_ENUM_TOSTRING, name, enumerators) \
283  default: \
284  SURGSIM_FAILURE() << "Can not find enum value in " << #name << ": " << rhs; \
285  } \
286  return result; \
287  } \
288  static bool decode(const Node& node, name& rhs) \
289  { \
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; \
294  return false; \
295  } \
296  }; \
297  }
298 
300 
301 #endif
SurgSim::Framework::Accessible::setAccessors
void setAccessors(const std::string &name, GetterType getter, SetterType setter)
Sets the accessors getter and setter in one function.
Definition: Accessible.cpp:86
SurgSim::Framework::Accessible::Functors::Functors
Functors()
Definition: Accessible.h:166
SurgSim::Framework::Accessible::DecoderType
std::function< void(const YAML::Node *)> DecoderType
Definition: Accessible.h:51
SurgSim::Framework::Accessible::encode
YAML::Node encode() const
Encode this Accessible to a YAML::Node.
Definition: Accessible.cpp:132
SurgSim::Framework::Accessible::setSerializable
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
SurgSim::Framework::Accessible::Functors::setter
SetterType setter
Definition: Accessible.h:168
Accessible-inl.h
Matrix.h
SurgSim::Framework::Accessible::EncoderType
std::function< YAML::Node(void)> EncoderType
Definition: Accessible.h:50
SurgSim::Framework::Accessible::setGetter
void setGetter(const std::string &name, GetterType func)
Sets a getter for a given property.
Definition: Accessible.cpp:74
SurgSim::Framework::Property::name
std::string name
Definition: Accessible.h:181
SurgSim
Definition: CompoundShapeToGraphics.cpp:29
SurgSim::Framework::Accessible::m_functors
std::unordered_map< std::string, Functors > m_functors
Definition: Accessible.h:173
SurgSim::Framework::Accessible
Mixin class for enabling a property system on OSS classes, the instance still needs to initialize pro...
Definition: Accessible.h:37
SurgSim::Framework::Property::accessible
std::weak_ptr< Accessible > accessible
Definition: Accessible.h:180
SurgSim::Framework::Property
Public struct to pair an accessible with its appropriate property.
Definition: Accessible.h:178
SurgSim::Framework::convert
SurgSim::Math::Matrix44f convert(boost::any val)
Wrap boost::any_cast to use in std::bind, for some reason it does not work by itself.
Definition: Accessible.cpp:210
SurgSim::Framework::Accessible::Functors::encoder
EncoderType encoder
Definition: Accessible.h:169
SurgSim::Framework::Accessible::~Accessible
~Accessible()
Destructor.
Definition: Accessible.cpp:31
SurgSim::Framework::Accessible::setDecoder
void setDecoder(const std::string &name, DecoderType decoder)
Sets the functions used to convert data from a YAML::Node.
Definition: Accessible.cpp:125
SurgSim::Framework::Accessible::GetterType
std::function< boost::any(void)> GetterType
Definition: Accessible.h:47
SurgSim::Framework::Accessible::Functors::getter
GetterType getter
Definition: Accessible.h:167
SurgSim::Math::Matrix44f
Eigen::Matrix< float, 4, 4, Eigen::RowMajor > Matrix44f
A 4x4 matrix of floats.
Definition: Matrix.h:43
SurgSim::Framework::Accessible::getValue
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
SurgSim::Framework::Accessible::Functors::decoder
DecoderType decoder
Definition: Accessible.h:170
SurgSim::Framework::Accessible::decode
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
SurgSim::Framework::Accessible::Functors
Private struct to keep the map under control.
Definition: Accessible.h:164
SurgSim::Framework::Accessible::removeAccessors
void removeAccessors(const std::string &name)
Removes all the accessors (getter and setter) for a given property.
Definition: Accessible.cpp:93
SurgSim::Framework::Accessible::isWriteable
bool isWriteable(const std::string &name) const
Check whether a property is writable.
Definition: Accessible.cpp:110
SurgSim::Framework::Accessible::Accessible
Accessible()
Default Constructor.
Definition: Accessible.cpp:26
SurgSim::Framework::Accessible::setSetter
void setSetter(const std::string &name, SetterType func)
Sets a setter for a given property.
Definition: Accessible.cpp:80
SurgSim::Framework::Accessible::SetterType
std::function< void(boost::any)> SetterType
Definition: Accessible.h:48
string
string(TOUPPER ${DEVICE} DEVICE_UPPER_CASE) option(BUILD_DEVICE_$
Definition: CMakeLists.txt:38
SurgSim::Framework::Accessible::isReadable
bool isReadable(const std::string &name) const
Check whether a property is readable.
Definition: Accessible.cpp:104
SurgSim::Framework::Accessible::operator=
Accessible & operator=(const Accessible &other)
SurgSim::Framework::Accessible::forwardProperty
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
SurgSim::Framework::Accessible::setValue
void setValue(const std::string &name, const boost::any &value)
Sets a value of a property that has setter.
Definition: Accessible.cpp:58