Infinity Engine v0.6.20
C++ API Documentation
Loading...
Searching...
No Matches
ImmutableMap.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 ImmutableMapSerializer;
14}
15
17{
84 class INFINITY_API_PUBLIC ImmutableMap : public Core::Data
85 {
86 public:
93
105
114
121 virtual ~ImmutableMap();
122
123 std::unique_ptr<Core::Base> clone() const override;
124
130 virtual const Infinity::Types::TypeID& typeId() const override;
131
141
151
165 bool hasKey(const std::string& key) const;
166
178 bool hasValueAtKey(const std::string& key) const;
179
194 void set(const std::string& key, std::shared_ptr<const Core::Data> data);
195
214 template<typename T>
215 void set(const std::string& key, const T& data)
216 {
217 if constexpr (std::is_base_of_v<Core::Data, T>)
218 {
219 _map[key] = std::make_shared<T>(data);
220 } else {
221 _map[key] = std::make_shared<Core::Value<T>>(data);
222 }
223 }
224
241 template<typename T>
242 void set(const std::string& key, T&& data)
243 {
244 if constexpr (std::is_base_of_v<Core::Data, T>)
245 {
246 _map[key] = std::make_shared<T>(std::move(data));
247 } else {
248 size_t size = sizeof(uint8_t) + sizeof(Infinity::Types::getTypeID<T>().hash) + sizeof(T);
249 _map[key] = std::make_shared<Core::Value<T>>(std::move(data));
250 }
251 }
252
269 const Core::Data* get(const std::string& key) const;
270
290 template<typename T>
291 const T& get(const std::string& key) const
292 {
293 if constexpr (std::is_base_of_v<Core::Data, T>)
294 {
295 const T& a = *dynamic_cast<const T*>(_map.at(key).get());
296 return a;
297 } else {
298 auto val = dynamic_cast<const Core::Value<T>*>(_map.at(key).get());
299 return val->get();
300 }
301 }
302
323 template<typename T>
324 const T& getValue(const std::string& key) const
325 {
326 return get<const Core::Value<T>>(key).get();
327 }
328
335 void clear();
336
342 size_t size() const noexcept;
343
344 private:
345 std::unordered_map<std::string, std::shared_ptr<const Core::Data>> _map;
346
347 friend class Infinity::IO::Data::ImmutableMapSerializer;
348 };
349}
350
351INFINITY_TYPE(Infinity::Types::Containers::ImmutableMap)
352INFINITY_TYPE(Infinity::Types::Containers::Array<Infinity::Types::Containers::ImmutableMap>)
Dynamic contiguous container for homogeneous elements in the Infinity type system.
Definition Array.hpp:77
Type-safe heterogeneous key-value container for configuration and metadata.
Definition ImmutableMap.hpp:85
const Core::Data * get(const std::string &key) const
Gets a type-erased pointer to the value at the specified key.
void clear()
Removes all key-value pairs from the map.
void set(const std::string &key, std::shared_ptr< const Core::Data > data)
Sets a key-value pair from a type-erased Data pointer.
bool hasKey(const std::string &key) const
Checks if a key exists in the map.
virtual const Infinity::Types::TypeID & typeId() const override
Gets the TypeID for ImmutableMap.
void set(const std::string &key, const T &data)
Sets a key-value pair with automatic type wrapping (copy).
Definition ImmutableMap.hpp:215
const T & getValue(const std::string &key) const
Gets the underlying value from a Value<T> wrapper.
Definition ImmutableMap.hpp:324
ImmutableMap & operator=(ImmutableMap &&move)
Move assignment operator.
bool hasValueAtKey(const std::string &key) const
Checks if a key exists and contains a plain value (POD type).
void set(const std::string &key, T &&data)
Sets a key-value pair with automatic type wrapping (move).
Definition ImmutableMap.hpp:242
ImmutableMap(const ImmutableMap &copy)
Copy constructor.
ImmutableMap & operator=(const ImmutableMap &copy)
Copy assignment operator.
ImmutableMap(ImmutableMap &&move)
Move constructor.
const T & get(const std::string &key) const
Gets a typed reference to the value at the specified key.
Definition ImmutableMap.hpp:291
size_t size() const noexcept
Gets the number of key-value pairs in the map.
std::unique_ptr< Core::Base > clone() const override
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