29#ifndef NOMLIB_SYSTEM_RESOURCE_CACHE_HPP
30#define NOMLIB_SYSTEM_RESOURCE_CACHE_HPP
38#include "nomlib/config.hpp"
39#include "nomlib/system/File.hpp"
40#include "nomlib/system/Path.hpp"
41#include "nomlib/system/ResourceFile.hpp"
46template <
typename ResourceType>
52 typedef self_type* raw_ptr;
53 typedef std::unique_ptr<self_type> unique_ptr;
54 typedef std::shared_ptr<self_type> shared_ptr;
56 typedef std::function<void(
const ResourceFile&, ResourceType& )> callback_type;
74 nom::size_type
size(
void )
const
94 return ResourceFile::null;
124 if( res.
exists() ==
false )
126 NOM_LOG_ERR( NOM,
"Could not add resource: " + res.
name() +
" does not exist at " + res.
path() );
131 ResourceType res_type;
147 this->
resources_.insert( { res, std::make_shared<ResourceType>(res_type) } );
170 if( res->first.loaded() ==
false )
174 if( res->second ==
nullptr ) {
175 NOM_LOG_ERR( NOM,
"Could not load resource: invalid resource." );
180 if( res->second.get()->load( res->first.path() ) ==
false )
182 NOM_LOG_ERR( NOM,
"Could not load resource: file path does not exist at " + res->first.path() );
187 res->first.loaded_ =
true;
191 return res->second.get();
208 void erase(
const std::string& key )
218 for(
auto itr = this->
resources_.begin(); itr != this->resources_.end(); ++itr )
220 NOM_LOG_INFO( NOM,
"Resource " + itr->first.name() +
" at path: " + itr->first.path() +
"." );
226 std::map<ResourceFile, std::shared_ptr<ResourceType>>
resources_;
Platform-agnostic file system interface.
const ResourceFile & find_resource(const std::string &key) const
Search the cache for a resource.
~ResourceCache(void)
Destructor – free all of the stored resources.
ResourceType * load_resource(const std::string &key)
Fully initialize a resource.
std::map< ResourceFile, std::shared_ptr< Font > > resources_
bool append_resource(const ResourceFile &res)
Add a file-based resource object.
void erase(const std::string &key)
Destroy a resource.
void clear(void)
Erase all resource elements in the cache.
ResourceCache(void)
Default constructor.
void set_resource_handler(const callback_type &delegate)
Set an optional callback to use in creation of the resource before it is inserted into the cache.
nom::size_type size(void) const
Get the total number of stored resources.
void dump(void)
Output the stored resource name and file paths.
Descriptor of a file-based resource.
const std::string & path(void) const
Get the absolute file path to the resource.
const std::string & name(void) const
Get the name of the resource.
bool exists(void) const
Get the status of resource existence at the file-system level.