PoDoFo 1.2.0
Loading...
Searching...
No Matches
PdfVariant.h
1// SPDX-FileCopyrightText: 2005 Dominik Seichter <domseichter@web.de>
2// SPDX-FileCopyrightText: 2020 Francesco Pretto <ceztko@gmail.com>
3// SPDX-License-Identifier: LGPL-2.0-or-later OR MPL-2.0
4
5#ifndef PDF_VARIANT_H
6#define PDF_VARIANT_H
7
8#include "PdfReference.h"
9#include "PdfName.h"
10#include "PdfString.h"
11
12namespace PoDoFo {
13
14class PdfArray;
15class PdfData;
16class PdfDictionary;
17class PdfString;
18class PdfDataContainer;
19
28class PODOFO_API PdfVariant final
29{
30 friend class PdfObject;
31 friend class PdfArray;
32 friend class PdfDictionary;
33 friend class PdfTokenizer;
34 friend class PdfParser;
35 PODOFO_PRIVATE_FRIEND(class PdfParserObject);
36
37public:
38
39 static const PdfVariant Null;
40
43 PdfVariant();
44
47 PdfVariant(bool value);
48
51 PdfVariant(int64_t value);
52
55 PdfVariant(double value);
56
62 PdfVariant(const PdfString& str);
63
66 PdfVariant(const PdfName& name);
67
71
78 PdfVariant(const PdfArray& arr);
79 PdfVariant(PdfArray&& arr) noexcept;
80
84 PdfVariant(PdfDictionary&& dict) noexcept;
85
88 PdfVariant(const PdfData& data);
89 PdfVariant(PdfData&& data) noexcept;
90
95 PdfVariant(PdfVariant&& rhs) noexcept;
96
98
101 std::string_view GetDataTypeString() const;
102
104 bool IsBool() const;
105
107 bool IsNumber() const;
108
112 bool IsRealStrict() const;
113
115 bool IsNumberOrReal() const;
116
118 bool IsString() const;
119
121 bool IsName() const;
122
124 bool IsArray() const;
125
127 bool IsDictionary() const;
128
130 bool IsRawData() const;
131
133 bool IsNull() const;
134
136 bool IsReference() const;
137
141 std::string ToString(PdfWriteFlags writeFlags = PdfWriteFlags::None) const;
142 void ToString(std::string& str, PdfWriteFlags writeFlags = PdfWriteFlags::None) const;
143
146 bool GetBool() const;
147 bool TryGetBool(bool& value) const;
148
153 int64_t GetNumberLenient() const;
154 bool TryGetNumberLenient(int64_t& value) const;
155
160 int64_t GetNumber() const;
161 bool TryGetNumber(int64_t& value) const;
162
167 double GetReal() const;
168 bool TryGetReal(double& value) const;
169
174 double GetRealStrict() const;
175 bool TryGetRealStrict(double& value) const;
176
178 const PdfString& GetString() const;
179 bool TryGetString(PdfString& str) const;
180 bool TryGetString(const PdfString*& str) const;
181
183 const PdfName& GetName() const;
184 bool TryGetName(PdfName& name) const;
185 bool TryGetName(const PdfName*& name) const;
186
189 PdfReference GetReference() const;
190 bool TryGetReference(PdfReference& ref) const;
191
194 const PdfArray& GetArray() const;
195 PdfArray& GetArray();
196 bool TryGetArray(const PdfArray*& arr) const;
197 bool TryGetArray(PdfArray*& arr);
198
201 const PdfDictionary& GetDictionary() const;
202 PdfDictionary& GetDictionary();
203 bool TryGetDictionary(const PdfDictionary*& dict) const;
204 bool TryGetDictionary(PdfDictionary*& dict);
205
211 void SetBool(bool value);
212
218 void SetNumber(int64_t value);
219
225 void SetReal(double value);
226
232 void SetName(const PdfName& name);
233
239 void SetString(const PdfString& str);
240
241 void SetReference(const PdfReference& ref);
242
249 const PdfStatefulEncrypt* encrypt, charbuff& buffer) const;
250
256 PdfVariant& operator=(const PdfVariant& rhs);
257 PdfVariant& operator=(PdfVariant&& rhs) noexcept;
258
261 bool operator==(const PdfVariant& rhs) const;
262
264 bool operator!=(const PdfVariant& rhs) const;
265
266 PdfDataType GetDataType() const;
267
268private:
269 // Delete constructor with nullptr
270 PdfVariant(std::nullptr_t) = delete;
271
273
275
276 const PdfReference& GetReferenceUnsafe() const
277 {
278 return m_Reference;
279 }
280
281 const PdfDictionary& GetDictionaryUnsafe() const;
282 const PdfArray& GetArrayUnsafe() const;
283 PdfDictionary& GetDictionaryUnsafe();
284 PdfArray& GetArrayUnsafe();
285
286private:
287 void assign(const PdfVariant& rhs);
288 bool tryGetDictionary(PdfDictionary*& dict) const;
289 bool tryGetArray(PdfArray*& arr) const;
290 bool tryGetName(const PdfName*& name) const;
291 bool tryGetString(const PdfString*& str) const;
292 void moveFrom(PdfVariant&& rhs);
293
294private:
296 void Reset();
297
320 template<typename T>
321 PdfVariant(T*) = delete;
322
323 template <typename T>
324 class PrimitiveMember : PdfDataMember
325 {
326 public:
327 PrimitiveMember(T value)
328 : PdfDataMember(getDataType()), Value(value) { }
329
330 constexpr PdfDataType getDataType()
331 {
332 if (std::is_same_v<T, int64_t>)
333 return PdfDataType::Number;
334 else if (std::is_same_v<T, double>)
335 return PdfDataType::Real;
336 else if (std::is_same_v<T, bool>)
337 return PdfDataType::Bool;
338 else if (std::is_same_v<T, PdfDictionary*>)
339 return PdfDataType::Dictionary;
340 else if (std::is_same_v<T, PdfArray*>)
341 return PdfDataType::Array;
342 else if (std::is_same_v<T, PdfData*>)
343 return PdfDataType::RawData;
344 else
345 return PdfDataType::Unknown;
346 }
347
348 public:
349 T Value;
350 };
351
352 class NullMember : PdfDataMember
353 {
354 public:
355 NullMember();
356 };
357
361 union
362 {
366 PdfName m_Name;
367 PdfReference m_Reference;
374 NullMember m_Null;
375 };
376};
377
378};
379
380#endif // PDF_VARIANT_H
An interface for writing blocks of data to a data source.
Definition OutputStream.h:15
This class represents a PdfArray Use it for all arrays that are written to a PDF file.
Definition PdfArray.h:76
A class to inherit for classes that are stored as union members in a PdfVariant.
Definition PdfBaseDataTypes.h:18
A datatype that allows to write arbitrary data to a PDF file.
Definition PdfData.h:20
The PDF dictionary data type of PoDoFo (inherits from PdfDataContainer, the base class for such repre...
Definition PdfDictionary.h:77
This class represents a PdfName.
Definition PdfName.h:21
This class represents a PDF indirect Object in memory.
Definition PdfObject.h:31
A reference is a pointer to a object in the PDF file of the form "4 0 R", where 4 is the object numbe...
Definition PdfReference.h:20
A string that can be written to a PDF document.
Definition PdfString.h:21
A simple tokenizer for PDF files and PDF content streams.
Definition PdfTokenizer.h:47
A variant data type which supports all data types supported by the PDF standard.
Definition PdfVariant.h:29
PdfString m_String
Holds references, strings, names, dictionaries and arrays.
Definition PdfVariant.h:365
Convenient type for char array storage and/or buffer with std::string compatibility.
Definition basetypes.h:30
All classes, functions, types and enums of PoDoFo are members of these namespace.
Definition basetypes.h:13
PdfDataType
Every PDF datatype that can occur in a PDF file is referenced by an own enum (e.g.
Definition PdfDeclarations.h:152
@ Null
The null datatype is always null.
PdfWriteFlags
Specify additional options for writing the PDF.
Definition PdfDeclarations.h:136