8 #include <unordered_map> 25 typedef std::shared_ptr<Blackboard>
Ptr;
38 return std::shared_ptr<Blackboard>(
new Blackboard(parent));
53 std::unique_lock<std::mutex> lock(
mutex_);
62 return parent->getAny(key);
67 return &(it->second->value);
81 bool get(
const std::string& key, T& value)
const 86 value = val->
cast<T>();
95 T
get(
const std::string& key)
const 100 return val->
cast<T>();
104 throw RuntimeError(
"Blackboard::get() error. Missing key [", key,
"]");
109 template <
typename T>
110 void set(
const std::string& key,
const T& value)
113 std::unique_lock<std::mutex> lock(
mutex_);
117 std::shared_ptr<Entry> entry;
124 Any new_value(value);
127 entry->value = new_value;
131 const PortInfo& port_info = entry->port_info;
132 auto& previous_any = entry->value;
133 const auto previous_type = port_info.
type();
135 Any new_value(value);
137 if (previous_type && *previous_type !=
typeid(T) &&
138 *previous_type != new_value.
type())
140 bool mismatching =
true;
141 if (std::is_constructible<StringView, T>::value)
144 if (any_from_string.
empty() ==
false)
147 new_value = std::move(any_from_string);
155 throw LogicError(
"Blackboard::set() failed: once declared, the type of a port " 156 "shall not change. Declared type [",
161 previous_any = std::move(new_value);
170 std::vector<StringView>
getKeys()
const;
174 std::unique_lock<std::mutex> lock(
mutex_);
201 value(
std::move(other_any)), port_info(info)
209 std::unordered_map<std::string, std::shared_ptr<Entry>>
storage_;
218 #endif // BLACKBOARD_H
virtual ~Blackboard()=default
std::shared_ptr< Entry > createEntryImpl(const std::string &key, const PortInfo &info)
static Blackboard::Ptr create(Blackboard::Ptr parent={})
const std::type_info & type() const noexcept
const PortInfo * portInfo(const std::string &key)
static pthread_mutex_t mutex
std::shared_ptr< Blackboard > Ptr
Any * getAny(const std::string &key)
const std::type_info * type() const
std::unordered_map< std::string, std::string > internal_to_external_
The Blackboard is the mechanism used by BehaviorTrees to exchange typed data.
std::string demangle(char const *name)
bool empty() const noexcept
nonstd::string_view StringView
std::weak_ptr< Blackboard > parent_bb_
Entry(Any &&other_any, const PortInfo &info)
std::mutex & entryMutex()
void createEntry(const std::string &key, const PortInfo &info)
std::unordered_map< std::string, std::shared_ptr< Entry > > storage_
Entry(const PortInfo &info)
const Any * getAny(const std::string &key) const
The method getAny allow the user to access directly the type erased value.
void debugMessage() const
std::vector< StringView > getKeys() const
Any parseString(const char *str) const
Blackboard(Blackboard::Ptr parent)
void addSubtreeRemapping(StringView internal, StringView external)
void enableAutoRemapping(bool remapping)