10 #ifndef _TurtleBraines_ResourceCache_hpp
11 #define _TurtleBraines_ResourceCache_hpp
13 #include <turtle_brains/core/tb_noncopyable.hpp>
14 #include <turtle_brains/core/tb_string.hpp>
15 #include <turtle_brains/core/tb_types.hpp>
19 #include <unordered_map>
106 ResourceType
GetResource(
const HandleType& handle)
const;
115 void SetResource(
const HandleType& handle,
const ResourceType& resource);
173 size_t ForEachValidResource(std::function<
void(ResourceType&, HandleType)> callbackFunction)
175 size_t currentIndex = 0;
176 size_t validResourceCount = 0;
177 for (
auto& resourcePair : mResources)
179 if (resourcePair.first > 0)
181 callbackFunction(resourcePair.second, HandleType(currentIndex));
182 ++validResourceCount;
187 return validResourceCount;
191 std::unordered_map<tbCore::tbString, HandleType> mResourceCache;
192 std::vector<std::pair<int, ResourceType>> mResources;
204 template<
typename ResourceType,
typename HandleType> HandleType
212 template<
typename ResourceType,
typename HandleType> HandleType
216 const auto& resourceIterator = mResourceCache.find(resourceName);
217 if (resourceIterator == mResourceCache.end())
219 size_t searchedHandle = 0;
220 for (; searchedHandle < mResources.size(); ++searchedHandle)
222 if (0 == mResources[searchedHandle].first)
224 mResources[searchedHandle].first = 1;
225 mResources[searchedHandle].second = resource;
230 if (searchedHandle == mResources.size())
232 mResources.emplace_back(1, resource);
235 if (
false == resourceName.empty())
237 tb_error_if(mResourceCache.end() != mResourceCache.find(resourceName),
"tbInternalError: Resource name already found in cache!");
239 mResourceCache.emplace(resourceName, HandleType(searchedHandle));
242 return searchedHandle;
248 tb_error(
"This probably should never happen...");
249 IncrementReferenceOf(resourceIterator->second);
250 return resourceIterator->second;
255 template<
typename ResourceType,
typename HandleType>
void
258 tb_error_if(InvalidResource() == handle,
"tbExternalError: Attempting to destroy a resource with an invalid handle.");
259 tb_error_if(handle.mValue >= mResources.size(),
"tbExternalError: Attempting to destroy a resource with an out-of-bounds handle.");
261 mResources[handle.mValue].first = 0;
263 for (
auto cacheIter = mResourceCache.begin(), cacheEnd = mResourceCache.end(); cacheIter != cacheEnd; )
265 if (cacheIter->second == handle)
267 cacheIter = mResourceCache.erase(cacheIter);
278 template<
typename ResourceType,
typename HandleType>
void
282 mResourceCache.clear();
287 template<
typename ResourceType,
typename HandleType>
void
290 tb_error_if(mResources[handle.mValue].first == 0,
"tbExternalError: Attempting to increment reference count of object with 0 references.");
291 ++mResources[handle.mValue].first;
296 template<
typename ResourceType,
typename HandleType>
bool
299 tb_error_if(mResources[handle.mValue].first == 0,
"tbInternalError: Attempting to decrement reference count of object with 0 references.");
300 --mResources[handle.mValue].first;
301 return 0 == mResources[handle.mValue].first;
306 template<
typename ResourceType,
typename HandleType> ResourceType
310 tb_error_if(InvalidResource() == handle,
"tbExternalError: Attempting to access a resource with an invalid handle.");
311 tb_error_if(handle.mValue >= mResources.size(),
"tbInternalError: Invalid handle, access out of range.");
312 return mResources[handle.mValue].second;
317 template<
typename ResourceType,
typename HandleType>
void
320 tb_error_if(InvalidResource() == handle,
"tbExternalError: Attempting to access a resource with an invalid handle.");
321 tb_error_if(handle.mValue >= mResources.size(),
"tbInternalError: Invalid handle, access out of range.");
322 mResources[handle.mValue].second = resource;
327 template<
typename ResourceType,
typename HandleType>
const ResourceType&
331 tb_error_if(InvalidResource() == handle,
"tbExternalError: Attempting to access a resource with an invalid handle.");
332 tb_error_if(handle.mValue >= mResources.size(),
"tbInternalError: Invalid handle, access out of range.");
333 return mResources[handle.mValue].second;
338 template<
typename ResourceType,
typename HandleType> ResourceType&
342 tb_error_if(InvalidResource() == handle,
"tbExternalError: Attempting to access a resource with an invalid handle.");
343 tb_error_if(handle.mValue >= mResources.size(),
"tbInternalError: Invalid handle, access out of range.");
344 return mResources[handle.mValue].second;
349 template<
typename ResourceType,
typename HandleType> HandleType
352 const auto& resourceIterator = mResourceCache.find(resourceName);
353 if (mResourceCache.end() != resourceIterator)
355 return resourceIterator->second;
363 template<
typename ResourceType,
typename HandleType>
bool
365 const HandleType& handle)
const
367 if (handle.mValue < mResources.size() && mResources[handle.mValue].first > 0)
HandleType GetResourceHandle(const tbCore::tbString &resourceName) const
Definition: tb_resource_cache.hpp:350
const ResourceType & GetResourceReference(const HandleType &handle) const
Definition: tb_resource_cache.hpp:328
#define tb_error(message,...)
Definition: tb_error.hpp:23
void ClearCache(void)
Definition: tb_resource_cache.hpp:279
Definition: tb_noncopyable.hpp:22
Here is some information about the primary namespace.
Definition: tb_application_dialog.hpp:21
ResourceType GetResource(const HandleType &handle) const
Definition: tb_resource_cache.hpp:307
void IncrementReferenceOf(const HandleType &handle)
Definition: tb_resource_cache.hpp:288
void SetResource(const HandleType &handle, const ResourceType &resource)
Definition: tb_resource_cache.hpp:318
void DestroyResource(const HandleType &handle)
Definition: tb_resource_cache.hpp:256
Definition: tb_resource_cache.hpp:31
Contains core functionality for each component of the API.
Definition: tb_debug_logger.hpp:88
#define tb_error_if(errorTest, message,...)
Definition: tb_error.hpp:42
static HandleType InvalidResource(void)
Definition: tb_resource_cache.hpp:205
bool IsValidHandle(const HandleType &handle) const
Definition: tb_resource_cache.hpp:364
HandleType CreateResource(const ResourceType &resource, const tbCore::tbString &resourceName="")
Definition: tb_resource_cache.hpp:213
std::string tbString
Definition: tb_string.hpp:335
bool DecrementReferenceOf(const HandleType &handle)
Definition: tb_resource_cache.hpp:297