A C++ container that can store any value of any type referenced by a unique ID.
Usually, Blackboards are used in games in combination of a behavior tree to store and retrieve easily data or computation results.
But they can also be used as a usefull container that can store anything.
Report a bug or request a feature
Clone the repo (git clone https:://github.com/torresflo/Blackboard.git
) and add the Container folder to your project, you are ready to go!
You can have a look at the example below.
#include <Container\Blackboard.hpp>
//Declaration of the blackboard and the managed types
Container::Blackboard<int, bool, std::string> blackboard;
//Declaration of some IDs to use
blackboard.declare<int>(BLACKBOARD_ID("IDInteger"), 0);
blackboard.declare<bool>(BLACKBOARD_ID("IDBool"), false);
blackboard.declare<std::string>(BLACKBOARD_ID("IDString"), "My String");
//You can register a callback to be informed when a value has changed
blackboard.declare<int>(BLACKBOARD_ID("IDIntegerWithCallback"), [](const int& currentValue, const int& newValue) { /* ... */ });
//Let's use the container
blackboard.setValue<int>(BLACKBOARD_ID("IDInteger"), 42);
int myInt = blackboard.getValue<int>(BLACKBOARD_ID("IDInteger"));
// Warning: you cannot access non existing IDs
bool myBoolean = blackboard.getValue<bool>(BLACKBOARD_ID("AnInvalidID")); //This will create an out_of_range exception.
//Better to do this if the id is unknown
if(blackboard.exist<bool>(BLACKBOARD_ID("AnUnknonwID")))
{
bool myBoolean = blackboard.getValue<bool>(BLACKBOARD_ID("AnUnknonwID")); //Can access safely
//...
}
//Or to do this
bool myBoolean = blackboard.getValue<bool>(BLACKBOARD_ID("AnUnknonwID"), false); //Will return the default value provided: false
//...
The generation of the Blackboard IDs are done with a hashing function that SHOULD be computed at compile time if the used string is constant.
Sadly, some compilers may not optimize it. In this case, a couple of bit operations are done for each letter of the string. It means, the longer is the string, the longer it will be to generate the Blackboard ID.
Internally, the Blackboard is using std::unordered_map
(you cannot access this container directly).
It means the complexity in both reading and writing is:
- Average case: constant
- Worst case: linear in size.
For information about performance and complexity of the std::unordered_map
, see here.
Contributions are what make the open source community such an amazing place to be learn, inspire, and create. Any contributions you make are greatly appreciated.
- Fork the Project
- Create your Feature Branch (
git checkout -b feature/AmazingFeature
) - Commit your Changes (
git commit -m 'Add some AmazingFeature
) - Push to the Branch (
git push origin feature/AmazingFeature
) - Open a Pull Request
Distributed under the GNU General Public License v3.0. See LICENSE
for more information.