PoDoFo 1.0.0-dev
Loading...
Searching...
No Matches
PdfName.h
1
7#ifndef PDF_NAME_H
8#define PDF_NAME_H
9
10#include "PdfBaseDataTypes.h"
11
12namespace PoDoFo {
13
23class PODOFO_API PdfName final : private PdfDataMember, public PdfDataProvider<PdfName>
24{
25 friend PdfName PODOFO_API operator""_n(const char*, size_t);
26
27public:
30 static const PdfName Null;
31
32public:
36 PdfName();
37
38 ~PdfName();
39
40 template<std::size_t N>
41 PdfName(const char(&str)[N])
43 {
44 initFromUtf8String(str, N - 1);
45 }
46
47 template<typename T, typename = std::enable_if_t<std::is_same_v<T, const char*>>>
48 PdfName(T str)
50 {
51 initFromUtf8String(str, std::char_traits<char>::length(str));
52 }
53
61 PdfName(const std::string_view& str);
62 PdfName(const std::string& str);
64
68 PdfName(const PdfName& rhs);
69 PdfName(PdfName&& rhs) noexcept;
70
71 static PdfName FromRaw(const bufferview& rawcontent);
72
79 static PdfName FromEscaped(const std::string_view& name);
80
87 std::string GetEscapedName() const;
88
90 const PdfStatefulEncrypt* encrypt, charbuff& buffer) const;
91
95 std::string_view GetString() const;
96
99 bool IsNull() const;
100
103 std::string_view GetRawData() const;
104
108 PdfName& operator=(const PdfName& rhs);
109 PdfName& operator=(PdfName&& rhs) noexcept;
110
114 bool operator==(const PdfName& rhs) const;
115 bool operator==(const char* str) const;
116 bool operator==(const std::string& str) const;
117 bool operator==(const std::string_view& view) const;
118
122 bool operator!=(const PdfName& rhs) const;
123 bool operator!=(const char* str) const;
124 bool operator!=(const std::string& str) const;
125 bool operator!=(const std::string_view& view) const;
126
131 operator std::string_view() const;
132
133private:
134 // Constructor for read-only string literals
135 PdfName(const char* str, size_t length);
136
137 // Delete constructor with nullptr
138 PdfName(std::nullptr_t) = delete;
139
140 void expandUtf8String();
141 void initFromUtf8String(const char* str, size_t length);
142 void initFromUtf8String(const std::string_view& view);
143 void moveFrom(PdfName&& rhs);
144
145private:
146 struct NameData
147 {
148 // The unescaped name raw data, without leading '/'.
149 // It can store also the utf8 expanded string, if coincident
150 charbuff Chars;
151 std::unique_ptr<std::string> Utf8String;
152 bool IsUtf8Expanded;
153 };
154private:
155 bool m_dataAllocated;
156 union
157 {
158 std::shared_ptr<NameData> m_data;
159 std::string_view m_Utf8View; // Holds only global read-only string literal
160 };
161};
162
167inline PdfName operator""_n(const char* name, size_t length)
168{
169 return PdfName(name, length);
170}
171
172// Comparator to enable heterogeneous lookup in
173// PdfDictionary with both PdfName and string_view
174// See https://stackoverflow.com/a/31924435/213871
175struct PODOFO_API PdfNameInequality
176{
177 using is_transparent = std::true_type;
178
179 bool operator()(const PdfName& lhs, const PdfName& rhs) const
180 {
181 return lhs.GetRawData() < rhs.GetRawData();
182 }
183 bool operator()(const PdfName& lhs, const std::string_view& rhs) const
184 {
185 return lhs.GetRawData() < rhs;
186 }
187 bool operator()(const std::string_view& lhs, const PdfName& rhs) const
188 {
189 return lhs < rhs.GetRawData();
190 }
191};
192
193struct PODOFO_API PdfNameHashing
194{
195 using is_transparent = std::true_type;
196
197 inline std::size_t operator()(const std::string_view& name) const
198 {
199 return std::hash<std::string_view>()(name);
200 }
201 inline std::size_t operator()(const PdfName& name) const
202 {
203 return std::hash<std::string_view>()(name);
204 }
205};
206
207struct PODOFO_API PdfNameEquality
208{
209 using is_transparent = std::true_type;
210
211 inline bool operator()(const PdfName& lhs, const PdfName& rhs) const
212 {
213 return lhs.GetRawData() == rhs.GetRawData();
214 }
215 inline bool operator()(const PdfName& lhs, const std::string_view& rhs) const
216 {
217 return lhs.GetRawData() == rhs;
218 }
219 inline bool operator()(const std::string_view& lhs, const PdfName& rhs) const
220 {
221 return lhs == rhs.GetRawData();
222 }
223};
224
225template<typename TValue>
226using PdfNameMap = std::map<PdfName, TValue, PdfNameInequality>;
227
228template<typename TValue>
229using PdfNameHashMap = std::unordered_map<PdfName, TValue, PdfNameHashing, PdfNameEquality>;
230
231};
232
233#endif // PDF_NAME_H
An interface for writing blocks of data to a data source.
Definition OutputStream.h:18
A class to inherit for classes that are stored as union members in a PdfVariant.
Definition PdfBaseDataTypes.h:21
An helper class to inherit to provide common serialization methods.
Definition PdfBaseDataTypes.h:40
This class represents a PdfName.
Definition PdfName.h:24
PdfName(const std::string_view &str)
Create a new PdfName object.
static const PdfName Null
Null name, corresponds to "/".
Definition PdfName.h:30
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
@ Name
Name datatype. Names are used as keys in dictionary to reference values.
PdfWriteFlags
Specify additional options for writing the PDF.
Definition PdfDeclarations.h:149
cspan< char > bufferview
Convenient read-only char buffer span.
Definition basetypes.h:19