blackboard.cpp
Go to the documentation of this file.
2 
3 namespace BT
4 {
5 void Blackboard::enableAutoRemapping(bool remapping)
6 {
7  autoremapping_ = remapping;
8 }
9 
10 const PortInfo* Blackboard::portInfo(const std::string& key)
11 {
12  std::unique_lock<std::mutex> lock(mutex_);
13 
14  auto it = storage_.find(key);
15  return (it == storage_.end()) ? nullptr : &(it->second->port_info);
16 }
17 
19 {
20  internal_to_external_.insert(
21  {static_cast<std::string>(internal), static_cast<std::string>(external)});
22 }
23 
25 {
26  for (const auto& it: storage_)
27  {
28  const auto& key = it.first;
29  const auto& entry = it.second;
30 
31  auto port_type = entry->port_info.type();
32  if (!port_type)
33  {
34  port_type = &(entry->value.type());
35  }
36 
37  std::cout << key << " (" << demangle(port_type) << ") -> ";
38 
39  if (auto parent = parent_bb_.lock())
40  {
41  auto remapping_it = internal_to_external_.find(key);
42  if (remapping_it != internal_to_external_.end())
43  {
44  std::cout << "remapped to parent [" << remapping_it->second << "]" << std::endl;
45  continue;
46  }
47  }
48  std::cout << ((entry->value.empty()) ? "empty" : "full") << std::endl;
49  }
50 }
51 
52 std::vector<StringView> Blackboard::getKeys() const
53 {
54  if (storage_.empty())
55  {
56  return {};
57  }
58  std::vector<StringView> out;
59  out.reserve(storage_.size());
60  for (const auto& entry_it : storage_)
61  {
62  out.push_back(entry_it.first);
63  }
64  return out;
65 }
66 
67 std::shared_ptr<Blackboard::Entry>
68 Blackboard::createEntryImpl(const std::string &key, const PortInfo& info)
69 {
70  std::unique_lock<std::mutex> lock(mutex_);
71  // This function might be called recursively, when we do remapping, because we move
72  // to the top scope to find already existing entries
73 
74  // search if exists already
75  auto storage_it = storage_.find(key);
76  if(storage_it != storage_.end())
77  {
78  const auto old_type = storage_it->second->port_info.type();
79  if (old_type && info.type() && old_type != info.type())
80  {
81  throw LogicError("Blackboard: once declared, the type of a port "
82  "shall not change. Previously declared type [",
83  BT::demangle(old_type), "] != new type [",
84  BT::demangle(info.type()), "]");
85  }
86  return storage_it->second;
87  }
88 
89  std::shared_ptr<Entry> entry;
90 
91  // manual remapping first
92  auto remapping_it = internal_to_external_.find(key);
93  if (remapping_it != internal_to_external_.end())
94  {
95  const auto& remapped_key = remapping_it->second;
96  if (auto parent = parent_bb_.lock())
97  {
98  entry = parent->createEntryImpl(remapped_key, info);
99  }
100  }
101  else if(autoremapping_)
102  {
103  if (auto parent = parent_bb_.lock())
104  {
105  entry = parent->createEntryImpl(key, info);
106  }
107  }
108  else // not remapped, nor found. Create locally.
109  {
110  entry = std::make_shared<Entry>(info);
111  }
112  storage_.insert( {key, entry} );
113  return entry;
114 }
115 
116 } // namespace BT
std::shared_ptr< Entry > createEntryImpl(const std::string &key, const PortInfo &info)
Definition: blackboard.cpp:68
const PortInfo * portInfo(const std::string &key)
Definition: blackboard.cpp:10
const std::type_info * type() const
std::unordered_map< std::string, std::string > internal_to_external_
Definition: blackboard.h:211
std::string demangle(char const *name)
Definition: demangle_util.h:72
bool autoremapping_
Definition: blackboard.h:213
nonstd::string_view StringView
Definition: basic_types.h:55
std::weak_ptr< Blackboard > parent_bb_
Definition: blackboard.h:210
std::mutex mutex_
Definition: blackboard.h:207
std::unordered_map< std::string, std::shared_ptr< Entry > > storage_
Definition: blackboard.h:209
void debugMessage() const
Definition: blackboard.cpp:24
std::vector< StringView > getKeys() const
Definition: blackboard.cpp:52
void addSubtreeRemapping(StringView internal, StringView external)
Definition: blackboard.cpp:18
void enableAutoRemapping(bool remapping)
Definition: blackboard.cpp:5


behaviortree_cpp_v3
Author(s): Michele Colledanchise, Davide Faconti
autogenerated on Mon Jul 3 2023 02:50:14