![]() |
KTech 1.1.0
C++ 2D terminal game engine library
|
Registry that retrieves world structure pointers by serializable ID
.
More...
#include <cachingregistry.hpp>
Public Member Functions | |
auto | operator[] (const ID< T > &id) -> T * |
Retrieve structure using its ID . | |
auto | Exists (const ID< T > &id) -> bool |
Check if an ID matches a registered structure. | |
Friends | |
class | Memory |
Registry that retrieves world structure pointers by serializable ID
.
From the "How does `CachingRegistry` work?" faq.md
entry:
KTech::ID
is initialized with a UUID value when constructed, meaning, references to world structures (classes that have anID m_id
member) are serializable. All that is left is some kind of a container to store world structure instances or register world structure pointers, so world structures can be retrieved using theirID
s.I chose to register pointers rather than store instances, and
KTech::CachingRegistry
is implementation. Its purpose is to retrieve a registered world structure pointer, given a corresponding UUID. As its name suggests, it doesn't simply iterate through all registered world structures and retrieves the one that has the matching UUID, but rather, it uses some kind of caching-related optimization.
KTech::ID
doesn't only comprise a UUID value: it also has an index (ID::m_i
). This member is used byCachingRegistry
to cache the last known index in the registry of the associated world structure. WhenCachingRegistry
attempts to retrieve a world structure pointer, it first checks if the UUID of the givenID
matches the UUID of the world structure registered at the given cached index. Optimally they match, so it simply returns the pointer from the cached index. If they don't, it iterates down the registry until it finds the matching UUID (i.e. the wanted world structure).
CachingRegistry
will update the cached index of anyID
it receives, expediting future usages of thatID
. The cached index ofID
is declaredmutable
, which is howCachingRegistry
can modify it even though it asks for const-references ofID
s.Additionally, considering KTech is designed to work single threaded, keeping a direct pointer to a world structure retrieved from
CachingRegistry
for the duration of a single function, after validating it (usingCachingRegistry::Exists()
or checking that the returned pointer is notnullptr
), is valid practice, as nothing external should erase the pointed world structure from memory in the meantime.
KTech::ID
|
inline |
|
inline |