Memory

High-level memory management utilities.

Overview

Typical use cases:

  • Managing memory allocation and deallocation.

  • Providing resource management functionalities.

Header

<RaeptorCogs/Memory.hpp>

Metadata

Author

Estorc

Version

v1.0

Copyright

Copyright (c) 2025 Estorc MIT License.

Structs

Structures

Struct

Description

RaeptorCogs::UniqueKey

Struct to create unique keys for resource management.

RaeptorCogs::is_streamable

Helper trait to check if a type is streamable to std::ostringstream.

struct UniqueKey

Struct to create unique keys for resource management.

Used by ResourceManager to uniquely identify resources based on constructor arguments.

Typical use cases:

  • Generating unique keys for textures, fonts, and other resources.

See also

ResourceManager

Public Functions

template<typename ...Args, typename = std::enable_if_t<(is_streamable<Args>::value && ...)>>
inline UniqueKey(Args&&... args)

Constructor to create a UniqueKey from various arguments.

Note

This constructor concatenates the string representations of the provided arguments to form a unique key.

Parameters:

args – Arguments used to compute the unique key.

inline UniqueKey()

Default constructor for an empty UniqueKey.

Initializes the unique key value to an empty string.

Public Members

std::string value

The computed unique key value.

Holds the string representation of the unique key.

template<typename T, typename = void>
struct is_streamable : public std::false_type

Helper trait to check if a type is streamable to std::ostringstream.

Classes

Classes

Class

Description

RaeptorCogs::Singletons::ResourceManager

ResourceManager class template.

template<typename T>
class ResourceManager

ResourceManager class template.

Provides resource management functionalities for various resource types.

Typical use cases:

  • Loading, retrieving, and managing resources like textures and fonts.

Template Parameters:

T – Type of resource to manage (e.g., Texture, Font).

// Example usage:
auto &textureManager = RaeptorCogs::ResourceManager<RaeptorCogs::Texture>();
Texture &tex = textureManager.get_or_create("path/to/texture.png");

Public Functions

template<typename ...Args>
inline bool exists(Args&&... args) const

Check if a resource exists.

Note

This method uses the UniqueKey struct to compute the key from the provided arguments.

Parameters:

args – Arguments used to compute the unique key.

Returns:

True if the resource exists, false otherwise.

template<typename ...Args>
inline T &get(Args&&... args)

Get a resource by its unique key.

Warning

Throws a runtime error if the resource is not found.

Parameters:

args – Arguments used to compute the unique key.

Returns:

Reference to the requested resource.

template<typename ...Args>
inline T &create(UniqueKey key, Args&&... args)

Create a new resource.

Note

This method uses the provided UniqueKey to store the resource.

Parameters:
  • keyUniqueKey used to identify the resource.

  • args – Arguments forwarded to the resource constructor.

Returns:

Reference to the newly created resource.

template<typename ...Args>
inline T &create(Args&&... args)

Create a new resource.

Note

This method computes a UniqueKey from the provided arguments to store the resource.

Parameters:

args – Arguments forwarded to the resource constructor.

Returns:

Reference to the newly created resource.

template<typename ...Args>
inline T &get_or_create(UniqueKey key, Args&&... args)

Get or create a resource.

Note

This method checks for the existence of the resource before creating it.

Parameters:
  • keyUniqueKey used to identify the resource.

  • args – Arguments forwarded to the resource constructor if creation is needed.

Returns:

Reference to the requested or newly created resource.

template<typename ...Args>
inline T &get_or_create(Args&&... args)

Get or create a resource.

Note

This method computes a UniqueKey from the provided arguments to check for existence before creating the resource.

Parameters:

args – Arguments forwarded to the resource constructor if creation is needed.

Returns:

Reference to the requested or newly created resource.

inline void remove(UniqueKey key)

Remove a resource by its unique key.

Note

This method removes the resource associated with the provided UniqueKey.

Warning

This don’t immediately free the memory if there are other shared pointers to the resource.

Parameters:

keyUniqueKey used to identify the resource.

template<typename ...Args>
inline void remove(Args&&... args)

Remove a resource by its unique key.

Note

This method computes a UniqueKey from the provided arguments to identify the resource to remove.

Warning

This don’t immediately free the memory if there are other shared pointers to the resource

Parameters:

args – Arguments used to compute the unique key.

inline void clear()

Clear all resources.

Note

This method removes all resources managed by the ResourceManager.

Warning

This don’t immediately free the memory if there are other shared pointers to the resources.

Private Functions

ResourceManager() = default

Default constructor for ResourceManager.

Initializes the ResourceManager instance.

Note

This constructor is private to enforce singleton behavior.

Private Members

std::unordered_map<std::string, std::shared_ptr<T>> resources

Map to store resources.

Maps unique keys to shared pointers of resources.

friend SingletonAccessor< ResourceManager< T > >