PoDoFo  1.0.0-dev
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 
14 namespace PoDoFo {
15 
16 class PdfArray;
17 class PdfData;
18 class PdfDictionary;
19 class PdfString;
20 class PdfDataContainer;
21 
32 class 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 
40 public:
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 
80  PdfVariant(const PdfReference& ref);
81 
89  PdfVariant(const PdfArray& arr);
90  PdfVariant(PdfArray&& arr) noexcept;
91 
95  PdfVariant(const PdfDictionary& dict);
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 
291  void Write(OutputStream& stream, PdfWriteFlags writeMode,
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 
316 private:
317  // Delete constructor with nullptr
318  PdfVariant(std::nullptr_t) = delete;
319 
320  PdfReference GetReferenceUnsafe() const;
321  const PdfDictionary& GetDictionaryUnsafe() const;
322  const PdfArray& GetArrayUnsafe() const;
323  PdfDictionary& GetDictionaryUnsafe();
324  PdfArray& GetArrayUnsafe();
325 
326 private:
327  void assign(const PdfVariant& rhs);
328  bool tryGetDictionary(PdfDictionary*& dict) const;
329  bool tryGetArray(PdfArray*& arr) const;
330  bool tryGetName(const PdfName*& name) const;
331  bool tryGetString(const PdfString*& str) const;
332  void moveFrom(PdfVariant&& rhs);
333 
334 private:
335  // Reset the variant to "null" type. To be called by PdfTokenizer
336  void Reset();
337 
362  template<typename T>
363  PdfVariant(T*) = delete;
364 
365  template <typename T>
366  class PrimitiveMember : PdfDataMember
367  {
368  public:
369  PrimitiveMember(T value)
370  : PdfDataMember(getDataType()), Value(value) { }
371 
372  constexpr PdfDataType getDataType()
373  {
374  if (std::is_same_v<T, int64_t>)
375  return PdfDataType::Number;
376  else if (std::is_same_v<T, double>)
377  return PdfDataType::Real;
378  else if (std::is_same_v<T, bool>)
379  return PdfDataType::Bool;
380  else if (std::is_same_v<T, PdfDictionary*>)
382  else if (std::is_same_v<T, PdfArray*>)
383  return PdfDataType::Array;
384  else if (std::is_same_v<T, PdfData*>)
385  return PdfDataType::RawData;
386  else
387  return PdfDataType::Unknown;
388  }
389 
390  public:
391  T Value;
392  };
393 
394  class NullMember : PdfDataMember
395  {
396  public:
397  NullMember();
398  };
399 
404  union
405  {
410  PdfName m_Name;
411  PdfReference m_Reference;
412  PrimitiveMember<int64_t> m_Number;
413  PrimitiveMember<double> m_Real;
414  PrimitiveMember<bool> m_Bool;
415  PrimitiveMember<PdfDictionary*> m_Dictionary;
416  PrimitiveMember<PdfArray*> m_Array;
417  PrimitiveMember<PdfData*> m_Data;
418  NullMember m_Null;
419  };
420 };
421 
422 };
423 
424 #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:53
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:409
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:155
@ Dictionary
A dictionary associates keys with values. A key can have another dictionary as value.
@ Array
An array of other PDF data types.
@ Real
Real datatype for floating point numbers.
@ Unknown
The Datatype is unknown. The value is chosen to enable value storage in 8-bit unsigned integer.
@ Number
Number datatype for integer values.
@ Null
The null datatype is always null.
@ Bool
Boolean datatype: Accepts the values "true" and "false".
@ RawData
Raw PDF data.
PdfWriteFlags
Specify additional options for writing the PDF.
Definition: PdfDeclarations.h:137