diff options
author | Oliver Sander <oliver.sander@tu-dresden.de> | 2020-02-14 23:37:35 +0100 |
---|---|---|
committer | Oliver Sander <oliver.sander@tu-dresden.de> | 2020-02-15 08:57:20 +0100 |
commit | ef5a9f53959d71be273fb9ee0956f516f6d3d39c (patch) | |
tree | 3d2bd8e819dd5001779460c04a11e4052fadd59d | |
parent | 5539aeba90d8e5b2ea78444495deaefc900d299c (diff) |
Use std::unique_ptr to pass around LinkDest objects
This provides no functional changes, but it makes the pointer
ownership easier to understand.
-rw-r--r-- | cpp/poppler-document.cpp | 12 | ||||
-rw-r--r-- | glib/poppler-document.cc | 16 | ||||
-rw-r--r-- | poppler/Catalog.cc | 17 | ||||
-rw-r--r-- | poppler/Catalog.h | 10 | ||||
-rw-r--r-- | poppler/PDFDoc.h | 2 | ||||
-rw-r--r-- | qt5/src/poppler-link.cc | 2 | ||||
-rw-r--r-- | utils/HtmlOutputDev.cc | 11 | ||||
-rw-r--r-- | utils/pdfinfo.cc | 17 |
8 files changed, 38 insertions, 49 deletions
diff --git a/cpp/poppler-document.cpp b/cpp/poppler-document.cpp index 9c7dc03b..1a0d9e6f 100644 --- a/cpp/poppler-document.cpp +++ b/cpp/poppler-document.cpp @@ -1010,14 +1010,12 @@ std::map<std::string, destination> document::create_destination_map() const const int nDests = catalog->numDests(); for (int i = 0; i < nDests; ++i ) { std::string key(catalog->getDestsName (i)); - LinkDest *link_dest = catalog->getDestsDest (i); + std::unique_ptr<LinkDest> link_dest = catalog->getDestsDest (i); if (link_dest) { - destination dest(new destination_private(link_dest, d->doc)); + destination dest(new destination_private(link_dest.get(), d->doc)); m.emplace(std::move(key), std::move(dest)); - - delete link_dest; } } @@ -1026,14 +1024,12 @@ std::map<std::string, destination> document::create_destination_map() const for (int i = 0; i < nDestsNameTree; ++i ) { std::string key(catalog->getDestNameTreeName (i)->c_str (), catalog->getDestNameTreeName (i)->getLength ()); - LinkDest *link_dest = catalog->getDestNameTreeDest (i); + std::unique_ptr<LinkDest> link_dest = catalog->getDestNameTreeDest (i); if (link_dest) { - destination dest(new destination_private(link_dest, d->doc)); + destination dest(new destination_private(link_dest.get(), d->doc)); m.emplace(std::move(key), std::move(dest)); - - delete link_dest; } } diff --git a/glib/poppler-document.cc b/glib/poppler-document.cc index 37038ff3..9a767df1 100644 --- a/glib/poppler-document.cc +++ b/glib/poppler-document.cc @@ -887,12 +887,11 @@ poppler_document_find_dest (PopplerDocument *document, GooString g_link_name ((const char*)data, (int)len); g_free (data); - LinkDest *link_dest = document->doc->findDest (&g_link_name); + std::unique_ptr<LinkDest> link_dest = document->doc->findDest (&g_link_name); if (link_dest == nullptr) return nullptr; - PopplerDest *dest = _poppler_dest_new_goto (document, link_dest); - delete link_dest; + PopplerDest *dest = _poppler_dest_new_goto (document, link_dest.get()); return dest; } @@ -931,7 +930,6 @@ poppler_document_create_dests_tree (PopplerDocument *document) { GTree *tree; Catalog *catalog; - LinkDest *link_dest; PopplerDest *dest; int i; gchar *key; @@ -955,10 +953,9 @@ poppler_document_create_dests_tree (PopplerDocument *document) key = poppler_named_dest_from_bytestring (reinterpret_cast<const guint8*> (name), strlen (name)); - link_dest = catalog->getDestsDest (i); + std::unique_ptr<LinkDest> link_dest = catalog->getDestsDest (i); if (link_dest) { - dest = _poppler_dest_new_goto (document, link_dest); - delete link_dest; + dest = _poppler_dest_new_goto (document, link_dest.get()); g_tree_insert (tree, key, dest); } } @@ -970,10 +967,9 @@ poppler_document_create_dests_tree (PopplerDocument *document) key = poppler_named_dest_from_bytestring (reinterpret_cast<const guint8*> (name->c_str ()), name->getLength ()); - link_dest = catalog->getDestNameTreeDest (i); + std::unique_ptr<LinkDest> link_dest = catalog->getDestNameTreeDest (i); if (link_dest) { - dest = _poppler_dest_new_goto (document, link_dest); - delete link_dest; + dest = _poppler_dest_new_goto (document, link_dest.get()); g_tree_insert (tree, key, dest); } } diff --git a/poppler/Catalog.cc b/poppler/Catalog.cc index ba4ec060..064fdcdd 100644 --- a/poppler/Catalog.cc +++ b/poppler/Catalog.cc @@ -337,7 +337,7 @@ int Catalog::findPage(const Ref pageRef) { return 0; } -LinkDest *Catalog::findDest(const GooString *name) { +std::unique_ptr<LinkDest> Catalog::findDest(const GooString *name) { // try named destination dictionary then name tree if (getDests()->isDict()) { Object obj1 = getDests()->dictLookup(name->c_str()); @@ -349,23 +349,22 @@ LinkDest *Catalog::findDest(const GooString *name) { return createLinkDest(&obj2); } -LinkDest *Catalog::createLinkDest(Object *obj) +std::unique_ptr<LinkDest> Catalog::createLinkDest(Object *obj) { - LinkDest *dest = nullptr; + std::unique_ptr<LinkDest> dest; if (obj->isArray()) { - dest = new LinkDest(obj->getArray()); + dest = std::make_unique<LinkDest>(obj->getArray()); } else if (obj->isDict()) { Object obj2 = obj->dictLookup("D"); if (obj2.isArray()) - dest = new LinkDest(obj2.getArray()); + dest = std::make_unique<LinkDest>(obj2.getArray()); else error(errSyntaxWarning, -1, "Bad named destination value"); } else { error(errSyntaxWarning, -1, "Bad named destination value"); } if (dest && !dest->isOk()) { - delete dest; - dest = nullptr; + dest.reset(); } return dest; @@ -393,7 +392,7 @@ const char *Catalog::getDestsName(int i) return obj->dictGetKey(i); } -LinkDest *Catalog::getDestsDest(int i) +std::unique_ptr<LinkDest> Catalog::getDestsDest(int i) { Object *obj = getDests(); if (!obj->isDict()) { @@ -403,7 +402,7 @@ LinkDest *Catalog::getDestsDest(int i) return createLinkDest(&obj1); } -LinkDest *Catalog::getDestNameTreeDest(int i) +std::unique_ptr<LinkDest> Catalog::getDestNameTreeDest(int i) { Object obj; diff --git a/poppler/Catalog.h b/poppler/Catalog.h index a3a48f87..921e96d2 100644 --- a/poppler/Catalog.h +++ b/poppler/Catalog.h @@ -43,6 +43,8 @@ #include <vector> #include <memory> +#include <poppler/Link.h> + class PDFDoc; class XRef; class Object; @@ -150,7 +152,7 @@ public: // Find a named destination. Returns the link destination, or // NULL if <name> is not a destination. - LinkDest *findDest(const GooString *name); + std::unique_ptr<LinkDest> findDest(const GooString *name); Object *getDests(); @@ -161,7 +163,7 @@ public: const char *getDestsName(int i); // Get the i'th named destination link destination in name-dict - LinkDest *getDestsDest(int i); + std::unique_ptr<LinkDest> getDestsDest(int i); // Get the number of named destinations in name-tree int numDestNameTree() { return getDestNameTree()->numEntries(); } @@ -170,7 +172,7 @@ public: GooString *getDestNameTreeName(int i) { return getDestNameTree()->getName(i); } // Get the i'th named destination link destination in name-tree - LinkDest *getDestNameTreeDest(int i); + std::unique_ptr<LinkDest> getDestNameTreeDest(int i); // Get the number of embedded files int numEmbeddedFiles() { return getEmbeddedFileNameTree()->numEntries(); } @@ -290,7 +292,7 @@ private: NameTree *getDestNameTree(); NameTree *getEmbeddedFileNameTree(); NameTree *getJSNameTree(); - LinkDest *createLinkDest(Object *obj); + std::unique_ptr<LinkDest> createLinkDest(Object *obj); mutable std::recursive_mutex mutex; }; diff --git a/poppler/PDFDoc.h b/poppler/PDFDoc.h index 85fc6130..d856744f 100644 --- a/poppler/PDFDoc.h +++ b/poppler/PDFDoc.h @@ -226,7 +226,7 @@ public: // Find a named destination. Returns the link destination, or // nullptr if <name> is not a destination. - LinkDest *findDest(const GooString *name) + std::unique_ptr<LinkDest> findDest(const GooString *name) { return catalog->findDest(name); } // Process the links for a page. diff --git a/qt5/src/poppler-link.cc b/qt5/src/poppler-link.cc index 436cd4bb..13862a4d 100644 --- a/qt5/src/poppler-link.cc +++ b/qt5/src/poppler-link.cc @@ -234,7 +234,7 @@ class LinkMoviePrivate : public LinkPrivate if ( data.namedDest && !ld && !data.externalDest ) { deleteDest = true; - ld = data.doc->doc->findDest( data.namedDest ); + ld = data.doc->doc->findDest( data.namedDest ).release(); } // in case this destination was named one, and it was not resolved if ( data.namedDest && !ld ) diff --git a/utils/HtmlOutputDev.cc b/utils/HtmlOutputDev.cc index b8a69e34..ff638ee0 100644 --- a/utils/HtmlOutputDev.cc +++ b/utils/HtmlOutputDev.cc @@ -1549,9 +1549,9 @@ GooString* HtmlOutputDev::getLinkDest(AnnotLink *link){ GooString* file = new GooString(gbasename(Docname->c_str())); int destPage=1; LinkGoTo *ha=(LinkGoTo *)link->getAction(); - LinkDest *dest=nullptr; + std::unique_ptr<LinkDest> dest; if (ha->getDest()!=nullptr) - dest=ha->getDest()->copy(); + dest=std::unique_ptr<LinkDest>(ha->getDest()->copy()); else if (ha->getNamedDest()!=nullptr) dest=catalog->findDest(ha->getNamedDest()); @@ -1564,8 +1564,6 @@ GooString* HtmlOutputDev::getLinkDest(AnnotLink *link){ destPage=dest->getPageNum(); } - delete dest; - /* complex simple frames file-4.html files.html#4 noframes file.html#4 file.html#4 @@ -1834,7 +1832,7 @@ int HtmlOutputDev::getOutlinePageNum(OutlineItem *item) { const LinkAction *action = item->getAction(); const LinkGoTo *link = nullptr; - LinkDest *linkdest = nullptr; + std::unique_ptr<LinkDest> linkdest; int pagenum = -1; if (!action || action->getKind() != actionGoTo) @@ -1846,7 +1844,7 @@ int HtmlOutputDev::getOutlinePageNum(OutlineItem *item) return pagenum; if (link->getDest()) - linkdest = link->getDest()->copy(); + linkdest = std::unique_ptr<LinkDest>(link->getDest()->copy()); else if (link->getNamedDest()) linkdest = catalog->findDest(link->getNamedDest()); @@ -1860,6 +1858,5 @@ int HtmlOutputDev::getOutlinePageNum(OutlineItem *item) pagenum = linkdest->getPageNum(); } - delete linkdest; return pagenum; } diff --git a/utils/pdfinfo.cc b/utils/pdfinfo.cc index 5e2eb0c9..550c1d94 100644 --- a/utils/pdfinfo.cc +++ b/utils/pdfinfo.cc @@ -303,7 +303,7 @@ struct GooStringCompare { } }; -static void printLinkDest(LinkDest *dest) { +static void printLinkDest(const std::unique_ptr<LinkDest>& dest) { GooString s; switch (dest->getKind()) { @@ -375,29 +375,29 @@ static void printLinkDest(LinkDest *dest) { } static void printDestinations(PDFDoc *doc, const UnicodeMap *uMap) { - std::map<Ref,std::map<GooString*,LinkDest*,GooStringCompare> > map; + std::map<Ref,std::map<GooString*,std::unique_ptr<LinkDest>,GooStringCompare> > map; int numDests = doc->getCatalog()->numDestNameTree(); for (int i = 0; i < numDests; i++) { GooString *name = new GooString(doc->getCatalog()->getDestNameTreeName(i)); - LinkDest *dest = doc->getCatalog()->getDestNameTreeDest(i); + std::unique_ptr<LinkDest> dest = doc->getCatalog()->getDestNameTreeDest(i); if (dest && dest->isPageRef()) { - map[dest->getPageRef()].insert(std::make_pair(name, dest)); + Ref pageRef = dest->getPageRef(); + map[pageRef].insert(std::make_pair(name, std::move(dest))); } else { delete name; - delete dest; } } numDests = doc->getCatalog()->numDests(); for (int i = 0; i < numDests; i++) { GooString *name = new GooString(doc->getCatalog()->getDestsName(i)); - LinkDest *dest = doc->getCatalog()->getDestsDest(i); + std::unique_ptr<LinkDest> dest = doc->getCatalog()->getDestsDest(i); if (dest && dest->isPageRef()) { - map[dest->getPageRef()].insert(std::make_pair(name, dest)); + Ref pageRef = dest->getPageRef(); + map[pageRef].insert(std::make_pair(name, std::move(dest))); } else { delete name; - delete dest; } } @@ -421,7 +421,6 @@ static void printDestinations(PDFDoc *doc, const UnicodeMap *uMap) { gfree(u); printf("\"\n"); delete it.first; - delete it.second; } } } |