2
1
mirror of https://github.com/qpdf/qpdf.git synced 2024-12-22 19:08:59 +00:00
qpdf/libqpdf/OHArray.cc

100 lines
2.1 KiB
C++

#include <qpdf/OHArray.hh>
#include <qpdf/QPDFObjectHandle.hh>
#include <qpdf/QPDFObject_private.hh>
#include <stdexcept>
static const QPDFObjectHandle null_oh = QPDFObjectHandle::newNull();
OHArray::OHArray()
{
}
size_t
OHArray::size() const
{
return elements.size();
}
void
OHArray::append(QPDFObjectHandle oh)
{
elements.push_back(oh.getObj());
}
void
OHArray::append(std::shared_ptr<QPDFObject>&& obj)
{
elements.push_back(std::move(obj));
}
QPDFObjectHandle
OHArray::at(size_t idx) const
{
if (idx >= elements.size()) {
throw std::logic_error(
"INTERNAL ERROR: bounds error accessing OHArray element");
}
auto const& obj = elements.at(idx);
return obj ? obj : null_oh;
}
void
OHArray::disconnect()
{
for (auto const& iter: elements) {
if (iter) {
QPDFObjectHandle oh = iter;
QPDFObjectHandle::DisconnectAccess::disconnect(oh);
}
}
}
void
OHArray::setAt(size_t idx, QPDFObjectHandle oh)
{
if (idx >= elements.size()) {
throw std::logic_error("bounds error setting item in OHArray");
}
elements[idx] = oh.getObj();
}
void
OHArray::erase(size_t idx)
{
if (idx >= elements.size()) {
throw std::logic_error("bounds error erasing item from OHArray");
}
int n = int(idx);
elements.erase(elements.cbegin() + n);
}
void
OHArray::insert(size_t idx, QPDFObjectHandle oh)
{
if (idx > elements.size()) {
throw std::logic_error("bounds error inserting item to OHArray");
} else if (idx == elements.size()) {
// Allow inserting to the last position
append(oh.getObj());
} else {
int n = int(idx);
elements.insert(elements.cbegin() + n, oh.getObj());
}
}
OHArray
OHArray::copy()
{
OHArray result;
result.elements.reserve(elements.size());
for (auto const& element: elements) {
result.elements.push_back(
element ? (element->getObjGen().isIndirect() ? element
: element->copy())
: element);
}
return result;
}