blackboard.cpp
Go to the documentation of this file.
2 
3 namespace BT{
4 
5 void Blackboard::setPortInfo(std::string key, const PortInfo& info)
6 {
7  std::unique_lock<std::mutex> lock(mutex_);
8 
9  if( auto parent = parent_bb_.lock())
10  {
11  auto remapping_it = internal_to_external_.find(key);
12  if( remapping_it != internal_to_external_.end())
13  {
14  parent->setPortInfo( remapping_it->second, info );
15  }
16  }
17 
18  auto it = storage_.find(key);
19  if( it == storage_.end() )
20  {
21  storage_.insert( { std::move(key), Entry(info) } );
22  }
23  else{
24  auto old_type = it->second.port_info.type();
25  if( old_type && old_type != info.type() )
26  {
27  throw LogicError( "Blackboard::set() failed: once declared, the type of a port shall not change. "
28  "Declared type [", BT::demangle( old_type ),
29  "] != current type [", BT::demangle( info.type() ), "]" );
30  }
31  }
32 }
33 
34 const PortInfo* Blackboard::portInfo(const std::string &key)
35 {
36  std::unique_lock<std::mutex> lock(mutex_);
37  auto it = storage_.find(key);
38  if( it == storage_.end() )
39  {
40  return nullptr;
41  }
42  return &(it->second.port_info);
43 }
44 
46 {
47  internal_to_external_.insert( {static_cast<std::string>(internal), static_cast<std::string>(external)} );
48 }
49 
51 {
52  for(const auto& entry_it: storage_)
53  {
54  auto port_type = entry_it.second.port_info.type();
55  if( !port_type )
56  {
57  port_type = &( entry_it.second.value.type() );
58  }
59 
60  std::cout << entry_it.first << " (" << demangle( port_type ) << ") -> ";
61 
62  if( auto parent = parent_bb_.lock())
63  {
64  auto remapping_it = internal_to_external_.find( entry_it.first );
65  if( remapping_it != internal_to_external_.end())
66  {
67  std::cout << "remapped to parent [" << remapping_it->second << "]" <<std::endl;
68  continue;
69  }
70  }
71  std::cout << ((entry_it.second.value.empty()) ? "empty" : "full") << std::endl;
72  }
73 }
74 
75 std::vector<StringView> Blackboard::getKeys() const
76 {
77  if( storage_.empty() ){
78  return {};
79  }
80  std::vector<StringView> out;
81  out.reserve( storage_.size() );
82  for(const auto& entry_it: storage_)
83  {
84  out.push_back( entry_it.first );
85  }
86  return out;
87 }
88 
89 }
void debugMessage() const
Definition: blackboard.cpp:50
const PortInfo * portInfo(const std::string &key)
Definition: blackboard.cpp:34
const std::type_info * type() const
std::string demangle(char const *name)
Definition: demangle_util.h:78
std::vector< StringView > getKeys() const
Definition: blackboard.cpp:75
nonstd::string_view StringView
Definition: basic_types.h:50
std::weak_ptr< Blackboard > parent_bb_
Definition: blackboard.h:217
std::mutex mutex_
Definition: blackboard.h:215
std::unordered_map< std::string, Entry > storage_
Definition: blackboard.h:216
void setPortInfo(std::string key, const PortInfo &info)
Definition: blackboard.cpp:5
std::unordered_map< std::string, std::string > internal_to_external_
Definition: blackboard.h:218
void addSubtreeRemapping(StringView internal, StringView external)
Definition: blackboard.cpp:45


behaviotree_cpp_v3
Author(s): Michele Colledanchise, Davide Faconti
autogenerated on Tue May 4 2021 02:56:24