diff --git a/include/qpdf/ObjectHandle.hh b/include/qpdf/ObjectHandle.hh index 296da03..d121f20 100644 --- a/include/qpdf/ObjectHandle.hh +++ b/include/qpdf/ObjectHandle.hh @@ -104,11 +104,18 @@ namespace qpdf BaseHandle& operator=(BaseHandle const&) = default; BaseHandle(BaseHandle&&) = default; BaseHandle& operator=(BaseHandle&&) = default; + + inline BaseHandle(QPDFObjectHandle const& oh); + inline BaseHandle(QPDFObjectHandle&& oh); + ~BaseHandle() = default; template T* as() const; + inline void assign(qpdf_object_type_e required, BaseHandle const& other); + inline void assign(qpdf_object_type_e required, BaseHandle&& other); + std::string description() const; std::runtime_error type_error(char const* expected_type) const; QPDFExc type_error(char const* expected_type, std::string const& message) const; diff --git a/libqpdf/QPDF_Array.cc b/libqpdf/QPDF_Array.cc index 7ab8b02..4e2813f 100644 --- a/libqpdf/QPDF_Array.cc +++ b/libqpdf/QPDF_Array.cc @@ -60,6 +60,21 @@ Array::array() const return nullptr; // unreachable } +Array::Array(bool empty) : + BaseHandle(empty ? QPDFObject::create() : nullptr) +{ +} + +Array::Array(std::vector const& items) : + BaseHandle(QPDFObject::create(items)) +{ +} + +Array::Array(std::vector&& items) : + BaseHandle(QPDFObject::create(std::move(items))) +{ +} + Array::iterator Array::begin() { diff --git a/libqpdf/qpdf/QPDFObjectHandle_private.hh b/libqpdf/qpdf/QPDFObjectHandle_private.hh index 9fa67cd..74872b4 100644 --- a/libqpdf/qpdf/QPDFObjectHandle_private.hh +++ b/libqpdf/qpdf/QPDFObjectHandle_private.hh @@ -12,6 +12,30 @@ namespace qpdf class Array final: public BaseHandle { public: + // Create an empty Array. If 'empty' is false, create a null Array. + Array(bool empty = true); + + Array(std::vector const& items); + + Array(std::vector&& items); + + Array(Array const& other) : + BaseHandle(other.obj) + { + } + + Array& + operator=(Array const& other) + { + if (obj != other.obj) { + obj = other.obj; + } + return *this; + } + + Array(Array&&) = default; + Array& operator=(Array&&) = default; + explicit Array(std::shared_ptr const& obj) : BaseHandle(obj) { @@ -22,6 +46,30 @@ namespace qpdf { } + Array(QPDFObjectHandle const& oh) : + BaseHandle(oh.resolved_type_code() == ::ot_array ? oh : QPDFObjectHandle()) + { + } + + Array& + operator=(QPDFObjectHandle const& oh) + { + assign(::ot_array, oh); + return *this; + } + + Array(QPDFObjectHandle&& oh) : + BaseHandle(oh.resolved_type_code() == ::ot_array ? std::move(oh) : QPDFObjectHandle()) + { + } + + Array& + operator=(QPDFObjectHandle&& oh) + { + assign(::ot_array, std::move(oh)); + return *this; + } + using iterator = std::vector::iterator; using const_iterator = std::vector::const_iterator; using const_reverse_iterator = std::vector::const_reverse_iterator; @@ -342,6 +390,32 @@ namespace qpdf return nullptr; } + inline BaseHandle::BaseHandle(QPDFObjectHandle const& oh) : + obj(oh.obj) + { + } + + inline BaseHandle::BaseHandle(QPDFObjectHandle&& oh) : + obj(std::move(oh.obj)) + { + } + + inline void + BaseHandle::assign(qpdf_object_type_e required, BaseHandle const& other) + { + if (obj != other.obj) { + obj = other.resolved_type_code() == required ? other.obj : nullptr; + } + } + + inline void + BaseHandle::assign(qpdf_object_type_e required, BaseHandle&& other) + { + if (obj != other.obj) { + obj = other.resolved_type_code() == required ? std::move(other.obj) : nullptr; + } + } + inline QPDFObjGen BaseHandle::id_gen() const {