Botcraft 1.21.4
Loading...
Searching...
No Matches
Botcraft::Blackboard Class Reference

A map wrapper to store arbitrary data. More...

#include <Blackboard.hpp>

Public Member Functions

 Blackboard ()
 
 ~Blackboard ()
 
template<class T >
const T & Get (const std::string &key)
 Get the map value at key, casting it to T.
 
template<class T >
const T & Get (const std::string &key, const T &default_value)
 Get the map value at key, casting it to T.
 
template<class T >
NotifyOnEndUseRef< T > GetRef (const std::string &key)
 Get a ref to the map value at key, casting it to T&.
 
template<class T >
NotifyOnEndUseRef< T > GetRef (const std::string &key, const T &default_value)
 Get a ref to the map value at key, casting it to T&.
 
template<class T >
void Set (const std::string &key, const T &value)
 Set map entry at key to value.
 
void Copy (const std::string &src, const std::string &dst)
 Copy a blackboard value.
 
void Erase (const std::string &key)
 Remove a map entry if present.
 
void Reset (const std::map< std::string, std::any > &values={})
 Clear all the entries in the blackboard and load new ones.
 
void Subscribe (BlackboardObserver *observer)
 
void Unsubscribe (BlackboardObserver *observer)
 

Private Member Functions

void NotifyCleared () const
 
void NotifyKeyRemoved (const std::string &key) const
 
void NotifyKeyChanged (const std::string &key, const std::any &value) const
 

Private Attributes

std::map< std::string, std::any > blackboard
 
std::vector< BlackboardObserver * > observers
 

Detailed Description

A map wrapper to store arbitrary data.

Definition at line 46 of file Blackboard.hpp.

Constructor & Destructor Documentation

◆ Blackboard()

Botcraft::Blackboard::Blackboard ( )

Definition at line 5 of file Blackboard.cpp.

◆ ~Blackboard()

Botcraft::Blackboard::~Blackboard ( )

Definition at line 10 of file Blackboard.cpp.

Member Function Documentation

◆ Copy()

void Botcraft::Blackboard::Copy ( const std::string &  src,
const std::string &  dst 
)

Copy a blackboard value.

Parameters
srcSource key, must exist in the blackboard
dstDestination key

Definition at line 15 of file Blackboard.cpp.

References blackboard, and NotifyKeyChanged().

Referenced by Botcraft::CopyBlackboardDataImpl().

◆ Erase()

void Botcraft::Blackboard::Erase ( const std::string &  key)

Remove a map entry if present.

Parameters
keykey we want to remove

Definition at line 22 of file Blackboard.cpp.

References blackboard, and NotifyKeyRemoved().

Referenced by Botcraft::RemoveBlackboardDataImpl().

◆ Get() [1/2]

template<class T >
const T & Botcraft::Blackboard::Get ( const std::string &  key)
inline

◆ Get() [2/2]

template<class T >
const T & Botcraft::Blackboard::Get ( const std::string &  key,
const T &  default_value 
)
inline

Get the map value at key, casting it to T.

If the key is not present in the map, add it with default_value, and returns it.

Template Parameters
TAny type, must match the type stored at key
Parameters
keykey to retrieve the value from
default_valueThe default value to return if key is not found
Returns
The stored value

Definition at line 78 of file Blackboard.hpp.

◆ GetRef() [1/2]

template<class T >
NotifyOnEndUseRef< T > Botcraft::Blackboard::GetRef ( const std::string &  key)
inline

Get a ref to the map value at key, casting it to T&.

key must exist in the blackboard. Usage example:

{
NotifyOnEndUseRef<int> raw_ref = blackboard.GetRef<int>("key");
int& my_ref = raw_ref.ref;
my_ref = 8;
} // End of scope, raw_ref is destroyed and blackboard is notified value has potentially changed
std::map< std::string, std::any > blackboard
Wrapper around a reference that will run a callback on destruct.
T & ref() const &
Reference getter.
Template Parameters
TAny type, must match the type stored at key
Parameters
keykey to retrieve the value from
Returns
The stored value

Definition at line 102 of file Blackboard.hpp.

◆ GetRef() [2/2]

template<class T >
NotifyOnEndUseRef< T > Botcraft::Blackboard::GetRef ( const std::string &  key,
const T &  default_value 
)
inline

Get a ref to the map value at key, casting it to T&.

If the key is not present in the map, add it with default_value, and returns it. Usage example:

{
NotifyOnEndUseRef<int> raw_ref = blackboard.GetRef<int>("key", 0);
int& my_ref = raw_ref.ref;
my_ref = 8;
} // End of scope, raw_ref is destroyed and blackboard is notified value has potentially changed
Template Parameters
TAny type, must match the type stored at key
Parameters
keykey to retrieve the value from
default_valueThe default value to return if key is not found
Returns
The stored value

Definition at line 129 of file Blackboard.hpp.

◆ NotifyCleared()

void Botcraft::Blackboard::NotifyCleared ( ) const
private

Definition at line 59 of file Blackboard.cpp.

References observers.

Referenced by Reset().

◆ NotifyKeyChanged()

void Botcraft::Blackboard::NotifyKeyChanged ( const std::string &  key,
const std::any &  value 
) const
private

Definition at line 81 of file Blackboard.cpp.

References observers.

Referenced by Copy(), and Reset().

◆ NotifyKeyRemoved()

void Botcraft::Blackboard::NotifyKeyRemoved ( const std::string &  key) const
private

Definition at line 70 of file Blackboard.cpp.

References observers.

Referenced by Erase().

◆ Reset()

void Botcraft::Blackboard::Reset ( const std::map< std::string, std::any > &  values = {})

Clear all the entries in the blackboard and load new ones.

Parameters
valuesValues to load into the blackboard after clearing

Definition at line 28 of file Blackboard.cpp.

References blackboard, NotifyCleared(), and NotifyKeyChanged().

Referenced by Botcraft::TemplatedBehaviourClient< TDerived >::TreeLoop().

◆ Set()

◆ Subscribe()

void Botcraft::Blackboard::Subscribe ( BlackboardObserver observer)

Definition at line 39 of file Blackboard.cpp.

References observers.

Referenced by Botcraft::BehaviourClient::BehaviourClient().

◆ Unsubscribe()

void Botcraft::Blackboard::Unsubscribe ( BlackboardObserver observer)

Definition at line 44 of file Blackboard.cpp.

References observers.

Referenced by Botcraft::BehaviourClient::~BehaviourClient().

Member Data Documentation

◆ blackboard

std::map<std::string, std::any> Botcraft::Blackboard::blackboard
private

Definition at line 173 of file Blackboard.hpp.

Referenced by Copy(), Erase(), and Reset().

◆ observers

std::vector<BlackboardObserver*> Botcraft::Blackboard::observers
private

The documentation for this class was generated from the following files: