CUGL
Cornell University Game Library
All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends
CUGenericLoader.h
1 //
2 // CUGenericLoader.h
3 // Cornell University Game Library (CUGL)
4 //
5 // This module provides an abstract class for loading generic assets (such
6 // as a model file or level layout) not explicitly included in the existing
7 // asset classes. It is fairly experimental, so use at your own risk. If there
8 // are certain assets that we overlooked that are the same across all projects,
9 // we will consider adding them to the engine at a later date.
10 //
11 // This module is meant to be used in conjunction with the Asset class which
12 // provides support for loading the asset. As such, this class really just
13 // functions as an asset manager.
14 //
15 // This class uses our standard shared-pointer architecture.
16 //
17 // 1. The constructor does not perform any initialization; it just sets all
18 // attributes to their defaults.
19 //
20 // 2. All initialization takes place via init methods, which can fail if an
21 // object is initialized more than once.
22 //
23 // 3. All allocation takes place via static constructors which return a shared
24 // pointer.
25 //
26 //
27 // CUGL zlib License:
28 // This software is provided 'as-is', without any express or implied
29 // warranty. In no event will the authors be held liable for any damages
30 // arising from the use of this software.
31 //
32 // Permission is granted to anyone to use this software for any purpose,
33 // including commercial applications, and to alter it and redistribute it
34 // freely, subject to the following restrictions:
35 //
36 // 1. The origin of this software must not be misrepresented; you must not
37 // claim that you wrote the original software. If you use this software
38 // in a product, an acknowledgment in the product documentation would be
39 // appreciated but is not required.
40 //
41 // 2. Altered source versions must be plainly marked as such, and must not
42 // be misrepresented as being the original software.
43 //
44 // 3. This notice may not be removed or altered from any source distribution.
45 //
46 // Author: Walker White
47 // Version: 1/7/16
48 //
49 #ifndef __CU_GENERIC_LOADER_H__
50 #define __CU_GENERIC_LOADER_H__
51 #include <cugl/assets/CULoader.h>
52 #include <cugl/assets/CUAsset.h>
53 
54 namespace cugl {
55 
68 template <class T>
69 class GenericLoader : public Loader<T> {
70 private:
72  CU_DISALLOW_COPY_AND_ASSIGN(GenericLoader);
73 
74 protected:
76  using Loader<T>::_assets;
78  using Loader<T>::_queue;
80  using BaseLoader::_loader;
81 
92  bool materialize(const std::string& key, const std::shared_ptr<T>& asset, LoaderCallback callback) {
93  bool success = false;
94  if (asset != nullptr) {
95  success = asset->materialize();
96  if (success) {
97  _assets[key] = asset;
98  }
99  }
100 
101  if (callback != nullptr) {
102  callback(key,success);
103  }
104  _queue.erase(key);
105  return success;
106  }
107 
126  virtual bool read(const std::string& key, const std::string& source,
127  LoaderCallback callback, bool async) override {
128  if (_assets.find(key) != _assets.end() || _queue.find(key) != _queue.end()) {
129  return false;
130  }
131  _queue.emplace(key);
132 
133  bool success = false;
134  if (_loader == nullptr || !async) {
135  std::shared_ptr<T> asset = std::make_shared<T>();
136  if (asset->preload(source)) {
137  success = materialize(key,asset,callback);
138  }
139  } else {
140  _loader->addTask([=](void) {
141  std::shared_ptr<T> asset = std::make_shared<T>();
142  if (!asset->preload(source)) {
143  asset = nullptr;
144  }
145  Application::get()->schedule([=](void){
146  this->materialize(key,asset,callback);
147  return false;
148  });
149  });
150  }
151 
152  return success;
153  }
154 
177  virtual bool read(const std::shared_ptr<JsonValue>& json,
178  LoaderCallback callback, bool async) override {
179  std::string key = json->key();
180  if (_assets.find(key) != _assets.end() || _queue.find(key) != _queue.end()) {
181  return false;
182  }
183  _queue.emplace(key);
184 
185  bool success = false;
186  if (_loader == nullptr || !async) {
187  std::shared_ptr<T> asset = std::make_shared<T>();
188  if (asset->preload(json)) {
189  success = materialize(key,asset,callback);
190  }
191  } else {
192  _loader->addTask([=](void) {
193  std::shared_ptr<T> asset = std::make_shared<T>();
194  if (!asset->preload(json)) {
195  asset = nullptr;
196  }
197  Application::get()->schedule([=](void){
198  this->materialize(key,asset,callback);
199  return false;
200  });
201  });
202  }
203 
204  return success;
205  }
206 
207 public:
208 #pragma mark -
209 #pragma mark Constructors
210 
217 
228  void dispose() override {
229  _assets.clear();
230  _loader = nullptr;
231  }
232 
244  static std::shared_ptr<GenericLoader<T>> alloc() {
245  std::shared_ptr<GenericLoader<T>> result = std::make_shared<GenericLoader<T>>();
246  return (result->init() ? result : nullptr);
247  }
248 
260  static std::shared_ptr<GenericLoader<T>> alloc(const std::shared_ptr<GenericLoader<T>>& threads) {
261  std::shared_ptr<GenericLoader> result = std::make_shared<GenericLoader>();
262  return (result->init(threads) ? result : nullptr);
263  }
264 
265 };
266 
267 }
268 #endif /* __CU_GENERIC_LOADER_H__ */
std::shared_ptr< ThreadPool > _loader
Definition: CULoader.h:102
static std::shared_ptr< GenericLoader< T > > alloc(const std::shared_ptr< GenericLoader< T >> &threads)
Definition: CUGenericLoader.h:260
GenericLoader()
Definition: CUGenericLoader.h:216
std::unordered_set< std::string > _queue
Definition: CULoader.h:681
std::unordered_map< std::string, std::shared_ptr< T > > _assets
Definition: CULoader.h:678
std::function< void(const std::string &key, bool success)> LoaderCallback
Definition: CULoader.h:81
static std::shared_ptr< GenericLoader< T > > alloc()
Definition: CUGenericLoader.h:244
static Application * get()
Definition: CUApplication.h:247
virtual bool read(const std::shared_ptr< JsonValue > &json, LoaderCallback callback, bool async) override
Definition: CUGenericLoader.h:177
Uint32 schedule(std::function< bool()> callback, Uint32 time=0)
virtual bool read(const std::string &key, const std::string &source, LoaderCallback callback, bool async) override
Definition: CUGenericLoader.h:126
Definition: CULoader.h:675
Definition: CUGenericLoader.h:69
Definition: CUAnimationNode.h:52
bool materialize(const std::string &key, const std::shared_ptr< T > &asset, LoaderCallback callback)
Definition: CUGenericLoader.h:92
void dispose() override
Definition: CUGenericLoader.h:228