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 "nomlib/config.hpp"
39#include "nomlib/system/File.hpp"
40#include "nomlib/system/Path.hpp"
41#include "nomlib/system/ResourceFile.hpp"
42
43namespace nom {
44
46template <typename ResourceType>
48{
49 public:
50 typedef ResourceCache self_type;
51
52 typedef self_type* raw_ptr;
53 typedef std::unique_ptr<self_type> unique_ptr;
54 typedef std::shared_ptr<self_type> shared_ptr;
55
56 typedef std::function<void( const ResourceFile&, ResourceType& )> callback_type;
57
60 {
61 // NOM_LOG_TRACE( NOM );
62 }
63
66 {
67 // NOM_LOG_TRACE( NOM );
68
69 // Goodbye cruel world!
70 this->resources_.clear();
71 }
72
74 nom::size_type size( void ) const
75 {
76 return this->resources_.size();
77 }
78
85 const ResourceFile& find_resource( const std::string& key ) const
86 {
87 auto res = this->resources_.find( ResourceFile( key ) );
88
89 if( res != this->resources_.end() )
90 {
91 return res->first;
92 }
93
94 return ResourceFile::null;
95 }
96
99 void set_resource_handler( const callback_type& delegate )
100 {
101 this->callback_ = delegate;
102 }
103
110 bool append_resource( const ResourceFile& res )
111 {
112 File fp;
113 ResourceFile member( res.name() );
114
115 auto itr = this->resources_.find( member );
116
117 // Duplicate resource entry; reject
118 if( itr != this->resources_.end() )
119 {
120 // NOM_LOG_ERR( NOM, "Could not add resource: " + res.name() + " already exists." );
121 return false;
122 }
123
124 if( res.exists() == false )
125 {
126 NOM_LOG_ERR( NOM, "Could not add resource: " + res.name() + " does not exist at " + res.path() );
127 return false;
128 }
129
130 // Create the resource before insertion
131 ResourceType res_type;
132
133 // Initialize the resource when type is a pointer; this resolves a
134 // compile-time warning: res_type is uninitialized when this condition is
135 // false.
136 // if( std::is_pointer<ResourceType>::value == true ) {
137 // res_type = nullptr;
138 // }
139
140 // Use the load handler callback helper for resource initialization, if
141 // it has been set.
142 if( this->callback_ != nullptr )
143 {
144 this->callback_( res, res_type );
145 }
146
147 this->resources_.insert( { res, std::make_shared<ResourceType>(res_type) } );
148
149 return true;
150 }
151
160 ResourceType* load_resource( const std::string& key )
161 {
162 ResourceFile member( key );
163
164 auto res = this->resources_.find( member );
165
166 // Found a match
167 if( res != this->resources_.end() )
168 {
169 // Load the resource only if it has not been yet
170 if( res->first.loaded() == false )
171 {
172 // Err if the resource is invalid
173 // if( res->second->valid() == false ) {
174 if( res->second == nullptr ) {
175 NOM_LOG_ERR( NOM, "Could not load resource: invalid resource." );
176 return nullptr;
177 }
178
179 // Err if the resource cannot be loaded
180 if( res->second.get()->load( res->first.path() ) == false )
181 {
182 NOM_LOG_ERR( NOM, "Could not load resource: file path does not exist at " + res->first.path() );
183 return nullptr;
184 }
185
186 // res->first.set_loaded( true );
187 res->first.loaded_ = true;
188
189 } // end if not loaded
190
191 return res->second.get();
192
193 } // end if resource found
194
195 // Err
196 return nullptr;
197 }
198
200 void clear( void )
201 {
202 this->resources_.clear();
203 }
204
208 void erase( const std::string& key )
209 {
210 this->resources_.erase( key );
211 }
212
216 void dump( void )
217 {
218 for( auto itr = this->resources_.begin(); itr != this->resources_.end(); ++itr )
219 {
220 NOM_LOG_INFO( NOM, "Resource " + itr->first.name() + " at path: " + itr->first.path() + "." );
221 }
222 }
223
224 private:
226 std::map<ResourceFile, std::shared_ptr<ResourceType>> resources_;
227
230 callback_type callback_;
231};
232
233} // namespace nom
234
235#endif // include guard defined
236
Platform-agnostic file system interface.
Definition File.hpp:55
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.