nomlib
Loading...
Searching...
No Matches
ResourceCache.hpp
1/******************************************************************************
2
3 nomlib - C++11 cross-platform game engine
4
5Copyright (c) 2013, 2014 Jeffrey Carpenter <i8degrees@gmail.com>
6All rights reserved.
7
8Redistribution and use in source and binary forms, with or without
9modification, are permitted provided that the following conditions are met:
10
111. Redistributions of source code must retain the above copyright notice, this
12 list of conditions and the following disclaimer.
132. Redistributions in binary form must reproduce the above copyright notice,
14 this list of conditions and the following disclaimer in the documentation
15 and/or other materials provided with the distribution.
16
17THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
18ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
20DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
21ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
22(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
23LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
24ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
26SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27
28******************************************************************************/
29#ifndef NOMLIB_SYSTEM_RESOURCE_CACHE_HPP
30#define NOMLIB_SYSTEM_RESOURCE_CACHE_HPP
31
32#include <string>
33#include <map>
34#include <memory>
35#include <functional>
36// #include <type_traits>
37
38#include "export.hpp"
39#include "nomlib/config.hpp"
40#include "nomlib/system/File.hpp"
41#include "nomlib/system/Path.hpp"
42#include "nomlib/system/ResourceFile.hpp"
43
44namespace nom {
45
47template <typename ResourceType>
48class NOM_EXPORT ResourceCache
49{
50 public:
51 typedef ResourceCache self_type;
52
53 typedef self_type* raw_ptr;
54 typedef std::unique_ptr<self_type> unique_ptr;
55 typedef std::shared_ptr<self_type> shared_ptr;
56
57 typedef std::function<void( const ResourceFile&, ResourceType& )> callback_type;
58
61 {
62 // NOM_LOG_TRACE( NOM );
63 }
64
67 {
68 // NOM_LOG_TRACE( NOM );
69
70 // Goodbye cruel world!
71 this->resources_.clear();
72 }
73
75 nom::size_type size( void ) const
76 {
77 return this->resources_.size();
78 }
79
86 const ResourceFile& find_resource( const std::string& key ) const
87 {
88 auto res = this->resources_.find( ResourceFile( key ) );
89
90 if( res != this->resources_.end() )
91 {
92 return res->first;
93 }
94
95 return ResourceFile::null;
96 }
97
100 void set_resource_handler( const callback_type& delegate )
101 {
102 this->callback_ = delegate;
103 }
104
111 bool append_resource( const ResourceFile& res )
112 {
113 File fp;
114 ResourceFile member( res.name() );
115
116 auto itr = this->resources_.find( member );
117
118 // Duplicate resource entry; reject
119 if( itr != this->resources_.end() )
120 {
121 // NOM_LOG_ERR( NOM, "Could not add resource: " + res.name() + " already exists." );
122 return false;
123 }
124
125 if( res.exists() == false )
126 {
127 NOM_LOG_ERR( NOM, "Could not add resource: " + res.name() + " does not exist at " + res.path() );
128 return false;
129 }
130
131 // Create the resource before insertion
132 ResourceType res_type;
133
134 // Initialize the resource when type is a pointer; this resolves a
135 // compile-time warning: res_type is uninitialized when this condition is
136 // false.
137 // if( std::is_pointer<ResourceType>::value == true ) {
138 // res_type = nullptr;
139 // }
140
141 // Use the load handler callback helper for resource initialization, if
142 // it has been set.
143 if( this->callback_ != nullptr )
144 {
145 this->callback_( res, res_type );
146 }
147
148 this->resources_.insert( { res, std::make_shared<ResourceType>(res_type) } );
149
150 return true;
151 }
152
161 ResourceType* load_resource( const std::string& key )
162 {
163 ResourceFile member( key );
164
165 auto res = this->resources_.find( member );
166
167 // Found a match
168 if( res != this->resources_.end() )
169 {
170 // Load the resource only if it has not been yet
171 if( res->first.loaded() == false )
172 {
173 // Err if the resource is invalid
174 // if( res->second->valid() == false ) {
175 if( res->second == nullptr ) {
176 NOM_LOG_ERR( NOM, "Could not load resource: invalid resource." );
177 return nullptr;
178 }
179
180 // Err if the resource cannot be loaded
181 if( res->second.get()->load( res->first.path() ) == false )
182 {
183 NOM_LOG_ERR( NOM, "Could not load resource: file path does not exist at " + res->first.path() );
184 return nullptr;
185 }
186
187 // res->first.set_loaded( true );
188 res->first.loaded_ = true;
189
190 } // end if not loaded
191
192 return res->second.get();
193
194 } // end if resource found
195
196 // Err
197 return nullptr;
198 }
199
201 void clear( void )
202 {
203 this->resources_.clear();
204 }
205
209 void erase( const std::string& key )
210 {
211 this->resources_.erase( key );
212 }
213
217 void dump( void )
218 {
219 for( auto itr = this->resources_.begin(); itr != this->resources_.end(); ++itr )
220 {
221 NOM_LOG_INFO( NOM, "Resource " + itr->first.name() + " at path: " + itr->first.path() + "." );
222 }
223 }
224
225 private:
227 std::map<ResourceFile, std::shared_ptr<ResourceType>> resources_;
228
231 callback_type callback_;
232};
233
234} // namespace nom
235
236#endif // include guard defined
237
Platform-agnostic file system interface.
Definition File.hpp:56
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.