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{
25public:
28 static const PdfName Null;
29
30public:
34 PdfName();
35
36 ~PdfName();
37
38 template<std::size_t N>
39 PdfName(const char(&str)[N])
41 {
42 initFromUtf8String(str, N - 1);
43 }
44
45 template<typename T, typename = std::enable_if_t<std::is_same_v<T, const char*>>>
46 PdfName(T str)
48 {
49 initFromUtf8String(str, std::char_traits<char>::length(str));
50 }
51
59 PdfName(const std::string_view& str);
60 PdfName(const std::string& str);
62
67 PdfName(const char& str, size_t length);
68
72 PdfName(const PdfName& rhs);
73 PdfName(PdfName&& rhs) noexcept;
74
75 static PdfName FromRaw(const bufferview& rawcontent);
76
83 static PdfName FromEscaped(const std::string_view& name);
84
91 std::string GetEscapedName() const;
92
94 const PdfStatefulEncrypt* encrypt, charbuff& buffer) const;
95
99 std::string_view GetString() const;
100
103 bool IsNull() const;
104
107 std::string_view GetRawData() const;
108
112 PdfName& operator=(const PdfName& rhs);
113 PdfName& operator=(PdfName&& rhs) noexcept;
114
118 bool operator==(const PdfName& rhs) const;
119 bool operator==(const char* str) const;
120 bool operator==(const std::string& str) const;
121 bool operator==(const std::string_view& view) const;
122
126 bool operator!=(const PdfName& rhs) const;
127 bool operator!=(const char* str) const;
128 bool operator!=(const std::string& str) const;
129 bool operator!=(const std::string_view& view) const;
130
135 operator std::string_view() const;
136
137private:
138 // Delete constructor with nullptr
139 PdfName(std::nullptr_t) = delete;
140
141 void expandUtf8String();
142 void initFromUtf8String(const char* str, size_t length);
143 void initFromUtf8String(const std::string_view& view);
144 void moveFrom(PdfName&& rhs);
145
146private:
147 struct NameData
148 {
149 // The unescaped name raw data, without leading '/'.
150 // It can store also the utf8 expanded string, if coincident
151 charbuff Chars;
152 std::unique_ptr<std::string> Utf8String;
153 bool IsUtf8Expanded;
154 };
155private:
156 bool m_dataAllocated;
157 union
158 {
159 std::shared_ptr<NameData> m_data;
160 std::string_view m_Utf8View; // Holds only global read-only string literal
161 };
162};
163
168inline PdfName operator""_n(const char* name, size_t length)
169{
170 return PdfName(*name, length);
171}
172
173// Comparator to enable heterogeneous lookup in
174// PdfDictionary with both PdfName and string_view
175// See https://stackoverflow.com/a/31924435/213871
176struct PODOFO_API PdfNameInequality
177{
178 using is_transparent = std::true_type;
179
180 bool operator()(const PdfName& lhs, const PdfName& rhs) const
181 {
182 return lhs.GetRawData() < rhs.GetRawData();
183 }
184 bool operator()(const PdfName& lhs, const std::string_view& rhs) const
185 {
186 return lhs.GetRawData() < rhs;
187 }
188 bool operator()(const std::string_view& lhs, const PdfName& rhs) const
189 {
190 return lhs < rhs.GetRawData();
191 }
192};
193
194struct PODOFO_API PdfNameHashing
195{
196 using is_transparent = std::true_type;
197
198 inline std::size_t operator()(const std::string_view& name) const
199 {
200 return std::hash<std::string_view>()(name);
201 }
202 inline std::size_t operator()(const PdfName& name) const
203 {
204 return std::hash<std::string_view>()(name);
205 }
206};
207
208struct PODOFO_API PdfNameEquality
209{
210 using is_transparent = std::true_type;
211
212 inline bool operator()(const PdfName& lhs, const PdfName& rhs) const
213 {
214 return lhs.GetRawData() == rhs.GetRawData();
215 }
216 inline bool operator()(const PdfName& lhs, const std::string_view& rhs) const
217 {
218 return lhs.GetRawData() == rhs;
219 }
220 inline bool operator()(const std::string_view& lhs, const PdfName& rhs) const
221 {
222 return lhs == rhs.GetRawData();
223 }
224};
225
226template<typename TValue>
227using PdfNameMap = std::map<PdfName, TValue, PdfNameInequality>;
228
229template<typename TValue>
230using PdfNameHashMap = std::unordered_map<PdfName, TValue, PdfNameHashing, PdfNameEquality>;
231
232};
233
234#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:28
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