CUGL 1.2
Cornell University Game Library
All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends
CULoader.h
1 //
2 // CULoader.h
3 // Cornell University Game Library (CUGL)
4 //
5 // This module provides the base templates for loader classes. Our goal with
6 // this module is to create a modular loader system that is as close as possible
7 // to Nathan Sweet's flexible and modular AssetManager for libGDX. To do this
8 // in C++ requires three layers:
9 //
10 // The first layer is a polymorphic base that is used by the AssetManager for
11 // adding and removing loaders. It is the C++ equivalent of a Java interface.
12 //
13 // The second layer is a template that provides type correctness when accessing
14 // components in the AssetManager. It has generic functionality that is
15 // necessary for all loaders, but is missing the information needed to load
16 // the specific asset.
17 //
18 // The top layer is a specific class for each asset that implements the specific
19 // loading functionality.
20 //
21 // This module implements the first two layers. As they are both a template
22 // and a pure polymorphic class, only a header file is necessary. There is no
23 // associated CPP file with this header.
24 //
25 //
26 // CUGL MIT License:
27 // This software is provided 'as-is', without any express or implied
28 // warranty. In no event will the authors be held liable for any damages
29 // arising from the use of this software.
30 //
31 // Permission is granted to anyone to use this software for any purpose,
32 // including commercial applications, and to alter it and redistribute it
33 // freely, subject to the following restrictions:
34 //
35 // 1. The origin of this software must not be misrepresented; you must not
36 // claim that you wrote the original software. If you use this software
37 // in a product, an acknowledgment in the product documentation would be
38 // appreciated but is not required.
39 //
40 // 2. Altered source versions must be plainly marked as such, and must not
41 // be misrepresented as being the original software.
42 //
43 // 3. This notice may not be removed or altered from any source distribution.
44 //
45 // Author: Walker White
46 // Version: 1/7/18
47 //
48 #ifndef __CU_LOADER_H__
49 #define __CU_LOADER_H__
50 #include <string>
51 #include <functional>
52 #include <unordered_map>
53 #include <unordered_set>
54 #include <cugl/assets/CUJsonValue.h>
55 #include <cugl/util/CUThreadPool.h>
56 
57 namespace cugl {
58 
60 class AssetManager;
61 
84 typedef std::function<void(const std::string& key, bool success)> LoaderCallback;
85 
86 #pragma mark -
87 #pragma mark Polymorphic Base
88 
98 class BaseLoader : public std::enable_shared_from_this<BaseLoader> {
99 protected:
105  std::shared_ptr<ThreadPool> _loader;
106 
113 
131  virtual bool read(const std::string& key, const std::string& source,
132  LoaderCallback callback, bool async) {
133  return false;
134  }
135 
158  virtual bool read(const std::shared_ptr<JsonValue>& json,
159  LoaderCallback callback, bool async) {
160  return false;
161  }
162 
176  virtual bool purge(const std::string& key) {
177  return false;
178  }
179 
196  virtual bool purge(const std::shared_ptr<JsonValue>& json) {
197  return purge(json->key());
198  }
199 
211  virtual bool verify(const std::string& key) const { return false; }
212 
213 
214 public:
215 #pragma mark Constructors
216 
223 
228 
243  virtual void dispose() { _manager = nullptr; }
244 
260  virtual bool init() {
261  return init(nullptr);
262  }
263 
278  virtual bool init(const std::shared_ptr<ThreadPool>& threads) {
279  _loader=threads;
280  return true;
281  }
282 
283 
284 #pragma mark AssetManager Support
285 
295  std::shared_ptr<BaseLoader> getHook() {
296  return shared_from_this();
297  }
298 
308  std::shared_ptr<ThreadPool> getThreadPool() const { return _loader; }
309 
324  void setThreadPool(const std::shared_ptr<ThreadPool>& threads) {
325  _loader = threads;
326  }
327 
336  void setManager(AssetManager* manager) {
337  _manager = manager;
338  }
339 
348  const AssetManager* getManager() const {
349  return _manager;
350  }
351 
352 
353 #pragma mark Loading/Unloading
354 
370  bool load(const std::string& key, const std::string& source) {
371  return read(key,source,nullptr,false);
372  }
373 
390  bool load(const char* key, const std::string& source) {
391  return read(std::string(key),source,nullptr,false);
392  }
393 
410  bool load(const std::string& key, const char* source) {
411  return read(key,std::string(source),nullptr,false);
412  }
413 
430  bool load(const char* key, const char* source) {
431  return read(std::string(key),std::string(source),nullptr,false);
432  }
433 
455  bool load(const std::shared_ptr<JsonValue>& json) {
456  return read(json,nullptr,false);
457  }
458 
478  void loadAsync(const std::string& key, const std::string& source, LoaderCallback callback) {
479  read(key, source, callback,true);
480  }
481 
501  void loadAsync(const char* key, const std::string& source, LoaderCallback callback) {
502  read(std::string(key), source, callback,true);
503  }
504 
524  void loadAsync(const std::string& key, const char* source, LoaderCallback callback) {
525  read(key, std::string(source), callback,true);
526  }
527 
547  void loadAsync(const char* key, const char* source, LoaderCallback callback) {
548  read(std::string(key), std::string(source), callback,true);
549  }
550 
576  void loadAsync(const std::shared_ptr<JsonValue>& json, LoaderCallback callback) {
577  read(json, callback,true);
578  }
579 
591  bool unload(const std::string& key) {
592  return purge(key);
593  }
594 
606  bool unload(const char* key) {
607  return purge(key);
608  }
609 
610  bool unload(const std::shared_ptr<JsonValue>& json) {
611  return purge(json);
612  }
613 
623  virtual void unloadAll() {}
624 
625 
626 #pragma mark Progress Monitoring
627 
636  bool contains(const std::string& key) const {
637  return verify(key);
638  }
639 
649  bool contains(const char* key) const {
650  return verify(std::string(key));
651 
652  }
653 
666  virtual size_t loadCount() const { return 0; }
667 
681  virtual size_t waitCount() const { return 0; }
682 
692  bool complete() const { return waitCount() == 0; }
693 
707  float progress() const {
708  size_t size = loadCount()+waitCount();
709  return (size == 0 ? 0.0f : ((float)loadCount())/size);
710  }
711 
712 };
713 
714 
715 #pragma mark -
716 #pragma mark Templated Middle Layer
717 
732 template <class T>
733 class Loader : public BaseLoader {
734 protected:
736  std::unordered_map<std::string, std::shared_ptr<T>> _assets;
737 
739  std::unordered_set<std::string> _queue;
740 
755  bool purge(const std::string& key) override {
756  auto it = _assets.find(key);
757  if (it != _assets.end()) {
758  _assets.erase(it);
759  return true;
760  }
761  return false;
762  }
763 
775  bool verify(const std::string& key) const override {
776  return _assets.find(key) != _assets.end();
777  }
778 
779 public:
780 #pragma mark Constructors
781 
788 
789 
790 #pragma mark Asset Access
791 
801  std::shared_ptr<T> get(const std::string& key) const {
802  auto it = _assets.find(key);
803  return (it == _assets.end() ? nullptr : it->second);
804  }
805 
816  std::shared_ptr<T> get(const char* key) const {
817  auto it = _assets.find(std::string(key));
818  return (it == _assets.end() ? nullptr : it->second);
819  }
820 
831  std::shared_ptr<T> operator[](const std::string& key) const { return get(key); }
832 
843  std::shared_ptr<T> operator[](const char* key) const { return get(key); }
844 
845 
846 #pragma mark Asset Loading
847 
856  size_t loadCount() const override { return _assets.size(); }
857 
868  size_t waitCount() const override { return _queue.size(); }
869 
877  void unloadAll() override {
878  _assets.clear();
879  }
880 };
881 
882 }
883 
884 
885 #endif /* __CU_LOADER_H__ */
std::shared_ptr< BaseLoader > getHook()
Definition: CULoader.h:295
std::shared_ptr< ThreadPool > _loader
Definition: CULoader.h:105
void loadAsync(const char *key, const char *source, LoaderCallback callback)
Definition: CULoader.h:547
virtual bool purge(const std::string &key)
Definition: CULoader.h:176
bool unload(const std::string &key)
Definition: CULoader.h:591
Definition: CULoader.h:98
size_t loadCount() const override
Definition: CULoader.h:856
virtual bool init()
Definition: CULoader.h:260
virtual bool read(const std::string &key, const std::string &source, LoaderCallback callback, bool async)
Definition: CULoader.h:131
virtual size_t waitCount() const
Definition: CULoader.h:681
std::shared_ptr< T > operator[](const std::string &key) const
Definition: CULoader.h:831
virtual bool verify(const std::string &key) const
Definition: CULoader.h:211
AssetManager * _manager
Definition: CULoader.h:112
bool load(const char *key, const std::string &source)
Definition: CULoader.h:390
virtual void unloadAll()
Definition: CULoader.h:623
virtual bool init(const std::shared_ptr< ThreadPool > &threads)
Definition: CULoader.h:278
bool load(const std::string &key, const char *source)
Definition: CULoader.h:410
bool load(const std::shared_ptr< JsonValue > &json)
Definition: CULoader.h:455
BaseLoader()
Definition: CULoader.h:222
std::unordered_set< std::string > _queue
Definition: CULoader.h:739
std::unordered_map< std::string, std::shared_ptr< T > > _assets
Definition: CULoader.h:736
bool unload(const char *key)
Definition: CULoader.h:606
std::shared_ptr< ThreadPool > getThreadPool() const
Definition: CULoader.h:308
void setManager(AssetManager *manager)
Definition: CULoader.h:336
virtual void dispose()
Definition: CULoader.h:243
size_t waitCount() const override
Definition: CULoader.h:868
std::shared_ptr< T > operator[](const char *key) const
Definition: CULoader.h:843
bool contains(const std::string &key) const
Definition: CULoader.h:636
virtual bool read(const std::shared_ptr< JsonValue > &json, LoaderCallback callback, bool async)
Definition: CULoader.h:158
bool purge(const std::string &key) override
Definition: CULoader.h:755
void loadAsync(const std::string &key, const char *source, LoaderCallback callback)
Definition: CULoader.h:524
bool verify(const std::string &key) const override
Definition: CULoader.h:775
virtual bool purge(const std::shared_ptr< JsonValue > &json)
Definition: CULoader.h:196
bool load(const std::string &key, const std::string &source)
Definition: CULoader.h:370
bool load(const char *key, const char *source)
Definition: CULoader.h:430
bool complete() const
Definition: CULoader.h:692
Definition: CULoader.h:733
bool contains(const char *key) const
Definition: CULoader.h:649
void loadAsync(const std::shared_ptr< JsonValue > &json, LoaderCallback callback)
Definition: CULoader.h:576
void loadAsync(const std::string &key, const std::string &source, LoaderCallback callback)
Definition: CULoader.h:478
~BaseLoader()
Definition: CULoader.h:227
const AssetManager * getManager() const
Definition: CULoader.h:348
void setThreadPool(const std::shared_ptr< ThreadPool > &threads)
Definition: CULoader.h:324
Definition: CUAssetManager.h:83
Definition: CUAction.h:51
void loadAsync(const char *key, const std::string &source, LoaderCallback callback)
Definition: CULoader.h:501
Loader()
Definition: CULoader.h:787
float progress() const
Definition: CULoader.h:707
virtual size_t loadCount() const
Definition: CULoader.h:666
void unloadAll() override
Definition: CULoader.h:877