[poppler] poppler/UnicodeMap.cc poppler/UnicodeMap.h

GitLab Mirror gitlab-mirror at kemper.freedesktop.org
Thu Feb 10 11:26:52 UTC 2022


 poppler/UnicodeMap.cc |   26 ++++++++++----------------
 poppler/UnicodeMap.h  |    9 +++------
 2 files changed, 13 insertions(+), 22 deletions(-)

New commits:
commit 88f158d3fd1e298ef425fafe84456f67c7f9a17c
Author: Albert Astals Cid <aacid at kde.org>
Date:   Thu Feb 10 11:49:26 2022 +0100

    Make UnicodeMap::parse return an unique_ptr

diff --git a/poppler/UnicodeMap.cc b/poppler/UnicodeMap.cc
index f92c4fb4..380a8c2b 100644
--- a/poppler/UnicodeMap.cc
+++ b/poppler/UnicodeMap.cc
@@ -51,10 +51,9 @@ struct UnicodeMapExt
 
 //------------------------------------------------------------------------
 
-UnicodeMap *UnicodeMap::parse(const std::string &encodingNameA)
+std::unique_ptr<UnicodeMap> UnicodeMap::parse(const std::string &encodingNameA)
 {
     FILE *f;
-    UnicodeMap *map;
     UnicodeMapRange *range;
     UnicodeMapExt *eMap;
     int size, eMapsSize;
@@ -65,10 +64,10 @@ UnicodeMap *UnicodeMap::parse(const std::string &encodingNameA)
 
     if (!(f = globalParams->getUnicodeMapFile(encodingNameA))) {
         error(errSyntaxError, -1, "Couldn't find unicodeMap file for the '{0:s}' encoding", encodingNameA.c_str());
-        return nullptr;
+        return {};
     }
 
-    map = new UnicodeMap(encodingNameA);
+    auto map = std::unique_ptr<UnicodeMap>(new UnicodeMap(encodingNameA));
 
     size = 8;
     UnicodeMapRange *customRanges = (UnicodeMapRange *)gmallocn(size, sizeof(UnicodeMapRange));
@@ -285,23 +284,18 @@ int UnicodeMap::mapUnicode(Unicode u, char *buf, int bufSize) const
 
 UnicodeMapCache::UnicodeMapCache() { }
 
-UnicodeMapCache::~UnicodeMapCache()
-{
-    for (UnicodeMap *map : cache) {
-        delete map;
-    }
-}
-
 const UnicodeMap *UnicodeMapCache::getUnicodeMap(const std::string &encodingName)
 {
-    for (UnicodeMap *map : cache) {
+    for (const std::unique_ptr<UnicodeMap> &map : cache) {
         if (map->match(encodingName)) {
-            return map;
+            return map.get();
         }
     }
-    UnicodeMap *map = UnicodeMap::parse(encodingName);
+    std::unique_ptr<UnicodeMap> map = UnicodeMap::parse(encodingName);
     if (map) {
-        cache.emplace_back(map);
+        UnicodeMap *m = map.get();
+        cache.emplace_back(std::move(map));
+        return m;
     }
-    return map;
+    return nullptr;
 }
diff --git a/poppler/UnicodeMap.h b/poppler/UnicodeMap.h
index c04d2d12..8ac40ded 100644
--- a/poppler/UnicodeMap.h
+++ b/poppler/UnicodeMap.h
@@ -33,6 +33,7 @@
 #include "CharTypes.h"
 
 #include <atomic>
+#include <memory>
 #include <string>
 #include <vector>
 
@@ -62,7 +63,7 @@ class POPPLER_PRIVATE_EXPORT UnicodeMap
 public:
     // Create the UnicodeMap specified by <encodingName>.  Sets the
     // initial reference count to 1.  Returns NULL on failure.
-    static UnicodeMap *parse(const std::string &encodingNameA);
+    static std::unique_ptr<UnicodeMap> parse(const std::string &encodingNameA);
 
     // Create a resident UnicodeMap.
     UnicodeMap(const char *encodingNameA, bool unicodeOutA, const UnicodeMapRange *rangesA, int lenA);
@@ -116,16 +117,12 @@ class UnicodeMapCache
 {
 public:
     UnicodeMapCache();
-    ~UnicodeMapCache();
-
-    UnicodeMapCache(const UnicodeMapCache &) = delete;
-    UnicodeMapCache &operator=(const UnicodeMapCache &) = delete;
 
     // Get the UnicodeMap for <encodingName>.  Returns NULL on failure.
     const UnicodeMap *getUnicodeMap(const std::string &encodingName);
 
 private:
-    std::vector<UnicodeMap *> cache;
+    std::vector<std::unique_ptr<UnicodeMap>> cache;
 };
 
 #endif


More information about the poppler mailing list