Commit 117a35dc6d96ca04e7aa4dcd9f84d01287687bc6
1 parent
c36b76be
Make Array iterable
Showing
2 changed files
with
108 additions
and
0 deletions
libqpdf/QPDF_Array.cc
| @@ -46,6 +46,96 @@ Array::array() const | @@ -46,6 +46,96 @@ Array::array() const | ||
| 46 | return nullptr; // unreachable | 46 | return nullptr; // unreachable |
| 47 | } | 47 | } |
| 48 | 48 | ||
| 49 | +Array::iterator | ||
| 50 | +Array::begin() | ||
| 51 | +{ | ||
| 52 | + if (auto a = as<QPDF_Array>()) { | ||
| 53 | + if (!a->sp) { | ||
| 54 | + return a->elements.begin(); | ||
| 55 | + } | ||
| 56 | + if (!sp_elements) { | ||
| 57 | + sp_elements = std::make_unique<std::vector<QPDFObjectHandle>>(getAsVector()); | ||
| 58 | + } | ||
| 59 | + return sp_elements->begin(); | ||
| 60 | + } | ||
| 61 | + return {}; | ||
| 62 | +} | ||
| 63 | + | ||
| 64 | +Array::iterator | ||
| 65 | +Array::end() | ||
| 66 | +{ | ||
| 67 | + if (auto a = as<QPDF_Array>()) { | ||
| 68 | + if (!a->sp) { | ||
| 69 | + return a->elements.end(); | ||
| 70 | + } | ||
| 71 | + if (!sp_elements) { | ||
| 72 | + sp_elements = std::make_unique<std::vector<QPDFObjectHandle>>(getAsVector()); | ||
| 73 | + } | ||
| 74 | + return sp_elements->end(); | ||
| 75 | + } | ||
| 76 | + return {}; | ||
| 77 | +} | ||
| 78 | + | ||
| 79 | +Array::const_iterator | ||
| 80 | +Array::cbegin() | ||
| 81 | +{ | ||
| 82 | + if (auto a = as<QPDF_Array>()) { | ||
| 83 | + if (!a->sp) { | ||
| 84 | + return a->elements.cbegin(); | ||
| 85 | + } | ||
| 86 | + if (!sp_elements) { | ||
| 87 | + sp_elements = std::make_unique<std::vector<QPDFObjectHandle>>(getAsVector()); | ||
| 88 | + } | ||
| 89 | + return sp_elements->cbegin(); | ||
| 90 | + } | ||
| 91 | + return {}; | ||
| 92 | +} | ||
| 93 | + | ||
| 94 | +Array::const_iterator | ||
| 95 | +Array::cend() | ||
| 96 | +{ | ||
| 97 | + if (auto a = as<QPDF_Array>()) { | ||
| 98 | + if (!a->sp) { | ||
| 99 | + return a->elements.cend(); | ||
| 100 | + } | ||
| 101 | + if (!sp_elements) { | ||
| 102 | + sp_elements = std::make_unique<std::vector<QPDFObjectHandle>>(getAsVector()); | ||
| 103 | + } | ||
| 104 | + return sp_elements->cend(); | ||
| 105 | + } | ||
| 106 | + return {}; | ||
| 107 | +} | ||
| 108 | + | ||
| 109 | +Array::const_reverse_iterator | ||
| 110 | +Array::crbegin() | ||
| 111 | +{ | ||
| 112 | + if (auto a = as<QPDF_Array>()) { | ||
| 113 | + if (!a->sp) { | ||
| 114 | + return a->elements.crbegin(); | ||
| 115 | + } | ||
| 116 | + if (!sp_elements) { | ||
| 117 | + sp_elements = std::make_unique<std::vector<QPDFObjectHandle>>(getAsVector()); | ||
| 118 | + } | ||
| 119 | + return sp_elements->crbegin(); | ||
| 120 | + } | ||
| 121 | + return {}; | ||
| 122 | +} | ||
| 123 | + | ||
| 124 | +Array::const_reverse_iterator | ||
| 125 | +Array::crend() | ||
| 126 | +{ | ||
| 127 | + if (auto a = as<QPDF_Array>()) { | ||
| 128 | + if (!a->sp) { | ||
| 129 | + return a->elements.crend(); | ||
| 130 | + } | ||
| 131 | + if (!sp_elements) { | ||
| 132 | + sp_elements = std::make_unique<std::vector<QPDFObjectHandle>>(getAsVector()); | ||
| 133 | + } | ||
| 134 | + return sp_elements->crend(); | ||
| 135 | + } | ||
| 136 | + return {}; | ||
| 137 | +} | ||
| 138 | + | ||
| 49 | QPDFObjectHandle | 139 | QPDFObjectHandle |
| 50 | Array::null() const | 140 | Array::null() const |
| 51 | { | 141 | { |
libqpdf/qpdf/QPDFObjectHandle_private.hh
| @@ -21,6 +21,22 @@ namespace qpdf | @@ -21,6 +21,22 @@ namespace qpdf | ||
| 21 | { | 21 | { |
| 22 | } | 22 | } |
| 23 | 23 | ||
| 24 | + using iterator = std::vector<QPDFObjectHandle>::iterator; | ||
| 25 | + using const_iterator = std::vector<QPDFObjectHandle>::const_iterator; | ||
| 26 | + using const_reverse_iterator = std::vector<QPDFObjectHandle>::const_reverse_iterator; | ||
| 27 | + | ||
| 28 | + iterator begin(); | ||
| 29 | + | ||
| 30 | + iterator end(); | ||
| 31 | + | ||
| 32 | + const_iterator cbegin(); | ||
| 33 | + | ||
| 34 | + const_iterator cend(); | ||
| 35 | + | ||
| 36 | + const_reverse_iterator crbegin(); | ||
| 37 | + | ||
| 38 | + const_reverse_iterator crend(); | ||
| 39 | + | ||
| 24 | int size() const; | 40 | int size() const; |
| 25 | std::pair<bool, QPDFObjectHandle> at(int n) const; | 41 | std::pair<bool, QPDFObjectHandle> at(int n) const; |
| 26 | bool setAt(int at, QPDFObjectHandle const& oh); | 42 | bool setAt(int at, QPDFObjectHandle const& oh); |
| @@ -35,6 +51,8 @@ namespace qpdf | @@ -35,6 +51,8 @@ namespace qpdf | ||
| 35 | QPDF_Array* array() const; | 51 | QPDF_Array* array() const; |
| 36 | void checkOwnership(QPDFObjectHandle const& item) const; | 52 | void checkOwnership(QPDFObjectHandle const& item) const; |
| 37 | QPDFObjectHandle null() const; | 53 | QPDFObjectHandle null() const; |
| 54 | + | ||
| 55 | + std::unique_ptr<std::vector<QPDFObjectHandle>> sp_elements{}; | ||
| 38 | }; | 56 | }; |
| 39 | 57 | ||
| 40 | // BaseDictionary is only used as a base class. It does not contain any methods exposed in the | 58 | // BaseDictionary is only used as a base class. It does not contain any methods exposed in the |