2 * Copyright (c) 2018 Samsung Electronics Co., Ltd. All rights reserved.
4 * Licensed under the Flora License, Version 1.1 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://floralicense.org/license/
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 #include "lottieloader.h"
18 #include "lottieparser.h"
21 #include <unordered_map>
25 class LottieFileCache {
27 static LottieFileCache &get()
29 static LottieFileCache CACHE;
33 std::shared_ptr<LOTModel> find(const std::string &key);
34 void add(const std::string &key, std::shared_ptr<LOTModel> value);
37 LottieFileCache() = default;
39 std::unordered_map<std::string, std::shared_ptr<LOTModel>> mHash;
42 std::shared_ptr<LOTModel> LottieFileCache::find(const std::string &key)
44 auto search = mHash.find(key);
45 if (search != mHash.end()) {
46 return search->second;
52 void LottieFileCache::add(const std::string &key, std::shared_ptr<LOTModel> value)
54 mHash[key] = std::move(value);
57 bool LottieLoader::load(const std::string &path)
59 LottieFileCache &fileCache = LottieFileCache::get();
61 mModel = fileCache.find(path);
62 if (mModel) return true;
68 vCritical << "failed to open file = " << path.c_str();
71 std::stringstream buf;
74 LottieParser parser(const_cast<char *>(buf.str().data()));
75 mModel = parser.model();
76 fileCache.add(path, mModel);
84 bool LottieLoader::loadFromData(std::string &&jsonData, const std::string &key)
86 LottieFileCache &fileCache = LottieFileCache::get();
88 mModel = fileCache.find(key);
89 if (mModel) return true;
91 LottieParser parser(const_cast<char *>(jsonData.c_str()));
92 mModel = parser.model();
93 fileCache.add(key, mModel);
98 std::shared_ptr<LOTModel> LottieLoader::model()