PoDoFo 1.1.0
Loading...
Searching...
No Matches
PdfVariant.h
1
7#ifndef PDF_VARIANT_H
8#define PDF_VARIANT_H
9
10#include "PdfReference.h"
11#include "PdfName.h"
12#include "PdfString.h"
13
14namespace PoDoFo {
15
16class PdfArray;
17class PdfData;
18class PdfDictionary;
19class PdfString;
20class PdfDataContainer;
21
32class PODOFO_API PdfVariant final
33{
34 friend class PdfObject;
35 friend class PdfArray;
36 friend class PdfDictionary;
37 friend class PdfTokenizer;
38 friend class PdfParser;
39 PODOFO_PRIVATE_FRIEND(class PdfParserObject);
40
41public:
42
43 static const PdfVariant Null;
44
48 PdfVariant();
49
53 PdfVariant(bool value);
54
58 PdfVariant(int64_t value);
59
63 PdfVariant(double value);
64
71 PdfVariant(const PdfString& str);
72
76 PdfVariant(const PdfName& name);
77
82
90 PdfVariant(const PdfArray& arr);
91 PdfVariant(PdfArray&& arr) noexcept;
92
97 PdfVariant(PdfDictionary&& dict) noexcept;
98
102 PdfVariant(const PdfData& data);
103 PdfVariant(PdfData&& data) noexcept;
104
109 PdfVariant(const PdfVariant& rhs);
110 PdfVariant(PdfVariant&& rhs) noexcept;
111
112 ~PdfVariant();
113
117 std::string_view GetDataTypeString() const;
118
121 bool IsBool() const;
122
125 bool IsNumber() const;
126
131 bool IsRealStrict() const;
132
135 bool IsNumberOrReal() const;
136
139 bool IsString() const;
140
143 bool IsName() const;
144
147 bool IsArray() const;
148
151 bool IsDictionary() const;
152
155 bool IsRawData() const;
156
159 bool IsNull() const;
160
163 bool IsReference() const;
164
169 std::string ToString(PdfWriteFlags writeFlags = PdfWriteFlags::None) const;
170 void ToString(std::string& str, PdfWriteFlags writeFlags = PdfWriteFlags::None) const;
171
175 bool GetBool() const;
176 bool TryGetBool(bool& value) const;
177
183 int64_t GetNumberLenient() const;
184 bool TryGetNumberLenient(int64_t& value) const;
185
191 int64_t GetNumber() const;
192 bool TryGetNumber(int64_t& value) const;
193
199 double GetReal() const;
200 bool TryGetReal(double& value) const;
201
207 double GetRealStrict() const;
208 bool TryGetRealStrict(double& value) const;
209
212 const PdfString& GetString() const;
213 bool TryGetString(PdfString& str) const;
214 bool TryGetString(const PdfString*& str) const;
215
218 const PdfName& GetName() const;
219 bool TryGetName(PdfName& name) const;
220 bool TryGetName(const PdfName*& name) const;
221
225 PdfReference GetReference() const;
226 bool TryGetReference(PdfReference& ref) const;
227
231 const PdfArray& GetArray() const;
232 PdfArray& GetArray();
233 bool TryGetArray(const PdfArray*& arr) const;
234 bool TryGetArray(PdfArray*& arr);
235
239 const PdfDictionary& GetDictionary() const;
240 PdfDictionary& GetDictionary();
241 bool TryGetDictionary(const PdfDictionary*& dict) const;
242 bool TryGetDictionary(PdfDictionary*& dict);
243
250 void SetBool(bool value);
251
258 void SetNumber(int64_t value);
259
266 void SetReal(double value);
267
274 void SetName(const PdfName& name);
275
282 void SetString(const PdfString& str);
283
284 void SetReference(const PdfReference& ref);
285
293 const PdfStatefulEncrypt* encrypt, charbuff& buffer) const;
294
301 PdfVariant& operator=(const PdfVariant& rhs);
302 PdfVariant& operator=(PdfVariant&& rhs) noexcept;
303
308 bool operator==(const PdfVariant& rhs) const;
309
313 bool operator!=(const PdfVariant& rhs) const;
314
315 PdfDataType GetDataType() const;
316
317private:
318 // Delete constructor with nullptr
319 PdfVariant(std::nullptr_t) = delete;
320
322
324
325 PdfReference GetReferenceUnsafe() const;
326 const PdfDictionary& GetDictionaryUnsafe() const;
327 const PdfArray& GetArrayUnsafe() const;
328 PdfDictionary& GetDictionaryUnsafe();
329 PdfArray& GetArrayUnsafe();
330
331private:
332 void assign(const PdfVariant& rhs);
333 bool tryGetDictionary(PdfDictionary*& dict) const;
334 bool tryGetArray(PdfArray*& arr) const;
335 bool tryGetName(const PdfName*& name) const;
336 bool tryGetString(const PdfString*& str) const;
337 void moveFrom(PdfVariant&& rhs);
338
339private:
340 // Reset the variant to "null" type. To be called by PdfTokenizer
341 void Reset();
342
367 template<typename T>
368 PdfVariant(T*) = delete;
369
370 template <typename T>
371 class PrimitiveMember : PdfDataMember
372 {
373 public:
374 PrimitiveMember(T value)
375 : PdfDataMember(getDataType()), Value(value) { }
376
377 constexpr PdfDataType getDataType()
378 {
379 if (std::is_same_v<T, int64_t>)
380 return PdfDataType::Number;
381 else if (std::is_same_v<T, double>)
382 return PdfDataType::Real;
383 else if (std::is_same_v<T, bool>)
384 return PdfDataType::Bool;
385 else if (std::is_same_v<T, PdfDictionary*>)
386 return PdfDataType::Dictionary;
387 else if (std::is_same_v<T, PdfArray*>)
388 return PdfDataType::Array;
389 else if (std::is_same_v<T, PdfData*>)
390 return PdfDataType::RawData;
391 else
392 return PdfDataType::Unknown;
393 }
394
395 public:
396 T Value;
397 };
398
399 class NullMember : PdfDataMember
400 {
401 public:
402 NullMember();
403 };
404
409 union
410 {
415 PdfName m_Name;
416 PdfReference m_Reference;
423 NullMember m_Null;
424 };
425};
426
427};
428
429#endif // PDF_VARIANT_H
An interface for writing blocks of data to a data source.
Definition OutputStream.h:18
This class represents a PdfArray Use it for all arrays that are written to a PDF file.
Definition PdfArray.h:81
A class to inherit for classes that are stored as union members in a PdfVariant.
Definition PdfBaseDataTypes.h:21
A datatype that allows to write arbitrary data to a PDF file.
Definition PdfData.h:23
The PDF dictionary data type of PoDoFo (inherits from PdfDataContainer, the base class for such repre...
Definition PdfDictionary.h:82
This class represents a PdfName.
Definition PdfName.h:24
This class represents a PDF indirect Object in memory.
Definition PdfObject.h:35
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:24
A string that can be written to a PDF document.
Definition PdfString.h:24
A simple tokenizer for PDF files and PDF content streams.
Definition PdfTokenizer.h:36
A variant data type which supports all data types supported by the PDF standard.
Definition PdfVariant.h:33
PdfString m_String
Holds references, strings, names, dictionaries and arrays.
Definition PdfVariant.h:414
Convenient type for char array storage and/or buffer with std::string compatibility.
Definition basetypes.h:38
SPDX-FileCopyrightText: (C) 2022 Francesco Pretto ceztko@gmail.com SPDX-License-Identifier: LGPL-2....
Definition basetypes.h:16
PdfDataType
Every PDF datatype that can occur in a PDF file is referenced by an own enum (e.g.
Definition PdfDeclarations.h:167
@ Null
The null datatype is always null.
PdfWriteFlags
Specify additional options for writing the PDF.
Definition PdfDeclarations.h:149