Aeon Engine c550894
AeonGames Open Source Game Engine
Loading...
Searching...
No Matches
ResourceCache.hpp
1/*
2Copyright (C) 2016-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
8http://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_RESOURCECACHE_H
17#define AEONGAMES_RESOURCECACHE_H
18#include <cstdint>
19#include <functional>
21#include "aeongames/UniqueAnyPtr.hpp"
22
23namespace AeonGames
24{
25 class ResourceId;
26#if 0
27 template<class B, class K, class D = B, typename... Args>
28 const std::shared_ptr<B> Get ( const K& key, Args... args )
29 {
31 static std::unordered_map<K, std::weak_ptr<B >> cache;
32 static std::mutex m;
33 std::lock_guard<std::mutex> hold ( m );
34 auto iter = cache.find ( key );
35 if ( iter == cache.end() )
36 {
37 auto retval = std::shared_ptr<B> ( std::make_unique<D> ( args... ).release(), [key] ( B * t )
38 {
39 delete ( t );
40 cache.erase ( cache.find ( key ) );
41 } );
42 cache[key] = retval;
43 return retval;
44 }
45 return iter->second.lock();
46 }
47//----------------------------------------------------------------------------------------
65 template<class K, class B>
66 class ResourceCache
67 {
68 public:
69 ResourceCache() = default;
70 ~ResourceCache() = default;
71 ResourceCache ( const ResourceCache& ) = delete;
72 ResourceCache ( ResourceCache&& ) = delete;
73 ResourceCache& operator= ( const ResourceCache& ) = delete;
74 ResourceCache& operator= ( ResourceCache&& ) = delete;
75 template<class D = B, typename... Args>
76 static const std::shared_ptr<B> Get ( const K& aKey, Args... args )
77 {
79 std::lock_guard<std::mutex> hold ( mMutex );
80 auto iter = mCache.find ( aKey );
81 if ( iter == mCache.end() )
82 {
83 auto retval = std::shared_ptr<B> ( std::make_unique<D> ( args... ).release(), [aKey] ( B * t )
84 {
85 delete ( t );
86 mCache.erase ( mCache.find ( aKey ) );
87 } );
88 mCache[aKey] = retval;
89 return retval;
90 }
91 return iter->second.lock();
92 }
93 static const K& GetKey ( const std::shared_ptr<B>& aValue )
94 {
95 std::lock_guard<std::mutex> hold ( mMutex );
96 auto i = std::find_if ( mCache.begin(), mCache.end(), [&aValue] ( const std::pair<const K, std::weak_ptr<B >> & aPair )
97 {
98 return aPair.second.lock() == aValue;
99 } );
100 if ( i != mCache.end() )
101 {
102 return i->first;
103 }
104 throw std::runtime_error ( "Resource not found in cache." );
105 }
106
107 private:
108 static std::unordered_map<K, std::weak_ptr<B >> mCache;
109 static std::mutex mMutex;
110 };
111 template<class K, class B>
112 std::unordered_map<K, std::weak_ptr<B >> ResourceCache<K, B>::mCache;
113 template<class K, class B>
114 std::mutex ResourceCache<K, B>::mMutex;
115#endif
117 DLL void ClearAllResources();
121 DLL void EnumerateResources ( const std::function<bool ( uint32_t, const UniqueAnyPtr& ) >& aEnumerator );
127 DLL const UniqueAnyPtr& StoreResource ( uint32_t aKey, UniqueAnyPtr&& pointer );
132 DLL UniqueAnyPtr DisposeResource ( uint32_t aKey );
137 DLL const UniqueAnyPtr& GetResource ( uint32_t aKey );
142 DLL const UniqueAnyPtr& GetResource ( const ResourceId& aResourceId );
143}
144#endif
Platform-specific macros, includes, and DLL export/import definitions.
Identifies a resource by its type and path CRC32 hashes.
A type-erased owning smart pointer with unique ownership semantics.
<- This is here just for the literals
Definition AABB.hpp:31
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.