dynamic-graph  4.1.0-8-gdab7-dirty
Dynamic graph library
command-bind.h
1 //
2 // Copyright 2010 CNRS
3 //
4 // Author: Nicolas Mansard
5 //
6 
7 #ifndef __dg_command_bind_h__
8 #define __dg_command_bind_h__
9 
10 /* Create a command from a bind directly. Examples are:
11 
12  * addCommand("myProcVoid",
13  * makeCommandVoid0(*this,&ClassName::functionNoArg,
14  * docCommandVoid0("Simple line doc here.")));
15 
16  * addCommand("myProcOneString",
17  * makeCommandVoid1(*this,&EntityName::functionOneStringArg,
18  * docCommandVoid1("Simple line doc here.",
19  * "string")));
20  *
21  */
22 
23 #include "dynamic-graph/command.h"
24 #include <boost/assign/list_of.hpp>
25 #include <boost/bind.hpp>
26 #include <boost/function.hpp>
27 
28 /* --- FUNCTION 0 ARGS ----------------------------------------------------- */
29 namespace dynamicgraph {
30 namespace command {
31 
32 template <class E> struct CommandVoid0 : public Command {
33  CommandVoid0(E &entity, boost::function<void(void)> function,
34  const std::string &docString)
35  : Command(entity, EMPTY_ARG, docString), fptr(function) {}
36 
37 protected:
38  virtual Value doExecute() {
39  assert(getParameterValues().size() == 0);
40  fptr();
41  return Value(); // void
42  }
43 
44 private:
45  boost::function<void(void)> fptr;
46 };
47 
48 template <class E>
49 CommandVoid0<E> *makeCommandVoid0(E &entity,
50  boost::function<void(void)> function,
51  const std::string &docString) {
52  return new CommandVoid0<E>(entity, function, docString);
53 }
54 
55 template <class E>
56 CommandVoid0<E> *makeCommandVoid0(E &entity,
57  boost::function<void(E *)> function,
58  const std::string &docString) {
59  return new CommandVoid0<E>(entity, boost::bind(function, &entity), docString);
60 }
61 
62 template <class E>
63 CommandVoid0<E> *makeCommandVoid0(E &entity, void (E::*function)(void),
64  const std::string &docString) {
65  return new CommandVoid0<E>(entity, boost::bind(function, &entity), docString);
66 }
67 
68 inline std::string docCommandVoid0(const std::string &doc) {
69  return std::string("\n") + doc + "\n\nNo input.\nVoid return.\n\n";
70 }
71 
72 } // namespace command
73 } // namespace dynamicgraph
74 
75 /* --- FUNCTION 1 ARGS ------------------------------------------------------ */
76 namespace dynamicgraph {
77 namespace command {
78 
79 template <class E, typename T> struct CommandVoid1 : public Command {
80  typedef boost::function<void(const T &)> function_t;
81 
82  CommandVoid1(E &entity, function_t function, const std::string &docString)
83  : Command(entity, boost::assign::list_of(ValueHelper<T>::TypeID),
84  docString),
85  fptr(function) {}
86 
87 protected:
88  virtual Value doExecute() {
89  assert(getParameterValues().size() == 1);
90  T val = getParameterValues()[0].value();
91  fptr(val);
92  return Value(); // void
93  }
94 
95 private:
96  function_t fptr;
97 };
98 
99 template <class E, typename T>
101 makeCommandVoid1(E &entity, boost::function<void(const T &)> function,
102  // typename CommandVoid1<E,T>::function_t function ,
103  const std::string &docString) {
104  return new CommandVoid1<E, T>(entity, function, docString);
105 }
106 
107 template <class E, typename T>
109 makeCommandVoid1(E &entity,
110  // The following syntaxt don't compile when not specializing
111  // the template arg... why ???
112  boost::function<void(E *, const T &)> function,
113  const std::string &docString) {
114  return new CommandVoid1<E, T>(entity, boost::bind(function, &entity, _1),
115  docString);
116 }
117 
118 template <class E, typename T>
119 CommandVoid1<E, T> *makeCommandVoid1(E &entity, void (E::*function)(const T &),
120  const std::string &docString) {
121  return new CommandVoid1<E, T>(entity, boost::bind(function, &entity, _1),
122  docString);
123  return NULL;
124 }
125 
126 inline std::string docCommandVoid1(const std::string &doc,
127  const std::string &type) {
128  return std::string("\n") + doc + "\n\nInput:\n - A " + type +
129  ".\nVoid return.\n\n";
130 }
131 
132 } // namespace command
133 } // namespace dynamicgraph
134 
135 /* --- FUNCTION 2 ARGS ------------------------------------------------------ */
136 namespace dynamicgraph {
137 namespace command {
138 
139 template <class E, typename T1, typename T2>
140 struct CommandVoid2 : public Command {
141  typedef boost::function<void(const T1 &, const T2 &)> function_t;
142 
143  CommandVoid2(E &entity, function_t function, const std::string &docString)
144  : Command(entity,
145  boost::assign::list_of(ValueHelper<T1>::TypeID)(
147  docString),
148  fptr(function) {}
149 
150 protected:
151  virtual Value doExecute() {
152  assert(getParameterValues().size() == 2);
153  T1 val1 = getParameterValues()[0].value();
154  T2 val2 = getParameterValues()[1].value();
155  fptr(val1, val2);
156  return Value(); // void
157  }
158 
159 private:
160  function_t fptr;
161 };
162 
163 template <class E, typename T1, typename T2>
165 makeCommandVoid2(E &entity,
166  boost::function<void(const T1 &, const T2 &)> function,
167  const std::string &docString) {
168  return new CommandVoid2<E, T1, T2>(entity, function, docString);
169 }
170 
171 template <class E, typename T1, typename T2>
173 makeCommandVoid2(E &entity,
174  // The following syntaxt don't compile when not specializing
175  // the template arg... why ???
176  boost::function<void(E *, const T1 &, const T2 &)> function,
177  const std::string &docString) {
178  return new CommandVoid2<E, T1, T2>(
179  entity, boost::bind(function, &entity, _1, _2), docString);
180 }
181 
182 template <class E, typename T1, typename T2>
184 makeCommandVoid2(E &entity, void (E::*function)(const T1 &, const T2 &),
185  const std::string &docString) {
186  return new CommandVoid2<E, T1, T2>(
187  entity, boost::bind(function, &entity, _1, _2), docString);
188  return NULL;
189 }
190 
191 inline std::string docCommandVoid2(const std::string &doc,
192  const std::string &type1,
193  const std::string &type2) {
194  return (std::string("\n") + doc + "\n\n" + "Input:\n - A " + type1 + ".\n" +
195  "Input:\n - A " + type2 + ".\n" + "Void return.\n\n");
196 }
197 
198 } // namespace command
199 } // namespace dynamicgraph
200 
201 /* --- FUNCTION 3 ARGS ------------------------------------------------------ */
202 namespace dynamicgraph {
203 namespace command {
204 
205 template <class E, typename T1, typename T2, typename T3>
206 struct CommandVoid3 : public Command {
207  typedef boost::function<void(const T1 &, const T2 &, const T3 &)> function_t;
208 
209  CommandVoid3(E &entity, function_t function, const std::string &docString)
210  : Command(entity,
211  boost::assign::list_of(ValueHelper<T1>::TypeID)(
213  docString),
214  fptr(function) {}
215 
216 protected:
217  virtual Value doExecute() {
218  assert(getParameterValues().size() == 3);
219  T1 val1 = getParameterValues()[0].value();
220  T2 val2 = getParameterValues()[1].value();
221  T3 val3 = getParameterValues()[2].value();
222  fptr(val1, val2, val3);
223  return Value(); // void
224  }
225 
226 private:
227  function_t fptr;
228 };
229 
230 template <class E, typename T1, typename T2, typename T3>
232 makeCommandVoid3(E &entity,
233  typename CommandVoid3<E, T1, T2, T3>::function_t function,
234  const std::string &docString) {
235  return new CommandVoid3<E, T1, T2, T3>(entity, function, docString);
236 }
237 
238 template <class E, typename T1, typename T2, typename T3>
239 CommandVoid3<E, T1, T2, T3> *makeCommandVoid3(
240  E &entity,
241  // The following syntaxt don't compile when not specializing the template
242  // arg... why ???
243  boost::function<void(E *, const T1 &, const T2 &, const T3 &)> function,
244  const std::string &docString) {
245  return new CommandVoid3<E, T1, T2, T3>(
246  entity, boost::bind(function, &entity, _1, _2, _3), docString);
247 }
248 
249 template <class E, typename T1, typename T2, typename T3>
251 makeCommandVoid3(E &entity,
252  void (E::*function)(const T1 &, const T2 &, const T3 &),
253  const std::string &docString) {
254  return new CommandVoid3<E, T1, T2, T3>(
255  entity, boost::bind(function, &entity, _1, _2, _3), docString);
256  return NULL;
257 }
258 
259 inline std::string docCommandVoid3(const std::string &doc,
260  const std::string &type1,
261  const std::string &type2,
262  const std::string &type3) {
263  return (std::string("\n") + doc + "\n\n" + "Input:\n - A " + type1 + ".\n" +
264  "Input:\n - A " + type2 + ".\n" + "Input:\n - A " + type3 + ".\n" +
265  "Void return.\n\n");
266 }
267 
268 } // namespace command
269 } // namespace dynamicgraph
270 
271 /* --- FUNCTION 4 ARGS ------------------------------------------------------ */
272 namespace dynamicgraph {
273 namespace command {
274 
275 template <class E, typename T1, typename T2, typename T3, typename T4>
276 struct CommandVoid4 : public Command {
277  typedef boost::function<void(const T1 &, const T2 &, const T3 &, const T4 &)>
278  function_t;
279  typedef void (E::*memberFunction_ptr_t)(const T1 &, const T2 &, const T3 &,
280  const T4 &);
281 
282  CommandVoid4(E &entity, function_t function, const std::string &docString)
283  : Command(entity,
284  boost::assign::list_of(ValueHelper<T1>::TypeID)(
287  docString),
288  fptr(function) {}
289 
290 protected:
291  virtual Value doExecute() {
292  assert(getParameterValues().size() == 4);
293  T1 val1 = getParameterValues()[0].value();
294  T2 val2 = getParameterValues()[1].value();
295  T3 val3 = getParameterValues()[2].value();
296  T4 val4 = getParameterValues()[3].value();
297  fptr(val1, val2, val3, val4);
298  return Value(); // void
299  }
300 
301 private:
302  function_t fptr;
303 };
304 
305 template <class E, typename T1, typename T2, typename T3, typename T4>
307 makeCommandVoid4(E &entity,
308  typename CommandVoid4<E, T1, T2, T3, T4>::function_t function,
309  const std::string &docString) {
310  return new CommandVoid4<E, T1, T2, T3, T4>(entity, function, docString);
311 }
312 
313 template <class E, typename T1, typename T2, typename T3, typename T4>
314 CommandVoid4<E, T1, T2, T3, T4> *makeCommandVoid4(
315  E &entity,
316  boost::function<void(E *, const T1 &, const T2 &, const T3 &, const T4 &)>
317  function,
318  const std::string &docString) {
320  entity, boost::bind(function, &entity, _1, _2, _3, _4), docString);
321 }
322 
323 template <class E, typename T1, typename T2, typename T3, typename T4>
324 CommandVoid4<E, T1, T2, T3, T4> *makeCommandVoid4(
325  E &entity,
326  void (E::*function)(const T1 &, const T2 &, const T3 &, const T4 &),
327  const std::string &docString) {
329  entity, boost::bind(function, &entity, _1, _2, _3, _4), docString);
330  return NULL;
331 }
332 
333 inline std::string docCommandVoid4(const std::string &doc,
334  const std::string &type1,
335  const std::string &type2,
336  const std::string &type3,
337  const std::string &type4) {
338  return (std::string("\n") + doc + "\n\n" + "Input:\n - A " + type1 + ".\n" +
339  "Input:\n - A " + type2 + ".\n" + "Input:\n - A " + type3 + ".\n" +
340  "Input:\n - A " + type4 + ".\n" + "Void return.\n\n");
341 }
342 
343 } // namespace command
344 } // namespace dynamicgraph
345 
346 /* --- FUNCTION VERBOSE ----------------------------------------------------- */
347 /* This bind a function void f( ostream& ) that display some results into
348  * a string f( void ) that return some string results. */
349 
350 namespace dynamicgraph {
351 namespace command {
352 template <class E> struct CommandVerbose : public Command {
353  typedef boost::function<void(std::ostream &)> function_t;
354 
355  CommandVerbose(E &entity, function_t function, const std::string &docString)
356  : Command(entity, EMPTY_ARG, docString), fptr(function) {}
357 
358 protected:
359  virtual Value doExecute() {
360  assert(getParameterValues().size() == 0);
361  std::ostringstream oss;
362  fptr(oss);
363  return Value(oss.str()); // return string
364  }
365 
366 private:
367  function_t fptr;
368 };
369 
370 template <class E>
372 makeCommandVerbose(E &entity, typename CommandVerbose<E>::function_t function,
373  const std::string &docString) {
374  return new CommandVerbose<E>(entity, function, docString);
375  return NULL;
376 }
377 
378 template <class E>
379 CommandVerbose<E> *makeCommandVerbose(E &entity,
380  void (E::*function)(std::ostream &),
381  const std::string &docString) {
382  return new CommandVerbose<E>(entity, boost::bind(function, &entity, _1),
383  docString);
384  return NULL;
385 }
386 
387 inline std::string docCommandVerbose(const std::string &doc) {
388  return std::string("\n") + doc + "\n\nNo input.\n Return a string.\n\n";
389 }
390 /*************************/
391 /* Template return types */
392 /*************************/
393 
394 template <class E, class ReturnType>
395 struct CommandReturnType0 : public Command {
396  CommandReturnType0(E &entity, boost::function<ReturnType(void)> function,
397  const std::string &docString)
398  : Command(entity, EMPTY_ARG, docString), fptr(function) {}
399 
400 protected:
401  virtual Value doExecute() {
402  assert(getParameterValues().size() == 0);
403  Value res;
404  res = fptr();
405  return res;
406  }
407 
408 private:
409  boost::function<ReturnType(void)> fptr;
410 };
411 
412 template <class E, class ReturnType>
414 makeCommandReturnType0(E &entity, boost::function<ReturnType(void)> function,
415  const std::string &docString) {
416  return new CommandReturnType0<E, ReturnType>(entity, function, docString);
417 }
418 
419 template <class E, class ReturnType>
421 makeCommandReturnType0(E &entity, boost::function<ReturnType(E *)> function,
422  const std::string &docString) {
424  entity, boost::bind(function, &entity), docString);
425 }
426 
427 template <class E, class ReturnType>
429 makeCommandReturnType0(E &entity, ReturnType (E::*function)(void),
430  const std::string &docString) {
432  entity, boost::bind(function, &entity), docString);
433 }
434 
435 template <typename ReturnType>
436 inline std::string docCommandReturnType0(const std::string &doc,
437  const std::string &return_type) {
438  return std::string("\n") + doc + "\n\nNo input.\n" +
439  typeid(ReturnType).name() + " return.\n\n";
440 }
441 
442 } // namespace command
443 } // namespace dynamicgraph
444 
445 /* --- FUNCTION 1 ARGS ------------------------------------------------------ */
446 namespace dynamicgraph {
447 namespace command {
448 
449 template <class E, typename ReturnType, typename T>
450 struct CommandReturnType1 : public Command {
451  typedef boost::function<ReturnType(const T &)> function_t;
452 
453  CommandReturnType1(E &entity, function_t function,
454  const std::string &docString)
455  : Command(entity, boost::assign::list_of(ValueHelper<T>::TypeID),
456  docString),
457  fptr(function) {}
458 
459 protected:
460  virtual Value doExecute() {
461  assert(getParameterValues().size() == 1);
462  T val = getParameterValues()[0].value();
463  Value res(fptr(val));
464  return res; // void
465  }
466 
467 private:
468  function_t fptr;
469 };
470 
471 template <class E, typename ReturnType, typename T>
473 makeCommandReturnType1(E &entity,
474  boost::function<ReturnType(const T &)> function,
475  const std::string &docString) {
476  return new CommandReturnType1<E, ReturnType, T>(entity, function, docString);
477 }
478 
479 template <class E, typename ReturnType, typename T>
481 makeCommandReturnType1(E &entity,
482  // The following syntaxt don't compile when not
483  // specializing the template arg... why ???
484  boost::function<ReturnType(E *, const T &)> function,
485  const std::string &docString) {
487  entity, boost::bind(function, &entity, _1), docString);
488 }
489 
490 template <class E, typename ReturnType, typename T>
492 makeCommandReturnType1(E &entity, ReturnType (E::*function)(const T &),
493  const std::string &docString) {
495  entity, boost::bind(function, &entity, _1), docString);
496  return NULL;
497 }
498 
499 template <typename ReturnType>
500 inline std::string docCommandReturnType1(const std::string &doc,
501  const std::string &type) {
502  return std::string("\n") + doc + "\n\nInput:\n - A " + type + ".\n" +
503  typeid(ReturnType).name() + "return.\n\n";
504 }
505 
506 } // namespace command
507 } // namespace dynamicgraph
508 
509 /*********** FUNCTION 2 Arguments ************************/
510 namespace dynamicgraph {
511 namespace command {
512 
513 template <class E, typename ReturnType, typename T1, typename T2>
514 struct CommandReturnType2 : public Command {
515  typedef boost::function<ReturnType(const T1 &, const T2 &)> function_t;
516 
517  CommandReturnType2(E &entity, function_t function,
518  const std::string &docString)
519  : Command(entity,
520  boost::assign::list_of(ValueHelper<T1>::TypeID)(
522  docString),
523  fptr(function) {}
524 
525 protected:
526  virtual Value doExecute() {
527  assert(getParameterValues().size() == 2);
528  T1 val1 = getParameterValues()[0].value();
529  T2 val2 = getParameterValues()[1].value();
530  fptr(val1, val2);
531  return Value(); // void
532  }
533 
534 private:
535  function_t fptr;
536 };
537 
538 template <class E, typename ReturnType, typename T1, typename T2>
539 CommandReturnType2<E, ReturnType, T1, T2> *makeCommandReturnType2(
540  E &entity, boost::function<ReturnType(const T1 &, const T2 &)> function,
541  const std::string &docString) {
542  return new CommandReturnType2<E, ReturnType, T1, T2>(entity, function,
543  docString);
544 }
545 
546 template <class E, typename ReturnType, typename T1, typename T2>
547 CommandReturnType2<E, ReturnType, T1, T2> *makeCommandReturnType2(
548  E &entity,
549  // The following syntaxt don't compile when not specializing the template
550  // arg... why ???
551  boost::function<ReturnType(E *, const T1 &, const T2 &)> function,
552  const std::string &docString) {
554  entity, boost::bind(function, &entity, _1, _2), docString);
555 }
556 
557 template <class E, typename ReturnType, typename T1, typename T2>
559 makeCommandReturnType2(E &entity,
560  ReturnType (E::*function)(const T1 &, const T2 &),
561  const std::string &docString) {
563  entity, boost::bind(function, &entity, _1, _2), docString);
564  return NULL;
565 }
566 
567 template <typename ReturnType>
568 inline std::string docCommandReturnType2(const std::string &doc,
569  const std::string &type1,
570  const std::string &type2) {
571  return (std::string("\n") + doc + "\n\n" + "Input:\n - A " + type1 + ".\n" +
572  "Input:\n - A " + type2 + ".\n" +
573  "ReturnType:\n - Returns:" + typeid(ReturnType).name() + +".\n\n");
574 }
575 
576 } // namespace command
577 } // namespace dynamicgraph
578 
579 #endif // __dg_command_bind_h__
virtual Value doExecute()
Specific action performed by the command.
Definition: command-bind.h:217
Command(Entity &entity, const std::vector< Value::Type > &valueTypes, const std::string &docstring)
This class implements a variant design pattern to handle basic types in Command.
Definition: value.h:46
virtual Value doExecute()
Specific action performed by the command.
Definition: command-bind.h:88
virtual Value doExecute()
Specific action performed by the command.
Definition: command-bind.h:460
virtual Value doExecute()
Specific action performed by the command.
Definition: command-bind.h:359
virtual Value doExecute()
Specific action performed by the command.
Definition: command-bind.h:291
virtual Value doExecute()
Specific action performed by the command.
Definition: command-bind.h:151
const std::vector< Value > & getParameterValues() const
Get parameter values.
virtual Value doExecute()
Specific action performed by the command.
Definition: command-bind.h:526
virtual Value doExecute()
Specific action performed by the command.
Definition: command-bind.h:38
virtual Value doExecute()
Specific action performed by the command.
Definition: command-bind.h:401