blackboard.h
Go to the documentation of this file.
00001 #ifndef BLACKBOARD_H
00002 #define BLACKBOARD_H
00003 
00004 #include <iostream>
00005 #include <string>
00006 #include <memory>
00007 #include <stdint.h>
00008 #include <unordered_map>
00009 
00010 #include "behaviortree_cpp/blackboard/safe_any.hpp"
00011 
00012 
00013 namespace BT
00014 {
00015 // This is the "backend" of the blackboard.
00016 // To create a new blackboard, user must inherit from BlackboardImpl
00017 // and override set and get.
00018 class BlackboardImpl
00019 {
00020   public:
00021     virtual ~BlackboardImpl() = default;
00022 
00023     virtual const SafeAny::Any* get(const std::string& key) const = 0;
00024     virtual void set(const std::string& key, const SafeAny::Any& value) = 0;
00025     virtual bool contains(const std::string& key) const = 0;
00026 };
00027 
00028 // This is the "frontend" to be used by the developer.
00029 //
00030 // Even if the abstract class BlackboardImpl can be used directly,
00031 // the templatized methods set() and get() are more user-friendly
00032 class Blackboard
00033 {
00034     // This is intentionally private. Use Blackboard::create instead
00035     Blackboard(std::unique_ptr<BlackboardImpl> base) : impl_(std::move(base))
00036     {
00037     }
00038 
00039   public:
00040     typedef std::shared_ptr<Blackboard> Ptr;
00041 
00042     Blackboard() = delete;
00043 
00047     template <typename ImplClass, typename... Args>
00048     static Blackboard::Ptr create(Args... args)
00049     {
00050         std::unique_ptr<BlackboardImpl> base(new ImplClass(args...));
00051         return std::shared_ptr<Blackboard>(new Blackboard(std::move(base)));
00052     }
00053 
00054     virtual ~Blackboard() = default;
00055 
00061     template <typename T>
00062     bool get(const std::string& key, T& value) const
00063     {
00064         if (!impl_)
00065         {
00066             return false;
00067         }
00068         const SafeAny::Any* val = impl_->get(key);
00069         if (!val)
00070         {
00071             return false;
00072         }
00073 
00074         value = val->cast<T>();
00075         return true;
00076     }
00077 
00078     const SafeAny::Any* getAny(const std::string& key) const
00079     {
00080         if (!impl_)
00081         {
00082             return nullptr;
00083         }
00084         return impl_->get(key);
00085     }
00086 
00087 
00088     template <typename T>
00089     T get(const std::string& key) const
00090     {
00091         T value;
00092         bool found = get(key, value);
00093         if (!found)
00094         {
00095             throw std::runtime_error("Missing key");
00096         }
00097         return value;
00098     }
00099 
00101     template <typename T>
00102     void set(const std::string& key, const T& value)
00103     {
00104         if (impl_)
00105         {
00106             impl_->set(key, SafeAny::Any(value));
00107         }
00108     }
00109 
00110     bool contains(const std::string& key) const
00111     {
00112         return (impl_ && impl_->contains(key));
00113     }
00114 
00115   private:
00116     std::unique_ptr<BlackboardImpl> impl_;
00117 };
00118 }
00119 
00120 #endif   // BLACKBOARD_H


behaviortree_cpp
Author(s): Michele Colledanchise, Davide Faconti
autogenerated on Sat Feb 2 2019 03:50:09