RavEngine
Loading...
Searching...
No Matches
Manager.hpp
1#pragma once
2#include "Ref.hpp"
3#include "WeakRef.hpp"
4#include "SpinLock.hpp"
5#include "Map.hpp"
6#include "Vector.hpp"
7#include <tuple>
8
9namespace RavEngine {
10
11 struct CacheBase {
12 using unique_key_t = uint32_t;
13 };
14
19template<typename key_t, typename T, bool keyIsConstructionParam = true>
21
22protected:
24 static SpinLock mtx;
25public:
32 template<typename ... A>
33 static inline Ref<T> Get(const key_t& str, A ... extras){
34 return GetWithKey(str, 0, extras...);
35 }
36
44 template<typename ... A>
45 static inline Ref<T> GetWithKey(const key_t& str, unique_key_t unique_key, A ... extras) {
46 auto key = std::tuple<key_t, unique_key_t>(str, unique_key);
47
48 //TODO: optimize
49 std::lock_guard guard(mtx);
50 if (items.contains(key)) {
51 auto value = items.at(key).lock();
52 if (auto ptr = value) {
53 return ptr;
54 }
55 }
56 Ref<T> m;
57 if constexpr (keyIsConstructionParam) {
58 m = std::make_shared<T>(str, extras...);
59 }
60 else {
61 m = std::make_shared<T>(extras...);
62 }
63 items[key] = m;
64 return m;
65 }
66
70 static void Compact(){
72 for(const auto& entry : items){
73 if (entry.second.expired()){
74 toremove.push_back(entry.first);
75 }
76 }
77 for(const auto& c : toremove){
78 items.erase(c);
79 }
80 }
81
85 static void Clear(){
86 std::lock_guard guard(mtx);
87 items.clear();
88 }
89};
90}
91template<typename key, typename T, bool keyIsConstructionParam>
93
94template<typename key,typename T, bool keyIsConstructionParam>
96
Definition SpinLock.hpp:9
Definition concurrentqueue.h:747
Definition Animation.hpp:6
Definition Manager.hpp:11
static Ref< T > GetWithKey(const key_t &str, unique_key_t unique_key, A ... extras)
Definition Manager.hpp:45
static void Compact()
Definition Manager.hpp:70
static Ref< T > Get(const key_t &str, A ... extras)
Definition Manager.hpp:33
static void Clear()
Definition Manager.hpp:85