rlottie: Add LOTTIE_CACHE_SUPPORT config variable to enable/disable lottie model...
authorsub.mohanty@samsung.com <smohantty@gmail.com>
Sun, 23 Jun 2019 03:25:25 +0000 (12:25 +0900)
committerHermet Park <hermetpark@gmail.com>
Tue, 25 Jun 2019 11:58:51 +0000 (20:58 +0900)
src/lottie/lottieloader.cpp
src/vector/config.h

index 7aab52585f71346a7e964b48c451bb72fc3eafdc..34c20ac8575243bc0686cb609e6d3d0b282ee852 100644 (file)
 #include <cstring>
 using namespace std;
 
+#ifdef LOTTIE_CACHE_SUPPORT
+
 class LottieFileCache {
 public:
-    static LottieFileCache &get()
+    static LottieFileCache &instance()
     {
         static LottieFileCache CACHE;
-
         return CACHE;
     }
-    std::shared_ptr<LOTModel> find(const std::string &key);
-    void add(const std::string &key, std::shared_ptr<LOTModel> value);
+    std::shared_ptr<LOTModel> find(const std::string &key)
+    {
+        auto search = mHash.find(key);
+        if (search != mHash.end()) {
+            return search->second;
+        } else {
+            return nullptr;
+        }
+    }
+    void add(const std::string &key, std::shared_ptr<LOTModel> value)
+    {
+        mHash[key] = std::move(value);
+    }
 
 private:
     LottieFileCache() = default;
@@ -41,20 +53,25 @@ private:
     std::unordered_map<std::string, std::shared_ptr<LOTModel>> mHash;
 };
 
-std::shared_ptr<LOTModel> LottieFileCache::find(const std::string &key)
-{
-    auto search = mHash.find(key);
-    if (search != mHash.end()) {
-        return search->second;
-    } else {
+#else
+
+class LottieFileCache {
+public:
+    static LottieFileCache &instance()
+    {
+        static LottieFileCache CACHE;
+        return CACHE;  
+    }
+    std::shared_ptr<LOTModel> find(const std::string &)
+    {
         return nullptr;
     }
-}
+    void add(const std::string &, std::shared_ptr<LOTModel>) {}
+};
+
+#endif
+
 
-void LottieFileCache::add(const std::string &key, std::shared_ptr<LOTModel> value)
-{
-    mHash[key] = std::move(value);
-}
 
 static std::string dirname(const std::string &path)
 {
@@ -65,9 +82,7 @@ static std::string dirname(const std::string &path)
 
 bool LottieLoader::load(const std::string &path)
 {
-    LottieFileCache &fileCache = LottieFileCache::get();
-
-    mModel = fileCache.find(path);
+    mModel = LottieFileCache::instance().find(path);
     if (mModel) return true;
 
     std::ifstream f;
@@ -82,7 +97,7 @@ bool LottieLoader::load(const std::string &path)
 
         LottieParser parser(const_cast<char *>(buf.str().data()), dirname(path).c_str());
         mModel = parser.model();
-        fileCache.add(path, mModel);
+        LottieFileCache::instance().add(path, mModel);
 
         f.close();
     }
@@ -92,14 +107,12 @@ bool LottieLoader::load(const std::string &path)
 
 bool LottieLoader::loadFromData(std::string &&jsonData, const std::string &key, const std::string &resourcePath)
 {
-    LottieFileCache &fileCache = LottieFileCache::get();
-
-    mModel = fileCache.find(key);
+    mModel = LottieFileCache::instance().find(key);
     if (mModel) return true;
 
     LottieParser parser(const_cast<char *>(jsonData.c_str()), resourcePath.c_str());
     mModel = parser.model();
-    fileCache.add(key, mModel);
+    LottieFileCache::instance().add(key, mModel);
 
     return true;
 }
index 6ac91599df832959008e3c36e25eb7e28f7a429e..b62bfb15788d4faea3144cc65a915f5be4b0da37 100644 (file)
@@ -10,4 +10,7 @@
 //enable static building of image loader
 //#define LOTTIE_STATIC_IMAGE_LOADER
 
+//enable lottie model caching
+#define LOTTIE_CACHE_SUPPORT
+
 #endif  // CONFIG_H