1 #include "lottieloader.h"
2 #include "lottieparser.h"
5 #include <unordered_map>
9 class LottieFileCache {
11 static LottieFileCache &get()
13 static LottieFileCache CACHE;
17 std::shared_ptr<LOTModel> find(const std::string &key);
18 void add(const std::string &key, std::shared_ptr<LOTModel> value);
21 LottieFileCache() = default;
23 std::unordered_map<std::string, std::shared_ptr<LOTModel>> mHash;
26 std::shared_ptr<LOTModel> LottieFileCache::find(const std::string &key)
28 auto search = mHash.find(key);
29 if (search != mHash.end()) {
30 return search->second;
36 void LottieFileCache::add(const std::string &key, std::shared_ptr<LOTModel> value)
38 mHash[key] = std::move(value);
41 bool LottieLoader::load(const std::string &path)
43 LottieFileCache &fileCache = LottieFileCache::get();
45 mModel = fileCache.find(path);
46 if (mModel) return true;
52 vCritical << "failed to open file = " << path.c_str();
55 std::stringstream buf;
58 LottieParser parser(const_cast<char *>(buf.str().data()));
59 mModel = parser.model();
60 fileCache.add(path, mModel);
68 bool LottieLoader::loadFromData(const char *jsonData, const char *key)
70 LottieFileCache &fileCache = LottieFileCache::get();
71 std::string keyString(key);
73 mModel = fileCache.find(std::string(keyString));
74 if (mModel) return true;
76 LottieParser parser(const_cast<char *>(std::string(jsonData).c_str()));
77 mModel = parser.model();
78 fileCache.add(keyString, mModel);
83 std::shared_ptr<LOTModel> LottieLoader::model()