dynamic-graph  4.2.2
Dynamic graph library
command-setter.t.cpp
1 //
2 // Copyright 2010 CNRS
3 //
4 // Author: Florent Lamiraux
5 //
6 
7 #ifndef DYNAMIC_GRAPH_COMMAND_SETTER_T_CPP
8 #define DYNAMIC_GRAPH_COMMAND_SETTER_T_CPP
9 
10 #include "dynamic-graph/command-setter.h"
11 #include "dynamic-graph/linear-algebra.h"
12 #include <boost/assign/list_of.hpp>
13 #include <sstream>
14 
15 namespace dynamicgraph {
16 class Entity;
17 namespace command {
18 
19 //
20 // Template specialization: bool
21 //
22 template <class E> class Setter<E, bool> : public Command {
23 public:
25  typedef void (E::*SetterMethod)(const bool &);
27  Setter(E &entity, SetterMethod setterMethod, const std::string &docString);
28 
29 protected:
30  virtual Value doExecute();
31 
32 private:
33  SetterMethod setterMethod_;
34 }; // Class Setter
35 
36 template <class E>
37 Setter<E, bool>::Setter(E &entity, SetterMethod setterMethod,
38  const std::string &docString)
39  : Command(entity, boost::assign::list_of(Value::BOOL), docString),
40  setterMethod_(setterMethod) {}
41 
42 template <class E> Value Setter<E, bool>::doExecute() {
43  const std::vector<Value> &values = getParameterValues();
44  // Get parameter
45  bool value = values[0].value();
46  E &entity = static_cast<E &>(owner());
47  (entity.*setterMethod_)(value);
48  return Value();
49 }
50 
51 //
52 // Template specialization: unsigned
53 //
54 template <class E> class Setter<E, unsigned> : public Command {
55 public:
57  typedef void (E::*SetterMethod)(const unsigned &);
59  Setter(E &entity, SetterMethod setterMethod, const std::string &docString);
60 
61 protected:
62  virtual Value doExecute();
63 
64 private:
65  SetterMethod setterMethod_;
66 }; // Class Setter
67 
68 template <class E>
69 Setter<E, unsigned>::Setter(E &entity, SetterMethod setterMethod,
70  const std::string &docString)
71  : Command(entity, boost::assign::list_of(Value::UNSIGNED), docString),
72  setterMethod_(setterMethod) {}
73 
74 template <class E> Value Setter<E, unsigned>::doExecute() {
75  const std::vector<Value> &values = getParameterValues();
76  // Get parameter
77  unsigned value = values[0].value();
78  E &entity = static_cast<E &>(owner());
79  (entity.*setterMethod_)(value);
80  return Value();
81 }
82 
83 //
84 // Template specialization: int
85 //
86 template <class E> class Setter<E, int> : public Command {
87 public:
89  typedef void (E::*SetterMethod)(const int &);
91  Setter(E &entity, SetterMethod setterMethod, const std::string &docString);
92 
93 protected:
94  virtual Value doExecute();
95 
96 private:
97  SetterMethod setterMethod_;
98 }; // Class Setter
99 
100 template <class E>
101 Setter<E, int>::Setter(E &entity, SetterMethod setterMethod,
102  const std::string &docString)
103  : Command(entity, boost::assign::list_of(Value::INT), docString),
104  setterMethod_(setterMethod) {}
105 
106 template <class E> Value Setter<E, int>::doExecute() {
107  const std::vector<Value> &values = getParameterValues();
108  // Get parameter
109  int value = values[0].value();
110  E &entity = static_cast<E &>(owner());
111  (entity.*setterMethod_)(value);
112  return Value();
113 }
114 
115 //
116 // Template specialization: float
117 //
118 template <class E> class Setter<E, float> : public Command {
119 public:
121  typedef void (E::*SetterMethod)(const float &);
123  Setter(E &entity, SetterMethod setterMethod, const std::string &docString);
124 
125 protected:
126  virtual Value doExecute();
127 
128 private:
129  SetterMethod setterMethod_;
130 }; // Class Setter
131 
132 template <class E>
133 Setter<E, float>::Setter(E &entity, SetterMethod setterMethod,
134  const std::string &docString)
135  : Command(entity, boost::assign::list_of(Value::FLOAT), docString),
136  setterMethod_(setterMethod) {}
137 
138 template <class E> Value Setter<E, float>::doExecute() {
139  const std::vector<Value> &values = getParameterValues();
140  // Get parameter
141  float value = values[0].value();
142  E &entity = static_cast<E &>(owner());
143  (entity.*setterMethod_)(value);
144  return Value();
145 }
146 
147 //
148 // Template specialization: double
149 //
150 template <class E> class Setter<E, double> : public Command {
151 public:
153  typedef void (E::*SetterMethod)(const double &);
155  Setter(E &entity, SetterMethod setterMethod, const std::string &docString);
156 
157 protected:
158  virtual Value doExecute();
159 
160 private:
161  SetterMethod setterMethod_;
162 }; // Class Setter
163 
164 template <class E>
165 Setter<E, double>::Setter(E &entity, SetterMethod setterMethod,
166  const std::string &docString)
167  : Command(entity, boost::assign::list_of(Value::DOUBLE), docString),
168  setterMethod_(setterMethod) {}
169 
170 template <class E> Value Setter<E, double>::doExecute() {
171  const std::vector<Value> &values = getParameterValues();
172  // Get parameter
173  double value = values[0].value();
174  E &entity = static_cast<E &>(owner());
175  (entity.*setterMethod_)(value);
176  return Value();
177 }
178 
179 //
180 // Template specialization: std::string
181 //
182 template <class E> class Setter<E, std::string> : public Command {
183 public:
185  typedef void (E::*SetterMethod)(const std::string &);
187  Setter(E &entity, SetterMethod setterMethod, const std::string &docString);
188 
189 protected:
190  virtual Value doExecute();
191 
192 private:
193  SetterMethod setterMethod_;
194 }; // Class Setter
195 
196 template <class E>
198  const std::string &docString)
199  : Command(entity, boost::assign::list_of(Value::STRING), docString),
200  setterMethod_(setterMethod) {}
201 
203  const std::vector<Value> &values = getParameterValues();
204  // Get parameter
205  std::string value = values[0].value();
206  E &entity = static_cast<E &>(owner());
207  (entity.*setterMethod_)(value);
208  return Value();
209 }
210 
211 //
212 // Template specialization: Vector
213 //
214 template <class E> class Setter<E, Vector> : public Command {
215 public:
217  typedef void (E::*SetterMethod)(const Vector &);
219  Setter(E &entity, SetterMethod setterMethod, const std::string &docString);
220 
221 protected:
222  virtual Value doExecute();
223 
224 private:
225  SetterMethod setterMethod_;
226 }; // Class Setter
227 
228 template <class E>
229 Setter<E, Vector>::Setter(E &entity, SetterMethod setterMethod,
230  const std::string &docString)
231  : Command(entity, boost::assign::list_of(Value::VECTOR), docString),
232  setterMethod_(setterMethod) {}
233 
234 template <class E> Value Setter<E, Vector>::doExecute() {
235  const std::vector<Value> &values = getParameterValues();
236  // Get parameter
237  Vector value = values[0].value();
238  E &entity = static_cast<E &>(owner());
239  (entity.*setterMethod_)(value);
240  return Value();
241 }
242 
243 //
244 // Template specialization: Matrix
245 //
246 template <class E> class Setter<E, Matrix> : public Command {
247 public:
249  typedef void (E::*SetterMethod)(const Matrix &);
251  Setter(E &entity, SetterMethod setterMethod, const std::string &docString);
252 
253 protected:
254  virtual Value doExecute();
255 
256 private:
257  SetterMethod setterMethod_;
258 }; // Class Setter
259 
260 template <class E>
261 Setter<E, Matrix>::Setter(E &entity, SetterMethod setterMethod,
262  const std::string &docString)
263  : Command(entity, boost::assign::list_of(Value::MATRIX), docString),
264  setterMethod_(setterMethod) {}
265 
266 template <class E> Value Setter<E, Matrix>::doExecute() {
267  const std::vector<Value> &values = getParameterValues();
268  // Get parameter
269  Matrix value = values[0].value();
270  E &entity = static_cast<E &>(owner());
271  (entity.*setterMethod_)(value);
272  return Value();
273 }
274 
275 } // namespace command
276 } // namespace dynamicgraph
277 
278 #endif // DYNAMIC_GRAPH_COMMAND_SETTER_T_CPP
Entity & owner()
Get a reference to the Entity owning this command.
This class implements a variant design pattern to handle basic types in Command.
Definition: value.h:46
Setter(E &entity, SetterMethod setterMethod, const std::string &docString)
Constructor.
void(E::* SetterMethod)(const T &)
Pointer to method that sets parameter of type T.
const std::vector< Value > & getParameterValues() const
Get parameter values.
virtual Value doExecute()
Specific action performed by the command.