16#ifndef AEONGAMES_ARCHIVE_H
17#define AEONGAMES_ARCHIVE_H
19#include <unordered_map>
22#include "aeongames/UniqueAnyPtr.hpp"
31 template<
class K,
class T>
42 template <
typename... Args>
43 T*
Store (
const K& k, Args... args )
45 mStorage.emplace ( std::make_pair<> ( k, std::make_unique<T> ( args... ) ) );
46 return mStorage[k].get();
54 T*
Store (
const K& k, std::unique_ptr<T>&& pointer )
56 mStorage.emplace ( std::make_pair<> ( k, std::move ( pointer ) ) );
57 return mStorage[k].get();
66 std::unique_ptr<T> result{};
67 auto i = mStorage.find ( k );
68 if ( i != mStorage.end() )
70 result = std::move ( ( *i ).second );
80 const T*
Get (
const K& k )
const
82 auto i = mStorage.find ( k );
83 if ( i != mStorage.end() )
85 return ( *i ).second.get();
96 return const_cast<T*
> (
static_cast<const Archive<K, T>*
> ( this )->
Get ( k ) );
106 auto i = std::find_if ( mStorage.begin(), mStorage.end(), [t] (
const std::pair<
const K, std::unique_ptr<T >> & ref )
108 return t == ref.second.get();
110 if ( i != mStorage.end() )
112 return ( ( *i ).first );
114 throw std::runtime_error (
"Key not found." );
117 std::unordered_map<K, std::unique_ptr<T >> mStorage{};
136 mStorage.emplace ( std::make_pair<> ( k, std::move ( pointer ) ) );
148 auto i = mStorage.find ( k );
149 if ( i != mStorage.end() )
151 result.
Swap ( ( *i ).second );
152 mStorage.erase ( i );
165 auto i = mStorage.find ( k );
166 if ( i != mStorage.end() )
168 return ( *i ).second;
170 return unique_nullptr;
191 auto i = std::find_if ( mStorage.begin(), mStorage.end(), [t] (
const std::pair<const K, UniqueAnyPtr>& ref )
193 return t == ref.second.GetRaw();
195 if ( i != mStorage.end() )
197 return ( ( *i ).first );
199 throw std::runtime_error (
"Key not found." );
202 std::unordered_map<K, UniqueAnyPtr> mStorage{};
Type-erased key-value archive that stores UniqueAnyPtr values.
const UniqueAnyPtr & Store(const K &k, UniqueAnyPtr &&pointer)
Store a type-erased pointer under the given key.
const UniqueAnyPtr & Get(const K &k) const
Retrieve a stored pointer by key (const).
const K & GetKey(const void *t) const
Find the key associated with a stored raw pointer.
const UniqueAnyPtr & Get(const K &k)
Retrieve a stored pointer by key (mutable).
UniqueAnyPtr Dispose(const K &k)
Remove and return the pointer associated with the given key.
Key-value archive that owns stored objects of type T, keyed by K.
std::unique_ptr< T > Dispose(const K &k)
Remove and return the object associated with the given key.
const K & GetKey(const T *t) const
Find the key associated with a stored object.
T * Store(const K &k, std::unique_ptr< T > &&pointer)
Store an existing uniquely-owned object under the given key.
T * Store(const K &k, Args... args)
Construct and store a new object under the given key.
const T * Get(const K &k) const
Retrieve a stored object by key (const).
T * Get(const K &k)
Retrieve a stored object by key (mutable).
A type-erased owning smart pointer with unique ownership semantics.
void Swap(UniqueAnyPtr &aUniqueAnyPtr) noexcept
Swaps the contents of this pointer with another.
<- This is here just for the literals