Aeon Engine c550894
AeonGames Open Source Game Engine
Loading...
Searching...
No Matches
Archive.hpp
1/*
2Copyright (C) 2018,2025,2026 Rodrigo Jose Hernandez Cordoba
3
4Licensed under the Apache License, Version 2.0 (the "License");
5you may not use this file except in compliance with the License.
6You may obtain a copy of the License at
7
8 http://www.apache.org/licenses/LICENSE-2.0
9
10Unless required by applicable law or agreed to in writing, software
11distributed under the License is distributed on an "AS IS" BASIS,
12WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13See the License for the specific language governing permissions and
14limitations under the License.
15*/
16#ifndef AEONGAMES_ARCHIVE_H
17#define AEONGAMES_ARCHIVE_H
18
19#include <unordered_map>
20#include <memory>
21#include <algorithm>
22#include "aeongames/UniqueAnyPtr.hpp"
23
24namespace AeonGames
25{
31 template<class K, class T>
32 class Archive
33 {
34 public:
42 template <typename... Args>
43 T* Store ( const K& k, Args... args )
44 {
45 mStorage.emplace ( std::make_pair<> ( k, std::make_unique<T> ( args... ) ) );
46 return mStorage[k].get();
47 }
48
54 T* Store ( const K& k, std::unique_ptr<T>&& pointer )
55 {
56 mStorage.emplace ( std::make_pair<> ( k, std::move ( pointer ) ) );
57 return mStorage[k].get();
58 }
59
64 std::unique_ptr<T> Dispose ( const K& k )
65 {
66 std::unique_ptr<T> result{};
67 auto i = mStorage.find ( k );
68 if ( i != mStorage.end() )
69 {
70 result = std::move ( ( *i ).second );
71 mStorage.erase ( i );
72 }
73 return result;
74 }
75
80 const T* Get ( const K& k ) const
81 {
82 auto i = mStorage.find ( k );
83 if ( i != mStorage.end() )
84 {
85 return ( *i ).second.get();
86 }
87 return nullptr;
88 }
89
94 T* Get ( const K& k )
95 {
96 return const_cast<T*> ( static_cast<const Archive<K, T>*> ( this )->Get ( k ) );
97 }
98
104 const K& GetKey ( const T* t ) const
105 {
106 auto i = std::find_if ( mStorage.begin(), mStorage.end(), [t] ( const std::pair<const K, std::unique_ptr<T >> & ref )
107 {
108 return t == ref.second.get();
109 } );
110 if ( i != mStorage.end() )
111 {
112 return ( ( *i ).first );
113 }
114 throw std::runtime_error ( "Key not found." );
115 }
116 private:
117 std::unordered_map<K, std::unique_ptr<T >> mStorage{};
118 };
119
124 template<class K>
126 {
127 public:
134 const UniqueAnyPtr& Store ( const K& k, UniqueAnyPtr&& pointer )
135 {
136 mStorage.emplace ( std::make_pair<> ( k, std::move ( pointer ) ) );
137 return mStorage[k];
138 }
139
145 UniqueAnyPtr Dispose ( const K& k )
146 {
147 UniqueAnyPtr result{};
148 auto i = mStorage.find ( k );
149 if ( i != mStorage.end() )
150 {
151 result.Swap ( ( *i ).second );
152 mStorage.erase ( i );
153 }
154 return result;
155 }
156
162 const UniqueAnyPtr& Get ( const K& k ) const
163 {
164 static const UniqueAnyPtr unique_nullptr{nullptr};
165 auto i = mStorage.find ( k );
166 if ( i != mStorage.end() )
167 {
168 return ( *i ).second;
169 }
170 return unique_nullptr;
171 }
172
178 const UniqueAnyPtr& Get ( const K& k )
179 {
180 return const_cast<UniqueAnyPtr&> ( static_cast<const ArchiveAny<K>*> ( this )->Get ( k ) );
181 }
182
189 const K& GetKey ( const void* t ) const
190 {
191 auto i = std::find_if ( mStorage.begin(), mStorage.end(), [t] ( const std::pair<const K, UniqueAnyPtr>& ref )
192 {
193 return t == ref.second.GetRaw();
194 } );
195 if ( i != mStorage.end() )
196 {
197 return ( ( *i ).first );
198 }
199 throw std::runtime_error ( "Key not found." );
200 }
201 private:
202 std::unordered_map<K, UniqueAnyPtr> mStorage{};
203 };
204}
205#endif
Type-erased key-value archive that stores UniqueAnyPtr values.
Definition Archive.hpp:126
const UniqueAnyPtr & Store(const K &k, UniqueAnyPtr &&pointer)
Store a type-erased pointer under the given key.
Definition Archive.hpp:134
const UniqueAnyPtr & Get(const K &k) const
Retrieve a stored pointer by key (const).
Definition Archive.hpp:162
const K & GetKey(const void *t) const
Find the key associated with a stored raw pointer.
Definition Archive.hpp:189
const UniqueAnyPtr & Get(const K &k)
Retrieve a stored pointer by key (mutable).
Definition Archive.hpp:178
UniqueAnyPtr Dispose(const K &k)
Remove and return the pointer associated with the given key.
Definition Archive.hpp:145
Key-value archive that owns stored objects of type T, keyed by K.
Definition Archive.hpp:33
std::unique_ptr< T > Dispose(const K &k)
Remove and return the object associated with the given key.
Definition Archive.hpp:64
const K & GetKey(const T *t) const
Find the key associated with a stored object.
Definition Archive.hpp:104
T * Store(const K &k, std::unique_ptr< T > &&pointer)
Store an existing uniquely-owned object under the given key.
Definition Archive.hpp:54
T * Store(const K &k, Args... args)
Construct and store a new object under the given key.
Definition Archive.hpp:43
const T * Get(const K &k) const
Retrieve a stored object by key (const).
Definition Archive.hpp:80
T * Get(const K &k)
Retrieve a stored object by key (mutable).
Definition Archive.hpp:94
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
Definition AABB.hpp:31