All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
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 
145  void decode(const YAML::Node& node);
146 
147 private:
148 
151  Accessible(const Accessible& other) /*= delete*/;
152  Accessible& operator=(const Accessible& other) /*= delete*/;
154 
156  struct Functors
157  {
162  };
163 
164  std::unordered_map<std::string, Functors> m_functors;
165 
166 };
167 
169 struct Property
170 {
171  std::weak_ptr<Accessible> accessible;
172  std::string name;
173 };
174 
175 template <>
176 boost::any Accessible::getValue(const std::string& name) const;
177 
178 
184 template <class T>
185 T convert(boost::any val);
186 
192 template <>
193 SurgSim::Math::Matrix44f convert(boost::any val);
194 
198 #define SURGSIM_ADD_RW_PROPERTY(class, type, property, getter, setter) \
199  setAccessors(#property, \
200  std::bind(&class::getter, this),\
201  std::bind(&class::setter, this, std::bind(SurgSim::Framework::convert<type>,std::placeholders::_1)))
202 
204 #define SURGSIM_ADD_RO_PROPERTY(class, type, property, getter) \
205  setGetter(#property, \
206  std::bind(&class::getter, this))
207 
210 #define SURGSIM_ADD_SERIALIZABLE_PROPERTY(class, type, property, getter, setter) \
211  setAccessors(#property, \
212  std::bind(&class::getter, this),\
213  std::bind(&class::setter, this, std::bind(SurgSim::Framework::convert<type>,std::placeholders::_1)));\
214  setSerializable(#property,\
215  std::bind(&YAML::convert<type>::encode, std::bind(&class::getter, this)),\
216  std::bind(&class::setter, this, std::bind(&YAML::Node::as<type>,std::placeholders::_1)))
217 
218 }; // Framework
219 }; // SurgSim
220 
222 
223 #endif
std::function< void(boost::any)> SetterType
Definition: Accessible.h:47
std::string name
Definition: Accessible.h:172
~Accessible()
Destructor.
Definition: Accessible.cpp:29
Private struct to keep the map under control.
Definition: Accessible.h:156
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:171
SetterType setter
Definition: Accessible.h:159
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
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:180
Public struct to pair an accessible with its appropriate property.
Definition: Accessible.h:169
void decode(const YAML::Node &node)
Decode this Accessible from a YAML::Node, will throw an exception if the data type cannot be converte...
Definition: Accessible.cpp:139
bool isWriteable(const std::string &name) const
Check whether a property is writable.
Definition: Accessible.cpp:110
EncoderType encoder
Definition: Accessible.h:160
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:56
Accessible()
Default Constructor.
Definition: Accessible.cpp:24
std::unordered_map< std::string, Functors > m_functors
Definition: Accessible.h:164
GetterType getter
Definition: Accessible.h:158
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:79
bool isReadable(const std::string &name) const
Check whether a property is readable.
Definition: Accessible.cpp:104
void setGetter(const std::string &name, GetterType func)
Sets a getter for a given property.
Definition: Accessible.cpp:72
void setAccessors(const std::string &name, GetterType getter, SetterType setter)
Sets the accessors getter and setter in one function.
Definition: Accessible.cpp:86
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:125
DecoderType decoder
Definition: Accessible.h:161
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:163
void removeAccessors(const std::string &name)
Removes all the accessors (getter and setter) for a given property.
Definition: Accessible.cpp:93