bt_factory.h
Go to the documentation of this file.
1 /* Copyright (C) 2018 Michele Colledanchise - All Rights Reserved
2  * Copyright (C) 2018 Davide Faconti - All Rights Reserved
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"),
5 * to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense,
6 * and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
7 * The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
8 *
9 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
10 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
11 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
12 */
13 
14 #ifndef BT_FACTORY_H
15 #define BT_FACTORY_H
16 
17 #include <functional>
18 #include <memory>
19 #include <map>
20 #include <set>
21 #include <cstring>
22 #include <algorithm>
23 
25 
26 namespace BT
27 {
29 typedef std::function<std::unique_ptr<TreeNode>(const std::string&, const NodeParameters&)>
31 
34 {
36  std::string registration_ID;
38 };
39 
40 const char PLUGIN_SYMBOL[] = "BT_RegisterNodesFromPlugin";
41 #define BT_REGISTER_NODES(factory) \
42  extern "C" void __attribute__((visibility("default"))) \
43  BT_RegisterNodesFromPlugin(BT::BehaviorTreeFactory& factory)
44 
46 {
47  public:
49 
50  bool unregisterBuilder(const std::string& ID);
51 
55  void registerBuilder(const TreeNodeManifest& manifest, NodeBuilder builder);
56 
58  void registerSimpleAction(const std::string& ID,
59  const SimpleActionNode::TickFunctor& tick_functor);
60 
62  void registerSimpleCondition(const std::string& ID,
63  const SimpleConditionNode::TickFunctor& tick_functor);
64 
66  void registerSimpleDecorator(const std::string& ID,
67  const SimpleDecoratorNode::TickFunctor& tick_functor);
68 
76  void registerFromPlugin(const std::string file_path);
77 
86  std::unique_ptr<TreeNode> instantiateTreeNode(const std::string& ID, const std::string& name,
87  const NodeParameters& params,
88  const Blackboard::Ptr& blackboard) const;
89 
97  template <typename T>
98  void registerNodeType(const std::string& ID)
99  {
100  static_assert(std::is_base_of<ActionNodeBase, T>::value ||
101  std::is_base_of<ControlNode, T>::value ||
102  std::is_base_of<DecoratorNode, T>::value ||
103  std::is_base_of<ConditionNode, T>::value,
104  "[registerBuilder]: accepts only classed derived from either ActionNodeBase, "
105  "DecoratorNode, ControlNode or ConditionNode");
106 
107  static_assert(!std::is_abstract<T>::value,
108  "[registerBuilder]: Some methods are pure virtual. "
109  "Did you override the methods tick() and halt()?");
110 
111  constexpr bool default_constructable = std::is_constructible<T, const std::string&>::value;
112  constexpr bool param_constructable =
113  std::is_constructible<T, const std::string&, const NodeParameters&>::value;
114  constexpr bool has_static_required_parameters =
116 
117  static_assert(default_constructable || param_constructable,
118  "[registerBuilder]: the registered class must have at least one of these two "
119  "constructors: "
120  " (const std::string&, const NodeParameters&) or (const std::string&).");
121 
122  static_assert(!(param_constructable && !has_static_required_parameters),
123  "[registerBuilder]: you MUST implement the static method: "
124  " const NodeParameters& requiredNodeParameters();\n");
125 
126  static_assert(!(has_static_required_parameters && !param_constructable),
127  "[registerBuilder]: since you have a static method requiredNodeParameters(), "
128  "you MUST add a constructor sign signature (const std::string&, const "
129  "NodeParameters&)\n");
130 
131  registerNodeTypeImpl<T>(ID);
132  }
133 
135  const std::map<std::string, NodeBuilder>& builders() const;
136 
138  const std::vector<TreeNodeManifest>& manifests() const;
139 
140  const std::set<std::string>& builtinNodes() const;
141 
142  private:
143  std::map<std::string, NodeBuilder> builders_;
144  std::vector<TreeNodeManifest> manifests_;
145  std::set<std::string> builtin_IDs_;
146 
147  // template specialization = SFINAE + black magic
148 
149  // clang-format off
150  template <typename T>
151  using has_default_constructor = typename std::is_constructible<T, const std::string&>;
152 
153  template <typename T>
154  using has_params_constructor = typename std::is_constructible<T, const std::string&, const NodeParameters&>;
155 
156  template <typename T, typename = void>
157  struct has_static_method_requiredParams: std::false_type {};
158 
159  template <typename T>
161  typename std::enable_if<std::is_same<decltype(T::requiredNodeParameters()), const NodeParameters&>::value>::type>
162  : std::true_type {};
163 
164  template <typename T>
165  void registerNodeTypeImpl(const std::string& ID)
166  {
167  NodeBuilder builder = getBuilderImpl<T>();
168  TreeNodeManifest manifest = { getType<T>(), ID,
169  getRequiredParamsImpl<T>() };
170  registerBuilder(manifest, builder);
171  }
172 
173  template <typename T>
174  NodeBuilder getBuilderImpl(typename std::enable_if< !has_params_constructor<T>::value >::type* = nullptr)
175  {
176  return [](const std::string& name, const NodeParameters&)
177  {
178  return std::unique_ptr<TreeNode>(new T(name));
179  };
180  }
181 
182  template <typename T>
184  {
185  return [this](const std::string& name, const NodeParameters& params)
186  {
187  // Special case. Use default constructor if parameters are empty
188  if( params.empty() && has_default_constructor<T>::value && getRequiredParamsImpl<T>().size()>0)
189  {
190  return std::unique_ptr<TreeNode>(new T(name));
191  }
192  return std::unique_ptr<TreeNode>(new T(name, params));
193  };
194  }
195 
196  template <typename T>
198  {
199  return [](const std::string& name, const NodeParameters& params)
200  {
201  return std::unique_ptr<TreeNode>(new T(name, params));
202  };
203  }
204 
205  template <typename T>
207  {
208  return T::requiredNodeParameters();
209  }
210 
211  template <typename T>
213  {
214  return NodeParameters();
215  }
216  // clang-format on
217 
218  void sortTreeNodeManifests();
219 
220 };
221 
222 } // end namespace
223 #endif // BT_FACTORY_H
NodeBuilder getBuilderImpl(typename std::enable_if< !has_params_constructor< T >::value >::type *=nullptr)
Definition: bt_factory.h:174
NodeBuilder getBuilderImpl(typename std::enable_if< has_default_constructor< T >::value &&has_params_constructor< T >::value >::type *=nullptr)
Definition: bt_factory.h:183
std::map< std::string, NodeBuilder > builders_
Definition: bt_factory.h:143
std::vector< TreeNodeManifest > manifests_
Definition: bt_factory.h:144
manifest
void registerNodeType(const std::string &ID)
Definition: bt_factory.h:98
std::function< NodeStatus(NodeStatus, TreeNode &)> TickFunctor
NodeBuilder getBuilderImpl(typename std::enable_if<!has_default_constructor< T >::value &&has_params_constructor< T >::value >::type *=nullptr)
Definition: bt_factory.h:197
void registerNodeTypeImpl(const std::string &ID)
Definition: bt_factory.h:165
NodeParameters getRequiredParamsImpl(typename std::enable_if< has_static_method_requiredParams< T >::value >::type *=nullptr)
Definition: bt_factory.h:206
std::set< std::string > builtin_IDs_
Definition: bt_factory.h:145
typename std::is_constructible< T, const std::string &, const NodeParameters & > has_params_constructor
Definition: bt_factory.h:154
Definition: any.hpp:455
std::shared_ptr< Blackboard > Ptr
Definition: blackboard.h:40
This information is used mostly by the XMLParser.
Definition: bt_factory.h:33
NodeParameters getRequiredParamsImpl(typename std::enable_if< !has_static_method_requiredParams< T >::value >::type *=nullptr)
Definition: bt_factory.h:212
std::unordered_map< std::string, std::string > NodeParameters
Definition: tree_node.h:33
typename std::is_constructible< T, const std::string & > has_default_constructor
Definition: bt_factory.h:151
std::string registration_ID
Definition: bt_factory.h:36
builder
Definition: build.py:70
const char PLUGIN_SYMBOL[]
Definition: bt_factory.h:40
std::function< NodeStatus(TreeNode &)> TickFunctor
Definition: action_node.h:80
std::function< std::unique_ptr< TreeNode >const std::string &, const NodeParameters &)> NodeBuilder
The term "Builder" refers to the Builder Pattern (https://en.wikipedia.org/wiki/Builder_pattern) ...
Definition: bt_factory.h:30
NodeParameters required_parameters
Definition: bt_factory.h:37
NodeType
Definition: basic_types.h:16
std::function< NodeStatus(TreeNode &)> TickFunctor


behaviortree_cpp
Author(s): Michele Colledanchise, Davide Faconti
autogenerated on Sat Feb 2 2019 04:01:53