Foundation
Loading...
Searching...
No Matches
Pool.hpp
Go to the documentation of this file.
1#pragma once
2#include "Core.hpp"
3#include <mutex>
4namespace Foundation::Core {
13 template<typename K, typename V>
14 class Pool {
18 mutable std::mutex mMutex;
23 void Push(K key) {
24 mKeys.push_back(key);
25 if (key >= mValues.size())
26 mValues.resize(key + 1);
27 }
28 void CheckContains(K key) const {
29 CHECK_MSG(key < mValues.size() && mValues[key], "Key not allocated");
30 }
32 K key{};
33 if (mKeys.empty())
34 key = mTop++;
35 else
36 key = mKeys.back(), mKeys.pop_back();
37 if (key >= mValues.size())
38 mValues.resize(key + 1);
39 return key;
40 }
41 public:
42 Pool(Allocator* alloc) : mAlloc(alloc), mKeys(alloc), mValues(alloc) {}
46 bool Contains(K key) const {
47 std::unique_lock lock(mMutex);
48 return key < mValues.size() && mValues[key];
49 }
54 V* At(K key) const {
55 std::unique_lock lock(mMutex);
57 return mValues[key].get();
58 }
62 template <typename... Args>
63 K Pop(Args&&... args)
64 {
65 std::unique_lock lock(mMutex);
66 K key = PopKey();
67 mValues[key] = ConstructUnique<V>(mAlloc, std::forward<Args>(args)...);
68 return key;
69 }
74 template <typename Derived, typename... Args>
76 {
77 std::unique_lock lock(mMutex);
78 K key = PopKey();
79 mValues[key] = ConstructUniqueBase<V, Derived>(mAlloc, std::forward<Args>(args)...);
80 return key;
81 }
85 void Free(K key) {
86 std::unique_lock lock(mMutex);
88 mValues[key].reset();
89 Push(key);
90 }
91 };
92}
#define CHECK_MSG(expr, format_str,...)
Definition Logging.hpp:31
General Purpose Allocator (GPA) interface.
Definition Allocator.hpp:24
Unbounded object pool with O(1) value mapping.
Definition Pool.hpp:14
V * At(K key) const
Retrieves a reference to the value associated with the given key. NOTE: Calling this function with a ...
Definition Pool.hpp:54
void Push(K key)
Adds a key to the internal key container and resizes the value container if necessary.
Definition Pool.hpp:23
Pool(Allocator *alloc)
Definition Pool.hpp:42
Vector< UniquePtr< V > > mValues
Definition Pool.hpp:17
K Pop(Args &&... args)
Allocates a new key in the pool and constructs the object in-place.
Definition Pool.hpp:63
bool Contains(K key) const
Checks if the specified key exists and has a value.
Definition Pool.hpp:46
K PopBase(Args &&... args)
Allocates a new key in the pool and constructs a derived object in-place.
Definition Pool.hpp:75
std::mutex mMutex
Definition Pool.hpp:18
K mTop
Definition Pool.hpp:19
Allocator * mAlloc
Definition Pool.hpp:15
void CheckContains(K key) const
Definition Pool.hpp:28
K PopKey()
Definition Pool.hpp:31
Vector< K > mKeys
Definition Pool.hpp:16
void Free(K key)
Frees the value associated with the specified key.
Definition Pool.hpp:85
Allocators, Data Structures and introspection implementations.
Definition Allocator.hpp:5
std::unique_ptr< T, StlDeleter< T > > UniquePtr
std::unique_ptr with custom deleter that uses a Foundation::Core::Allocator to deallocate memory.
Definition Allocator.hpp:161