Commit ef5a9f53 authored by Oliver Sander's avatar Oliver Sander

Use std::unique_ptr to pass around LinkDest objects

This provides no functional changes, but it makes the pointer
ownership easier to understand.
parent 5539aeba
Pipeline #108986 passed with stage
in 6 minutes and 59 seconds
......@@ -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;
}
}
......
......@@ -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);
}
}
......
......@@ -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;
......
......@@ -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;
};
......
......@@ -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.
......
......@@ -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 )
......
......@@ -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;
}
......@@ -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;
}
}
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment