Accessible.h
Go to the documentation of this file.
1 // This file is a part of the OpenSurgSim project.
2 // Copyright 2013, 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 <string>
20 #include <memory>
21 #include <unordered_map>
22 #include <functional>
23 #include <boost/any.hpp>
24 #include <yaml-cpp/yaml.h>
25 
26 #include "SurgSim/Math/Matrix.h"
27 
28 namespace SurgSim
29 {
30 namespace Framework
31 {
32 
37 {
38 public:
39 
41  Accessible();
42 
44  ~Accessible();
45 
46  typedef std::function<boost::any(void)> GetterType;
47  typedef std::function<void (boost::any)> SetterType;
48 
49  typedef std::function<YAML::Node(void)> EncoderType;
50  typedef std::function<void(const YAML::Node*)> DecoderType;
51 
52 
53 
60  template <class T>
61  T getValue(const std::string& name) const;
62 
67  boost::any getValue(const std::string& name) const;
68 
69 
76  template <class T>
77  bool getValue(const std::string& name, T* value) const;
78 
83  void setValue(const std::string& name, const boost::any& value);
84 
88  bool isReadable(const std::string& name) const;
89 
93  bool isWriteable(const std::string& name) const;
94 
99  void setGetter(const std::string& name, GetterType func);
100 
105  void setSetter(const std::string& name, SetterType func);
106 
112  void setAccessors(const std::string& name, GetterType getter, SetterType setter);
113 
116  void removeAccessors(const std::string& name);
117 
126  void forwardProperty(const std::string& name, const Accessible& target, const std::string& targetProperty);
127 
134  void setSerializable(const std::string& name, EncoderType encoder, DecoderType decoder);
135 
138  YAML::Node encode() const;
139 
146  void decode(const YAML::Node& node, const std::vector<std::string>& ignoredProperties = std::vector<std::string>());
147 
148 private:
149 
152  Accessible(const Accessible& other) /*= delete*/;
153  Accessible& operator=(const Accessible& other) /*= delete*/;
155 
157  struct Functors
158  {
159  GetterType getter;
160  SetterType setter;
161  EncoderType encoder;
162  DecoderType decoder;
163  };
164 
165  std::unordered_map<std::string, Functors> m_functors;
166 
167 };
168 
170 struct Property
171 {
172  std::weak_ptr<Accessible> accessible;
174 };
175 
176 template <>
177 boost::any Accessible::getValue(const std::string& name) const;
178 
179 
185 template <class T>
186 T convert(boost::any val);
187 
193 template <>
194 SurgSim::Math::Matrix44f convert(boost::any val);
195 
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)))
203 
205 #define SURGSIM_ADD_RO_PROPERTY(class, type, property, getter) \
206  setGetter(#property, \
207  std::bind(&class::getter, this))
208 
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)))
218 
219 }; // Framework
220 }; // SurgSim
221 
223 
224 #endif
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<T>() 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
string(TOUPPER ${DEVICE}DEVICE_UPPER_CASE) option(BUILD_DEVICE_ $
Definition: CMakeLists.txt:35
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