VEGO-Engine  0.1
Loading...
Searching...
No Matches
DataComponent.h
1#pragma once
2
3#include <map>
4#include <any>
5#include <string>
6#include <optional>
7#include "Component.h"
8
10class DataComponent : public Component
11{
12public:
13 DataComponent() {};
14 ~DataComponent() {};
21 template <typename T>
22 void setEntry(const std::string& key, const T& value) { dataMap.insert_or_assign(key, value); }
23
30 template<typename T>
31 std::optional<T> getEntry(std::string key) const {
32 if (!this->dataMap.contains(key)) return std::nullopt;
33 const std::any& value = this->dataMap.at(key);
34 if (value.type() != typeid(T)) { return std::nullopt; }
35 return std::any_cast<T>(value);
36 }
37private:
38 std::map<std::string, std::any> dataMap;
39};
void setEntry(const std::string &key, const T &value)
Set a key-value pair of any type in the data map.
Definition DataComponent.h:22
std::optional< T > getEntry(std::string key) const
Get a value of type T from the data map.
Definition DataComponent.h:31