|
| 1 | +--- |
| 2 | +sidebar_position: 12 |
| 3 | +sidebar_label: 12. Default Port values |
| 4 | +--- |
| 5 | + |
| 6 | +# Default Port values |
| 7 | + |
| 8 | +When defining a port, it might be convenient to add a default value, |
| 9 | +i.e. the value that the port should have if not specified in the XML. |
| 10 | + |
| 11 | +:::note |
| 12 | +Some of the examples shown in this tutorial will require version 4.5.2 or later. |
| 13 | +::: |
| 14 | + |
| 15 | +## Default InputPorts |
| 16 | + |
| 17 | +Let's consider a node initializing multiple ports. We use a custom type **Point2D**, |
| 18 | +but the same is true for simple types, such as `int`, `double` or `string`. |
| 19 | + |
| 20 | +```cpp |
| 21 | + static PortsList providedPorts() |
| 22 | + { |
| 23 | + return { |
| 24 | + BT::InputPort<Point2D>("input"), |
| 25 | + BT::InputPort<Point2D>("pointA", Point2D{1, 2}, "default value is x=1, y=2"), |
| 26 | + BT::InputPort<Point2D>("pointB", "3,4", "default value is x=3, y=4"), |
| 27 | + BT::InputPort<Point2D>("pointC", "{point}", "point by default to BB entry {point}"), |
| 28 | + BT::InputPort<Point2D>("pointD", "{=}", "point by default to BB entry {pointD}") |
| 29 | + }; |
| 30 | + } |
| 31 | +``` |
| 32 | +
|
| 33 | +The very first one (`input`) has no default value and it is mandatory to providing either a value |
| 34 | +or blackboard entry in the XML. |
| 35 | +
|
| 36 | +### Default values |
| 37 | +
|
| 38 | +```cpp |
| 39 | +BT::InputPort<Point2D>("pointA", Point2D{1, 2}, "..."); |
| 40 | +``` |
| 41 | + |
| 42 | +If the template specialization `convertFromString<Point2D>()` is implemented, we can use that too. |
| 43 | + |
| 44 | +In other words, the following syntaxes should be equivalent, if our **converFromString** expects |
| 45 | +two comma-separated values: |
| 46 | + |
| 47 | +```cpp |
| 48 | +BT::InputPort<Point2D>("pointB", "3,4", "..."); |
| 49 | +// should be equivalent to: |
| 50 | +BT::InputPort<Point2D>("pointB", Point2D{3, 4}, "..."); |
| 51 | +``` |
| 52 | +
|
| 53 | +### Default blackboard entry |
| 54 | +
|
| 55 | +Alternatively, we can define the default blackboard entry that the port should point at. |
| 56 | +
|
| 57 | +```cpp |
| 58 | +BT::InputPort<Point2D>("pointC", "{point}", "..."); |
| 59 | +``` |
| 60 | + |
| 61 | +If the name of the port and the blackboard entry are the **same**, you can use "{=}" |
| 62 | + |
| 63 | +```cpp |
| 64 | +BT::InputPort<Point2D>("pointD", "{=}", "..."); |
| 65 | +// equivalent to: |
| 66 | +BT::InputPort<Point2D>("pointD", "{pointD}", "..."); |
| 67 | +``` |
| 68 | + |
| 69 | +## Default OutputPorts |
| 70 | + |
| 71 | +Output ports are more limited and can only point to a blackboard entry. |
| 72 | +You can still use "{=}" when the two names are the same. |
| 73 | + |
| 74 | +```cpp |
| 75 | + static PortsList providedPorts() |
| 76 | + { |
| 77 | + return { |
| 78 | + BT::OutputPort<Point2D>("result", "{target}", "point by default to BB entry {target}"); |
| 79 | + }; |
| 80 | + } |
| 81 | +``` |
0 commit comments