#pragma once #include #include #include #include #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 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("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 std::optional 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(value); } private: std::map dataMap; };