blackboard.h
Go to the documentation of this file.
1 #ifndef BLACKBOARD_H
2 #define BLACKBOARD_H
3 
4 #include <iostream>
5 #include <string>
6 #include <memory>
7 #include <stdint.h>
8 #include <unordered_map>
9 #include <mutex>
10 #include <sstream>
11 
15 
16 namespace BT
17 {
23 {
24 public:
25  typedef std::shared_ptr<Blackboard> Ptr;
26 
27 protected:
28  // This is intentionally protected. Use Blackboard::create instead
30  {}
31 
32 public:
37  {
38  return std::shared_ptr<Blackboard>(new Blackboard(parent));
39  }
40 
41  virtual ~Blackboard() = default;
42 
43  void enableAutoRemapping(bool remapping);
44 
51  const Any* getAny(const std::string& key) const;
52 
53  Any* getAny(const std::string& key)
54  {
55  // "Avoid Duplication in const and Non-const Member Function,"
56  // on p. 23, in Item 3 "Use const whenever possible," in Effective C++, 3d ed
57  return const_cast<Any*>(static_cast<const Blackboard&>(*this).getAny(key));
58  }
59 
63  template <typename T>
64  bool get(const std::string& key, T& value) const
65  {
66  const Any* val = getAny(key);
67  if (val)
68  {
69  value = val->cast<T>();
70  }
71  return (bool)val;
72  }
73 
77  template <typename T>
78  T get(const std::string& key) const
79  {
80  const Any* val = getAny(key);
81  if (val)
82  {
83  return val->cast<T>();
84  }
85  else
86  {
87  throw RuntimeError("Blackboard::get() error. Missing key [", key, "]");
88  }
89  }
90 
92  template <typename T>
93  void set(const std::string& key, const T& value)
94  {
95  std::unique_lock<std::mutex> lock_entry(entry_mutex_);
96  std::unique_lock<std::mutex> lock(mutex_);
97 
98  // check local storage
99  auto it = storage_.find(key);
100  std::shared_ptr<Entry> entry;
101  if (it != storage_.end())
102  {
103  entry = it->second;
104  }
105  else
106  {
107  Any new_value(value);
108  std::shared_ptr<Blackboard::Entry> entry;
109  lock.unlock();
110  if(std::is_constructible<std::string, T>::value)
111  {
113  }
114  else {
115  entry = createEntryImpl(key, PortInfo(PortDirection::INOUT, new_value.type(), {}));
116  }
117  entry->value = new_value;
118  return;
119  }
120 
121  const PortInfo& port_info = entry->port_info;
122  auto& previous_any = entry->value;
123  const auto previous_type = port_info.type();
124 
125  Any new_value(value);
126 
127  if (previous_type && *previous_type != typeid(T) &&
128  *previous_type != new_value.type())
129  {
130  bool mismatching = true;
131  if (std::is_constructible<StringView, T>::value)
132  {
133  Any any_from_string = port_info.parseString(value);
134  if (any_from_string.empty() == false)
135  {
136  mismatching = false;
137  new_value = std::move(any_from_string);
138  }
139  }
140 
141  if (mismatching)
142  {
143  debugMessage();
144 
145  throw LogicError("Blackboard::set() failed: once declared, the type of a port "
146  "shall not change. Declared type [",
147  BT::demangle(previous_type), "] != current type [",
148  BT::demangle(typeid(T)), "]");
149  }
150  }
151  previous_any = std::move(new_value);
152  }
153 
154  const PortInfo* portInfo(const std::string& key);
155 
156  void addSubtreeRemapping(StringView internal, StringView external);
157 
158  void debugMessage() const;
159 
160  std::vector<StringView> getKeys() const;
161 
162  void clear()
163  {
164  std::unique_lock<std::mutex> lock(mutex_);
165  storage_.clear();
166  internal_to_external_.clear();
167  }
168 
169  // Lock this mutex before using get() and getAny() and unlock it while you have
170  // done using the value.
172  {
173  return entry_mutex_;
174  }
175 
176  void createEntry(const std::string& key, const PortInfo& info)
177  {
178  createEntryImpl(key, info);
179  }
180 
181  struct Entry
182  {
185 
186  Entry(const PortInfo& info) : port_info(info)
187  {}
188 
189  Entry(Any&& other_any, const PortInfo& info) :
190  value(std::move(other_any)), port_info(info)
191  {}
192  };
193 
194  std::shared_ptr<Entry> getEntry(const std::string& key) const
195  {
196  auto it = storage_.find(key);
197  return it == storage_.end() ? std::shared_ptr<Entry>() : it->second;
198  }
199 
200 private:
201 
202  std::shared_ptr<Entry> createEntryImpl(const std::string& key, const PortInfo& info);
203 
206  std::unordered_map<std::string, std::shared_ptr<Entry>> storage_;
207  std::weak_ptr<Blackboard> parent_bb_;
208  std::unordered_map<std::string, std::string> internal_to_external_;
209 
210  bool autoremapping_ = false;
211 };
212 
213 } // namespace BT
214 
215 #endif // BLACKBOARD_H
BT::PortInfo
Definition: basic_types.h:225
BT
Definition: ex01_wrap_legacy.cpp:29
BT::Blackboard::Entry::Entry
Entry(const PortInfo &info)
Definition: blackboard.h:186
BT::Blackboard::set
void set(const std::string &key, const T &value)
Update the entry with the given key.
Definition: blackboard.h:93
BT::Blackboard::parent_bb_
std::weak_ptr< Blackboard > parent_bb_
Definition: blackboard.h:207
BT::demangle
std::string demangle(char const *name)
Definition: demangle_util.h:72
BT::Blackboard::clear
void clear()
Definition: blackboard.h:162
BT::PortInfo::type
const std::type_info * type() const
Definition: basic_types.cpp:292
BT::PortDirection::INOUT
@ INOUT
exceptions.h
BT::Any
Definition: safe_any.hpp:22
minitrace::mutex
static pthread_mutex_t mutex
Definition: minitrace.cpp:61
basic_types.h
BT::Blackboard::get
bool get(const std::string &key, T &value) const
Definition: blackboard.h:64
BT::Blackboard::createEntry
void createEntry(const std::string &key, const PortInfo &info)
Definition: blackboard.h:176
BT::Blackboard::mutex_
std::mutex mutex_
Definition: blackboard.h:204
BT::Blackboard::entryMutex
std::mutex & entryMutex()
Definition: blackboard.h:171
BT::Blackboard::storage_
std::unordered_map< std::string, std::shared_ptr< Entry > > storage_
Definition: blackboard.h:206
BT::Blackboard::getKeys
std::vector< StringView > getKeys() const
Definition: blackboard.cpp:81
BT::LogicError
Definition: exceptions.h:45
BT::Blackboard::getAny
const Any * getAny(const std::string &key) const
The method getAny allow the user to access directly the type erased value.
Definition: blackboard.cpp:10
BT::Blackboard::Blackboard
Blackboard(Blackboard::Ptr parent)
Definition: blackboard.h:29
BT::Blackboard::debugMessage
void debugMessage() const
Definition: blackboard.cpp:53
BT::Blackboard::getEntry
std::shared_ptr< Entry > getEntry(const std::string &key) const
Definition: blackboard.h:194
BT::Blackboard::get
T get(const std::string &key) const
Definition: blackboard.h:78
BT::Blackboard::enableAutoRemapping
void enableAutoRemapping(bool remapping)
Definition: blackboard.cpp:5
BT::Blackboard::Entry
Definition: blackboard.h:181
BT::Blackboard::~Blackboard
virtual ~Blackboard()=default
BT::Any::empty
bool empty() const noexcept
Definition: safe_any.hpp:153
BT::Blackboard::addSubtreeRemapping
void addSubtreeRemapping(StringView internal, StringView external)
Definition: blackboard.cpp:47
BT::PortInfo::parseString
Any parseString(const char *str) const
Definition: basic_types.cpp:297
BT::RuntimeError
Definition: exceptions.h:58
BT::Blackboard::createEntryImpl
std::shared_ptr< Entry > createEntryImpl(const std::string &key, const PortInfo &info)
Definition: blackboard.cpp:97
BT::Blackboard
The Blackboard is the mechanism used by BehaviorTrees to exchange typed data.
Definition: blackboard.h:22
safe_any.hpp
BT::Any::cast
T cast() const
Definition: safe_any.hpp:120
BT::Blackboard::create
static Blackboard::Ptr create(Blackboard::Ptr parent={})
Definition: blackboard.h:36
BT::Blackboard::Entry::Entry
Entry(Any &&other_any, const PortInfo &info)
Definition: blackboard.h:189
BT::Blackboard::portInfo
const PortInfo * portInfo(const std::string &key)
Definition: blackboard.cpp:39
BT::Blackboard::Entry::value
Any value
Definition: blackboard.h:183
std
Definition: any.hpp:455
BT::Blackboard::getAny
Any * getAny(const std::string &key)
Definition: blackboard.h:53
BT::Blackboard::internal_to_external_
std::unordered_map< std::string, std::string > internal_to_external_
Definition: blackboard.h:208
BT::Any::type
const std::type_info & type() const noexcept
Definition: safe_any.hpp:143
BT::Blackboard::Entry::port_info
const PortInfo port_info
Definition: blackboard.h:184
BT::Blackboard::Ptr
std::shared_ptr< Blackboard > Ptr
Definition: blackboard.h:25
BT::StringView
nonstd::string_view StringView
Definition: basic_types.h:55
BT::Blackboard::autoremapping_
bool autoremapping_
Definition: blackboard.h:210
BT::Blackboard::entry_mutex_
std::mutex entry_mutex_
Definition: blackboard.h:205


behaviortree_cpp_v3
Author(s): Michele Colledanchise, Davide Faconti
autogenerated on Wed Jun 26 2024 02:51:19