PoDoFo 1.0.0-dev
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 PODOFO_PRIVATE_FRIEND(class PdfParserObject);
39
40public:
41
42 static const PdfVariant Null;
43
47 PdfVariant();
48
52 PdfVariant(bool value);
53
57 PdfVariant(int64_t value);
58
62 PdfVariant(double value);
63
70 PdfVariant(const PdfString& str);
71
75 PdfVariant(const PdfName& name);
76
81
89 PdfVariant(const PdfArray& arr);
90 PdfVariant(PdfArray&& arr) noexcept;
91
96 PdfVariant(PdfDictionary&& dict) noexcept;
97
101 PdfVariant(const PdfData& data);
102 PdfVariant(PdfData&& data) noexcept;
103
108 PdfVariant(const PdfVariant& rhs);
109 PdfVariant(PdfVariant&& rhs) noexcept;
110
111 ~PdfVariant();
112
116 std::string_view GetDataTypeString() const;
117
120 bool IsBool() const;
121
124 bool IsNumber() const;
125
130 bool IsRealStrict() const;
131
134 bool IsNumberOrReal() const;
135
138 bool IsString() const;
139
142 bool IsName() const;
143
146 bool IsArray() const;
147
150 bool IsDictionary() const;
151
154 bool IsRawData() const;
155
158 bool IsNull() const;
159
162 bool IsReference() const;
163
168 std::string ToString(PdfWriteFlags writeFlags = PdfWriteFlags::None) const;
169 void ToString(std::string& str, PdfWriteFlags writeFlags = PdfWriteFlags::None) const;
170
174 bool GetBool() const;
175 bool TryGetBool(bool& value) const;
176
182 int64_t GetNumberLenient() const;
183 bool TryGetNumberLenient(int64_t& value) const;
184
190 int64_t GetNumber() const;
191 bool TryGetNumber(int64_t& value) const;
192
198 double GetReal() const;
199 bool TryGetReal(double& value) const;
200
206 double GetRealStrict() const;
207 bool TryGetRealStrict(double& value) const;
208
211 const PdfString& GetString() const;
212 bool TryGetString(PdfString& str) const;
213 bool TryGetString(const PdfString*& str) const;
214
217 const PdfName& GetName() const;
218 bool TryGetName(PdfName& name) const;
219 bool TryGetName(const PdfName*& name) const;
220
224 PdfReference GetReference() const;
225 bool TryGetReference(PdfReference& ref) const;
226
230 const PdfArray& GetArray() const;
231 PdfArray& GetArray();
232 bool TryGetArray(const PdfArray*& arr) const;
233 bool TryGetArray(PdfArray*& arr);
234
238 const PdfDictionary& GetDictionary() const;
239 PdfDictionary& GetDictionary();
240 bool TryGetDictionary(const PdfDictionary*& dict) const;
241 bool TryGetDictionary(PdfDictionary*& dict);
242
249 void SetBool(bool value);
250
257 void SetNumber(int64_t value);
258
265 void SetReal(double value);
266
273 void SetName(const PdfName& name);
274
281 void SetString(const PdfString& str);
282
283 void SetReference(const PdfReference& ref);
284
292 const PdfStatefulEncrypt* encrypt, charbuff& buffer) const;
293
300 PdfVariant& operator=(const PdfVariant& rhs);
301 PdfVariant& operator=(PdfVariant&& rhs) noexcept;
302
307 bool operator==(const PdfVariant& rhs) const;
308
312 bool operator!=(const PdfVariant& rhs) const;
313
314 PdfDataType GetDataType() const;
315
316private:
317 // Delete constructor with nullptr
318 PdfVariant(std::nullptr_t) = delete;
319
321
323
324 PdfReference GetReferenceUnsafe() const;
325 const PdfDictionary& GetDictionaryUnsafe() const;
326 const PdfArray& GetArrayUnsafe() const;
327 PdfDictionary& GetDictionaryUnsafe();
328 PdfArray& GetArrayUnsafe();
329
330private:
331 void assign(const PdfVariant& rhs);
332 bool tryGetDictionary(PdfDictionary*& dict) const;
333 bool tryGetArray(PdfArray*& arr) const;
334 bool tryGetName(const PdfName*& name) const;
335 bool tryGetString(const PdfString*& str) const;
336 void moveFrom(PdfVariant&& rhs);
337
338private:
339 // Reset the variant to "null" type. To be called by PdfTokenizer
340 void Reset();
341
366 template<typename T>
367 PdfVariant(T*) = delete;
368
369 template <typename T>
370 class PrimitiveMember : PdfDataMember
371 {
372 public:
373 PrimitiveMember(T value)
374 : PdfDataMember(getDataType()), Value(value) { }
375
376 constexpr PdfDataType getDataType()
377 {
378 if (std::is_same_v<T, int64_t>)
379 return PdfDataType::Number;
380 else if (std::is_same_v<T, double>)
381 return PdfDataType::Real;
382 else if (std::is_same_v<T, bool>)
383 return PdfDataType::Bool;
384 else if (std::is_same_v<T, PdfDictionary*>)
385 return PdfDataType::Dictionary;
386 else if (std::is_same_v<T, PdfArray*>)
387 return PdfDataType::Array;
388 else if (std::is_same_v<T, PdfData*>)
389 return PdfDataType::RawData;
390 else
391 return PdfDataType::Unknown;
392 }
393
394 public:
395 T Value;
396 };
397
398 class NullMember : PdfDataMember
399 {
400 public:
401 NullMember();
402 };
403
408 union
409 {
414 PdfName m_Name;
415 PdfReference m_Reference;
422 NullMember m_Null;
423 };
424};
425
426};
427
428#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:413
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