Created
November 29, 2017 17:12
-
-
Save moccy/286d4ca5ed29ec39814438dd18ff4aea to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "Blackboard.h" | |
Blackboard::Blackboard(const Blackboard * copy) | |
{ | |
*this = *copy; | |
} | |
Blackboard& Blackboard::operator=(const Blackboard& copy) { | |
if (© == this) return *this; | |
CopyMap<bool>(copy.bools_, bools_); | |
CopyMap<int>(copy.ints_, ints_); | |
CopyMap<float>(copy.floats_, floats_); | |
CopyMap<double>(copy.doubles_, doubles_); | |
CopyMap<std::string>(copy.strings_, strings_); | |
return *this; | |
} | |
bool Blackboard::operator==(const Blackboard& param) const { | |
return !(*this != param); | |
} | |
bool Blackboard::operator!=(const Blackboard& param) const { | |
return this->bools_ != param.bools_ && | |
this->ints_ != param.ints_ && | |
this->floats_ != param.floats_ && | |
this->doubles_ != param.doubles_ && | |
this->strings_ != param.strings_; | |
} | |
void Blackboard::SetBool(std::string key, bool value) | |
{ | |
bools_.insert_or_assign(key, value); | |
} | |
bool Blackboard::GetBool(std::string key) { | |
return Get<bool>(bools_, key); | |
} | |
void Blackboard::SetInt(std::string key, int value) { | |
ints_[key] = value; | |
} | |
int Blackboard::GetInt(std::string key) { | |
return Get<int>(ints_, key); | |
} | |
void Blackboard::SetFloat(std::string key, float value) { | |
floats_[key] = value; | |
} | |
float Blackboard::GetFloat(std::string key) { | |
return Get<float>(floats_, key); | |
} | |
void Blackboard::SetDouble(std::string key, double value) { | |
doubles_[key] = value; | |
} | |
double Blackboard::GetDouble(std::string key) { | |
return Get<double>(doubles_, key); | |
} | |
void Blackboard::SetString(std::string key, std::string value) { | |
strings_[key] = value; | |
} | |
std::string Blackboard::GetString(std::string key) { | |
return Get<std::string>(strings_, key); | |
} | |
void Blackboard::PrintMaps() { | |
std::cout << "==== Bools ====" << std::endl; | |
for (auto pair : bools_) { | |
std::cout << "Key: " << pair.first.c_str() << " | Value: " << std::boolalpha << pair.second << std::endl; | |
} | |
std::cout << std::endl; | |
std::cout << "==== Ints ====" << std::endl; | |
for (auto pair : ints_) { | |
std::cout << "Key: " << pair.first.c_str() << " | Value: " << pair.second << std::endl; | |
} | |
std::cout << std::endl; | |
std::cout << "==== Floats ====" << std::endl; | |
for (auto pair : floats_) { | |
std::cout << "Key: " << pair.first.c_str() << " | Value: " << pair.second << std::endl; | |
} | |
std::cout << std::endl; | |
std::cout << "==== Doubles ====" << std::endl; | |
for (auto pair : doubles_) { | |
std::cout << "Key: " << pair.first.c_str() << " | Value: " << pair.second << std::endl; | |
} | |
std::cout << std::endl; | |
std::cout << "==== Strings ====" << std::endl; | |
for (auto pair : strings_) { | |
std::cout << "Key: " << pair.first.c_str() << " | Value: " << pair.second.c_str() << std::endl; | |
} | |
std::cout << std::endl; | |
} | |
template <typename T> | |
T Blackboard::Get(const std::unordered_map<std::string, T>& map, std::string key) { | |
auto search = map.find(key); | |
if (search != map.end()) | |
{ | |
return search->second; | |
} | |
std::cerr << "Failed to find value with key: " << key.c_str() << std::endl; | |
return T(); | |
} | |
template <typename T> | |
void Blackboard::CopyMap(const std::unordered_map<std::string, T>& from, | |
std::unordered_map<std::string, T>& to) const { | |
if (from.size() != to.size()) { | |
to.clear(); | |
to.reserve(from.bucket_count()); | |
} | |
to.insert(from.begin(), from.end()); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#pragma once | |
#include <unordered_map> | |
#include <iostream> | |
class Blackboard { | |
public: | |
Blackboard() {}; | |
~Blackboard() {}; | |
explicit Blackboard(const Blackboard *copy); | |
Blackboard &operator= (const Blackboard ©); | |
bool operator== (const Blackboard ¶m) const; | |
bool operator!= (const Blackboard ¶m) const; | |
void SetBool(std::string key, bool value); | |
bool GetBool(std::string key); | |
void SetInt(std::string key, int value); | |
int GetInt(std::string key); | |
void SetFloat(std::string key, float value); | |
float GetFloat(std::string key); | |
void SetDouble(std::string key, double value); | |
double GetDouble(std::string key); | |
void SetString(std::string key, std::string value); | |
std::string GetString(std::string key); | |
void PrintMaps(); | |
private: | |
std::unordered_map<std::string, bool> bools_; | |
std::unordered_map<std::string, int> ints_; | |
std::unordered_map<std::string, float> floats_; | |
std::unordered_map<std::string, double> doubles_; | |
std::unordered_map<std::string, std::string> strings_; | |
template<typename T> | |
T Get(const std::unordered_map<std::string, T>& map, std::string key); | |
template<typename T> | |
void CopyMap(const std::unordered_map<std::string, T>& from, | |
std::unordered_map<std::string, T>& to) const; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment