|
| 1 | +/** \page subpage_command Commands |
| 2 | +
|
| 3 | +\section subpage_command_intro Quick introduction |
| 4 | +
|
| 5 | +Commands are allowing to expand the entities. |
| 6 | +It has no real interest in C++, and is mostly targeted to scripting languages |
| 7 | +used to manipulate the control graph. |
| 8 | +dynamic-graph-python provides a mecanism which is automatically binding command |
| 9 | +to python. The main interest for a programmer is that there is no additional |
| 10 | +lines to add. It is realized through CMake rules provided by the submodule |
| 11 | +jrl-cmakemodules. |
| 12 | +
|
| 13 | +\section subpage_command_using Extending entities |
| 14 | +
|
| 15 | +\subsection subpage_command_setters_and_getters Setters and getters |
| 16 | +
|
| 17 | +To modify a quantity with a special method of your class, it is recommanded to |
| 18 | +use Setter. |
| 19 | +
|
| 20 | +For instance to specify the size of the state in one entity and calls the |
| 21 | +method setStateSize it is possible to write: |
| 22 | +
|
| 23 | +\code |
| 24 | + docstring = "\n" |
| 25 | + " Set size of state vector\n" |
| 26 | + "\n"; |
| 27 | + addCommand("resize", new command::Setter<Device, unsigned int>( |
| 28 | + *this, &Device::setStateSize, docstring)); |
| 29 | +\endcode |
| 30 | +
|
| 31 | +Getting the information of the member of the class can be realized with |
| 32 | +the following snippet: |
| 33 | +
|
| 34 | +\code |
| 35 | + addCommand("getTimeStep", |
| 36 | + command::makeDirectGetter( |
| 37 | + *this, &this->timestep_, |
| 38 | + command::docDirectGetter("Time step", "double"))); |
| 39 | +\endcode |
| 40 | +
|
| 41 | +\subsubsection subsubpage_command_void_multiple_args Implementing a command |
| 42 | +
|
| 43 | +Templates are provided to create commands returning no value, but with up to |
| 44 | +4 arguments. |
| 45 | +
|
| 46 | +In order to call the following method: |
| 47 | +\code |
| 48 | + void four_args(const int &, const int &, const int &, const int &) { |
| 49 | + test_four_args_ = true; |
| 50 | + } |
| 51 | +\endcode |
| 52 | +
|
| 53 | +It is sufficient to add in the constructor: |
| 54 | +\code |
| 55 | + addCommand("4_args", |
| 56 | + makeCommandVoid4( |
| 57 | + *this, &CustomEntity::four_args, |
| 58 | + docCommandVoid4("four args", "int", "int", "int", "int"))); |
| 59 | +\endcode |
| 60 | +
|
| 61 | +The arguments are limited to the types provided by the class Value. |
| 62 | +
|
| 63 | +\subsection subpage_command_void_multiple_args Commands returning a value |
| 64 | +
|
| 65 | +The templates with the prefix makeCommandReturnType are allowing to return |
| 66 | +a type. |
| 67 | +
|
| 68 | +For instance to add a command returning a type with two arguments: |
| 69 | +\code |
| 70 | + std::string two_args_ret(const int &, const int &) |
| 71 | + { test_two_args_ = true; return std::string("return");} |
| 72 | +\endcode |
| 73 | +
|
| 74 | +In the constructor, the following snippet shows to create the command: |
| 75 | +\code |
| 76 | + addCommand("2_args_r", |
| 77 | + makeCommandReturnType2( |
| 78 | + *this, &CustomEntity::two_args_ret, |
| 79 | + docCommandVoid2("two args", "int","int"))); |
| 80 | +\endcode |
| 81 | +
|
| 82 | +\section section_calling_a_generic_command Calling a generic command |
| 83 | +
|
| 84 | +Of course calling in C++ a command amounts to call directly the method. |
| 85 | +However in the context of writing a wrapper for another language, |
| 86 | +one may wants to find a command and build the call. |
| 87 | +
|
| 88 | +All the commands of an entity are store in a map. The strings storing the |
| 89 | +commands name are the map keys. |
| 90 | +
|
| 91 | +Therefore calling the previous command can be done with the following snippet: |
| 92 | +\code |
| 93 | +
|
| 94 | +std::map<const std::string, Command *> aCommandMap = |
| 95 | + this->getNewStyleCommandMap(); |
| 96 | +
|
| 97 | +std::string cmd_name = "4_args"; |
| 98 | +
|
| 99 | +std::map<const std::string, Command *>::iterator it_map; |
| 100 | + |
| 101 | +it_map = aCommandMap.find(cmd_name); |
| 102 | +if (it_map == aCommandMap.end()) |
| 103 | +
|
| 104 | +int first_arg = 1; |
| 105 | +Value aValue(first_arg); |
| 106 | +std::vector<Value> values; |
| 107 | +
|
| 108 | +for (unsigned int i = 0; i < 4; i++) |
| 109 | + values.push_back(aValue); |
| 110 | + |
| 111 | +it_map->second->setParameterValues(values); |
| 112 | +it_map->second->execute(); |
| 113 | +it_map->second->owner(); |
| 114 | +it_map->second->getDocstring(); |
| 115 | +
|
| 116 | +\endcode |
| 117 | +
|
| 118 | +when returning a value the line with the call to execute is : |
| 119 | +\code |
| 120 | +Value aValue =it_map->second->execute(); |
| 121 | +\endcode |
| 122 | +
|
| 123 | +*/ |
0 commit comments