Infinity Engine v0.6.20
C++ API Documentation
Loading...
Searching...
No Matches
Map.hpp
1#pragma once
2
3#include <Infinity/Types/Core/Data.hpp>
4#include <Infinity/Types/Core/Value.hpp>
5#include <Infinity/Types/Containers/Array.hpp>
6
7#include <unordered_map>
8#include <optional>
9#include <memory>
10
11namespace Infinity::IO::Data
12{
13 class MapSerializer;
14}
15
17{
91 class INFINITY_API_PUBLIC Map : public Core::Data
92 {
93 public:
99 Map();
100
111 Map(const Map& copy);
112
120 Map(Map&& move);
121
128 virtual ~Map();
129
130 std::unique_ptr<Core::Base> clone() const override;
131
137 virtual const Infinity::Types::TypeID& typeId() const override;
138
147 Map& operator=(const Map& copy);
148
157 Map& operator=(Map&& move);
158
171 bool hasKey(const std::string& key) const;
172
184 bool hasValueAtKey(const std::string& key) const;
185
201 void set(const std::string& key, std::shared_ptr<Core::Data> data);
202
221 template<typename T>
222 void set(const std::string& key, const T& data)
223 {
224 if constexpr (std::is_base_of_v<Core::Data, T>)
225 {
226 _map[key] = std::make_shared<T>(data);
227 } else {
228 _map[key] = std::make_shared<Core::Value<T>>(data);
229 }
230 }
231
245 template<typename T>
246 void set(const std::string& key, T& data)
247 {
248 if constexpr (std::is_base_of_v<Core::Data, T>)
249 {
250 _map[key] = std::make_shared<T>(data);
251 } else {
252 _map[key] = std::make_shared<Core::Value<T>>(data);
253 }
254 }
255
272 template<typename T>
273 void set(const std::string& key, T&& data)
274 {
275 if constexpr (std::is_base_of_v<Core::Data, T>)
276 {
277 _map[key] = std::make_shared<T>(std::move(data));
278 } else {
279 size_t size = sizeof(uint8_t) + sizeof(Infinity::Types::getTypeID<T>().hash) + sizeof(T);
280 _map[key] = std::make_shared<Core::Value<T>>(std::move(data));
281 }
282 }
283
300 const Core::Data* get(const std::string& key) const;
301
327 template<typename T>
328 T& get(const std::string& key)
329 {
330 if constexpr (std::is_base_of_v<Core::Data, T>)
331 {
332 T& a = *dynamic_cast<T*>(_map.at(key).get());
333 return a;
334 } else {
335 auto val = dynamic_cast<Core::Value<T>*>(_map.at(key).get());
336 return val->get();
337 }
338 }
339
360 template<typename T>
361 const T& get(const std::string& key) const
362 {
363 if constexpr (std::is_base_of_v<Core::Data, T>)
364 {
365 const T& a = *dynamic_cast<const T*>(_map.at(key).get());
366 return a;
367 } else {
368 auto val = dynamic_cast<const Core::Value<T>*>(_map.at(key).get());
369 return val->get();
370 }
371 }
372
392 template<typename T>
393 T& getValue(const std::string& key)
394 {
395 return get<Core::Value<T>>(key).get();
396 }
397
412 template<typename T>
413 const T& getValue(const std::string& key) const
414 {
415 return get<const Core::Value<T>>(key).get();
416 }
417
424 void clear();
425
431 size_t size() const noexcept;
432
433 private:
434 std::unordered_map<std::string, std::shared_ptr<Core::Data>> _map;
435
436 friend class Infinity::IO::Data::MapSerializer;
437 };
438}
439
440INFINITY_TYPE(Infinity::Types::Containers::Map)
441INFINITY_TYPE(Infinity::Types::Containers::Array<Infinity::Types::Containers::Map>)
Dynamic contiguous container for homogeneous elements in the Infinity type system.
Definition Array.hpp:77
Mutable heterogeneous key-value container for dynamic data structures.
Definition Map.hpp:92
Map(Map &&move)
Move constructor.
void set(const std::string &key, std::shared_ptr< Core::Data > data)
Sets a key-value pair from a type-erased Data pointer.
void set(const std::string &key, T &data)
Sets a key-value pair with automatic type wrapping (copy, non-const reference).
Definition Map.hpp:246
Map()
Default constructor.
std::unique_ptr< Core::Base > clone() const override
size_t size() const noexcept
Gets the number of key-value pairs in the map.
bool hasValueAtKey(const std::string &key) const
Checks if a key exists and contains a plain value (POD type).
virtual const Infinity::Types::TypeID & typeId() const override
Gets the TypeID for Map.
void set(const std::string &key, T &&data)
Sets a key-value pair with automatic type wrapping (move).
Definition Map.hpp:273
void set(const std::string &key, const T &data)
Sets a key-value pair with automatic type wrapping (copy, const reference).
Definition Map.hpp:222
Map & operator=(const Map &copy)
Copy assignment operator.
bool hasKey(const std::string &key) const
Checks if a key exists in the map.
T & get(const std::string &key)
Gets a mutable typed reference to the value at the specified key.
Definition Map.hpp:328
void clear()
Removes all key-value pairs from the map.
Map(const Map &copy)
Copy constructor.
const T & get(const std::string &key) const
Gets a const typed reference to the value at the specified key.
Definition Map.hpp:361
const Core::Data * get(const std::string &key) const
Gets a type-erased const pointer to the value at the specified key.
Map & operator=(Map &&move)
Move assignment operator.
const T & getValue(const std::string &key) const
Gets a const reference to the underlying value from a Value<T> wrapper.
Definition Map.hpp:413
T & getValue(const std::string &key)
Gets a mutable reference to the underlying value from a Value<T> wrapper.
Definition Map.hpp:393
Base class for complex data types with memory wrapping and property support.
Definition Data.hpp:49
Template wrapper for primitive types to integrate with the Infinity type system.
Definition Value.hpp:89
T & get()
Gets a mutable reference to the underlying value.
Definition Value.inl:60
Definition Array.hpp:17
Definition Array.hpp:25
Definition Asset.hpp:11
Definition Span.hpp:599
Runtime type identifier for the Infinity type system.
Definition TypeID.hpp:71