PoDoFo 1.1.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
30class PODOFO_API PdfVariant final
31{
32 friend class PdfObject;
33 friend class PdfArray;
34 friend class PdfDictionary;
35 friend class PdfTokenizer;
36 friend class PdfParser;
37 PODOFO_PRIVATE_FRIEND(class PdfParserObject);
38
39public:
40
41 static const PdfVariant Null;
42
46 PdfVariant();
47
51 PdfVariant(bool value);
52
56 PdfVariant(int64_t value);
57
61 PdfVariant(double value);
62
69 PdfVariant(const PdfString& str);
70
74 PdfVariant(const PdfName& name);
75
80
88 PdfVariant(const PdfArray& arr);
89 PdfVariant(PdfArray&& arr) noexcept;
90
95 PdfVariant(PdfDictionary&& dict) noexcept;
96
100 PdfVariant(const PdfData& data);
101 PdfVariant(PdfData&& data) noexcept;
102
107 PdfVariant(const PdfVariant& rhs);
108 PdfVariant(PdfVariant&& rhs) noexcept;
109
110 ~PdfVariant();
111
115 std::string_view GetDataTypeString() const;
116
119 bool IsBool() const;
120
123 bool IsNumber() const;
124
129 bool IsRealStrict() const;
130
133 bool IsNumberOrReal() const;
134
137 bool IsString() const;
138
141 bool IsName() const;
142
145 bool IsArray() const;
146
149 bool IsDictionary() const;
150
153 bool IsRawData() const;
154
157 bool IsNull() const;
158
161 bool IsReference() const;
162
167 std::string ToString(PdfWriteFlags writeFlags = PdfWriteFlags::None) const;
168 void ToString(std::string& str, PdfWriteFlags writeFlags = PdfWriteFlags::None) const;
169
173 bool GetBool() const;
174 bool TryGetBool(bool& value) const;
175
181 int64_t GetNumberLenient() const;
182 bool TryGetNumberLenient(int64_t& value) const;
183
189 int64_t GetNumber() const;
190 bool TryGetNumber(int64_t& value) const;
191
197 double GetReal() const;
198 bool TryGetReal(double& value) const;
199
205 double GetRealStrict() const;
206 bool TryGetRealStrict(double& value) const;
207
210 const PdfString& GetString() const;
211 bool TryGetString(PdfString& str) const;
212 bool TryGetString(const PdfString*& str) const;
213
216 const PdfName& GetName() const;
217 bool TryGetName(PdfName& name) const;
218 bool TryGetName(const PdfName*& name) const;
219
223 PdfReference GetReference() const;
224 bool TryGetReference(PdfReference& ref) const;
225
229 const PdfArray& GetArray() const;
230 PdfArray& GetArray();
231 bool TryGetArray(const PdfArray*& arr) const;
232 bool TryGetArray(PdfArray*& arr);
233
237 const PdfDictionary& GetDictionary() const;
238 PdfDictionary& GetDictionary();
239 bool TryGetDictionary(const PdfDictionary*& dict) const;
240 bool TryGetDictionary(PdfDictionary*& dict);
241
248 void SetBool(bool value);
249
256 void SetNumber(int64_t value);
257
264 void SetReal(double value);
265
272 void SetName(const PdfName& name);
273
280 void SetString(const PdfString& str);
281
282 void SetReference(const PdfReference& ref);
283
291 const PdfStatefulEncrypt* encrypt, charbuff& buffer) const;
292
299 PdfVariant& operator=(const PdfVariant& rhs);
300 PdfVariant& operator=(PdfVariant&& rhs) noexcept;
301
306 bool operator==(const PdfVariant& rhs) const;
307
311 bool operator!=(const PdfVariant& rhs) const;
312
313 PdfDataType GetDataType() const;
314
315private:
316 // Delete constructor with nullptr
317 PdfVariant(std::nullptr_t) = delete;
318
320
322
323 PdfReference GetReferenceUnsafe() const;
324 const PdfDictionary& GetDictionaryUnsafe() const;
325 const PdfArray& GetArrayUnsafe() const;
326 PdfDictionary& GetDictionaryUnsafe();
327 PdfArray& GetArrayUnsafe();
328
329private:
330 void assign(const PdfVariant& rhs);
331 bool tryGetDictionary(PdfDictionary*& dict) const;
332 bool tryGetArray(PdfArray*& arr) const;
333 bool tryGetName(const PdfName*& name) const;
334 bool tryGetString(const PdfString*& str) const;
335 void moveFrom(PdfVariant&& rhs);
336
337private:
338 // Reset the variant to "null" type. To be called by PdfTokenizer
339 void Reset();
340
365 template<typename T>
366 PdfVariant(T*) = delete;
367
368 template <typename T>
369 class PrimitiveMember : PdfDataMember
370 {
371 public:
372 PrimitiveMember(T value)
373 : PdfDataMember(getDataType()), Value(value) { }
374
375 constexpr PdfDataType getDataType()
376 {
377 if (std::is_same_v<T, int64_t>)
378 return PdfDataType::Number;
379 else if (std::is_same_v<T, double>)
380 return PdfDataType::Real;
381 else if (std::is_same_v<T, bool>)
382 return PdfDataType::Bool;
383 else if (std::is_same_v<T, PdfDictionary*>)
384 return PdfDataType::Dictionary;
385 else if (std::is_same_v<T, PdfArray*>)
386 return PdfDataType::Array;
387 else if (std::is_same_v<T, PdfData*>)
388 return PdfDataType::RawData;
389 else
390 return PdfDataType::Unknown;
391 }
392
393 public:
394 T Value;
395 };
396
397 class NullMember : PdfDataMember
398 {
399 public:
400 NullMember();
401 };
402
407 union
408 {
413 PdfName m_Name;
414 PdfReference m_Reference;
421 NullMember m_Null;
422 };
423};
424
425};
426
427#endif // PDF_VARIANT_H
An interface for writing blocks of data to a data source.
Definition OutputStream.h:16
This class represents a PdfArray Use it for all arrays that are written to a PDF file.
Definition PdfArray.h:79
A class to inherit for classes that are stored as union members in a PdfVariant.
Definition PdfBaseDataTypes.h:19
A datatype that allows to write arbitrary data to a PDF file.
Definition PdfData.h:21
The PDF dictionary data type of PoDoFo (inherits from PdfDataContainer, the base class for such repre...
Definition PdfDictionary.h:80
This class represents a PdfName.
Definition PdfName.h:22
This class represents a PDF indirect Object in memory.
Definition PdfObject.h:33
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:22
A string that can be written to a PDF document.
Definition PdfString.h:22
A simple tokenizer for PDF files and PDF content streams.
Definition PdfTokenizer.h:34
A variant data type which supports all data types supported by the PDF standard.
Definition PdfVariant.h:31
PdfString m_String
Holds references, strings, names, dictionaries and arrays.
Definition PdfVariant.h:412
Convenient type for char array storage and/or buffer with std::string compatibility.
Definition basetypes.h:35
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:165
@ Null
The null datatype is always null.
PdfWriteFlags
Specify additional options for writing the PDF.
Definition PdfDeclarations.h:147