Infinity Engine v0.6.20
C++ API Documentation
Loading...
Searching...
No Matches
Infinity::Types::Containers::Map Class Reference

Mutable heterogeneous key-value container for dynamic data structures. More...

#include <Map.hpp>

Inheritance diagram for Infinity::Types::Containers::Map:
[legend]
Collaboration diagram for Infinity::Types::Containers::Map:
[legend]

Public Member Functions

 Map ()
 Default constructor.
 
 Map (const Map &copy)
 Copy constructor.
 
 Map (Map &&move)
 Move constructor.
 
virtual ~Map ()
 Destructor.
 
std::unique_ptr< Core::Baseclone () const override
 
virtual const Infinity::Types::TypeIDtypeId () const override
 Gets the TypeID for Map.
 
Mapoperator= (const Map &copy)
 Copy assignment operator.
 
Mapoperator= (Map &&move)
 Move assignment operator.
 
bool hasKey (const std::string &key) const
 Checks if a key exists in the map.
 
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, std::shared_ptr< Core::Data > data)
 Sets a key-value pair from a type-erased Data pointer.
 
template<typename T >
void set (const std::string &key, const T &data)
 Sets a key-value pair with automatic type wrapping (copy, const reference).
 
template<typename T >
void set (const std::string &key, T &data)
 Sets a key-value pair with automatic type wrapping (copy, non-const reference).
 
template<typename T >
void set (const std::string &key, T &&data)
 Sets a key-value pair with automatic type wrapping (move).
 
const Core::Dataget (const std::string &key) const
 Gets a type-erased const pointer to the value at the specified key.
 
template<typename T >
T & get (const std::string &key)
 Gets a mutable typed reference to the value at the specified key.
 
template<typename T >
const T & get (const std::string &key) const
 Gets a const typed reference to the value at the specified key.
 
template<typename T >
T & getValue (const std::string &key)
 Gets a mutable reference to the underlying value from a Value<T> wrapper.
 
template<typename T >
const T & getValue (const std::string &key) const
 Gets a const reference to the underlying value from a Value<T> wrapper.
 
void clear ()
 Removes all key-value pairs from the map.
 
size_t size () const noexcept
 Gets the number of key-value pairs in the map.
 
- Public Member Functions inherited from Infinity::Types::Core::Data
 Data ()
 
 Data (const Data &other)
 
 Data (Data &&other)
 
Dataoperator= (const Data &other)
 
Dataoperator= (Data &&other)
 
virtual ~Data ()
 Virtual destructor.
 
bool hasProperty (const std::string &key) const
 
template<typename T >
void setProperty (const std::string &key, T &&value)
 Sets a typed property value.
 
template<typename T >
void setProperty (const std::string &key, const T &value)
 Sets a typed property value.
 
void setProperty (const std::string &key, PropertyValue &&value)
 Sets a typed property value.
 
const PropertyValuegetProperty (const std::string &key) const
 Gets a property value without type checking.
 
template<typename T >
const T & getProperty (const std::string &key) const
 Gets a typed property value.
 
void removeProperty (const std::string &key)
 
- Public Member Functions inherited from Infinity::Types::Core::Base
virtual ~Base ()
 Virtual destructor.
 
virtual std::istream & legibleDataRead (std::istream &in) override
 Deserializes the object from a single-line text representation.
 
virtual std::ostream & legibleDataWrite (std::ostream &out) const override
 Serializes the object to a single-line text representation.
 

Friends

class Infinity::IO::Data::MapSerializer
 

Additional Inherited Members

- Public Attributes inherited from Infinity::Types::Core::Data
std::unordered_map< std::string, PropertyValueproperties
 Property storage for arbitrary metadata.
 

Detailed Description

Mutable heterogeneous key-value container for dynamic data structures.

Map provides a dictionary-like container that stores string-keyed values of heterogeneous types with full mutability. Unlike ImmutableMap, Map stores shared_ptr<Data> (non-const), allowing stored values to be modified after insertion. This makes it suitable for:

  • Dynamic runtime state that changes during execution
  • Mutable component properties and settings
  • Cache and temporary storage during procedural generation
  • Accumulator patterns for iterative algorithms
  • Shared state between multiple components

The map automatically handles type wrapping, storing POD types as Value<T> and Data-derived types directly. It provides both const and non-const accessors, enabling full read-write access to stored values.

Key features:

  • Heterogeneous storage with different value types in the same map
  • Type-safe retrieval with automatic unwrapping
  • Mutable access to stored values (unlike ImmutableMap)
  • Automatic Value<T> wrapping for POD types
  • Full serialization support (read/write/wrap)
  • Integration with ProceduralSystem input/output ports
  • Move semantics for efficient large object storage
  • Shared ownership of stored data

The key difference from ImmutableMap is mutability: stored values can be retrieved as non-const references and modified in place, making Map ideal for dynamic runtime state rather than static configuration.

Note
Keys are case-sensitive strings.
Attempting to retrieve a non-existent key throws std::out_of_range.
Use hasKey() to safely check for key existence before retrieval.
Multiple maps can share references to the same data objects.

Example usage:

// Create and populate a mutable map
Map state;
state.set("iteration", 0);
state.set("total_volume", 0.0f);
state.set("active", true);
// Modify values in place during execution
int& iter = state.get<int>("iteration");
iter++; // Mutable access
float& volume = state.get<float>("total_volume");
volume += calculateVolume();
// Store and modify complex types
state.set("control_points", std::move(points));
// Later, modify the stored array
Array<Vector3>& pts = state.get<Array<Vector3>>("control_points");
pts.push_back(Vector3{1.0f, 2.0f, 3.0f});
// Use with ProceduralSystem for runtime state
system->setIn("runtime_state", std::move(state));
// Iterative accumulation pattern
Map accumulator;
accumulator.set("sum", 0.0f);
accumulator.set("count", 0);
for (const auto& value : values) {
accumulator.get<float>("sum") += value;
accumulator.get<int>("count")++;
}
Dynamic contiguous container for homogeneous elements in the Infinity type system.
Definition Array.hpp:77
void push_back(const T &value)
Appends an element to the end of the array.
Definition Array.hpp:333
Mutable heterogeneous key-value container for dynamic data structures.
Definition Map.hpp:92
void set(const std::string &key, std::shared_ptr< Core::Data > data)
Sets a key-value pair from a type-erased Data pointer.
const Core::Data * get(const std::string &key) const
Gets a type-erased const pointer to the value at the specified key.

Constructor & Destructor Documentation

◆ Map() [1/3]

Infinity::Types::Containers::Map::Map ( )

Default constructor.

Constructs an empty map with no key-value pairs.

◆ Map() [2/3]

Infinity::Types::Containers::Map::Map ( const Map copy)

Copy constructor.

Creates a shallow copy of the map. The underlying shared pointers are copied, so both maps reference the same data objects.

Parameters
copyMap to copy from.
Note
This is a shallow copy - modifications to data objects affect all maps sharing them.

◆ Map() [3/3]

Infinity::Types::Containers::Map::Map ( Map &&  move)

Move constructor.

Transfers ownership of the map's contents without copying.

Parameters
moveMap to move from.

◆ ~Map()

virtual Infinity::Types::Containers::Map::~Map ( )
virtual

Destructor.

Releases all stored shared pointers. Data objects are destroyed when their reference count reaches zero.

Member Function Documentation

◆ clear()

void Infinity::Types::Containers::Map::clear ( )

Removes all key-value pairs from the map.

Clears the map, releasing all stored shared pointers. Data objects are destroyed if no other references exist.

◆ clone()

std::unique_ptr< Core::Base > Infinity::Types::Containers::Map::clone ( ) const
overridevirtual

Reimplemented from Infinity::Types::Core::Data.

◆ get() [1/3]

template<typename T >
T & Infinity::Types::Containers::Map::get ( const std::string &  key)
inline

Gets a mutable typed reference to the value at the specified key.

Retrieves and unwraps the value with automatic type checking, providing mutable access for modification:

  • For Data-derived types: returns the object directly
  • For POD types: unwraps from Value<T> and returns the underlying value
Template Parameters
TExpected type of the value.
Parameters
keyKey to retrieve.
Returns
Mutable reference to the value.
Exceptions
std::out_of_rangeIf key does not exist.
std::bad_castIf the value cannot be cast to type T.
int& count = map.get<int>("iteration_count");
count++; // Modify in place
Vector3& pos = map.get<Vector3>("position");
pos.x += 10.0f; // Modify component directly
Array<float>& buffer = map.get<Array<float>>("data_buffer");
buffer.push_back(3.14f); // Modify array

◆ get() [2/3]

const Core::Data * Infinity::Types::Containers::Map::get ( const std::string &  key) const

Gets a type-erased const pointer to the value at the specified key.

Retrieves the raw Data pointer without type-specific unwrapping. Useful when the specific type is unknown or when working generically.

Parameters
keyKey to retrieve.
Returns
Const pointer to the Data object.
Exceptions
std::out_of_rangeIf key does not exist.
const Core::Data* data = map.get("some_key");
TypeID type = data->typeId();
// Inspect or cast based on type...
Base class for complex data types with memory wrapping and property support.
Definition Data.hpp:49
Runtime type identifier for the Infinity type system.
Definition TypeID.hpp:71

◆ get() [3/3]

template<typename T >
const T & Infinity::Types::Containers::Map::get ( const std::string &  key) const
inline

Gets a const typed reference to the value at the specified key.

Retrieves and unwraps the value with automatic type checking for read-only access:

  • For Data-derived types: returns the object directly
  • For POD types: unwraps from Value<T> and returns the underlying value
Template Parameters
TExpected type of the value.
Parameters
keyKey to retrieve.
Returns
Const reference to the value.
Exceptions
std::out_of_rangeIf key does not exist.
std::bad_castIf the value cannot be cast to type T.
const int& count = map.get<int>("iteration_count");
const Vector3& pos = map.get<Vector3>("position");
const Array<float>& buffer = map.get<Array<float>>("data_buffer");

◆ getValue() [1/2]

template<typename T >
T & Infinity::Types::Containers::Map::getValue ( const std::string &  key)
inline

Gets a mutable reference to the underlying value from a Value<T> wrapper.

Specialized getter that explicitly unwraps a Value<T> to access the POD value inside with mutable access.

Template Parameters
TType of the wrapped value.
Parameters
keyKey to retrieve.
Returns
Mutable reference to the unwrapped value.
Exceptions
std::out_of_rangeIf key does not exist.
std::bad_castIf the value is not a Value<T>.
Note
Prefer get<T>() for most use cases, which handles unwrapping automatically.
float& scale = map.getValue<float>("scale");
scale *= 2.0f; // Modify directly

◆ getValue() [2/2]

template<typename T >
const T & Infinity::Types::Containers::Map::getValue ( const std::string &  key) const
inline

Gets a const reference to the underlying value from a Value<T> wrapper.

Specialized getter that explicitly unwraps a Value<T> to access the POD value inside with read-only access.

Template Parameters
TType of the wrapped value.
Parameters
keyKey to retrieve.
Returns
Const reference to the unwrapped value.
Exceptions
std::out_of_rangeIf key does not exist.
std::bad_castIf the value is not a Value<T>.
Note
Prefer get<T>() for most use cases, which handles unwrapping automatically.

◆ hasKey()

bool Infinity::Types::Containers::Map::hasKey ( const std::string &  key) const

Checks if a key exists in the map.

Parameters
keyKey to check for existence.
Returns
True if the key exists, false otherwise.
if (map.hasKey("cache_data")) {
auto& data = map.get<CacheType>("cache_data");
}

◆ hasValueAtKey()

bool Infinity::Types::Containers::Map::hasValueAtKey ( const std::string &  key) const

Checks if a key exists and contains a plain value (POD type).

Returns true if the key exists and its value is a Value<T> wrapper (indicating a POD type like int, float, etc.).

Parameters
keyKey to check.
Returns
True if key exists and contains a Value<T>, false otherwise.
Note
This is distinct from hasKey() - it also checks the value type.

◆ operator=() [1/2]

Map & Infinity::Types::Containers::Map::operator= ( const Map copy)

Copy assignment operator.

Replaces the map's contents with a shallow copy of another map.

Parameters
copyMap to copy from.
Returns
Reference to this map.

◆ operator=() [2/2]

Map & Infinity::Types::Containers::Map::operator= ( Map &&  move)

Move assignment operator.

Replaces the map's contents by moving from another map.

Parameters
moveMap to move from.
Returns
Reference to this map.

◆ set() [1/4]

template<typename T >
void Infinity::Types::Containers::Map::set ( const std::string &  key,
const T &  data 
)
inline

Sets a key-value pair with automatic type wrapping (copy, const reference).

Automatically wraps the value appropriately:

  • For Data-derived types: stores directly
  • For POD types: wraps in Value<T>
Template Parameters
TType of the value to store.
Parameters
keyKey to associate with the value.
dataValue to store (copied).
map.set("iteration", 0); // Stored as Value<int>
map.set("scale", 1.5f); // Stored as Value<float>
Vector3 pos{1, 2, 3};
map.set("position", pos); // Stored as Vector3 (Data type)

◆ set() [2/4]

void Infinity::Types::Containers::Map::set ( const std::string &  key,
std::shared_ptr< Core::Data data 
)

Sets a key-value pair from a type-erased Data pointer.

Stores the data directly without additional wrapping. Useful for storing pre-wrapped Data objects or when the type is already erased.

Parameters
keyKey to associate with the data.
dataShared pointer to Data to store (mutable).
auto data = std::make_shared<Vector3>(1.0f, 2.0f, 3.0f);
map.set("position", data);
// Can later modify through the shared pointer

◆ set() [3/4]

template<typename T >
void Infinity::Types::Containers::Map::set ( const std::string &  key,
T &&  data 
)
inline

Sets a key-value pair with automatic type wrapping (move).

Automatically wraps the value appropriately using move semantics:

  • For Data-derived types: moves directly
  • For POD types: wraps in Value<T> with move
Template Parameters
TType of the value to store.
Parameters
keyKey to associate with the value.
dataValue to store (moved).
map.set("mesh", std::move(largeMesh)); // Efficient for large objects
map.set("buffer", std::move(dataBuffer)); // Moves ownership

◆ set() [4/4]

template<typename T >
void Infinity::Types::Containers::Map::set ( const std::string &  key,
T &  data 
)
inline

Sets a key-value pair with automatic type wrapping (copy, non-const reference).

Automatically wraps the value appropriately:

  • For Data-derived types: stores directly
  • For POD types: wraps in Value<T>
Template Parameters
TType of the value to store.
Parameters
keyKey to associate with the value.
dataValue to store (copied).
Note
This overload exists to handle non-const lvalue references.

◆ size()

size_t Infinity::Types::Containers::Map::size ( ) const
noexcept

Gets the number of key-value pairs in the map.

Returns
Number of entries in the map.

◆ typeId()

virtual const Infinity::Types::TypeID & Infinity::Types::Containers::Map::typeId ( ) const
overridevirtual

Gets the TypeID for Map.

Returns
TypeID identifying this Map type.

Reimplemented from Infinity::Types::Core::Data.

Friends And Related Symbol Documentation

◆ Infinity::IO::Data::MapSerializer

friend class Infinity::IO::Data::MapSerializer
friend