Skip to content

A C++ container that can store any value of any type referenced by a unique ID.

License

Notifications You must be signed in to change notification settings

torresflo/Blackboard

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

6 Commits
 
 
 
 
 
 
 
 

Repository files navigation

GitHub license PRs Welcome GitHub contributors GitHub issues

Blackboard

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

Table of Contents

Getting Started

Installation

Clone the repo (git clone https:://github.com/torresflo/Blackboard.git) and add the Container folder to your project, you are ready to go!

Usage

You can have a look at the example below.

Example

#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

//...

Performance

Blackboard ID

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.

Storing

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.

Contributing

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.

  1. Fork the Project
  2. Create your Feature Branch (git checkout -b feature/AmazingFeature)
  3. Commit your Changes (git commit -m 'Add some AmazingFeature)
  4. Push to the Branch (git push origin feature/AmazingFeature)
  5. Open a Pull Request

License

Distributed under the GNU General Public License v3.0. See LICENSE for more information.

About

A C++ container that can store any value of any type referenced by a unique ID.

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages