CUGL 1.3
Cornell University Game Library
All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends
CUWidgetLoader.h
1 //
2 // CUJsonLoader.h
3 // Cornell University Game Library (CUGL)
4 //
5 // This module provides a specific implementation of the Loader class to load
6 // (non-directory) json assets. It is essentially a wrapper around JsonReader
7 // that allows it to be used with AssetManager.
8 //
9 // As with all of our loaders, this loader is designed to be attached to an
10 // asset manager. In addition, this class uses our standard shared-pointer
11 // architecture.
12 //
13 // 1. The constructor does not perform any initialization; it just sets all
14 // attributes to their defaults.
15 //
16 // 2. All initialization takes place via init methods, which can fail if an
17 // object is initialized more than once.
18 //
19 // 3. All allocation takes place via static constructors which return a shared
20 // pointer.
21 //
22 //
23 // CUGL MIT License:
24 // This software is provided 'as-is', without any express or implied
25 // warranty. In no event will the authors be held liable for any damages
26 // arising from the use of this software.
27 //
28 // Permission is granted to anyone to use this software for any purpose,
29 // including commercial applications, and to alter it and redistribute it
30 // freely, subject to the following restrictions:
31 //
32 // 1. The origin of this software must not be misrepresented; you must not
33 // claim that you wrote the original software. If you use this software
34 // in a product, an acknowledgment in the product documentation would be
35 // appreciated but is not required.
36 //
37 // 2. Altered source versions must be plainly marked as such, and must not
38 // be misrepresented as being the original software.
39 //
40 // 3. This notice may not be removed or altered from any source distribution.
41 //
42 // Author: Graham Rutledge
43 // Version: 5/20/19
44 //
45 #ifndef __CU_WIDGET_LOADER_H__
46 #define __CU_WIDGET_LOADER_H__
47 #include <cugl/assets/CULoader.h>
48 #include <cugl/assets/CUJsonLoader.h>
49 #include <cugl/assets/CUJsonValue.h>
50 #include <cugl/assets/CUWidgetValue.h>
51 
52 namespace cugl {
53 
65 class WidgetLoader : public Loader<WidgetValue> {
66 private:
68  CU_DISALLOW_COPY_AND_ASSIGN(WidgetLoader);
69 
70 protected:
84  void materialize(const std::string& key, const std::shared_ptr<WidgetValue>& widget,
85  LoaderCallback callback);
86 
104  virtual bool read(const std::string& key, const std::string& source,
105  LoaderCallback callback, bool async) override;
106 
127  virtual bool read(const std::shared_ptr<JsonValue>& json,
128  LoaderCallback callback, bool async) override;
129 
130 public:
131 #pragma mark -
132 #pragma mark Constructors
133 
140 
151  void dispose() override {
152  _assets.clear();
153  _loader = nullptr;
154  }
155 
167  static std::shared_ptr<WidgetLoader> alloc() {
168  std::shared_ptr<WidgetLoader> result = std::make_shared<WidgetLoader>();
169  return (result->init() ? result : nullptr);
170  }
171 
185  static std::shared_ptr<WidgetLoader> alloc(const std::shared_ptr<ThreadPool>& threads) {
186  std::shared_ptr<WidgetLoader> result = std::make_shared<WidgetLoader>();
187  return (result->init(threads) ? result : nullptr);
188  }
189 };
190 
191 }
192 
193 #endif /* __CU_WIDGET_LOADER_H__ */
cugl::Loader
Definition: CULoader.h:749
cugl::BaseLoader::_loader
std::shared_ptr< ThreadPool > _loader
Definition: CULoader.h:105
cugl::WidgetLoader::read
virtual bool read(const std::string &key, const std::string &source, LoaderCallback callback, bool async) override
cugl::WidgetLoader::WidgetLoader
WidgetLoader()
Definition: CUWidgetLoader.h:139
cugl::WidgetLoader::alloc
static std::shared_ptr< WidgetLoader > alloc()
Definition: CUWidgetLoader.h:167
cugl::Loader< WidgetValue >::_assets
std::unordered_map< std::string, std::shared_ptr< WidgetValue > > _assets
Definition: CULoader.h:752
cugl::WidgetLoader
Definition: CUWidgetLoader.h:65
cugl::WidgetLoader::materialize
void materialize(const std::string &key, const std::shared_ptr< WidgetValue > &widget, LoaderCallback callback)
cugl::WidgetLoader::alloc
static std::shared_ptr< WidgetLoader > alloc(const std::shared_ptr< ThreadPool > &threads)
Definition: CUWidgetLoader.h:185
cugl::WidgetLoader::dispose
void dispose() override
Definition: CUWidgetLoader.h:151