-
Notifications
You must be signed in to change notification settings - Fork 2
/
Blackboard.h
603 lines (487 loc) · 21.3 KB
/
Blackboard.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
#pragma once
#include <typeinfo>
#include <unordered_map>
#include <mutex>
#include <assert.h>
#include <stdexcept>
namespace Utilities {
//! Forward declare the base type of the data storage object
namespace Templates { class BaseMap; }
//! Define alias' for the different types of event callbacks that can be defined
template<typename T> using EventKeyCallback = void(*)(const std::string&);
template<typename T> using EventValueCallback = void(*)(const T&);
template<typename T> using EventKeyValueCallback = void(*)(const std::string&, const T&);
/*
* Name: Blackboard
* Author: Mitchell Croft
* Created: 08/11/2016
* Modified: 09/11/2016
*
* Purpose:
* Provide a singleton location for a user to store
* generic data in a easily accessible location.
*
* Callback functionality will be implemented to allow
* listening for when specific keyed data is changed.
*
* The create and destroy functions must be called
* prior to use.
*
* Warning:
* Data types stored on the blackboard must have valid
* default and copy constructors defined as well as the
* assignment operator.
*
* Only one callback event of each type will be kept for
* each key of every value type.
**/
class Blackboard {
/*----------Singleton Values----------*/
static Blackboard* mInstance;
Blackboard() = default;
~Blackboard() = default;
/*----------Variables----------*/
//! Store a map of all of the different value types
std::unordered_map<size_t, Templates::BaseMap*> mDataStorage;
//! Store a mutex for locking data when in use
std::recursive_mutex mDataLock;
//! Convert a template type into a unique ID value
template<typename T> inline size_t templateToID() const;
//! Ensure that a ValueMap objects exists for a specific type
template<typename T> inline size_t supportType();
public:
//! Creation/destruction
/*----------------*/ static bool create();
/*----------------*/ static void destroy();
//! Data reading/writing
template<typename T> static void write(const std::string& pKey, const T& pValue, bool pRaiseCallbacks = true);
template<typename T> static const T& read(const std::string& pKey);
template<typename T> static void wipeTypeKey(const std::string& pKey);
/*----------------*/ static void wipeKey(const std::string& pKey);
/*----------------*/ static void wipeBoard(bool pWipeCallbacks = false);
//! Callback functions
template<typename T> static void subscribe(const std::string& pKey, EventKeyCallback<T> pCb);
template<typename T> static void subscribe(const std::string& pKey, EventValueCallback<T> pCb);
template<typename T> static void subscribe(const std::string& pKey, EventKeyValueCallback<T> pCb);
template<typename T> static void unsubscribe(const std::string& pKey);
/*----------------*/ static void unsubscribeAll(const std::string& pKey);
//! Getters
/*----------------*/ static inline bool isReady() { return (mInstance != nullptr); }
};
namespace Templates {
/*
* Name: BaseMap
* Author: Mitchell Croft
* Created: 08/11/2016
* Modified: 08/11/2016
*
* Purpose:
* Provide a base point for the templated ValueMap
* objects to inherit from. This allows the
* blackboard to store pointers to the templated
* versions for storing data.
**/
class BaseMap {
protected:
//! Set the Value map to be a friend of the blackboard to allow for construction/destruction of the object
friend class Blackboard;
//! Privatise the constructor/destructor to prevent external use
BaseMap() = default;
virtual ~BaseMap() = 0;
//! Provide virtual methods for wiping keyed information
inline virtual void wipeKey(const std::string& pKey) = 0;
inline virtual void wipeAll() = 0;
inline virtual void unsubscribe(const std::string& pKey) = 0;
inline virtual void clearAllEvents() = 0;
};
//! Define the default destructor for the BaseMap's pure virtual destructor
inline BaseMap::~BaseMap() {}
/*
* Name: ValueMap (General)
* Author: Mitchell Croft
* Created: 08/11/2016
* Modified: 08/11/2016
*
* Purpose:
* Store templated data type information for recollection
* and use within the Blackboard singleton object
**/
template<typename T>
class ValueMap : BaseMap {
protected:
//! Set the Value map to be a friend of the blackboard to allow for construction/destruction of the object
friend class Blackboard;
/*----------Variables----------*/
//! Store a map of the values for this type
std::unordered_map<std::string, T> mValues;
//! Store maps for the callback events
std::unordered_map<std::string, EventKeyCallback<T>> mKeyEvents;
std::unordered_map<std::string, EventValueCallback<T>> mValueEvents;
std::unordered_map<std::string, EventKeyValueCallback<T>> mPairEvents;
/*----------Functions----------*/
//! Privatise the constructor/destructor to prevent external use
ValueMap() = default;
~ValueMap() override {}
//! Override the functions used to remove keyed information
inline void wipeKey(const std::string& pKey) override;
inline void wipeAll() override;
inline void unsubscribe(const std::string& pKey) override;
inline void clearAllEvents() override;
};
}
#pragma region Template Definitions
#pragma region Blackboard
/*
Blackboard : templateToID<T> - Convert the template type T to a unique hash code
Author: Mitchell Croft
Created: 08/11/2016
Modified: 08/11/2016
template T - A generic, non void type
return size_t - Returns the ID as a size_t value
*/
template<typename T>
inline size_t Utilities::Blackboard::templateToID() const {
//Get the type info
const std::type_info& type = typeid(T);
//Return the hash code
return type.hash_code();
}
/*
Blackboard : supportType<T> - Using the type of the template ensure that there is a Value map to support
holding data of its type
Author: Mitchell Croft
Created: 08/11/2016
Modified: 08/11/2016
template T - A generic, non void type
return size_t - Returns the unique hash code for the template type T
*/
template<typename T>
inline size_t Utilities::Blackboard::supportType() {
//Get the hash code for the type
size_t key = templateToID<T>();
//If there isn't a entry for the hash code create a new map
if (mDataStorage.find(key) == mDataStorage.end())
mDataStorage[key] = new Utilities::Templates::ValueMap<T>();
//Return the key
return key;
}
/*
Blackboard : write<T> - Write a data value to the Blackboard
Author: Mitchell Croft
Created: 08/11/2016
Modified: 08/11/2016
template T - A generic, non void type
param[in] pKey - The key value to save the data value at
param[in] pValue - The data value to be saved to the key location
param[in] pRaiseCallbacks - A flag to indicate if callback events should be raised (Default true)
*/
template<typename T>
inline void Utilities::Blackboard::write(const std::string& pKey, const T& pValue, bool pRaiseCallbacks) {
//Ensure that the singleton has been created
assert(mInstance);
//Lock the data
std::lock_guard<std::recursive_mutex> guard(mInstance->mDataLock);
//Ensure the key for this type is supported
size_t key = mInstance->supportType<T>();
//Cast the Value Map to the type of T
Utilities::Templates::ValueMap<T>* map = (Utilities::Templates::ValueMap<T>*)(mInstance->mDataStorage[key]);
//Copy the data value across
map->mValues[pKey] = pValue;
//Check event flag
if (pRaiseCallbacks) {
//Check for events to raise
if (map->mKeyEvents.find(pKey) != map->mKeyEvents.end() && map->mKeyEvents[pKey]) map->mKeyEvents[pKey](pKey);
if (map->mValueEvents.find(pKey) != map->mValueEvents.end() && map->mValueEvents[pKey]) map->mValueEvents[pKey](map->mValues[pKey]);
if (map->mPairEvents.find(pKey) != map->mPairEvents.end() && map->mPairEvents[pKey]) map->mPairEvents[pKey](pKey, map->mValues[pKey]);
}
}
/*
Blackboard : read<T> - Read the value of a key value from the Blackboard
Author: Mitchell Croft
Created: 08/11/2016
Modified: 08/11/2016
template T - A generic, non void type
param[in] pKey - The key value to read the data value of
return const T& - Returns a constant reference to the data type of type T
*/
template<typename T>
inline const T& Utilities::Blackboard::read(const std::string& pKey) {
//Ensure that the singleton has been created
assert(mInstance);
//Lock the data
std::lock_guard<std::recursive_mutex> guard(mInstance->mDataLock);
//Ensure the key for this type is supported
size_t key = mInstance->supportType<T>();
//Cast the Value Map to the type of T
Utilities::Templates::ValueMap<T>* map = (Utilities::Templates::ValueMap<T>*)(mInstance->mDataStorage[key]);
//Return the value at the key location
return map->mValues[pKey];
}
/*
Blackboard : wipeTypeKey - Wipe the value stored at a specific key for the specified type
Author: Mitchell Croft
Created: 09/11/2016
Modified: 09/11/2016
param[in] pKey - A string object containing the key of the value(s) to remove
*/
template<typename T>
inline void Utilities::Blackboard::wipeTypeKey(const std::string& pKey) {
//Ensure that the singleton has been created
assert(mInstance);
//Lock the data
std::lock_guard<std::recursive_mutex> guard(mInstance->mDataLock);
//Ensure the key for this type is supported
size_t key = mInstance->supportType<T>();
//Cast the Value Map to the type of T
Utilities::Templates::ValueMap<T>* map = (Utilities::Templates::ValueMap<T>*)(mInstance->mDataStorage[key]);
//Wipe the key from the value map
map->wipeKey(pKey);
}
/*
Blackboard : subscribe<T> - Set the callback event for a specific key value on a type of data
Author: Mitchell Croft
Created: 08/11/2016
Modified: 08/11/2016
template T - A generic, non void type
param[in] pKey - The key to assign the callback event to
param[in] pCb - A function pointer that takes in a constant string reference as its only parameter
*/
template<typename T>
inline void Utilities::Blackboard::subscribe(const std::string& pKey, EventKeyCallback<T> pCb) {
//Ensure that the singleton has been created
assert(mInstance);
//Lock the data
std::lock_guard<std::recursive_mutex> guard(mInstance->mDataLock);
//Ensure the key for this type is supported
size_t key = mInstance->supportType<T>();
//Cast the Value Map to the type of T
Utilities::Templates::ValueMap<T>* map = (Utilities::Templates::ValueMap<T>*)(mInstance->mDataStorage[key]);
//Set the event callback
map->mKeyEvents[pKey] = pCb;
}
/*
Blackboard : subscribe<T> - Set the callback event for a specific key value on a type of data
Author: Mitchell Croft
Created: 08/11/2016
Modified: 08/11/2016
template T - A generic, non void type
param[in] pKey - The key to assign the callback event to
param[in] pCb - A function pointer that takes in a constant reference to the new value that was assigned
as its only parameters
*/
template<typename T>
inline void Utilities::Blackboard::subscribe(const std::string& pKey, EventValueCallback<T> pCb) {
//Ensure that the singleton has been created
assert(mInstance);
//Lock the data
std::lock_guard<std::recursive_mutex> guard(mInstance->mDataLock);
//Ensure the key for this type is supported
size_t key = mInstance->supportType<T>();
//Cast the Value Map to the type of T
Utilities::Templates::ValueMap<T>* map = (Utilities::Templates::ValueMap<T>*)(mInstance->mDataStorage[key]);
//Set the event callback
map->mValueEvents[pKey] = pCb;
}
/*
Blackboard : subscribe<T> - Set the callback event for a specific key value on a type of data
Author: Mitchell Croft
Created: 08/11/2016
Modified: 08/11/2016
template T - A generic, non void type
param[in] pKey - The key to assign the callback event to
param[in] pCb - A function pointer that takes in a constant string reference and a constant reference to
the new value as its only parameters
*/
template<typename T>
inline void Utilities::Blackboard::subscribe(const std::string& pKey, EventKeyValueCallback<T> pCb) {
//Ensure that the singleton has been created
assert(mInstance);
//Lock the data
std::lock_guard<std::recursive_mutex> guard(mInstance->mDataLock);
//Ensure the key for this type is supported
size_t key = mInstance->supportType<T>();
//Cast the Value Map to the type of T
Utilities::Templates::ValueMap<T>* map = (Utilities::Templates::ValueMap<T>*)(mInstance->mDataStorage[key]);
//Set the event callback
map->mPairEvents[pKey] = pCb;
}
/*
Blackboard : unsubscribe - Unsubscribe all events associated with a key value
for a specific type
Author: Mitchell Croft
Created: 08/11/2016
Modified: 08/11/2016
param[in] pKey - The key to remove the callback events from
*/
template<typename T>
inline void Utilities::Blackboard::unsubscribe(const std::string& pKey) {
//Ensure that the singleton has been created
assert(mInstance);
//Lock the data
std::lock_guard<std::recursive_mutex> guard(mInstance->mDataLock);
//Ensure the key for this type is supported
size_t key = mInstance->supportType<T>();
//Cast the Value Map to the type of T
Utilities::Templates::ValueMap<T>* map = (Utilities::Templates::ValueMap<T>*)(mInstance->mDataStorage[key]);
//Pass the unsubscribe key to the Value Map
map->unsubscribe(pKey);
}
#pragma endregion
#pragma region ValueMap
/*
ValueMap<T> : wipeKey - Clear the value associated with a key value
Author: Mitchell Croft
Created: 08/11/2016
Modified: 08/11/2016
template T - A generic, non void type
param[in] pKey - The key value to clear the entry of
*/
template<typename T>
inline void Utilities::Templates::ValueMap<T>::wipeKey(const std::string& pKey) { mValues.erase(pKey); }
/*
ValueMap<T> : wipeAll - Erase all data stored in the map
Author: Mitchell Croft
Created: 08/11/2016
Modified: 08/11/2016
template T - A generic, non void type
*/
template<typename T>
inline void Utilities::Templates::ValueMap<T>::wipeAll() { mValues.clear(); }
/*
ValueMap<T> : unsubscribe - Remove all callback events associated with a key value
Author: Mitchell Croft
Created: 08/11/2016
Modified: 08/11/2016
template T - A generic, non void type
param[in] pKey - The key to wipe all callback events associated with
*/
template<typename T>
inline void Utilities::Templates::ValueMap<T>::unsubscribe(const std::string& pKey) {
//Find iterators
auto key = mKeyEvents.find(pKey);
auto val = mValueEvents.find(pKey);
auto pair = mPairEvents.find(pKey);
//Remove the callbacks
if (key != mKeyEvents.end()) mKeyEvents.erase(key);
if (val != mValueEvents.end()) mValueEvents.erase(val);
if (pair != mPairEvents.end()) mPairEvents.erase(pair);
}
/*
ValueMap<T> : clearAllEvents - Clear all event callbacks stored within the Value map
Author: Mitchell Croft
Created: 08/11/2016
Modified: 08/11/2016
template T - A generic, non void type
*/
template<typename T>
inline void Utilities::Templates::ValueMap<T>::clearAllEvents() {
//Clear all event maps
mKeyEvents.clear();
mValueEvents.clear();
mPairEvents.clear();
}
#pragma endregion
#pragma endregion
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
///// ////
///// Object Definition ////
///// ////
///// Include a define for _BLACKBOARD_ in a single .cpp file inside of the project to use the Blackboard functionality ////
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
#ifdef _BLACKBOARD_
//! Define the Blackboards static singleton instance
Utilities::Blackboard* Utilities::Blackboard::mInstance = nullptr;
/*
Blackboard : create - Initialise the Blackboard singleton for use
Author: Mitchell Croft
Created: 08/11/2016
Modified: 08/11/2016
return bool - Returns true if the Blackboard was initialised successfully
*/
bool Utilities::Blackboard::create() {
//Check if the Blackboard has already been set up
if (isReady()) destroy();
//Create the instance
mInstance = new Blackboard();
//Return success state
return (mInstance != nullptr);
}
/*
Blackboard : destroy - Deallocate all data associated with the Blackboard and destroy
the singleton instance
Author: Mitchell Croft
Created: 08/11/2016
Modified: 08/11/2016
*/
void Utilities::Blackboard::destroy() {
//Check that there is an instance to destroy
if (mInstance) {
//Lock the data values
mInstance->mDataLock.lock();
//Delete all Value Map values
for (auto pair : mInstance->mDataStorage)
delete pair.second;
//Unlock the data
mInstance->mDataLock.unlock();
//Delete the singleton
delete mInstance;
//Reset the instance pointer
mInstance = nullptr;
}
}
/*
Blackboard : wipeKey - Clear all data associated with the passed in key value
Author: Mitchell Croft
Created: 08/11/2016
Modified: 08/11/2016
param[in] pKey - A string object containing the key of the value(s) to remove
*/
void Utilities::Blackboard::wipeKey(const std::string& pKey) {
//Ensure that the singleton has been created
assert(mInstance);
//Lock the data
std::lock_guard<std::recursive_mutex> guard(mInstance->mDataLock);
//Loop through the different type collections
for (auto pair : mInstance->mDataStorage)
pair.second->wipeKey(pKey);
}
/*
Blackboard : wipeBoard - Clear all data stored on the Blackboard
Author: Mitchell Croft
Created: 08/11/2016
Modified: 08/11/2016
param[in] pWipeCallbacks - Flags if all of the set event callbacks should be cleared
as well as the values (Default false)
*/
void Utilities::Blackboard::wipeBoard(bool pWipeCallbacks) {
//Ensure that the singleton has been created
assert(mInstance);
//Lock the data
std::lock_guard<std::recursive_mutex> guard(mInstance->mDataLock);
//Loop through all stored Value maps
for (auto pair : mInstance->mDataStorage) {
//Clear the data values
pair.second->wipeAll();
//Clear the callbacks
if (pWipeCallbacks) pair.second->clearAllEvents();
}
}
/*
Blackboard : unsubscribeAll - Remove the associated callback events for a key
from every type map
Author: Mitchell Croft
Created: 08/11/2016
Modified: 08/11/2016
param[in] pKey - The key to remove the callback events from
*/
void Utilities::Blackboard::unsubscribeAll(const std::string& pKey) {
//Ensure that the singleton has been created
assert(mInstance);
//Lock the data
std::lock_guard<std::recursive_mutex> guard(mInstance->mDataLock);
//Loop through all stored Value maps
for (auto pair : mInstance->mDataStorage)
pair.second->unsubscribe(pKey);
}
#endif //_BLACKBOARD_