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(std::string &&jsonData, const std::string &key)
70 LottieFileCache &fileCache = LottieFileCache::get();
72 mModel = fileCache.find(key);
73 if (mModel) return true;
75 LottieParser parser(const_cast<char *>(jsonData.c_str()));
76 mModel = parser.model();
77 fileCache.add(key, mModel);
82 std::shared_ptr<LOTModel> LottieLoader::model()