mirror of
https://github.com/Nimac0/SDL_Minigame
synced 2026-01-12 09:03:42 +00:00
38 lines
1.3 KiB
C++
38 lines
1.3 KiB
C++
#pragma once
|
|
|
|
#include <map>
|
|
#include <any>
|
|
#include <string>
|
|
#include <optional>
|
|
#include "Component.h"
|
|
|
|
class DataComponent : public Component
|
|
{
|
|
public:
|
|
DataComponent() {};
|
|
~DataComponent() {};
|
|
/**
|
|
* @brief Set a key-value pair of any type in the data map
|
|
* @details e.g. setEntry("speed", 180); in this case the key is "speed" and the value is set to an integer of 180
|
|
* @param key The name to store the value under
|
|
* @param value The value to store of type T
|
|
*/
|
|
template <typename T>
|
|
void setEntry(const std::string& key, const T& value) { dataMap.insert_or_assign(key, value); }
|
|
|
|
/**
|
|
* @brief Get a value of type T from the data map
|
|
* @details e.g. getEntry<int>("speed"); in this case the key is "speed" and the value is returned as an integer
|
|
* @param key The name to retrieve the value from
|
|
* @return An optional of type T containing the value if it exists and matches in typeid, otherwise std::nullopt
|
|
*/
|
|
template<typename T>
|
|
std::optional<T> getEntry(std::string key) const {
|
|
if (!this->dataMap.contains(key)) return std::nullopt;
|
|
const std::any& value = this->dataMap.at(key);
|
|
if (value.type() != typeid(T)) { return std::nullopt; }
|
|
return std::any_cast<T>(value);
|
|
}
|
|
private:
|
|
std::map<std::string, std::any> dataMap;
|
|
}; |