NoJobEngine
2.0.7
Toggle main menu visibility
Loading...
Searching...
No Matches
BTBlackboard.h
Go to the documentation of this file.
1
6
#pragma once
7
8
#include <any>
9
#include <algorithm>
10
#include <functional>
11
#include <cstdint>
12
#include <vector>
13
#include <stdexcept>
14
#include <string>
15
#include <string_view>
16
#include <type_traits>
17
#include <typeindex>
18
#include <unordered_map>
19
#include <utility>
20
21
namespace
NoJob
{
22
23
// Data owned by one BTContext/BTInstance, never by a shared BTTree.
24
// Keys are case-sensitive. Types must match exactly (no implicit conversions).
25
class
BTBlackboard
{
26
public
:
27
template
<
typename
T>
28
void
Set
(std::string key, T&& value) {
29
using
Stored = std::decay_t<T>;
30
static_assert
(std::is_copy_constructible_v<Stored>,
31
"Blackboard values must be copy constructible"
);
32
if
(key.empty())
throw
std::invalid_argument(
"Blackboard key cannot be empty"
);
33
const
auto
existing = m_Values.find(key);
34
if
(existing != m_Values.end() && existing->second.type() !=
typeid
(Stored))
35
throw
std::invalid_argument(
"Blackboard type mismatch for key: "
+ key);
36
m_Values.insert_or_assign(key, std::any(std::forward<T>(value)));
37
Notify(key);
38
}
39
40
// C-string literals are stored as std::string, not const char*.
41
void
Set
(std::string key,
const
char
* value) {
42
if
(!value)
throw
std::invalid_argument(
"Blackboard string cannot be null"
);
43
Set
(std::move(key), std::string(value));
44
}
45
46
template
<
typename
T>
47
const
T*
TryGet
(std::string_view key)
const
{
48
const
auto
it = m_Values.find(std::string(key));
49
return
it == m_Values.end() ? nullptr : std::any_cast<T>(&it->second);
50
}
51
52
template
<
typename
T>
53
T*
TryGet
(std::string_view key) {
54
const
auto
it = m_Values.find(std::string(key));
55
return
it == m_Values.end() ? nullptr : std::any_cast<T>(&it->second);
56
}
57
58
template
<
typename
T>
59
const
T&
Get
(std::string_view key)
const
{
60
const
T* value =
TryGet<T>
(key);
61
if
(!value)
throw
std::runtime_error(
62
"Blackboard key missing or type mismatch: "
+ std::string(key));
63
return
*value;
64
}
65
66
template
<
typename
T>
67
T&
Get
(std::string_view key) {
68
T* value =
TryGet<T>
(key);
69
if
(!value)
throw
std::runtime_error(
70
"Blackboard key missing or type mismatch: "
+ std::string(key));
71
return
*value;
72
}
73
74
template
<
typename
T>
75
T
GetOr
(std::string_view key, T fallback)
const
{
76
if
(
const
T* value =
TryGet<T>
(key))
return
*value;
77
return
fallback;
78
}
79
80
bool
Contains
(std::string_view key)
const
{
81
return
m_Values.find(std::string(key)) != m_Values.end();
82
}
83
84
bool
Remove
(std::string_view key) {
85
const
bool
removed = m_Values.erase(std::string(key)) != 0;
86
if
(removed) Notify(std::string(key));
87
return
removed;
88
}
89
90
void
Clear
() {
91
std::vector<std::string> keys;
92
for
(
const
auto
& entry : m_Values) keys.push_back(entry.first);
93
m_Values.clear();
94
for
(
const
auto
& key : keys) Notify(key);
95
}
96
using
ObserverID
= std::uint64_t;
97
ObserverID
Observe
(std::string key, std::function<
void
(std::string_view)> callback) {
98
if
(!callback)
throw
std::invalid_argument(
"Blackboard observer requires callback"
);
99
const
auto
id
= ++m_NextObserver;
100
m_Observers.emplace(
id
, Observer{std::move(key), std::move(callback)});
101
return
id;
102
}
103
bool
Unobserve
(
ObserverID
id
) {
return
m_Observers.erase(
id
) != 0; }
104
std::uint64_t
Revision
() const noexcept {
return
m_Revision; }
105
std::size_t
Size
()
const
{
return
m_Values.size(); }
106
107
struct
DebugEntry
{ std::string
Key
,
Type
,
Value
; };
108
std::vector<DebugEntry>
DebugEntries
()
const
{
109
std::vector<DebugEntry> entries;
110
entries.reserve(m_Values.size());
111
for
(
const
auto
& [key, value] : m_Values) {
112
DebugEntry
e{key, value.type().name(),
"<unsupported type>"
};
113
if
(
const
auto
* boolValue = std::any_cast<bool>(&value)) {
114
e.
Type
=
"bool"
; e.
Value
= *boolValue ?
"true"
:
"false"
;
115
}
else
if
(
const
auto
* intValue = std::any_cast<int>(&value)) {
116
e.
Type
=
"int"
; e.
Value
= std::to_string(*intValue);
117
}
else
if
(
const
auto
* uintValue = std::any_cast<std::uint32_t>(&value)) {
118
e.
Type
=
"uint32"
; e.
Value
= std::to_string(*uintValue);
119
}
else
if
(
const
auto
* floatValue = std::any_cast<float>(&value)) {
120
e.
Type
=
"float"
; e.
Value
= std::to_string(*floatValue);
121
}
else
if
(
const
auto
* doubleValue = std::any_cast<double>(&value)) {
122
e.
Type
=
"double"
; e.
Value
= std::to_string(*doubleValue);
123
}
else
if
(
const
auto
* stringValue = std::any_cast<std::string>(&value)) {
124
e.
Type
=
"string"
; e.
Value
= *stringValue;
125
}
126
entries.push_back(std::move(e));
127
}
128
std::sort(entries.begin(), entries.end(), [](
const
auto
& a,
const
auto
& b) { return a.Key < b.Key; });
129
return
entries;
130
}
131
132
// For debugging and future editor tooling; returns typeid(void) if absent.
133
std::type_index
TypeOf
(std::string_view key)
const
{
134
const
auto
it = m_Values.find(std::string(key));
135
return
it == m_Values.end() ? std::type_index(
typeid
(
void
))
136
: std::type_index(it->second.type());
137
}
138
139
private
:
140
struct
Observer { std::string Key; std::function<void(std::string_view)> Callback; };
141
void
Notify(
const
std::string& key) {
142
++m_Revision;
143
// Snapshot IDs so callbacks may safely unsubscribe.
144
std::vector<ObserverID> pending;
145
for
(
const
auto
& [
id
, observer] : m_Observers)
146
if
(observer.Key.empty() || observer.Key == key) pending.push_back(
id
);
147
for
(
auto
id
: pending) {
148
auto
it = m_Observers.find(
id
);
149
if
(it != m_Observers.end()) it->second.Callback(key);
150
}
151
}
152
std::unordered_map<ObserverID, Observer> m_Observers;
153
ObserverID
m_NextObserver = 0;
154
std::uint64_t m_Revision = 0;
155
std::unordered_map<std::string, std::any> m_Values;
156
};
157
158
}
// namespace NoJob
NoJob::BTBlackboard
Definition
BTBlackboard.h:25
NoJob::BTBlackboard::Set
void Set(std::string key, const char *value)
Definition
BTBlackboard.h:41
NoJob::BTBlackboard::Set
void Set(std::string key, T &&value)
Definition
BTBlackboard.h:28
NoJob::BTBlackboard::Revision
std::uint64_t Revision() const noexcept
Definition
BTBlackboard.h:104
NoJob::BTBlackboard::GetOr
T GetOr(std::string_view key, T fallback) const
Definition
BTBlackboard.h:75
NoJob::BTBlackboard::TypeOf
std::type_index TypeOf(std::string_view key) const
Definition
BTBlackboard.h:133
NoJob::BTBlackboard::ObserverID
std::uint64_t ObserverID
Definition
BTBlackboard.h:96
NoJob::BTBlackboard::Contains
bool Contains(std::string_view key) const
Definition
BTBlackboard.h:80
NoJob::BTBlackboard::Get
T & Get(std::string_view key)
Definition
BTBlackboard.h:67
NoJob::BTBlackboard::DebugEntries
std::vector< DebugEntry > DebugEntries() const
Definition
BTBlackboard.h:108
NoJob::BTBlackboard::Get
const T & Get(std::string_view key) const
Definition
BTBlackboard.h:59
NoJob::BTBlackboard::Clear
void Clear()
Definition
BTBlackboard.h:90
NoJob::BTBlackboard::Size
std::size_t Size() const
Definition
BTBlackboard.h:105
NoJob::BTBlackboard::TryGet
const T * TryGet(std::string_view key) const
Definition
BTBlackboard.h:47
NoJob::BTBlackboard::Remove
bool Remove(std::string_view key)
Definition
BTBlackboard.h:84
NoJob::BTBlackboard::Observe
ObserverID Observe(std::string key, std::function< void(std::string_view)> callback)
Definition
BTBlackboard.h:97
NoJob::BTBlackboard::TryGet
T * TryGet(std::string_view key)
Definition
BTBlackboard.h:53
NoJob::BTBlackboard::Unobserve
bool Unobserve(ObserverID id)
Definition
BTBlackboard.h:103
NoJob
Definition
BTActionNode.h:13
NoJob::BTBlackboard::DebugEntry
Definition
BTBlackboard.h:107
NoJob::BTBlackboard::DebugEntry::Key
std::string Key
Definition
BTBlackboard.h:107
NoJob::BTBlackboard::DebugEntry::Type
std::string Type
Definition
BTBlackboard.h:107
NoJob::BTBlackboard::DebugEntry::Value
std::string Value
Definition
BTBlackboard.h:107
Engine
AI
BehaviorTree
BTBlackboard.h
Generated by
1.18.0