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 PdfReference GetReferenceUnsafe() const;
277 const PdfDictionary& GetDictionaryUnsafe() const;
278 const PdfArray& GetArrayUnsafe() const;
279 PdfDictionary& GetDictionaryUnsafe();
280 PdfArray& GetArrayUnsafe();
281
282private:
283 void assign(const PdfVariant& rhs);
284 bool tryGetDictionary(PdfDictionary*& dict) const;
285 bool tryGetArray(PdfArray*& arr) const;
286 bool tryGetName(const PdfName*& name) const;
287 bool tryGetString(const PdfString*& str) const;
288 void moveFrom(PdfVariant&& rhs);
289
290private:
291 // Reset the variant to "null" type. To be called by PdfTokenizer
292 void Reset();
293
316 template<typename T>
317 PdfVariant(T*) = delete;
318
319 template <typename T>
320 class PrimitiveMember : PdfDataMember
321 {
322 public:
323 PrimitiveMember(T value)
324 : PdfDataMember(getDataType()), Value(value) { }
325
326 constexpr PdfDataType getDataType()
327 {
328 if (std::is_same_v<T, int64_t>)
329 return PdfDataType::Number;
330 else if (std::is_same_v<T, double>)
331 return PdfDataType::Real;
332 else if (std::is_same_v<T, bool>)
333 return PdfDataType::Bool;
334 else if (std::is_same_v<T, PdfDictionary*>)
335 return PdfDataType::Dictionary;
336 else if (std::is_same_v<T, PdfArray*>)
337 return PdfDataType::Array;
338 else if (std::is_same_v<T, PdfData*>)
339 return PdfDataType::RawData;
340 else
341 return PdfDataType::Unknown;
342 }
343
344 public:
345 T Value;
346 };
347
348 class NullMember : PdfDataMember
349 {
350 public:
351 NullMember();
352 };
353
357 union
358 {
362 PdfName m_Name;
363 PdfReference m_Reference;
370 NullMember m_Null;
371 };
372};
373
374};
375
376#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:32
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:361
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