CUGL
Cornell University Game Library
CUJsonLoader.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 zlib 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: Walker White
43 // Version: 1/7/16
44 //
45 #ifndef __CU_JSON_LOADER_H__
46 #define __CU_JSON_LOADER_H__
47 #include <cugl/assets/CULoader.h>
48 #include <cugl/assets/CUJsonValue.h>
49 
50 namespace cugl {
51 
63 class JsonLoader : public Loader<JsonValue> {
64 private:
66  CU_DISALLOW_COPY_AND_ASSIGN(JsonLoader);
67 
68 protected:
82  void materialize(const std::string& key, const std::shared_ptr<JsonValue>& json,
83  LoaderCallback callback);
84 
102  virtual bool read(const std::string& key, const std::string& source,
103  LoaderCallback callback, bool async) override;
104 
125  virtual bool read(const std::shared_ptr<JsonValue>& json,
126  LoaderCallback callback, bool async) override;
127 
128 public:
129 #pragma mark -
130 #pragma mark Constructors
131 
138 
149  void dispose() override {
150  _assets.clear();
151  _loader = nullptr;
152  }
153 
165  static std::shared_ptr<JsonLoader> alloc() {
166  std::shared_ptr<JsonLoader> result = std::make_shared<JsonLoader>();
167  return (result->init() ? result : nullptr);
168  }
169 
183  static std::shared_ptr<JsonLoader> alloc(const std::shared_ptr<ThreadPool>& threads) {
184  std::shared_ptr<JsonLoader> result = std::make_shared<JsonLoader>();
185  return (result->init(threads) ? result : nullptr);
186  }
187 };
188 
189 }
190 
191 #endif /* __CU_JSON_LOADER_H__ */
void materialize(const std::string &key, const std::shared_ptr< JsonValue > &json, LoaderCallback callback)
std::shared_ptr< ThreadPool > _loader
Definition: CULoader.h:102
static std::shared_ptr< JsonLoader > alloc(const std::shared_ptr< ThreadPool > &threads)
Definition: CUJsonLoader.h:183
JsonLoader()
Definition: CUJsonLoader.h:137
virtual bool read(const std::string &key, const std::string &source, LoaderCallback callback, bool async) override
Definition: CUJsonLoader.h:63
std::unordered_map< std::string, std::shared_ptr< JsonValue > > _assets
Definition: CULoader.h:678
std::function< void(const std::string &key, bool success)> LoaderCallback
Definition: CULoader.h:81
static std::shared_ptr< JsonLoader > alloc()
Definition: CUJsonLoader.h:165
Definition: CULoader.h:675
void dispose() override
Definition: CUJsonLoader.h:149
Definition: CUAnimationNode.h:52