Commit 1f3f872dfbf9f31fad1c8e97e5b7ca2754101da2

Authored by m-holger
1 parent 1174cd2c

Add move and copy operations to `BaseHandle` and `Array` private API classes, st…

…reamline object assignment, and introduce constructors for `QPDFObjectHandle`.
include/qpdf/ObjectHandle.hh
@@ -104,11 +104,18 @@ namespace qpdf @@ -104,11 +104,18 @@ namespace qpdf
104 BaseHandle& operator=(BaseHandle const&) = default; 104 BaseHandle& operator=(BaseHandle const&) = default;
105 BaseHandle(BaseHandle&&) = default; 105 BaseHandle(BaseHandle&&) = default;
106 BaseHandle& operator=(BaseHandle&&) = default; 106 BaseHandle& operator=(BaseHandle&&) = default;
  107 +
  108 + inline BaseHandle(QPDFObjectHandle const& oh);
  109 + inline BaseHandle(QPDFObjectHandle&& oh);
  110 +
107 ~BaseHandle() = default; 111 ~BaseHandle() = default;
108 112
109 template <typename T> 113 template <typename T>
110 T* as() const; 114 T* as() const;
111 115
  116 + inline void assign(qpdf_object_type_e required, BaseHandle const& other);
  117 + inline void assign(qpdf_object_type_e required, BaseHandle&& other);
  118 +
112 std::string description() const; 119 std::string description() const;
113 std::runtime_error type_error(char const* expected_type) const; 120 std::runtime_error type_error(char const* expected_type) const;
114 QPDFExc type_error(char const* expected_type, std::string const& message) const; 121 QPDFExc type_error(char const* expected_type, std::string const& message) const;
libqpdf/QPDF_Array.cc
@@ -60,6 +60,21 @@ Array::array() const @@ -60,6 +60,21 @@ Array::array() const
60 return nullptr; // unreachable 60 return nullptr; // unreachable
61 } 61 }
62 62
  63 +Array::Array(bool empty) :
  64 + BaseHandle(empty ? QPDFObject::create<QPDF_Array>() : nullptr)
  65 +{
  66 +}
  67 +
  68 +Array::Array(std::vector<QPDFObjectHandle> const& items) :
  69 + BaseHandle(QPDFObject::create<QPDF_Array>(items))
  70 +{
  71 +}
  72 +
  73 +Array::Array(std::vector<QPDFObjectHandle>&& items) :
  74 + BaseHandle(QPDFObject::create<QPDF_Array>(std::move(items)))
  75 +{
  76 +}
  77 +
63 Array::iterator 78 Array::iterator
64 Array::begin() 79 Array::begin()
65 { 80 {
libqpdf/qpdf/QPDFObjectHandle_private.hh
@@ -12,6 +12,30 @@ namespace qpdf @@ -12,6 +12,30 @@ namespace qpdf
12 class Array final: public BaseHandle 12 class Array final: public BaseHandle
13 { 13 {
14 public: 14 public:
  15 + // Create an empty Array. If 'empty' is false, create a null Array.
  16 + Array(bool empty = true);
  17 +
  18 + Array(std::vector<QPDFObjectHandle> const& items);
  19 +
  20 + Array(std::vector<QPDFObjectHandle>&& items);
  21 +
  22 + Array(Array const& other) :
  23 + BaseHandle(other.obj)
  24 + {
  25 + }
  26 +
  27 + Array&
  28 + operator=(Array const& other)
  29 + {
  30 + if (obj != other.obj) {
  31 + obj = other.obj;
  32 + }
  33 + return *this;
  34 + }
  35 +
  36 + Array(Array&&) = default;
  37 + Array& operator=(Array&&) = default;
  38 +
15 explicit Array(std::shared_ptr<QPDFObject> const& obj) : 39 explicit Array(std::shared_ptr<QPDFObject> const& obj) :
16 BaseHandle(obj) 40 BaseHandle(obj)
17 { 41 {
@@ -22,6 +46,30 @@ namespace qpdf @@ -22,6 +46,30 @@ namespace qpdf
22 { 46 {
23 } 47 }
24 48
  49 + Array(QPDFObjectHandle const& oh) :
  50 + BaseHandle(oh.resolved_type_code() == ::ot_array ? oh : QPDFObjectHandle())
  51 + {
  52 + }
  53 +
  54 + Array&
  55 + operator=(QPDFObjectHandle const& oh)
  56 + {
  57 + assign(::ot_array, oh);
  58 + return *this;
  59 + }
  60 +
  61 + Array(QPDFObjectHandle&& oh) :
  62 + BaseHandle(oh.resolved_type_code() == ::ot_array ? std::move(oh) : QPDFObjectHandle())
  63 + {
  64 + }
  65 +
  66 + Array&
  67 + operator=(QPDFObjectHandle&& oh)
  68 + {
  69 + assign(::ot_array, std::move(oh));
  70 + return *this;
  71 + }
  72 +
25 using iterator = std::vector<QPDFObjectHandle>::iterator; 73 using iterator = std::vector<QPDFObjectHandle>::iterator;
26 using const_iterator = std::vector<QPDFObjectHandle>::const_iterator; 74 using const_iterator = std::vector<QPDFObjectHandle>::const_iterator;
27 using const_reverse_iterator = std::vector<QPDFObjectHandle>::const_reverse_iterator; 75 using const_reverse_iterator = std::vector<QPDFObjectHandle>::const_reverse_iterator;
@@ -342,6 +390,32 @@ namespace qpdf @@ -342,6 +390,32 @@ namespace qpdf
342 return nullptr; 390 return nullptr;
343 } 391 }
344 392
  393 + inline BaseHandle::BaseHandle(QPDFObjectHandle const& oh) :
  394 + obj(oh.obj)
  395 + {
  396 + }
  397 +
  398 + inline BaseHandle::BaseHandle(QPDFObjectHandle&& oh) :
  399 + obj(std::move(oh.obj))
  400 + {
  401 + }
  402 +
  403 + inline void
  404 + BaseHandle::assign(qpdf_object_type_e required, BaseHandle const& other)
  405 + {
  406 + if (obj != other.obj) {
  407 + obj = other.resolved_type_code() == required ? other.obj : nullptr;
  408 + }
  409 + }
  410 +
  411 + inline void
  412 + BaseHandle::assign(qpdf_object_type_e required, BaseHandle&& other)
  413 + {
  414 + if (obj != other.obj) {
  415 + obj = other.resolved_type_code() == required ? std::move(other.obj) : nullptr;
  416 + }
  417 + }
  418 +
345 inline QPDFObjGen 419 inline QPDFObjGen
346 BaseHandle::id_gen() const 420 BaseHandle::id_gen() const
347 { 421 {