PoDoFo 1.2.0
Loading...
Searching...
No Matches
PdfName.h
1// SPDX-FileCopyrightText: 2006 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_NAME_H
6#define PDF_NAME_H
7
8#include "PdfBaseDataTypes.h"
9
10namespace PoDoFo {
11
20class PODOFO_API PdfName final : private PdfDataMember, public PdfDataProvider<PdfName>
21{
22public:
24 static const PdfName Null;
25
26public:
29 PdfName();
30
31 ~PdfName();
32
33 template<std::size_t N>
34 PdfName(const char(&str)[N])
36 {
37 initFromUtf8String(str, N - 1);
38 }
39
40 template<typename T, typename = std::enable_if_t<std::is_same_v<T, const char*>>>
41 PdfName(T str)
43 {
44 initFromUtf8String(str, std::char_traits<char>::length(str));
45 }
46
53 PdfName(const std::string_view& str);
54 PdfName(const std::string& str);
56
61 PdfName(const char& str, size_t length);
62
65 PdfName(const PdfName& rhs);
66 PdfName(PdfName&& rhs) noexcept;
67
68 static PdfName FromRaw(const bufferview& rawcontent);
69
75 static PdfName FromEscaped(const std::string_view& name);
76
82 std::string GetEscapedName() const;
83
85 const PdfStatefulEncrypt* encrypt, charbuff& buffer) const;
86
89 std::string_view GetString() const;
90
92 bool IsNull() const;
93
95 std::string_view GetRawData() const;
96
99 PdfName& operator=(const PdfName& rhs);
100 PdfName& operator=(PdfName&& rhs) noexcept;
101
104 bool operator==(const PdfName& rhs) const;
105 bool operator==(const char* str) const;
106 bool operator==(const std::string& str) const;
107 bool operator==(const std::string_view& view) const;
108
111 bool operator!=(const PdfName& rhs) const;
112 bool operator!=(const char* str) const;
113 bool operator!=(const std::string& str) const;
114 bool operator!=(const std::string_view& view) const;
115
119 operator std::string_view() const;
120
121private:
122 // Delete constructor with nullptr
123 PdfName(std::nullptr_t) = delete;
124
125 void expandUtf8String();
129 void initFromUtf8String(const char* str, size_t length);
130
133 void initFromUtf8String(const std::string_view& view);
134 void moveFrom(PdfName&& rhs);
135
136private:
137 struct NameData
138 {
139 // The unescaped name raw data, without leading '/'.
140 // It can store also the utf8 expanded string, if coincident
141 charbuff Chars;
142 std::unique_ptr<std::string> Utf8String;
143 bool IsUtf8Expanded;
144 };
145private:
146 bool m_dataAllocated;
147 union
148 {
149 std::shared_ptr<NameData> m_data;
150 std::string_view m_Utf8View; // Holds only global read-only string literal
151 };
152};
153
157inline PdfName operator""_n(const char* name, size_t length)
158{
159 return PdfName(*name, length);
160}
161
162// Comparator to enable heterogeneous lookup in
163// PdfDictionary with both PdfName and string_view
164// See https://stackoverflow.com/a/31924435/213871
165struct PODOFO_API PdfNameInequality
166{
167 using is_transparent = std::true_type;
168
169 bool operator()(const PdfName& lhs, const PdfName& rhs) const
170 {
171 return lhs.GetRawData() < rhs.GetRawData();
172 }
173 bool operator()(const PdfName& lhs, const std::string_view& rhs) const
174 {
175 return lhs.GetRawData() < rhs;
176 }
177 bool operator()(const std::string_view& lhs, const PdfName& rhs) const
178 {
179 return lhs < rhs.GetRawData();
180 }
181};
182
183struct PODOFO_API PdfNameHashing
184{
185 using is_transparent = std::true_type;
186
187 inline std::size_t operator()(const std::string_view& name) const
188 {
189 return std::hash<std::string_view>()(name);
190 }
191 inline std::size_t operator()(const PdfName& name) const
192 {
193 return std::hash<std::string_view>()(name);
194 }
195};
196
197struct PODOFO_API PdfNameEquality
198{
199 using is_transparent = std::true_type;
200
201 inline bool operator()(const PdfName& lhs, const PdfName& rhs) const
202 {
203 return lhs.GetRawData() == rhs.GetRawData();
204 }
205 inline bool operator()(const PdfName& lhs, const std::string_view& rhs) const
206 {
207 return lhs.GetRawData() == rhs;
208 }
209 inline bool operator()(const std::string_view& lhs, const PdfName& rhs) const
210 {
211 return lhs == rhs.GetRawData();
212 }
213};
214
215template<typename TValue>
216using PdfNameMap = std::map<PdfName, TValue, PdfNameInequality>;
217
218template<typename TValue>
219using PdfNameHashMap = std::unordered_map<PdfName, TValue, PdfNameHashing, PdfNameEquality>;
220
221};
222
223#endif // PDF_NAME_H
An interface for writing blocks of data to a data source.
Definition OutputStream.h:15
A class to inherit for classes that are stored as union members in a PdfVariant.
Definition PdfBaseDataTypes.h:18
An helper class to inherit to provide common serialization methods.
Definition PdfBaseDataTypes.h:36
This class represents a PdfName.
Definition PdfName.h:21
PdfName(const std::string_view &str)
Create a new PdfName object.
static const PdfName Null
Null name, corresponds to "/".
Definition PdfName.h:24
Convenient type for char array storage and/or buffer with std::string compatibility.
Definition basetypes.h:30
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
@ Name
Name datatype. Names are used as keys in dictionary to reference values.
PdfWriteFlags
Specify additional options for writing the PDF.
Definition PdfDeclarations.h:136
cspan< char > bufferview
Convenient read-only char buffer span.
Definition basetypes.h:15