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 {
18 
24 {
25  public:
26  typedef std::shared_ptr<Blackboard> Ptr;
27 
28  protected:
29  // This is intentionally protected. Use Blackboard::create instead
31  {}
32 
33  public:
34 
39  {
40  return std::shared_ptr<Blackboard>( new Blackboard(parent) );
41  }
42 
43  virtual ~Blackboard() = default;
44 
51  const Any* getAny(const std::string& key) const
52  {
53  std::unique_lock<std::mutex> lock(mutex_);
54 
55  if( auto parent = parent_bb_.lock())
56  {
57  auto remapping_it = internal_to_external_.find(key);
58  if( remapping_it != internal_to_external_.end())
59  {
60  return parent->getAny( remapping_it->second );
61  }
62  }
63  auto it = storage_.find(key);
64  return ( it == storage_.end()) ? nullptr : &(it->second.value);
65  }
66 
67  Any* getAny(const std::string& key)
68  {
69  std::unique_lock<std::mutex> lock(mutex_);
70 
71  if( auto parent = parent_bb_.lock())
72  {
73  auto remapping_it = internal_to_external_.find(key);
74  if( remapping_it != internal_to_external_.end())
75  {
76  return parent->getAny( remapping_it->second );
77  }
78  }
79  auto it = storage_.find(key);
80  return ( it == storage_.end()) ? nullptr : &(it->second.value);
81  }
82 
86  template <typename T>
87  bool get(const std::string& key, T& value) const
88  {
89  const Any* val = getAny(key);
90  if (val)
91  {
92  value = val->cast<T>();
93  }
94  return (bool)val;
95  }
96 
100  template <typename T>
101  T get(const std::string& key) const
102  {
103  const Any* val = getAny(key);
104  if (val)
105  {
106  return val->cast<T>();
107  }
108  else
109  {
110  throw RuntimeError("Blackboard::get() error. Missing key [", key, "]");
111  }
112  }
113 
114 
116  template <typename T>
117  void set(const std::string& key, const T& value)
118  {
119  std::unique_lock<std::mutex> lock(mutex_);
120  auto it = storage_.find(key);
121 
122  if( auto parent = parent_bb_.lock())
123  {
124  auto remapping_it = internal_to_external_.find(key);
125  if( remapping_it != internal_to_external_.end())
126  {
127  const auto& remapped_key = remapping_it->second;
128  if( it == storage_.end() ) // virgin entry
129  {
130  auto parent_info = parent->portInfo(remapped_key);
131  if( parent_info )
132  {
133  storage_.insert( {key, Entry( *parent_info ) } );
134  }
135  else{
136  storage_.insert( {key, Entry( PortInfo() ) } );
137  }
138  }
139  parent->set( remapped_key, value );
140  return;
141  }
142  }
143 
144  if( it != storage_.end() ) // already there. check the type
145  {
146  const PortInfo& port_info = it->second.port_info;
147  auto& previous_any = it->second.value;
148  const auto locked_type = port_info.type();
149 
150  Any temp(value);
151 
152  if( locked_type && locked_type != &typeid(T) && locked_type != &temp.type() )
153  {
154  bool mismatching = true;
155  if( std::is_constructible<StringView, T>::value )
156  {
157  Any any_from_string = port_info.parseString( value );
158  if( any_from_string.empty() == false)
159  {
160  mismatching = false;
161  temp = std::move( any_from_string );
162  }
163  }
164 
165  if( mismatching )
166  {
167  debugMessage();
168 
169  throw LogicError( "Blackboard::set() failed: once declared, the type of a port shall not change. "
170  "Declared type [", demangle( locked_type ),
171  "] != current type [", demangle( typeid(T) ),"]" );
172  }
173  }
174  previous_any = std::move(temp);
175  }
176  else{ // create for the first time without any info
177  storage_.emplace( key, Entry( Any(value), PortInfo() ) );
178  }
179  return;
180  }
181 
182  void setPortInfo(std::string key, const PortInfo& info);
183 
184  const PortInfo *portInfo(const std::string& key);
185 
186  void addSubtreeRemapping(std::string internal, std::string external);
187 
188  void debugMessage() const;
189 
190  private:
191 
192  struct Entry{
195 
196  Entry( const PortInfo& info ):
197  port_info(info)
198  {}
199 
200  Entry(Any&& other_any, const PortInfo& info):
201  value(std::move(other_any)),
202  port_info(info)
203  {}
204  };
205 
207  std::unordered_map<std::string, Entry> storage_;
208  std::weak_ptr<Blackboard> parent_bb_;
209  std::unordered_map<std::string,std::string> internal_to_external_;
210 
211 };
212 
213 
214 } // end namespace
215 
216 #endif // BLACKBOARD_H
virtual ~Blackboard()=default
T cast() const
Definition: safe_any.hpp:119
static Blackboard::Ptr create(Blackboard::Ptr parent={})
Definition: blackboard.h:38
const std::type_info & type() const noexcept
Definition: safe_any.hpp:140
void debugMessage() const
Definition: blackboard.cpp:50
const PortInfo * portInfo(const std::string &key)
Definition: blackboard.cpp:34
static pthread_mutex_t mutex
Definition: minitrace.cpp:61
Definition: any.hpp:455
std::shared_ptr< Blackboard > Ptr
Definition: blackboard.h:26
Any * getAny(const std::string &key)
Definition: blackboard.h:67
The Blackboard is the mechanism used by BehaviorTrees to exchange typed data.
Definition: blackboard.h:23
const std::type_info * type() const
Any parseString(const char *str) const
std::string demangle(char const *name)
Definition: demangle_util.h:78
const Any * getAny(const std::string &key) const
The method getAny allow the user to access directly the type erased value.
Definition: blackboard.h:51
bool empty() const noexcept
Definition: safe_any.hpp:150
std::weak_ptr< Blackboard > parent_bb_
Definition: blackboard.h:208
void addSubtreeRemapping(std::string internal, std::string external)
Definition: blackboard.cpp:45
Entry(Any &&other_any, const PortInfo &info)
Definition: blackboard.h:200
std::mutex mutex_
Definition: blackboard.h:206
const PortInfo port_info
Definition: blackboard.h:194
std::unordered_map< std::string, Entry > storage_
Definition: blackboard.h:207
Entry(const PortInfo &info)
Definition: blackboard.h:196
void setPortInfo(std::string key, const PortInfo &info)
Definition: blackboard.cpp:5
Blackboard(Blackboard::Ptr parent)
Definition: blackboard.h:30
std::unordered_map< std::string, std::string > internal_to_external_
Definition: blackboard.h:209


behaviortree_cpp
Author(s): Michele Colledanchise, Davide Faconti
autogenerated on Sat Jun 8 2019 18:04:04