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 
11 
12 
13 namespace BT
14 {
15 // This is the "backend" of the blackboard.
16 // To create a new blackboard, user must inherit from BlackboardImpl
17 // and override set and get.
19 {
20  public:
21  virtual ~BlackboardImpl() = default;
22 
23  virtual const SafeAny::Any* get(const std::string& key) const = 0;
24  virtual void set(const std::string& key, const SafeAny::Any& value) = 0;
25  virtual bool contains(const std::string& key) const = 0;
26 };
27 
28 // This is the "frontend" to be used by the developer.
29 //
30 // Even if the abstract class BlackboardImpl can be used directly,
31 // the templatized methods set() and get() are more user-friendly
33 {
34  // This is intentionally private. Use Blackboard::create instead
35  Blackboard(std::unique_ptr<BlackboardImpl> base) : impl_(std::move(base))
36  {
37  }
38 
39  public:
40  typedef std::shared_ptr<Blackboard> Ptr;
41 
42  Blackboard() = delete;
43 
47  template <typename ImplClass, typename... Args>
48  static Blackboard::Ptr create(Args... args)
49  {
50  std::unique_ptr<BlackboardImpl> base(new ImplClass(args...));
51  return std::shared_ptr<Blackboard>(new Blackboard(std::move(base)));
52  }
53 
54  virtual ~Blackboard() = default;
55 
61  template <typename T>
62  bool get(const std::string& key, T& value) const
63  {
64  if (!impl_)
65  {
66  return false;
67  }
68  const SafeAny::Any* val = impl_->get(key);
69  if (!val)
70  {
71  return false;
72  }
73 
74  value = val->cast<T>();
75  return true;
76  }
77 
78  const SafeAny::Any* getAny(const std::string& key) const
79  {
80  if (!impl_)
81  {
82  return nullptr;
83  }
84  return impl_->get(key);
85  }
86 
87 
88  template <typename T>
89  T get(const std::string& key) const
90  {
91  T value;
92  bool found = get(key, value);
93  if (!found)
94  {
95  throw std::runtime_error("Missing key");
96  }
97  return value;
98  }
99 
101  template <typename T>
102  void set(const std::string& key, const T& value)
103  {
104  if (impl_)
105  {
106  impl_->set(key, SafeAny::Any(value));
107  }
108  }
109 
110  bool contains(const std::string& key) const
111  {
112  return (impl_ && impl_->contains(key));
113  }
114 
115  private:
116  std::unique_ptr<BlackboardImpl> impl_;
117 };
118 }
119 
120 #endif // BLACKBOARD_H
Definition: any.hpp:455
std::shared_ptr< Blackboard > Ptr
Definition: blackboard.h:40
bool contains(const std::string &key) const
Definition: blackboard.h:110
virtual bool contains(const std::string &key) const =0
static Blackboard::Ptr create(Args...args)
Definition: blackboard.h:48
const SafeAny::Any * getAny(const std::string &key) const
Definition: blackboard.h:78
Blackboard(std::unique_ptr< BlackboardImpl > base)
Definition: blackboard.h:35
std::unique_ptr< BlackboardImpl > impl_
Definition: blackboard.h:116
T cast() const
Definition: safe_any.hpp:81
virtual ~BlackboardImpl()=default


behaviortree_cpp
Author(s): Michele Colledanchise, Davide Faconti
autogenerated on Sat Feb 2 2019 04:01:53