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 Any *Blackboard::getAny(const std::string &key) const
11 {
12  std::unique_lock<std::mutex> lock(mutex_);
13  auto it = storage_.find(key);
14 
15  if(it == storage_.end())
16  {
17  // Try with autoremapping. This should work recursively
18  auto remapping_it = internal_to_external_.find(key);
19  if (remapping_it != internal_to_external_.end())
20  {
21  const auto& remapped_key = remapping_it->second;
22  if (auto parent = parent_bb_.lock())
23  {
24  return parent->getAny(remapped_key);
25  }
26  }
27 
28  else if(autoremapping_)
29  {
30  if(auto parent = parent_bb_.lock()) {
31  return parent->getAny(key);
32  }
33  }
34  return nullptr;
35  }
36  return &(it->second->value);
37 }
38 
39 const PortInfo* Blackboard::portInfo(const std::string& key)
40 {
41  std::unique_lock<std::mutex> lock(mutex_);
42 
43  auto it = storage_.find(key);
44  return (it == storage_.end()) ? nullptr : &(it->second->port_info);
45 }
46 
48 {
49  internal_to_external_.insert(
50  {static_cast<std::string>(internal), static_cast<std::string>(external)});
51 }
52 
54 {
55  for (const auto& it: storage_)
56  {
57  const auto& key = it.first;
58  const auto& entry = it.second;
59 
60  auto port_type = entry->port_info.type();
61  if (!port_type)
62  {
63  port_type = &(entry->value.type());
64  }
65 
66  std::cout << key << " (" << demangle(port_type) << ") -> ";
67 
68  if (auto parent = parent_bb_.lock())
69  {
70  auto remapping_it = internal_to_external_.find(key);
71  if (remapping_it != internal_to_external_.end())
72  {
73  std::cout << "remapped to parent [" << remapping_it->second << "]" << std::endl;
74  continue;
75  }
76  }
77  std::cout << ((entry->value.empty()) ? "empty" : "full") << std::endl;
78  }
79 }
80 
81 std::vector<StringView> Blackboard::getKeys() const
82 {
83  if (storage_.empty())
84  {
85  return {};
86  }
87  std::vector<StringView> out;
88  out.reserve(storage_.size());
89  for (const auto& entry_it : storage_)
90  {
91  out.push_back(entry_it.first);
92  }
93  return out;
94 }
95 
96 std::shared_ptr<Blackboard::Entry>
97 Blackboard::createEntryImpl(const std::string &key, const PortInfo& info)
98 {
99  std::unique_lock<std::mutex> lock(mutex_);
100  // This function might be called recursively, when we do remapping, because we move
101  // to the top scope to find already existing entries
102 
103  // search if exists already
104  auto storage_it = storage_.find(key);
105  if(storage_it != storage_.end())
106  {
107  const auto& prev_info = storage_it->second->port_info;
108  if (prev_info.type() != info.type() &&
109  prev_info.isStronglyTyped() &&
110  info.isStronglyTyped())
111  {
112  throw LogicError("Blackboard: once declared, the type of a port "
113  "shall not change. Previously declared type [",
114  BT::demangle(prev_info.type()), "] != new type [",
115  BT::demangle(info.type()), "]");
116  }
117  return storage_it->second;
118  }
119 
120  std::shared_ptr<Entry> entry;
121 
122  // manual remapping first
123  auto remapping_it = internal_to_external_.find(key);
124  if (remapping_it != internal_to_external_.end())
125  {
126  const auto& remapped_key = remapping_it->second;
127  if (auto parent = parent_bb_.lock())
128  {
129  entry = parent->createEntryImpl(remapped_key, info);
130  }
131  }
132  else if(autoremapping_)
133  {
134  if (auto parent = parent_bb_.lock())
135  {
136  entry = parent->createEntryImpl(key, info);
137  }
138  }
139  else // not remapped, nor found. Create locally.
140  {
141  entry = std::make_shared<Entry>(info);
142  }
143  storage_.insert( {key, entry} );
144  return entry;
145 }
146 
147 } // namespace BT
BT::PortInfo
Definition: basic_types.h:225
BT
Definition: ex01_wrap_legacy.cpp:29
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::PortInfo::type
const std::type_info * type() const
Definition: basic_types.cpp:292
BT::Any
Definition: safe_any.hpp:22
BT::Blackboard::mutex_
std::mutex mutex_
Definition: blackboard.h:204
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::debugMessage
void debugMessage() const
Definition: blackboard.cpp:53
BT::Blackboard::enableAutoRemapping
void enableAutoRemapping(bool remapping)
Definition: blackboard.cpp:5
BT::Blackboard::addSubtreeRemapping
void addSubtreeRemapping(StringView internal, StringView external)
Definition: blackboard.cpp:47
BT::Blackboard::createEntryImpl
std::shared_ptr< Entry > createEntryImpl(const std::string &key, const PortInfo &info)
Definition: blackboard.cpp:97
BT::Blackboard::portInfo
const PortInfo * portInfo(const std::string &key)
Definition: blackboard.cpp:39
BT::PortInfo::isStronglyTyped
bool isStronglyTyped() const
Definition: basic_types.h:260
BT::Blackboard::internal_to_external_
std::unordered_map< std::string, std::string > internal_to_external_
Definition: blackboard.h:208
blackboard.h
BT::StringView
nonstd::string_view StringView
Definition: basic_types.h:55
BT::Blackboard::autoremapping_
bool autoremapping_
Definition: blackboard.h:210


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