CUGL 1.3
Cornell University Game Library
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 
575  void loadAsync(const std::shared_ptr<JsonValue>& json, LoaderCallback callback) {
576  read(json, callback,true);
577  }
578 
592  bool unload(const std::string& key) {
593  return purge(key);
594  }
595 
609  bool unload(const char* key) {
610  return purge(key);
611  }
612 
626  bool unload(const std::shared_ptr<JsonValue>& json) {
627  return purge(json);
628  }
629 
639  virtual void unloadAll() {}
640 
641 
642 #pragma mark Progress Monitoring
643 
652  bool contains(const std::string& key) const {
653  return verify(key);
654  }
655 
665  bool contains(const char* key) const {
666  return verify(std::string(key));
667 
668  }
669 
682  virtual size_t loadCount() const { return 0; }
683 
697  virtual size_t waitCount() const { return 0; }
698 
708  bool complete() const { return waitCount() == 0; }
709 
723  float progress() const {
724  size_t size = loadCount()+waitCount();
725  return (size == 0 ? 0.0f : ((float)loadCount())/size);
726  }
727 
728 };
729 
730 
731 #pragma mark -
732 #pragma mark Templated Middle Layer
733 
748 template <class T>
749 class Loader : public BaseLoader {
750 protected:
752  std::unordered_map<std::string, std::shared_ptr<T>> _assets;
753 
755  std::unordered_set<std::string> _queue;
756 
771  bool purge(const std::string& key) override {
772  auto it = _assets.find(key);
773  if (it != _assets.end()) {
774  _assets.erase(it);
775  return true;
776  }
777  return false;
778  }
779 
791  bool verify(const std::string& key) const override {
792  return _assets.find(key) != _assets.end();
793  }
794 
795 public:
796 #pragma mark Constructors
797 
804 
805 
806 #pragma mark Asset Access
807 
817  std::shared_ptr<T> get(const std::string& key) const {
818  auto it = _assets.find(key);
819  return (it == _assets.end() ? nullptr : it->second);
820  }
821 
832  std::shared_ptr<T> get(const char* key) const {
833  auto it = _assets.find(std::string(key));
834  return (it == _assets.end() ? nullptr : it->second);
835  }
836 
847  std::shared_ptr<T> operator[](const std::string& key) const { return get(key); }
848 
859  std::shared_ptr<T> operator[](const char* key) const { return get(key); }
860 
861 
862 #pragma mark Asset Loading
863 
872  size_t loadCount() const override { return _assets.size(); }
873 
884  size_t waitCount() const override { return _queue.size(); }
885 
893  void unloadAll() override {
894  _assets.clear();
895  }
896 };
897 
898 }
899 
900 
901 #endif /* __CU_LOADER_H__ */
cugl::BaseLoader::load
bool load(const char *key, const std::string &source)
Definition: CULoader.h:390
cugl::Loader::loadCount
size_t loadCount() const override
Definition: CULoader.h:872
cugl::BaseLoader::getHook
std::shared_ptr< BaseLoader > getHook()
Definition: CULoader.h:295
cugl::BaseLoader::purge
virtual bool purge(const std::shared_ptr< JsonValue > &json)
Definition: CULoader.h:196
cugl::BaseLoader::complete
bool complete() const
Definition: CULoader.h:708
cugl::BaseLoader::loadAsync
void loadAsync(const std::shared_ptr< JsonValue > &json, LoaderCallback callback)
Definition: CULoader.h:575
cugl::Loader
Definition: CULoader.h:749
cugl::BaseLoader::_manager
AssetManager * _manager
Definition: CULoader.h:112
cugl::BaseLoader::BaseLoader
BaseLoader()
Definition: CULoader.h:222
cugl::BaseLoader::setThreadPool
void setThreadPool(const std::shared_ptr< ThreadPool > &threads)
Definition: CULoader.h:324
cugl::BaseLoader::init
virtual bool init(const std::shared_ptr< ThreadPool > &threads)
Definition: CULoader.h:278
cugl::BaseLoader::_loader
std::shared_ptr< ThreadPool > _loader
Definition: CULoader.h:105
cugl::BaseLoader::unload
bool unload(const std::string &key)
Definition: CULoader.h:592
cugl::Loader::waitCount
size_t waitCount() const override
Definition: CULoader.h:884
cugl::BaseLoader::getThreadPool
std::shared_ptr< ThreadPool > getThreadPool() const
Definition: CULoader.h:308
cugl::Loader::get
std::shared_ptr< T > get(const std::string &key) const
Definition: CULoader.h:817
cugl::BaseLoader::load
bool load(const std::shared_ptr< JsonValue > &json)
Definition: CULoader.h:455
cugl::Loader::operator[]
std::shared_ptr< T > operator[](const std::string &key) const
Definition: CULoader.h:847
cugl::BaseLoader::load
bool load(const std::string &key, const char *source)
Definition: CULoader.h:410
cugl::BaseLoader::loadCount
virtual size_t loadCount() const
Definition: CULoader.h:682
cugl::BaseLoader::loadAsync
void loadAsync(const char *key, const std::string &source, LoaderCallback callback)
Definition: CULoader.h:501
cugl::Loader::Loader
Loader()
Definition: CULoader.h:803
cugl::BaseLoader::~BaseLoader
~BaseLoader()
Definition: CULoader.h:227
cugl::Loader::unloadAll
void unloadAll() override
Definition: CULoader.h:893
cugl::BaseLoader::read
virtual bool read(const std::shared_ptr< JsonValue > &json, LoaderCallback callback, bool async)
Definition: CULoader.h:158
cugl::BaseLoader::dispose
virtual void dispose()
Definition: CULoader.h:243
cugl::BaseLoader::init
virtual bool init()
Definition: CULoader.h:260
cugl::BaseLoader::verify
virtual bool verify(const std::string &key) const
Definition: CULoader.h:211
cugl::Loader::get
std::shared_ptr< T > get(const char *key) const
Definition: CULoader.h:832
cugl::Loader::operator[]
std::shared_ptr< T > operator[](const char *key) const
Definition: CULoader.h:859
cugl::Loader::_assets
std::unordered_map< std::string, std::shared_ptr< T > > _assets
Definition: CULoader.h:752
cugl::BaseLoader::unloadAll
virtual void unloadAll()
Definition: CULoader.h:639
cugl::BaseLoader::load
bool load(const char *key, const char *source)
Definition: CULoader.h:430
cugl::BaseLoader::loadAsync
void loadAsync(const std::string &key, const char *source, LoaderCallback callback)
Definition: CULoader.h:524
cugl::BaseLoader::loadAsync
void loadAsync(const char *key, const char *source, LoaderCallback callback)
Definition: CULoader.h:547
cugl::BaseLoader::loadAsync
void loadAsync(const std::string &key, const std::string &source, LoaderCallback callback)
Definition: CULoader.h:478
cugl::BaseLoader::load
bool load(const std::string &key, const std::string &source)
Definition: CULoader.h:370
cugl::BaseLoader::getManager
const AssetManager * getManager() const
Definition: CULoader.h:348
cugl::Loader::verify
bool verify(const std::string &key) const override
Definition: CULoader.h:791
cugl::Loader::_queue
std::unordered_set< std::string > _queue
Definition: CULoader.h:755
cugl::BaseLoader::read
virtual bool read(const std::string &key, const std::string &source, LoaderCallback callback, bool async)
Definition: CULoader.h:131
cugl::BaseLoader::unload
bool unload(const std::shared_ptr< JsonValue > &json)
Definition: CULoader.h:626
cugl::BaseLoader::contains
bool contains(const std::string &key) const
Definition: CULoader.h:652
cugl::BaseLoader::purge
virtual bool purge(const std::string &key)
Definition: CULoader.h:176
cugl::Loader::purge
bool purge(const std::string &key) override
Definition: CULoader.h:771
cugl::BaseLoader::progress
float progress() const
Definition: CULoader.h:723
cugl::BaseLoader
Definition: CULoader.h:98
cugl::BaseLoader::waitCount
virtual size_t waitCount() const
Definition: CULoader.h:697
cugl::BaseLoader::setManager
void setManager(AssetManager *manager)
Definition: CULoader.h:336
cugl::AssetManager
Definition: CUAssetManager.h:83
cugl::BaseLoader::unload
bool unload(const char *key)
Definition: CULoader.h:609
cugl::BaseLoader::contains
bool contains(const char *key) const
Definition: CULoader.h:665