nomlib
Toggle main menu visibility
Loading...
Searching...
No Matches
include
nomlib
system
ResourceCache.hpp
1
/******************************************************************************
2
3
nomlib - C++11 cross-platform game engine
4
5
Copyright (c) 2013, 2014 Jeffrey Carpenter <i8degrees@gmail.com>
6
All rights reserved.
7
8
Redistribution and use in source and binary forms, with or without
9
modification, are permitted provided that the following conditions are met:
10
11
1. Redistributions of source code must retain the above copyright notice, this
12
list of conditions and the following disclaimer.
13
2. 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
17
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
18
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
20
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
21
ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
22
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
23
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
24
ON 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
26
SOFTWARE, 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
44
namespace
nom {
45
47
template
<
typename
ResourceType>
48
class
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
60
ResourceCache
(
void
)
61
{
62
// NOM_LOG_TRACE( NOM );
63
}
64
66
~ResourceCache
(
void
)
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
nom::File
Platform-agnostic file system interface.
Definition
File.hpp:56
nom::ResourceCache::find_resource
const ResourceFile & find_resource(const std::string &key) const
Search the cache for a resource.
Definition
ResourceCache.hpp:86
nom::ResourceCache::~ResourceCache
~ResourceCache(void)
Destructor – free all of the stored resources.
Definition
ResourceCache.hpp:66
nom::ResourceCache::load_resource
ResourceType * load_resource(const std::string &key)
Fully initialize a resource.
Definition
ResourceCache.hpp:161
nom::ResourceCache< Font >::resources_
std::map< ResourceFile, std::shared_ptr< Font > > resources_
Definition
ResourceCache.hpp:227
nom::ResourceCache::append_resource
bool append_resource(const ResourceFile &res)
Add a file-based resource object.
Definition
ResourceCache.hpp:111
nom::ResourceCache< Font >::callback_
callback_type callback_
Definition
ResourceCache.hpp:231
nom::ResourceCache::erase
void erase(const std::string &key)
Destroy a resource.
Definition
ResourceCache.hpp:209
nom::ResourceCache::clear
void clear(void)
Erase all resource elements in the cache.
Definition
ResourceCache.hpp:201
nom::ResourceCache::ResourceCache
ResourceCache(void)
Default constructor.
Definition
ResourceCache.hpp:60
nom::ResourceCache::set_resource_handler
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.
Definition
ResourceCache.hpp:100
nom::ResourceCache::size
nom::size_type size(void) const
Get the total number of stored resources.
Definition
ResourceCache.hpp:75
nom::ResourceCache::dump
void dump(void)
Output the stored resource name and file paths.
Definition
ResourceCache.hpp:217
nom::ResourceFile
Descriptor of a file-based resource.
Definition
ResourceFile.hpp:45
nom::ResourceFile::path
const std::string & path(void) const
Get the absolute file path to the resource.
nom::ResourceFile::name
const std::string & name(void) const
Get the name of the resource.
nom::ResourceFile::exists
bool exists(void) const
Get the status of resource existence at the file-system level.
Generated on
for nomlib by
1.18.0