command-bind.h 19.6 KB
Newer Older
Nicolas Mansard's avatar
IVIGIT.  
Nicolas Mansard committed
1
2
3
4
5
6
7
8
9
10
11
12
//
// Copyright 2010 CNRS
//
// Author: Nicolas Mansard
//

#ifndef __dg_command_bind_h__
#define __dg_command_bind_h__

/* Create a command from a bind directly. Examples are:

 *    addCommand("myProcVoid",
13
14
 *               makeCommandVoid0(*this,&ClassName::functionNoArg,
 *                                docCommandVoid0("Simple line doc here.")));
Nicolas Mansard's avatar
IVIGIT.  
Nicolas Mansard committed
15
16

 *    addCommand("myProcOneString",
17
18
19
 *               makeCommandVoid1(*this,&EntityName::functionOneStringArg,
 *                                docCommandVoid1("Simple line doc here.",
 *                                                "string")));
Nicolas Mansard's avatar
IVIGIT.  
Nicolas Mansard committed
20
21
22
23
24
 *
 */

#include "dynamic-graph/command.h"
#include <boost/assign/list_of.hpp>
Nicolas Mansard's avatar
Nicolas Mansard committed
25
#include <boost/bind.hpp>
26
#include <boost/function.hpp>
Nicolas Mansard's avatar
IVIGIT.  
Nicolas Mansard committed
27

28
/* --- FUNCTION 0 ARGS ----------------------------------------------------- */
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
namespace dynamicgraph {
namespace command {

template <class E> struct CommandVoid0 : public Command {
  CommandVoid0(E &entity, boost::function<void(void)> function,
               const std::string &docString)
      : Command(entity, EMPTY_ARG, docString), fptr(function) {}

protected:
  virtual Value doExecute() {
    assert(getParameterValues().size() == 0);
    fptr();
    return Value(); // void
  }

private:
  boost::function<void(void)> fptr;
};

template <class E>
CommandVoid0<E> *makeCommandVoid0(E &entity,
                                  boost::function<void(void)> function,
                                  const std::string &docString) {
  return new CommandVoid0<E>(entity, function, docString);
}

template <class E>
CommandVoid0<E> *makeCommandVoid0(E &entity,
                                  boost::function<void(E *)> function,
                                  const std::string &docString) {
  return new CommandVoid0<E>(entity, boost::bind(function, &entity), docString);
}

template <class E>
CommandVoid0<E> *makeCommandVoid0(E &entity, void (E::*function)(void),
                                  const std::string &docString) {
  return new CommandVoid0<E>(entity, boost::bind(function, &entity), docString);
}

inline std::string docCommandVoid0(const std::string &doc) {
  return std::string("\n") + doc + "\n\nNo input.\nVoid return.\n\n";
}

} // namespace command
} // namespace dynamicgraph
Nicolas Mansard's avatar
IVIGIT.  
Nicolas Mansard committed
74
75

/* --- FUNCTION 1 ARGS ------------------------------------------------------ */
76
77
78
79
80
81
82
namespace dynamicgraph {
namespace command {

template <class E, typename T> struct CommandVoid1 : public Command {
  typedef boost::function<void(const T &)> function_t;

  CommandVoid1(E &entity, function_t function, const std::string &docString)
Olivier Stasse's avatar
Olivier Stasse committed
83
      : Command(entity, boost::assign::list_of(ValueHelper<T>::TypeID),
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
                docString),
        fptr(function) {}

protected:
  virtual Value doExecute() {
    assert(getParameterValues().size() == 1);
    T val = getParameterValues()[0].value();
    fptr(val);
    return Value(); // void
  }

private:
  function_t fptr;
};

template <class E, typename T>
CommandVoid1<E, T> *
makeCommandVoid1(E &entity, boost::function<void(const T &)> function,
                 // typename CommandVoid1<E,T>::function_t function ,
                 const std::string &docString) {
  return new CommandVoid1<E, T>(entity, function, docString);
}

template <class E, typename T>
Olivier Stasse's avatar
Olivier Stasse committed
108
109
110
111
112
113
CommandVoid1<E, T> *
makeCommandVoid1(E &entity,
                 // The following syntaxt don't compile when not specializing
                 // the template arg... why ???
                 boost::function<void(E *, const T &)> function,
                 const std::string &docString) {
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
  return new CommandVoid1<E, T>(entity, boost::bind(function, &entity, _1),
                                docString);
}

template <class E, typename T>
CommandVoid1<E, T> *makeCommandVoid1(E &entity, void (E::*function)(const T &),
                                     const std::string &docString) {
  return new CommandVoid1<E, T>(entity, boost::bind(function, &entity, _1),
                                docString);
  return NULL;
}

inline std::string docCommandVoid1(const std::string &doc,
                                   const std::string &type) {
  return std::string("\n") + doc + "\n\nInput:\n - A " + type +
         ".\nVoid return.\n\n";
}

} // namespace command
} // namespace dynamicgraph
Nicolas Mansard's avatar
IVIGIT.  
Nicolas Mansard committed
134

135
/* --- FUNCTION 2 ARGS ------------------------------------------------------ */
136
137
138
139
140
141
142
143
144
namespace dynamicgraph {
namespace command {

template <class E, typename T1, typename T2>
struct CommandVoid2 : public Command {
  typedef boost::function<void(const T1 &, const T2 &)> function_t;

  CommandVoid2(E &entity, function_t function, const std::string &docString)
      : Command(entity,
Olivier Stasse's avatar
Olivier Stasse committed
145
146
                boost::assign::list_of(ValueHelper<T1>::TypeID)(
                    ValueHelper<T2>::TypeID),
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
                docString),
        fptr(function) {}

protected:
  virtual Value doExecute() {
    assert(getParameterValues().size() == 2);
    T1 val1 = getParameterValues()[0].value();
    T2 val2 = getParameterValues()[1].value();
    fptr(val1, val2);
    return Value(); // void
  }

private:
  function_t fptr;
};

template <class E, typename T1, typename T2>
CommandVoid2<E, T1, T2> *
makeCommandVoid2(E &entity,
                 boost::function<void(const T1 &, const T2 &)> function,
                 const std::string &docString) {
  return new CommandVoid2<E, T1, T2>(entity, function, docString);
}

template <class E, typename T1, typename T2>
Olivier Stasse's avatar
Olivier Stasse committed
172
173
174
175
176
177
CommandVoid2<E, T1, T2> *
makeCommandVoid2(E &entity,
                 // The following syntaxt don't compile when not specializing
                 // the template arg... why ???
                 boost::function<void(E *, const T1 &, const T2 &)> function,
                 const std::string &docString) {
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
  return new CommandVoid2<E, T1, T2>(
      entity, boost::bind(function, &entity, _1, _2), docString);
}

template <class E, typename T1, typename T2>
CommandVoid2<E, T1, T2> *
makeCommandVoid2(E &entity, void (E::*function)(const T1 &, const T2 &),
                 const std::string &docString) {
  return new CommandVoid2<E, T1, T2>(
      entity, boost::bind(function, &entity, _1, _2), docString);
  return NULL;
}

inline std::string docCommandVoid2(const std::string &doc,
                                   const std::string &type1,
                                   const std::string &type2) {
  return (std::string("\n") + doc + "\n\n" + "Input:\n - A " + type1 + ".\n" +
          "Input:\n - A " + type2 + ".\n" + "Void return.\n\n");
}

} // namespace command
} // namespace dynamicgraph
Nicolas Mansard's avatar
Nicolas Mansard committed
200
201

/* --- FUNCTION 3 ARGS ------------------------------------------------------ */
202
203
204
205
206
207
208
209
210
namespace dynamicgraph {
namespace command {

template <class E, typename T1, typename T2, typename T3>
struct CommandVoid3 : public Command {
  typedef boost::function<void(const T1 &, const T2 &, const T3 &)> function_t;

  CommandVoid3(E &entity, function_t function, const std::string &docString)
      : Command(entity,
Olivier Stasse's avatar
Olivier Stasse committed
211
212
                boost::assign::list_of(ValueHelper<T1>::TypeID)(
                    ValueHelper<T2>::TypeID)(ValueHelper<T3>::TypeID),
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
                docString),
        fptr(function) {}

protected:
  virtual Value doExecute() {
    assert(getParameterValues().size() == 3);
    T1 val1 = getParameterValues()[0].value();
    T2 val2 = getParameterValues()[1].value();
    T3 val3 = getParameterValues()[2].value();
    fptr(val1, val2, val3);
    return Value(); // void
  }

private:
  function_t fptr;
};

template <class E, typename T1, typename T2, typename T3>
CommandVoid3<E, T1, T2, T3> *
makeCommandVoid3(E &entity,
                 typename CommandVoid3<E, T1, T2, T3>::function_t function,
                 const std::string &docString) {
  return new CommandVoid3<E, T1, T2, T3>(entity, function, docString);
}

template <class E, typename T1, typename T2, typename T3>
CommandVoid3<E, T1, T2, T3> *makeCommandVoid3(
    E &entity,
    // The following syntaxt don't compile when not specializing the template
    // arg... why ???
    boost::function<void(E *, const T1 &, const T2 &, const T3 &)> function,
    const std::string &docString) {
  return new CommandVoid3<E, T1, T2, T3>(
      entity, boost::bind(function, &entity, _1, _2, _3), docString);
}

template <class E, typename T1, typename T2, typename T3>
CommandVoid3<E, T1, T2, T3> *
makeCommandVoid3(E &entity,
                 void (E::*function)(const T1 &, const T2 &, const T3 &),
                 const std::string &docString) {
  return new CommandVoid3<E, T1, T2, T3>(
      entity, boost::bind(function, &entity, _1, _2, _3), docString);
  return NULL;
}

inline std::string docCommandVoid3(const std::string &doc,
                                   const std::string &type1,
                                   const std::string &type2,
                                   const std::string &type3) {
  return (std::string("\n") + doc + "\n\n" + "Input:\n - A " + type1 + ".\n" +
          "Input:\n - A " + type2 + ".\n" + "Input:\n - A " + type3 + ".\n" +
          "Void return.\n\n");
}

} // namespace command
} // namespace dynamicgraph
Nicolas Mansard's avatar
Nicolas Mansard committed
270
271

/* --- FUNCTION 4 ARGS ------------------------------------------------------ */
272
273
274
275
276
277
278
279
280
281
282
283
namespace dynamicgraph {
namespace command {

template <class E, typename T1, typename T2, typename T3, typename T4>
struct CommandVoid4 : public Command {
  typedef boost::function<void(const T1 &, const T2 &, const T3 &, const T4 &)>
      function_t;
  typedef void (E::*memberFunction_ptr_t)(const T1 &, const T2 &, const T3 &,
                                          const T4 &);

  CommandVoid4(E &entity, function_t function, const std::string &docString)
      : Command(entity,
Olivier Stasse's avatar
Olivier Stasse committed
284
285
286
                boost::assign::list_of(ValueHelper<T1>::TypeID)(
                    ValueHelper<T2>::TypeID)(ValueHelper<T3>::TypeID)(
                    ValueHelper<T4>::TypeID),
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
                docString),
        fptr(function) {}

protected:
  virtual Value doExecute() {
    assert(getParameterValues().size() == 4);
    T1 val1 = getParameterValues()[0].value();
    T2 val2 = getParameterValues()[1].value();
    T3 val3 = getParameterValues()[2].value();
    T4 val4 = getParameterValues()[3].value();
    fptr(val1, val2, val3, val4);
    return Value(); // void
  }

private:
  function_t fptr;
};

template <class E, typename T1, typename T2, typename T3, typename T4>
CommandVoid4<E, T1, T2, T3, T4> *
makeCommandVoid4(E &entity,
                 typename CommandVoid4<E, T1, T2, T3, T4>::function_t function,
                 const std::string &docString) {
  return new CommandVoid4<E, T1, T2, T3, T4>(entity, function, docString);
}

template <class E, typename T1, typename T2, typename T3, typename T4>
CommandVoid4<E, T1, T2, T3, T4> *makeCommandVoid4(
    E &entity,
    boost::function<void(E *, const T1 &, const T2 &, const T3 &, const T4 &)>
        function,
    const std::string &docString) {
  return new CommandVoid4<E, T1, T2, T3, T4>(
      entity, boost::bind(function, &entity, _1, _2, _3, _4), docString);
}

template <class E, typename T1, typename T2, typename T3, typename T4>
CommandVoid4<E, T1, T2, T3, T4> *makeCommandVoid4(
    E &entity,
    void (E::*function)(const T1 &, const T2 &, const T3 &, const T4 &),
    const std::string &docString) {
  return new CommandVoid4<E, T1, T2, T3, T4>(
      entity, boost::bind(function, &entity, _1, _2, _3, _4), docString);
  return NULL;
}

inline std::string docCommandVoid4(const std::string &doc,
                                   const std::string &type1,
                                   const std::string &type2,
                                   const std::string &type3,
                                   const std::string &type4) {
  return (std::string("\n") + doc + "\n\n" + "Input:\n - A " + type1 + ".\n" +
          "Input:\n - A " + type2 + ".\n" + "Input:\n - A " + type3 + ".\n" +
          "Input:\n - A " + type4 + ".\n" + "Void return.\n\n");
}

} // namespace command
} // namespace dynamicgraph
Nicolas Mansard's avatar
Nicolas Mansard committed
345

346
347
348
349
/* --- FUNCTION VERBOSE ----------------------------------------------------- */
/* This bind a function void f( ostream& ) that display some results into
 * a string f( void ) that return some string results. */

350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
namespace dynamicgraph {
namespace command {
template <class E> struct CommandVerbose : public Command {
  typedef boost::function<void(std::ostream &)> function_t;

  CommandVerbose(E &entity, function_t function, const std::string &docString)
      : Command(entity, EMPTY_ARG, docString), fptr(function) {}

protected:
  virtual Value doExecute() {
    assert(getParameterValues().size() == 0);
    std::ostringstream oss;
    fptr(oss);
    return Value(oss.str()); // return string
  }

private:
  function_t fptr;
};

template <class E>
CommandVerbose<E> *
makeCommandVerbose(E &entity, typename CommandVerbose<E>::function_t function,
                   const std::string &docString) {
  return new CommandVerbose<E>(entity, function, docString);
  return NULL;
}

template <class E>
Olivier Stasse's avatar
Olivier Stasse committed
379
380
381
382
CommandVerbose<E> *makeCommandVerbose(E &entity,
                                      void (E::*function)(std::ostream &),
                                      const std::string &docString) {
  return new CommandVerbose<E>(entity, boost::bind(function, &entity, _1),
383
384
385
386
387
388
389
                               docString);
  return NULL;
}

inline std::string docCommandVerbose(const std::string &doc) {
  return std::string("\n") + doc + "\n\nNo input.\n Return a string.\n\n";
}
390
391
392
393
394
395
396
397
398
399
400
401
402
/*************************/
/* Template return types */
/*************************/

template <class E, class ReturnType>
struct CommandReturnType0 : public Command {
  CommandReturnType0(E &entity, boost::function<ReturnType(void)> function,
                     const std::string &docString)
      : Command(entity, EMPTY_ARG, docString), fptr(function) {}

protected:
  virtual Value doExecute() {
    assert(getParameterValues().size() == 0);
Guilhem Saurel's avatar
format    
Guilhem Saurel committed
403
    Value res(fptr());
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
    return res;
  }

private:
  boost::function<ReturnType(void)> fptr;
};

template <class E, class ReturnType>
CommandReturnType0<E, ReturnType> *
makeCommandReturnType0(E &entity, boost::function<ReturnType(void)> function,
                       const std::string &docString) {
  return new CommandReturnType0<E, ReturnType>(entity, function, docString);
}

template <class E, class ReturnType>
CommandReturnType0<E, ReturnType> *
makeCommandReturnType0(E &entity, boost::function<ReturnType(E *)> function,
                       const std::string &docString) {
  return new CommandReturnType0<E, ReturnType>(
      entity, boost::bind(function, &entity), docString);
}

template <class E, class ReturnType>
CommandReturnType0<E, ReturnType> *
makeCommandReturnType0(E &entity, ReturnType (E::*function)(void),
                       const std::string &docString) {
  return new CommandReturnType0<E, ReturnType>(
      entity, boost::bind(function, &entity), docString);
}

template <typename ReturnType>
Guilhem Saurel's avatar
Format    
Guilhem Saurel committed
435
436
437
inline std::string
docCommandReturnType0(const std::string &doc,
                      const std::string & /* return_type */) {
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
  return std::string("\n") + doc + "\n\nNo input.\n" +
         typeid(ReturnType).name() + " return.\n\n";
}

} // namespace command
} // namespace dynamicgraph

/* --- FUNCTION 1 ARGS ------------------------------------------------------ */
namespace dynamicgraph {
namespace command {

template <class E, typename ReturnType, typename T>
struct CommandReturnType1 : public Command {
  typedef boost::function<ReturnType(const T &)> function_t;

  CommandReturnType1(E &entity, function_t function,
                     const std::string &docString)
Olivier Stasse's avatar
Olivier Stasse committed
455
      : Command(entity, boost::assign::list_of(ValueHelper<T>::TypeID),
456
457
458
459
460
461
462
463
                docString),
        fptr(function) {}

protected:
  virtual Value doExecute() {
    assert(getParameterValues().size() == 1);
    T val = getParameterValues()[0].value();
    Value res(fptr(val));
Joseph Mirabel's avatar
Joseph Mirabel committed
464
    return res;
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
  }

private:
  function_t fptr;
};

template <class E, typename ReturnType, typename T>
CommandReturnType1<E, ReturnType, T> *
makeCommandReturnType1(E &entity,
                       boost::function<ReturnType(const T &)> function,
                       const std::string &docString) {
  return new CommandReturnType1<E, ReturnType, T>(entity, function, docString);
}

template <class E, typename ReturnType, typename T>
Olivier Stasse's avatar
Olivier Stasse committed
480
481
482
483
484
485
CommandReturnType1<E, ReturnType, T> *
makeCommandReturnType1(E &entity,
                       // The following syntaxt don't compile when not
                       // specializing the template arg... why ???
                       boost::function<ReturnType(E *, const T &)> function,
                       const std::string &docString) {
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
  return new CommandReturnType1<E, ReturnType, T>(
      entity, boost::bind(function, &entity, _1), docString);
}

template <class E, typename ReturnType, typename T>
CommandReturnType1<E, ReturnType, T> *
makeCommandReturnType1(E &entity, ReturnType (E::*function)(const T &),
                       const std::string &docString) {
  return new CommandReturnType1<E, ReturnType, T>(
      entity, boost::bind(function, &entity, _1), docString);
  return NULL;
}

template <typename ReturnType>
inline std::string docCommandReturnType1(const std::string &doc,
                                         const std::string &type) {
  return std::string("\n") + doc + "\n\nInput:\n - A " + type + ".\n" +
         typeid(ReturnType).name() + "return.\n\n";
}

} // namespace command
} // namespace dynamicgraph

/*********** FUNCTION 2 Arguments ************************/
namespace dynamicgraph {
namespace command {

template <class E, typename ReturnType, typename T1, typename T2>
struct CommandReturnType2 : public Command {
  typedef boost::function<ReturnType(const T1 &, const T2 &)> function_t;

  CommandReturnType2(E &entity, function_t function,
                     const std::string &docString)
      : Command(entity,
Olivier Stasse's avatar
Olivier Stasse committed
520
521
                boost::assign::list_of(ValueHelper<T1>::TypeID)(
                    ValueHelper<T2>::TypeID),
522
523
524
525
526
527
528
529
                docString),
        fptr(function) {}

protected:
  virtual Value doExecute() {
    assert(getParameterValues().size() == 2);
    T1 val1 = getParameterValues()[0].value();
    T2 val2 = getParameterValues()[1].value();
Joseph Mirabel's avatar
Joseph Mirabel committed
530
531
    Value res(fptr(val1, val2));
    return res;
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
  }

private:
  function_t fptr;
};

template <class E, typename ReturnType, typename T1, typename T2>
CommandReturnType2<E, ReturnType, T1, T2> *makeCommandReturnType2(
    E &entity, boost::function<ReturnType(const T1 &, const T2 &)> function,
    const std::string &docString) {
  return new CommandReturnType2<E, ReturnType, T1, T2>(entity, function,
                                                       docString);
}

template <class E, typename ReturnType, typename T1, typename T2>
CommandReturnType2<E, ReturnType, T1, T2> *makeCommandReturnType2(
    E &entity,
    // The following syntaxt don't compile when not specializing the template
    // arg... why ???
    boost::function<ReturnType(E *, const T1 &, const T2 &)> function,
    const std::string &docString) {
  return new CommandReturnType2<E, ReturnType, T1, T2>(
      entity, boost::bind(function, &entity, _1, _2), docString);
}

template <class E, typename ReturnType, typename T1, typename T2>
CommandReturnType2<E, ReturnType, T1, T2> *
makeCommandReturnType2(E &entity,
                       ReturnType (E::*function)(const T1 &, const T2 &),
                       const std::string &docString) {
  return new CommandReturnType2<E, ReturnType, T1, T2>(
      entity, boost::bind(function, &entity, _1, _2), docString);
  return NULL;
}

template <typename ReturnType>
inline std::string docCommandReturnType2(const std::string &doc,
                                         const std::string &type1,
                                         const std::string &type2) {
  return (std::string("\n") + doc + "\n\n" + "Input:\n - A " + type1 + ".\n" +
          "Input:\n - A " + type2 + ".\n" +
          "ReturnType:\n - Returns:" + typeid(ReturnType).name() + +".\n\n");
}
575
576
577
578
579

} // namespace command
} // namespace dynamicgraph

#endif // __dg_command_bind_h__