3#include "ResourcePolicies.h"
7 template<
typename HandleType,
typename ResourceType,
typename StoragePolicy,
typename LockingPolicy,
typename VerificationPolicy>
11 StoragePolicy resources;
13 using Lock = std::lock_guard<LockingPolicy>;
25 resources.clear(
true);
35 HandleType
addResource(
const ResourceType& resource,
bool persistent =
false)
39 return resources.allocate(resource, persistent);
49 HandleType
addResource(ResourceType&& resource,
bool persistent =
false)
53 return resources.allocate(std::move(resource), persistent);
65 VerificationPolicy::verify(handle, resources);
67 resources.deallocate(handle);
80 return resources.hasResource(handle);
90 return this->resources.size();
94 typename StoragePolicy::iterator
begin() {
return resources.begin(); }
97 typename StoragePolicy::iterator
end() {
return resources.end(); }
115 const ResourceType &
operator[](
const HandleType handle)
const
117 if (StoragePolicy::SafeDereference()) {
118 VerificationPolicy::verify(handle, resources);
120 return resources[handle];
122 std::lock_guard<LockingPolicy> lock(m);
124 VerificationPolicy::verify(handle, resources);
126 return resources[handle];
138 if (StoragePolicy::SafeDereference()) {
139 VerificationPolicy::verify(handle, resources);
141 return resources[handle];
143 std::lock_guard<LockingPolicy> lock(m);
145 VerificationPolicy::verify(handle, resources);
147 return resources[handle];
158 HandleType
getHandle(
const ResourceType & r) {
return resources.getHandle(r); }
165 void pin(
const HandleType handle)
167 VerificationPolicy::verify(handle, resources);
169 resources.pin(handle);
173 bool pinned(
const HandleType & handle)
const {
return resources.pinned(handle); }
179 template<
class HandleType,
class ResourceType>
182 template<
typename HandleType,
typename ResourceType>
Base allocator implementation.
HandleType getHandle(const ResourceType &r)
Gets a handle to the resource given.
StoragePolicy::iterator end()
Gets an iterator to the end of the resource map.
ResourceType & operator[](const HandleType handle)
Get a reference to the resource with the given handle.
size_t size() const
Get the number or resources in the resource map.
void clear()
Clear out the resources in the map.
ResourceMapBase(size_t capacity, size_t pageSize, Memory::Allocator *allocator)
Initialize the resource map with the given allocator.
ResourceMapBase()=default
Initialize a new, empty resource map.
StoragePolicy::iterator begin()
Gets an iterator to the beginning of the resource map.
void removeResource(HandleType handle)
Remove the resource with the given handle.
bool hasResource(HandleType handle)
Check if the resource with the given handle exists in the map.
void pin(const HandleType handle)
Pin the resource with the given handle, preventing it from being cleared from the resource map automa...
const ResourceType & operator[](const HandleType handle) const
Get a const reference to the resource with the given handle.
HandleType addResource(const ResourceType &resource, bool persistent=false)
Add a new resource to the map.
bool pinned(const HandleType &handle) const
Gets if the given resource handle is pinned.
HandleType addResource(ResourceType &&resource, bool persistent=false)
Add a new resource to the map.
Contains all Cogs related functionality.