1 | //======================================================================== |
2 | // |
3 | // Array.cc |
4 | // |
5 | // Copyright 1996-2003 Glyph & Cog, LLC |
6 | // |
7 | //======================================================================== |
8 | |
9 | //======================================================================== |
10 | // |
11 | // Modified under the Poppler project - http://poppler.freedesktop.org |
12 | // |
13 | // All changes made under the Poppler project to this file are licensed |
14 | // under GPL version 2 or later |
15 | // |
16 | // Copyright (C) 2005 Kristian Høgsberg <krh@redhat.com> |
17 | // Copyright (C) 2012 Fabio D'Urso <fabiodurso@hotmail.it> |
18 | // Copyright (C) 2013 Thomas Freitag <Thomas.Freitag@alfa.de> |
19 | // Copyright (C) 2013, 2017, 2019, 2022 Albert Astals Cid <aacid@kde.org> |
20 | // Copyright (C) 2017 Adrian Johnson <ajohnson@redneon.com> |
21 | // Copyright (C) 2018, 2019 Adam Reichold <adam.reichold@t-online.de> |
22 | // |
23 | // To see a description of the changes please see the Changelog file that |
24 | // came with your tarball or type make ChangeLog if you are building from git |
25 | // |
26 | //======================================================================== |
27 | |
28 | #include <config.h> |
29 | |
30 | #include <cassert> |
31 | |
32 | #include "Object.h" |
33 | #include "Array.h" |
34 | |
35 | //------------------------------------------------------------------------ |
36 | // Array |
37 | //------------------------------------------------------------------------ |
38 | |
39 | #define arrayLocker() const std::scoped_lock locker(mutex) |
40 | |
41 | Array::Array(XRef *xrefA) |
42 | { |
43 | xref = xrefA; |
44 | ref = 1; |
45 | } |
46 | |
47 | Array::~Array() { } |
48 | |
49 | Array *Array::copy(XRef *xrefA) const |
50 | { |
51 | arrayLocker(); |
52 | Array *a = new Array(xrefA); |
53 | a->elems.reserve(n: elems.size()); |
54 | for (const auto &elem : elems) { |
55 | a->elems.push_back(x: elem.copy()); |
56 | } |
57 | return a; |
58 | } |
59 | |
60 | Array *Array::deepCopy() const |
61 | { |
62 | arrayLocker(); |
63 | Array *a = new Array(xref); |
64 | a->elems.reserve(n: elems.size()); |
65 | for (const auto &elem : elems) { |
66 | a->elems.push_back(x: elem.deepCopy()); |
67 | } |
68 | return a; |
69 | } |
70 | |
71 | void Array::add(Object &&elem) |
72 | { |
73 | arrayLocker(); |
74 | elems.push_back(x: std::move(elem)); |
75 | } |
76 | |
77 | void Array::remove(int i) |
78 | { |
79 | arrayLocker(); |
80 | if (i < 0 || std::size_t(i) >= elems.size()) { |
81 | assert(i >= 0 && std::size_t(i) < elems.size()); |
82 | return; |
83 | } |
84 | elems.erase(position: elems.begin() + i); |
85 | } |
86 | |
87 | Object Array::get(int i, int recursion) const |
88 | { |
89 | if (i < 0 || std::size_t(i) >= elems.size()) { |
90 | return Object(objNull); |
91 | } |
92 | return elems[i].fetch(xref, recursion); |
93 | } |
94 | |
95 | Object Array::get(int i, Ref *returnRef, int recursion) const |
96 | { |
97 | if (i < 0 || std::size_t(i) >= elems.size()) { |
98 | *returnRef = Ref::INVALID(); |
99 | return Object(objNull); |
100 | } |
101 | if (elems[i].getType() == objRef) { |
102 | *returnRef = elems[i].getRef(); |
103 | } else { |
104 | *returnRef = Ref::INVALID(); |
105 | } |
106 | return elems[i].fetch(xref, recursion); |
107 | } |
108 | |
109 | const Object &Array::getNF(int i) const |
110 | { |
111 | if (i < 0 || std::size_t(i) >= elems.size()) { |
112 | static Object nullObj(objNull); |
113 | return nullObj; |
114 | } |
115 | return elems[i]; |
116 | } |
117 | |
118 | bool Array::getString(int i, GooString *string) const |
119 | { |
120 | const Object &obj = getNF(i); |
121 | if (obj.isString()) { |
122 | string->clear(); |
123 | string->append(str: obj.getString()); |
124 | return true; |
125 | } else { |
126 | return false; |
127 | } |
128 | } |
129 | |