mirror of
https://github.com/qpdf/qpdf.git
synced 2024-09-22 18:19:08 +00:00
Add new convenience class QPDFObjGen::set
This commit is contained in:
parent
a6d7b79e65
commit
9b08017217
@ -24,6 +24,10 @@
|
|||||||
|
|
||||||
#include <qpdf/DLL.h>
|
#include <qpdf/DLL.h>
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
|
#include <set>
|
||||||
|
|
||||||
|
class QPDFObjectHandle;
|
||||||
|
class QPDFObjectHelper;
|
||||||
|
|
||||||
// This class represents an object ID and generation pair. It is
|
// This class represents an object ID and generation pair. It is
|
||||||
// suitable to use as a key in a map or set.
|
// suitable to use as a key in a map or set.
|
||||||
@ -31,6 +35,7 @@
|
|||||||
class QPDFObjGen
|
class QPDFObjGen
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
|
// ABI: change to default.
|
||||||
QPDF_DLL
|
QPDF_DLL
|
||||||
QPDFObjGen() :
|
QPDFObjGen() :
|
||||||
obj(0),
|
obj(0),
|
||||||
@ -84,12 +89,72 @@ class QPDFObjGen
|
|||||||
QPDF_DLL
|
QPDF_DLL
|
||||||
friend std::ostream& operator<<(std::ostream& os, const QPDFObjGen& og);
|
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:
|
private:
|
||||||
// This class does not use the Members pattern to avoid a memory
|
// This class does not use the Members pattern to avoid a memory
|
||||||
// allocation for every one of these. A lot of these get created
|
// allocation for every one of these. A lot of these get created
|
||||||
// and destroyed.
|
// and destroyed.
|
||||||
int obj;
|
int obj{0};
|
||||||
int gen;
|
int gen{0};
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif // QPDFOBJGEN_HH
|
#endif // QPDFOBJGEN_HH
|
||||||
|
@ -1,7 +1,12 @@
|
|||||||
#include <qpdf/QPDFObjGen.hh>
|
#include <qpdf/QPDFObjGen.hh>
|
||||||
|
|
||||||
#include <qpdf/QUtil.hh>
|
#include <qpdf/QPDFObjectHandle.hh>
|
||||||
|
#include <qpdf/QPDFObjectHelper.hh>
|
||||||
|
#include <qpdf/QPDFObject_private.hh>
|
||||||
|
|
||||||
|
#include <stdexcept>
|
||||||
|
|
||||||
|
// ABI: inline and pass og by value
|
||||||
std::ostream&
|
std::ostream&
|
||||||
operator<<(std::ostream& os, const QPDFObjGen& og)
|
operator<<(std::ostream& os, const QPDFObjGen& og)
|
||||||
{
|
{
|
||||||
@ -9,8 +14,55 @@ operator<<(std::ostream& os, const QPDFObjGen& og)
|
|||||||
return os;
|
return os;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// ABI: inline
|
||||||
std::string
|
std::string
|
||||||
QPDFObjGen::unparse(char separator) const
|
QPDFObjGen::unparse(char separator) const
|
||||||
{
|
{
|
||||||
return std::to_string(this->obj) + separator + std::to_string(this->gen);
|
return std::to_string(obj) + separator + std::to_string(gen);
|
||||||
|
}
|
||||||
|
|
||||||
|
bool
|
||||||
|
QPDFObjGen::set::add(QPDFObjectHandle const& oh)
|
||||||
|
{
|
||||||
|
if (auto* ptr = oh.getObjectPtr()) {
|
||||||
|
return add(ptr->getObjGen());
|
||||||
|
} else {
|
||||||
|
throw std::logic_error("attempt to retrieve QPDFObjGen from "
|
||||||
|
"uninitialized QPDFObjectHandle");
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
bool
|
||||||
|
QPDFObjGen::set::add(QPDFObjectHelper const& helper)
|
||||||
|
{
|
||||||
|
if (auto* ptr = helper.getObjectHandle().getObjectPtr()) {
|
||||||
|
return add(ptr->getObjGen());
|
||||||
|
} else {
|
||||||
|
throw std::logic_error("attempt to retrieve QPDFObjGen from "
|
||||||
|
"uninitialized QPDFObjectHandle");
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void
|
||||||
|
QPDFObjGen::set::erase(QPDFObjectHandle const& oh)
|
||||||
|
{
|
||||||
|
if (auto* ptr = oh.getObjectPtr()) {
|
||||||
|
erase(ptr->getObjGen());
|
||||||
|
} else {
|
||||||
|
throw std::logic_error("attempt to retrieve QPDFObjGen from "
|
||||||
|
"uninitialized QPDFObjectHandle");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void
|
||||||
|
QPDFObjGen::set::erase(QPDFObjectHelper const& helper)
|
||||||
|
{
|
||||||
|
if (auto* ptr = helper.getObjectHandle().getObjectPtr()) {
|
||||||
|
erase(ptr->getObjGen());
|
||||||
|
} else {
|
||||||
|
throw std::logic_error("attempt to retrieve QPDFObjGen from "
|
||||||
|
"uninitialized QPDFObjectHandle");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user