Aeon Engine c550894
AeonGames Open Source Game Engine
Loading...
Searching...
No Matches
ResourceCache.cpp
1/*
2Copyright (C) 2018,2025 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#include <unordered_map>
17#include "aeongames/ResourceCache.hpp"
18#include "aeongames/ResourceFactory.hpp"
19#include "aeongames/ResourceId.hpp"
20
21namespace AeonGames
22{
23 static std::unordered_map<uint32_t, UniqueAnyPtr> gResourceStore{};
24
26 {
27 gResourceStore.clear();
28 }
29
30 void EnumerateResources ( const std::function<bool ( uint32_t, const UniqueAnyPtr& ) >& aEnumerator )
31 {
32 for ( auto& i : gResourceStore )
33 {
34 if ( !aEnumerator ( i.first, i.second ) )
35 {
36 return;
37 }
38 }
39 }
40
41 const UniqueAnyPtr& StoreResource ( uint32_t aKey, UniqueAnyPtr&& pointer )
42 {
43 static const UniqueAnyPtr unique_nullptr{nullptr};
44 // Don't store nullptrs
45 if ( pointer.GetRaw() )
46 {
47 gResourceStore.emplace ( std::make_pair<> ( aKey, std::move ( pointer ) ) );
48 return gResourceStore[aKey];
49 }
50 return unique_nullptr;
51 }
52
53 UniqueAnyPtr DisposeResource ( uint32_t aKey )
54 {
55 UniqueAnyPtr result{};
56 auto i = gResourceStore.find ( aKey );
57 if ( i != gResourceStore.end() )
58 {
59 result.Swap ( ( *i ).second );
60 gResourceStore.erase ( i );
61 }
62 return result;
63 }
64
65 const UniqueAnyPtr& GetResource ( uint32_t aKey )
66 {
67 static const UniqueAnyPtr unique_nullptr{nullptr};
68 auto i = gResourceStore.find ( aKey );
69 if ( i != gResourceStore.end() )
70 {
71 return ( *i ).second;
72 }
73 return unique_nullptr;
74 }
75
76 const UniqueAnyPtr& GetResource ( const ResourceId& aResourceId )
77 {
78 auto i = gResourceStore.find ( aResourceId.GetPath() );
79 if ( i != gResourceStore.end() )
80 {
81 return ( *i ).second;
82 }
83 return GetDefaultResource ( aResourceId.GetType() );
84 }
85}
Identifies a resource by its type and path CRC32 hashes.
uint32_t GetType() const
Get the CRC32 hash of the resource type.
uint32_t GetPath() const
Get the CRC32 hash of the resource path.
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
DLL const UniqueAnyPtr & GetDefaultResource(uint32_t aType)
Get the default resource for a given type.
DLL const UniqueAnyPtr & StoreResource(uint32_t aKey, UniqueAnyPtr &&pointer)
Store a resource in the cache.
DLL UniqueAnyPtr DisposeResource(uint32_t aKey)
Remove and return a resource from the cache.
DLL const UniqueAnyPtr & GetResource(uint32_t aKey)
Retrieve a cached resource by key.
DLL void EnumerateResources(const std::function< bool(uint32_t, const UniqueAnyPtr &) > &aEnumerator)
Enumerate all cached resources.
DLL void ClearAllResources()
Remove all resources from the cache.