13 enum class BlackboardError {
20 BlackboardError SetValue(BlackboardKey key, T value) {
21 static_assert(
sizeof(T) <=
sizeof(BlackboardValue),
"Value is too big - use a pointer.");
22 std::type_index type = (
typeid(T));
24 auto it = entries.find(key);
25 if (it == entries.end()) {
27 memcpy(&insertedEntry.first->second.value, &value,
sizeof(T));
29 else if (it->second.type != type) {
30 return BlackboardError::MismatchingType;
33 memcpy(&it->second.value, &value,
sizeof(T));
36 return BlackboardError::Success;
40 BlackboardError SetValueIfExists(BlackboardKey key, T value) {
41 static_assert(
sizeof(T) <=
sizeof(BlackboardValue),
"Value is too big - use a pointer.");
42 std::type_index type = (
typeid(T));
44 auto it = entries.find(key);
45 if (it == entries.end()) {
46 return BlackboardError::KeyNotFound;
48 else if (it->second.type != type) {
49 return BlackboardError::MismatchingType;
52 memcpy(&it->second.value, &value,
sizeof(T));
55 return BlackboardError::Success;
60 static_assert(
sizeof(T) <=
sizeof(BlackboardValue),
"Value is too big - use a pointer.");
61 std::type_index type = (
typeid(T));
62 auto it = entries.find(key);
63 if (it == entries.end()) {
64 return BlackboardError::KeyNotFound;
66 else if (it->second.type != type) {
67 return BlackboardError::MismatchingType;
71 memcpy(&value, &it->second.value,
sizeof(T));
77 [[nodiscard]]
bool TryGetValue(BlackboardKey key, T& outValue)
const {
78 static_assert(
sizeof(T) <=
sizeof(BlackboardValue),
"Value is too big - use a pointer.");
79 std::type_index type = (
typeid(T));
80 auto it = entries.find(key);
81 if (it == entries.end()) {
85 if (it->second.type != type) {
89 memcpy(&outValue, &it->second,
sizeof(T));
95 static inline BlackboardKey GetBlackboardKeyFromType() {
97 return typeid(T).name();
101 BlackboardError SetValue(T value) {
102 const BlackboardKey key = GetBlackboardKeyFromType<T>();
103 static_assert(
sizeof(T) <=
sizeof(BlackboardValue),
"Value is too big - use a pointer.");
104 std::type_index type = (
typeid(T));
106 auto it = entries.find(key);
107 if (it == entries.end()) {
109 memcpy(&insertedEntry.first->second.value, &value,
sizeof(T));
111 else if (it->second.type != type) {
112 return BlackboardError::MismatchingType;
115 memcpy(&it->second.value, &value,
sizeof(T));
118 return BlackboardError::Success;
122 BlackboardError SetValueIfExists(T value) {
123 const BlackboardKey key = GetBlackboardKeyFromType<T>();
124 static_assert(
sizeof(T) <=
sizeof(BlackboardValue),
"Value is too big - use a pointer.");
125 std::type_index type = (
typeid(T));
127 auto it = entries.find(key);
128 if (it == entries.end()) {
129 return BlackboardError::KeyNotFound;
131 else if (it->second.type != type) {
132 return BlackboardError::MismatchingType;
135 memcpy(&it->second.value, value,
sizeof(T));
138 return BlackboardError::Success;
143 const BlackboardKey key = GetBlackboardKeyFromType<T>();
144 static_assert(
sizeof(T) <=
sizeof(BlackboardValue),
"Value is too big - use a pointer.");
145 std::type_index type = (
typeid(T));
146 auto it = entries.find(key);
147 if (it == entries.end()) {
148 return BlackboardError::KeyNotFound;
150 else if (it->second.type != type) {
151 return BlackboardError::MismatchingType;
155 memcpy(&value, &it->second.value,
sizeof(T));
161 [[nodiscard]]
bool TryGetValue(T& outValue)
const {
162 const BlackboardKey key = GetBlackboardKeyFromType<T>();
163 static_assert(
sizeof(T) <=
sizeof(BlackboardValue),
"Value is too big - use a pointer.");
164 std::type_index type = (
typeid(T));
165 auto it = entries.find(key);
166 if (it == entries.end()) {
170 if (it->second.type != type) {
174 memcpy(&outValue, &it->second.value,
sizeof(T));
179 std::map<BlackboardKey, BlackboardEntry> entries;
185struct std::formatter<
Grindstone::Blackboard::BlackboardError> {
186 constexpr auto parse(std::format_parse_context& ctx) {
190 auto format(
const Grindstone::Blackboard::BlackboardError& error, std::format_context& ctx)
const {
192 case Grindstone::Blackboard::BlackboardError::Success:
193 return std::format_to(ctx.out(),
"Success");
194 case Grindstone::Blackboard::BlackboardError::MismatchingType:
195 return std::format_to(ctx.out(),
"Mismatching Type");
196 case Grindstone::Blackboard::BlackboardError::KeyNotFound:
197 return std::format_to(ctx.out(),
"Key not found");
199 return std::format_to(ctx.out(),
"Unknown Error");
Definition BlackboardEntry.hpp:10