14 #ifndef BEHAVIORTREECORE_TREENODE_H 15 #define BEHAVIORTREECORE_TREENODE_H 17 #include <condition_variable> 26 #pragma warning(disable : 4127) 56 typedef std::shared_ptr<TreeNode>
Ptr;
77 virtual void halt() = 0;
79 bool isHalted()
const;
86 const std::string& name()
const;
110 uint16_t UID()
const;
113 const std::string& registrationName()
const;
126 template <
typename T>
127 Result getInput(
const std::string& key, T& destination)
const;
132 template <
typename T>
136 auto res = getInput(key, out);
137 return (res) ?
Optional<T>(out) : nonstd::make_unexpected(res.error());
140 template <
typename T>
141 Result setOutput(
const std::string& key,
const T& value);
160 registration_ID_.assign(ID.data(), ID.size());
163 void modifyPortsRemapping(
const PortsRemapping& new_remapping);
184 template <
typename T>
187 auto remap_it = config_.input_ports.find(key);
188 if (remap_it == config_.input_ports.end())
190 return nonstd::make_unexpected(
StrCat(
"getInput() failed because " 191 "NodeConfiguration::input_ports " 192 "does not contain the key: [",
195 auto remapped_res = getRemappedKey(key, remap_it->second);
200 destination = convertFromString<T>(remap_it->second);
203 const auto& remapped_key = remapped_res.value();
205 if (!config_.blackboard)
207 return nonstd::make_unexpected(
"getInput() trying to access a Blackboard(BB) entry, " 208 "but BB is invalid");
211 const Any* val = config_.blackboard->getAny(remapped_key.to_string());
212 if (val && val->
empty() ==
false)
214 if (std::is_same<T, std::string>::value ==
false && val->
type() ==
typeid(std::string))
216 destination = convertFromString<T>(val->
cast<std::string>());
220 destination = val->
cast<T>();
225 return nonstd::make_unexpected(
StrCat(
"getInput() failed because it was unable to find the " 227 key,
"] remapped to [", remapped_key,
"]"));
229 catch (std::exception& err)
231 return nonstd::make_unexpected(err.what());
235 template <
typename T>
238 if (!config_.blackboard)
240 return nonstd::make_unexpected(
"setOutput() failed: trying to access a " 241 "Blackboard(BB) entry, but BB is invalid");
244 auto remap_it = config_.output_ports.find(key);
245 if (remap_it == config_.output_ports.end())
247 return nonstd::make_unexpected(
StrCat(
"setOutput() failed: NodeConfiguration::output_ports " 249 "contain the key: [",
253 if (remapped_key ==
"=")
257 if (isBlackboardPointer(remapped_key))
259 remapped_key = stripBlackboardPointer(remapped_key);
261 const auto& key_str = remapped_key.to_string();
263 config_.blackboard->set(key_str, value);
269 template <
typename T>
272 for (
const auto& it : getProvidedPorts<T>())
274 const auto& port_name = it.first;
275 const auto direction = it.second.direction();
std::string registration_ID_
std::condition_variable state_condition_variable_
NodeConfiguration config_
StatusChangeSignal::Subscriber StatusChangeSubscriber
std::function< void(CallableArgs...)> CallableFunction
const std::type_info & type() const noexcept
StatusChangeSignal::CallableFunction StatusChangeCallback
static pthread_mutex_t mutex
std::shared_ptr< Blackboard > Ptr
This information is used mostly by the XMLParser.
Result setOutput(const std::string &key, const T &value)
Blackboard::Ptr blackboard
std::shared_ptr< CallableFunction > Subscriber
The BehaviorTreeFactory is used to create instances of a TreeNode at run-time.
bool empty() const noexcept
nonstd::string_view StringView
std::string registration_ID
Result getInput(const std::string &key, T &destination) const
PortsRemapping output_ports
Abstract base class for Behavior Tree Nodes.
std::unordered_map< std::string, PortInfo > PortsList
StatusChangeSignal state_change_signal_
Optional< T > getInput(const std::string &key) const
nonstd::expected< T, std::string > Optional
void setRegistrationID(StringView ID)
std::shared_ptr< TreeNode > Ptr
NodeType
Enumerates the possible types of nodes.
PortsRemapping input_ports
void assignDefaultRemapping(NodeConfiguration &config)
std::unordered_map< std::string, std::string > PortsRemapping