aboutsummaryrefslogtreecommitdiffstats
path: root/include
diff options
context:
space:
mode:
authorJay Berkenbilt <jberkenbilt@users.noreply.github.com>2023-05-20 20:09:49 +0200
committerGitHub <noreply@github.com>2023-05-20 20:09:49 +0200
commita85635b839755765713eb27d767f42b357056b2e (patch)
tree369b4e49b78e54e04a0409b774a659206017addd /include
parentfd17c8e3fe38a56abf50ce0edec1cde48d4f74cb (diff)
parent50bc82b4e035853bd06a9935722d6b5aec902133 (diff)
downloadqpdf-a85635b839755765713eb27d767f42b357056b2e.tar.zst
Merge pull request #929 from m-holger/ogguard
Add new convenience class QPDFObjGen::Guard
Diffstat (limited to 'include')
-rw-r--r--include/qpdf/QPDF.hh8
-rw-r--r--include/qpdf/QPDFAcroFormDocumentHelper.hh2
-rw-r--r--include/qpdf/QPDFJob.hh2
-rw-r--r--include/qpdf/QPDFObjGen.hh69
-rw-r--r--include/qpdf/QPDFObjectHandle.hh2
-rw-r--r--include/qpdf/QPDFOutlineDocumentHelper.hh10
6 files changed, 79 insertions, 14 deletions
diff --git a/include/qpdf/QPDF.hh b/include/qpdf/QPDF.hh
index 03490af7..f1dcb7f7 100644
--- a/include/qpdf/QPDF.hh
+++ b/include/qpdf/QPDF.hh
@@ -1017,7 +1017,7 @@ class QPDF
public:
std::map<QPDFObjGen, QPDFObjectHandle> object_map;
std::vector<QPDFObjectHandle> to_copy;
- std::set<QPDFObjGen> visiting;
+ QPDFObjGen::set visiting;
};
class EncryptionParameters
@@ -1252,8 +1252,8 @@ class QPDF
void getAllPagesInternal(
QPDFObjectHandle cur_pages,
- std::set<QPDFObjGen>& visited,
- std::set<QPDFObjGen>& seen);
+ QPDFObjGen::set& visited,
+ QPDFObjGen::set& seen);
void insertPage(QPDFObjectHandle newpage, int pos);
void flattenPagesTree();
void insertPageobjToPage(
@@ -1645,7 +1645,7 @@ class QPDF
ObjUser const& ou,
QPDFObjectHandle oh,
std::function<int(QPDFObjectHandle&)> skip_stream_parameters,
- std::set<QPDFObjGen>& visited,
+ QPDFObjGen::set& visited,
bool top);
void filterCompressedObjects(std::map<int, int> const& object_stream_data);
diff --git a/include/qpdf/QPDFAcroFormDocumentHelper.hh b/include/qpdf/QPDFAcroFormDocumentHelper.hh
index 4539b52d..d1ac6253 100644
--- a/include/qpdf/QPDFAcroFormDocumentHelper.hh
+++ b/include/qpdf/QPDFAcroFormDocumentHelper.hh
@@ -254,7 +254,7 @@ class QPDFAcroFormDocumentHelper: public QPDFDocumentHelper
QPDFObjectHandle field,
QPDFObjectHandle parent,
int depth,
- std::set<QPDFObjGen>& visited);
+ QPDFObjGen::set& visited);
QPDFObjectHandle getOrCreateAcroForm();
void adjustInheritedFields(
QPDFObjectHandle obj,
diff --git a/include/qpdf/QPDFJob.hh b/include/qpdf/QPDFJob.hh
index 7396cd6a..51b54d80 100644
--- a/include/qpdf/QPDFJob.hh
+++ b/include/qpdf/QPDFJob.hh
@@ -571,7 +571,7 @@ class QPDFJob
// JSON
void doJSON(QPDF& pdf, Pipeline*);
- std::set<QPDFObjGen> getWantedJSONObjects();
+ QPDFObjGen::set getWantedJSONObjects();
void doJSONObject(
Pipeline* p, bool& first, std::string const& key, QPDFObjectHandle&);
void doJSONObjects(Pipeline* p, bool& first, QPDF& pdf);
diff --git a/include/qpdf/QPDFObjGen.hh b/include/qpdf/QPDFObjGen.hh
index ccab4ba2..0d14efaf 100644
--- a/include/qpdf/QPDFObjGen.hh
+++ b/include/qpdf/QPDFObjGen.hh
@@ -24,6 +24,10 @@
#include <qpdf/DLL.h>
#include <iostream>
+#include <set>
+
+class QPDFObjectHandle;
+class QPDFObjectHelper;
// This class represents an object ID and generation pair. It is
// suitable to use as a key in a map or set.
@@ -31,6 +35,7 @@
class QPDFObjGen
{
public:
+ // ABI: change to default.
QPDF_DLL
QPDFObjGen() :
obj(0),
@@ -84,12 +89,72 @@ class QPDFObjGen
QPDF_DLL
friend std::ostream& operator<<(std::ostream& os, const QPDFObjGen& og);
+ // Convenience class for loop detection when processing objects.
+ //
+ // The class adds 'add' methods to a std::set<QPDFObjGen> which allows
+ // to test whether an QPDFObjGen is present in the set and to insert it in
+ // a single operation. The 'add' method is overloaded to take a QPDFObjGen,
+ // QPDFObjectHandle or an QPDFObjectHelper as parameter.
+ //
+ // The erase method is modified to ignore requests to erase
+ // QPDFObjGen(0, 0).
+ //
+ // Usage example:
+ //
+ // void process_object(QPDFObjectHandle oh, QPDFObjGen::Tracker& seen)
+ // {
+ // if (seen.add(oh)) {
+ // // handle first encounter of oh
+ // } else {
+ // // handle loop / subsequent encounter of oh
+ // }
+ // }
+ class QPDF_DLL_CLASS set: public std::set<QPDFObjGen>
+ {
+ public:
+ // Add 'og' to the set. Return false if 'og' is already present in
+ // the set. Attempts to insert QPDFObjGen(0, 0) are ignored.
+ QPDF_DLL
+ bool
+ add(QPDFObjGen og)
+ {
+ if (og.isIndirect()) {
+ if (count(og) > 0) {
+ return false;
+ }
+ emplace(og);
+ }
+ return true;
+ }
+
+ QPDF_DLL
+ bool add(QPDFObjectHandle const& oh);
+
+ QPDF_DLL
+ bool add(QPDFObjectHelper const& oh);
+
+ QPDF_DLL
+ void
+ erase(QPDFObjGen og)
+ {
+ if (og.isIndirect()) {
+ std::set<QPDFObjGen>::erase(og);
+ }
+ }
+
+ QPDF_DLL
+ void erase(QPDFObjectHandle const& oh);
+
+ QPDF_DLL
+ void erase(QPDFObjectHelper const& oh);
+ };
+
private:
// This class does not use the Members pattern to avoid a memory
// allocation for every one of these. A lot of these get created
// and destroyed.
- int obj;
- int gen;
+ int obj{0};
+ int gen{0};
};
#endif // QPDFOBJGEN_HH
diff --git a/include/qpdf/QPDFObjectHandle.hh b/include/qpdf/QPDFObjectHandle.hh
index 198ca42e..eaab6dc6 100644
--- a/include/qpdf/QPDFObjectHandle.hh
+++ b/include/qpdf/QPDFObjectHandle.hh
@@ -1611,7 +1611,7 @@ class QPDFObjectHandle
void objectWarning(std::string const& warning);
void assertType(char const* type_name, bool istype);
inline bool dereference();
- void makeDirect(std::set<QPDFObjGen>& visited, bool stop_at_streams);
+ void makeDirect(QPDFObjGen::set& visited, bool stop_at_streams);
void disconnect();
void setParsedOffset(qpdf_offset_t offset);
void parseContentStream_internal(
diff --git a/include/qpdf/QPDFOutlineDocumentHelper.hh b/include/qpdf/QPDFOutlineDocumentHelper.hh
index cd11884d..38310302 100644
--- a/include/qpdf/QPDFOutlineDocumentHelper.hh
+++ b/include/qpdf/QPDFOutlineDocumentHelper.hh
@@ -22,13 +22,13 @@
#ifndef QPDFOUTLINEDOCUMENTHELPER_HH
#define QPDFOUTLINEDOCUMENTHELPER_HH
+#include <qpdf/QPDF.hh>
#include <qpdf/QPDFDocumentHelper.hh>
#include <qpdf/QPDFNameTreeObjectHelper.hh>
+#include <qpdf/QPDFObjGen.hh>
#include <qpdf/QPDFOutlineObjectHelper.hh>
-#include <qpdf/QPDF.hh>
#include <map>
-#include <set>
#include <vector>
#include <qpdf/DLL.h>
@@ -69,16 +69,16 @@ class QPDFOutlineDocumentHelper: public QPDFDocumentHelper
{
friend class QPDFOutlineObjectHelper;
+ // ABI: remove QPDF_DLL and pass og by value.
QPDF_DLL
static bool
checkSeen(QPDFOutlineDocumentHelper& dh, QPDFObjGen const& og)
{
- return dh.checkSeen(og);
+ return !dh.m->seen.add(og);
}
};
private:
- bool checkSeen(QPDFObjGen const& og);
void initializeByPage();
class Members
@@ -94,7 +94,7 @@ class QPDFOutlineDocumentHelper: public QPDFDocumentHelper
Members(Members const&) = delete;
std::vector<QPDFOutlineObjectHelper> outlines;
- std::set<QPDFObjGen> seen;
+ QPDFObjGen::set seen;
QPDFObjectHandle dest_dict;
std::shared_ptr<QPDFNameTreeObjectHelper> names_dest;
std::map<QPDFObjGen, std::vector<QPDFOutlineObjectHelper>> by_page;