#pragma once #include #include #include #include #include "Component.h" //! \brief DataComponent class to centrally store data about an entity such as stats class DataComponent : public Component { public: //! \brief The data component only has a default constructor DataComponent() {}; ~DataComponent() {}; /** * @brief Set a key-value pair of any type in the data map * @details e.g. \code{.cpp}setEntry("speed", 180);\endcode 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. \code{.cpp}getEntry("speed").value();\endcode in this case the key is "speed" and the value is returned as an integer * @details the value() or value_or() is NEEDED to handle the optional return type * @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; };