CUGL
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 zlib 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/16
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 
81 typedef std::function<void(const std::string& key, bool success)> LoaderCallback;
82 
83 #pragma mark -
84 #pragma mark Polymorphic Base
85 
95 class BaseLoader : public std::enable_shared_from_this<BaseLoader> {
96 protected:
102  std::shared_ptr<ThreadPool> _loader;
103 
121  virtual bool read(const std::string& key, const std::string& source,
122  LoaderCallback callback, bool async) {
123  return false;
124  }
125 
148  virtual bool read(const std::shared_ptr<JsonValue>& json,
149  LoaderCallback callback, bool async) {
150  return false;
151  }
152 
167  virtual bool purge(const std::string& key) {
168  return false;
169  }
170 
182  virtual bool verify(const std::string& key) const { return false; }
183 
184 
185 public:
186 #pragma mark Constructors
187 
194 
199 
214  virtual void dispose() { }
215 
231  virtual bool init() {
232  return init(nullptr);
233  }
234 
249  virtual bool init(const std::shared_ptr<ThreadPool>& threads) {
250  _loader=threads;
251  return true;
252  }
253 
254 #pragma mark AssetManager Support
255 
266  std::shared_ptr<BaseLoader> getHook() {
267  return shared_from_this();
268  }
269 
279  std::shared_ptr<ThreadPool> getThreadPool() const { return _loader; }
280 
295  void setThreadPool(const std::shared_ptr<ThreadPool>& threads) {
296  _loader = threads;
297  }
298 
299 #pragma mark Loading/Unloading
300 
316  bool load(const std::string& key, const std::string& source) {
317  return read(key,source,nullptr,false);
318  }
319 
336  bool load(const char* key, const std::string& source) {
337  return read(std::string(key),source,nullptr,false);
338  }
339 
356  bool load(const std::string& key, const char* source) {
357  return read(key,std::string(source),nullptr,false);
358  }
359 
376  bool load(const char* key, const char* source) {
377  return read(std::string(key),std::string(source),nullptr,false);
378  }
379 
401  bool load(const std::shared_ptr<JsonValue>& json) {
402  return read(json,nullptr,false);
403  }
404 
424  void loadAsync(const std::string& key, const std::string& source, LoaderCallback callback) {
425  read(key, source, callback,true);
426  }
427 
447  void loadAsync(const char* key, const std::string& source, LoaderCallback callback) {
448  read(std::string(key), source, callback,true);
449  }
450 
470  void loadAsync(const std::string& key, const char* source, LoaderCallback callback) {
471  read(key, std::string(source), callback,true);
472  }
473 
493  void loadAsync(const char* key, const char* source, LoaderCallback callback) {
494  read(std::string(key), std::string(source), callback,true);
495  }
496 
522  void loadAsync(const std::shared_ptr<JsonValue>& json, LoaderCallback callback) {
523  read(json, callback,true);
524  }
525 
537  bool unload(const std::string& key) {
538  return purge(key);
539  }
540 
552  bool unload(const char* key) {
553  return purge(key);
554  }
555 
565  virtual void unloadAll() {}
566 
567 
568 #pragma mark Progress Monitoring
569 
578  bool contains(const std::string& key) const {
579  return verify(key);
580  }
581 
591  bool contains(const char* key) const {
592  return verify(std::string(key));
593 
594  }
595 
608  virtual size_t loadCount() const { return 0; }
609 
623  virtual size_t waitCount() const { return 0; }
624 
634  bool complete() const { return waitCount() == 0; }
635 
649  float progress() const {
650  size_t size = loadCount()+waitCount();
651  return (size == 0 ? 0.0f : ((float)loadCount())/size);
652  }
653 
654 };
655 
656 
657 #pragma mark -
658 #pragma mark Templated Middle Layer
659 
674 template <class T>
675 class Loader : public BaseLoader {
676 protected:
678  std::unordered_map<std::string, std::shared_ptr<T>> _assets;
679 
681  std::unordered_set<std::string> _queue;
682 
697  bool purge(const std::string& key) override {
698  auto it = _assets.find(key);
699  if (it != _assets.end()) {
700  _assets.erase(it);
701  return true;
702  }
703  return false;
704  }
705 
717  bool verify(const std::string& key) const override {
718  return _assets.find(key) != _assets.end();
719  }
720 
721 public:
722 #pragma mark Constructors
723 
730 
731 
732 #pragma mark Asset Access
733 
743  std::shared_ptr<T> get(const std::string& key) const {
744  auto it = _assets.find(key);
745  return (it == _assets.end() ? nullptr : it->second);
746  }
747 
758  std::shared_ptr<T> get(const char* key) const {
759  auto it = _assets.find(std::string(key));
760  return (it == _assets.end() ? nullptr : it->second);
761  }
762 
773  std::shared_ptr<T> operator[](const std::string& key) const { return get(key); }
774 
785  std::shared_ptr<T> operator[](const char* key) const { return get(key); }
786 
787 
788 #pragma mark Asset Loading
789 
798  size_t loadCount() const override { return _assets.size(); }
799 
810  size_t waitCount() const override { return _queue.size(); }
811 
819  void unloadAll() override {
820  _assets.clear();
821  }
822 };
823 
824 }
825 
826 
827 #endif /* __CU_LOADER_H__ */
std::shared_ptr< BaseLoader > getHook()
Definition: CULoader.h:266
std::shared_ptr< ThreadPool > _loader
Definition: CULoader.h:102
void loadAsync(const char *key, const char *source, LoaderCallback callback)
Definition: CULoader.h:493
virtual bool purge(const std::string &key)
Definition: CULoader.h:167
bool unload(const std::string &key)
Definition: CULoader.h:537
Definition: CULoader.h:95
size_t loadCount() const override
Definition: CULoader.h:798
virtual bool init()
Definition: CULoader.h:231
virtual bool read(const std::string &key, const std::string &source, LoaderCallback callback, bool async)
Definition: CULoader.h:121
virtual size_t waitCount() const
Definition: CULoader.h:623
std::shared_ptr< T > operator[](const std::string &key) const
Definition: CULoader.h:773
virtual bool verify(const std::string &key) const
Definition: CULoader.h:182
bool load(const char *key, const std::string &source)
Definition: CULoader.h:336
virtual void unloadAll()
Definition: CULoader.h:565
virtual bool init(const std::shared_ptr< ThreadPool > &threads)
Definition: CULoader.h:249
bool load(const std::string &key, const char *source)
Definition: CULoader.h:356
bool load(const std::shared_ptr< JsonValue > &json)
Definition: CULoader.h:401
BaseLoader()
Definition: CULoader.h:193
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
bool unload(const char *key)
Definition: CULoader.h:552
std::shared_ptr< ThreadPool > getThreadPool() const
Definition: CULoader.h:279
virtual void dispose()
Definition: CULoader.h:214
size_t waitCount() const override
Definition: CULoader.h:810
std::shared_ptr< T > operator[](const char *key) const
Definition: CULoader.h:785
bool contains(const std::string &key) const
Definition: CULoader.h:578
virtual bool read(const std::shared_ptr< JsonValue > &json, LoaderCallback callback, bool async)
Definition: CULoader.h:148
bool purge(const std::string &key) override
Definition: CULoader.h:697
void loadAsync(const std::string &key, const char *source, LoaderCallback callback)
Definition: CULoader.h:470
bool verify(const std::string &key) const override
Definition: CULoader.h:717
bool load(const std::string &key, const std::string &source)
Definition: CULoader.h:316
bool load(const char *key, const char *source)
Definition: CULoader.h:376
bool complete() const
Definition: CULoader.h:634
Definition: CULoader.h:675
bool contains(const char *key) const
Definition: CULoader.h:591
void loadAsync(const std::shared_ptr< JsonValue > &json, LoaderCallback callback)
Definition: CULoader.h:522
void loadAsync(const std::string &key, const std::string &source, LoaderCallback callback)
Definition: CULoader.h:424
~BaseLoader()
Definition: CULoader.h:198
void setThreadPool(const std::shared_ptr< ThreadPool > &threads)
Definition: CULoader.h:295
Definition: CUAnimationNode.h:52
void loadAsync(const char *key, const std::string &source, LoaderCallback callback)
Definition: CULoader.h:447
Loader()
Definition: CULoader.h:729
float progress() const
Definition: CULoader.h:649
virtual size_t loadCount() const
Definition: CULoader.h:608
void unloadAll() override
Definition: CULoader.h:819