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;
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
70 PdfVariant(const PdfReference& ref);
71
78 PdfVariant(const PdfArray& arr);
79 PdfVariant(PdfArray&& arr) noexcept;
80
83 PdfVariant(const PdfDictionary& dict);
84 PdfVariant(PdfDictionary&& dict) noexcept;
85
88 PdfVariant(const PdfData& data);
89 PdfVariant(PdfData&& data) noexcept;
90
94 PdfVariant(const PdfVariant& rhs);
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
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
248 void Write(OutputStream& stream, PdfWriteFlags writeMode,
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
272 PdfVariant(PdfDictionary* dict);
273
274 PdfVariant(PdfArray* arr);
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*>)
340 else if (std::is_same_v<T, PdfArray*>)
341 return PdfDataType::Array;
342 else if (std::is_same_v<T, PdfData*>)
344 else
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;
368 PrimitiveMember<int64_t> m_Number;
369 PrimitiveMember<double> m_Real;
370 PrimitiveMember<bool> m_Bool;
371 PrimitiveMember<PdfDictionary*> m_Dictionary;
372 PrimitiveMember<PdfArray*> m_Array;
373 PrimitiveMember<PdfData*> m_Data;
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:77
A PdfDataProvider object with a PdfObject owner, specialized in holding objects.
Definition PdfDataContainer.h:18
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:22
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:22
std::string ToString(PdfWriteFlags writeFlags=PdfWriteFlags::None) const
Converts the current object into a string representation which can be written directly to a PDF file ...
Definition PdfVariant.cpp:202
bool IsRawData() const
Definition PdfVariant.cpp:988
PdfString m_String
Holds references, strings, names, dictionaries and arrays.
Definition PdfVariant.h:365
PdfReference GetReference() const
Get the reference values of this object.
Definition PdfVariant.cpp:778
bool IsNumber() const
Definition PdfVariant.cpp:952
int64_t GetNumber() const
Get the value of the object as int64_t.
Definition PdfVariant.cpp:629
void SetBool(bool value)
Set the value of this object as bool.
Definition PdfVariant.cpp:899
const PdfString & GetString() const
Definition PdfVariant.cpp:700
double GetRealStrict() const
Get the value of the object as floating point number.
Definition PdfVariant.cpp:679
bool operator!=(const PdfVariant &rhs) const
Definition PdfVariant.cpp:492
bool IsString() const
Definition PdfVariant.cpp:968
double GetReal() const
Get the value of the object as a floating point.
Definition PdfVariant.cpp:650
bool IsBool() const
Definition PdfVariant.cpp:947
PdfVariant & operator=(const PdfVariant &rhs)
Assign the values of another PdfVariant to this one.
Definition PdfVariant.cpp:232
void SetReal(double value)
Set the value of this object as double.
Definition PdfVariant.cpp:915
bool IsArray() const
Definition PdfVariant.cpp:978
void Write(OutputStream &stream, PdfWriteFlags writeMode, const PdfStatefulEncrypt *encrypt, charbuff &buffer) const
Write the complete variant to an output device.
Definition PdfVariant.cpp:121
const PdfArray & GetArray() const
Returns the value of the object as array.
Definition PdfVariant.cpp:799
bool IsReference() const
Definition PdfVariant.cpp:998
std::string_view GetDataTypeString() const
Definition PdfVariant.cpp:379
bool IsNull() const
Definition PdfVariant.cpp:993
bool GetBool() const
Get the value if this object is a bool.
Definition PdfVariant.cpp:579
void SetString(const PdfString &str)
Set the string value of this object.
Definition PdfVariant.cpp:931
bool operator==(const PdfVariant &rhs) const
Test to see if the value contained by this variant is the same as the value of the other variant.
Definition PdfVariant.cpp:410
int64_t GetNumberLenient() const
Get the value of the object as int64_t.
Definition PdfVariant.cpp:600
const PdfName & GetName() const
Definition PdfVariant.cpp:739
const PdfDictionary & GetDictionary() const
Returns the dictionary value of this object.
Definition PdfVariant.cpp:827
bool IsNumberOrReal() const
Definition PdfVariant.cpp:962
void SetName(const PdfName &name)
Set the name value of this object.
Definition PdfVariant.cpp:923
void SetNumber(int64_t value)
Set the value of this object as int64_t.
Definition PdfVariant.cpp:907
bool IsDictionary() const
Definition PdfVariant.cpp:983
PdfVariant()
Construct an empty variant type IsNull() will return true.
Definition PdfVariant.cpp:22
bool IsName() const
Definition PdfVariant.cpp:973
bool IsRealStrict() const
Definition PdfVariant.cpp:957
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
@ Dictionary
A dictionary associates keys with values. A key can have another dictionary as value.
Definition PdfDeclarations.h:160
@ Array
An array of other PDF data types.
Definition PdfDeclarations.h:159
@ Real
Real datatype for floating point numbers.
Definition PdfDeclarations.h:156
@ Unknown
The Datatype is unknown. The value is chosen to enable value storage in 8-bit unsigned integer.
Definition PdfDeclarations.h:153
@ Number
Number datatype for integer values.
Definition PdfDeclarations.h:155
@ Bool
Boolean datatype: Accepts the values "true" and "false".
Definition PdfDeclarations.h:154
@ RawData
Raw PDF data.
Definition PdfDeclarations.h:163
PdfWriteFlags
Specify additional options for writing the PDF.
Definition PdfDeclarations.h:136