dynamic-graph  4.3.1
Dynamic graph library
command-direct-setter.h
1 //
2 // Copyright 2010 CNRS
3 //
4 // Author: Nicolas Mansard
5 //
6 
7 #ifndef __dg_command_direct_setter_h__
8 #define __dg_command_direct_setter_h__
9 
10 /* Define a setter command directly on the attribute (no need to pass by
11  * an explicit function). A typical use is given here:
12  * addCommand("setSize",
13  * makeDirectSetter(*this,&_dimension,
14  * docDirectSetter("dimension","int")));
15  *
16  */
17 
18 #include "dynamic-graph/command.h"
19 #include <boost/assign/list_of.hpp>
20 
21 /* --- SETTER --------------------------------------------------------- */
22 namespace dynamicgraph {
23 namespace command {
24 
25 template <class E, typename T> class DirectSetter : public Command {
26 public:
27  DirectSetter(E &entity, T *ptr, const std::string &docString)
28  : Command(entity, boost::assign::list_of(ValueHelper<T>::TypeID),
29  docString),
30  T_ptr(ptr) {}
31 
32 protected:
33  virtual Value doExecute() {
34  const std::vector<Value> &values = getParameterValues();
35  T val = values[0].value();
36  (*T_ptr) = val;
37  return Value(); // void
38  }
39 
40 private:
41  T *T_ptr;
42 };
43 
44 template <class E, typename T>
45 DirectSetter<E, T> *makeDirectSetter(E &entity, T *ptr,
46  const std::string &docString) {
47  return new DirectSetter<E, T>(entity, ptr, docString);
48 }
49 
50 inline std::string docDirectSetter(const std::string &name,
51  const std::string &type) {
52  return std::string("\nSet the ") + name + ".\n\nInput:\n - a " + type +
53  ".\nVoid return.\n\n";
54 }
55 
56 } // namespace command
57 } // namespace dynamicgraph
58 
59 #endif // __dg_command_direct_setter_h__
Command(Entity &entity, const std::vector< Value::Type > &valueTypes, const std::string &docstring)
virtual Value doExecute()
Specific action performed by the command.
This class implements a variant design pattern to handle basic types in Command.
Definition: value.h:46
const std::vector< Value > & getParameterValues() const
Get parameter values.