VTK  9.3.0
vtkStringToken.h
Go to the documentation of this file.
1// SPDX-FileCopyrightText: Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
2// SPDX-License-Identifier: BSD-3-Clause
3#ifndef vtkStringToken_h
4#define vtkStringToken_h
26#include "vtkSmartPointer.h"
27
28#include <cstdint> // for `std::uint*_t`
29
30VTK_ABI_NAMESPACE_BEGIN
32
33class VTKCOMMONCORE_EXPORT vtkStringToken
34{
35public:
36 using Hash = std::uint32_t;
37
39 vtkStringToken(const char* data = nullptr, std::size_t size = std::string::npos);
41 vtkStringToken(const std::string& data);
44 inline constexpr vtkStringToken(Hash tokenId) noexcept
45 : Id(tokenId)
46 {
47 }
48
50 Hash GetId() const { return this->Id; }
52 const std::string& Data() const;
53
55 bool operator==(const vtkStringToken& other) const;
57 bool operator!=(const vtkStringToken& other) const;
58
60 bool operator<(const vtkStringToken& other) const;
61 bool operator>(const vtkStringToken& other) const;
62 bool operator<=(const vtkStringToken& other) const;
63 bool operator>=(const vtkStringToken& other) const;
64
67 inline static constexpr Hash StringHash(const char* data, std::size_t size) noexcept
68 {
69 return vtkStringToken::hash_32_fnv1a_const(data, size);
70 }
71
74
75protected:
78
80
81 // ----
82 // Adapted from https://notes.underscorediscovery.com/constexpr-fnv1a/index.html
83 // which declared the source as public domain or equivalent. Retrieved on 2022-07-22.
84 static constexpr uint32_t hash32a_const = 0x811c9dc5;
85 static constexpr uint32_t hash32b_const = 0x1000193;
86 static constexpr uint64_t hash64a_const = 0xcbf29ce484222325;
87 static constexpr uint64_t hash64b_const = 0x100000001b3;
88
89 // Compute a 32-bit hash of a string.
90 // Unlike the original, this version handles embedded null characters so that
91 // unicode multi-byte sequences can be hashed.
92 inline static constexpr uint32_t hash_32_fnv1a_const(
93 const char* const str, std::size_t size, const uint32_t value = hash32a_const) noexcept
94 {
95 return (!str || size <= 0)
96 ? value
97 : hash_32_fnv1a_const(&str[1], size - 1, (value ^ uint32_t(str[0])) * hash32b_const);
98 }
99
100#if 0
101 // Compute a 64-bit hash of a string.
102 inline static constexpr uint64_t hash_64_fnv1a_const(
103 const char* const str, std::size_t size, const uint64_t value = hash64a_const) noexcept
104 {
105 return (!str || size <= 0) ? value :
106 hash_64_fnv1a_const(&str[1], size - 1, (value ^ uint64_t(str[0])) * hash64b_const);
107 }
108#endif
109};
110VTK_ABI_NAMESPACE_END
111
112#ifndef __VTK_WRAP__
113namespace vtk
114{
115namespace literals
116{
117VTK_ABI_NAMESPACE_BEGIN
118
139inline constexpr VTKCOMMONCORE_EXPORT vtkStringToken::Hash operator"" _hash(
140 const char* data, std::size_t size)
141{
142 return vtkStringToken::StringHash(data, size);
143}
144
154inline constexpr VTKCOMMONCORE_EXPORT vtkStringToken operator"" _token(
155 const char* data, std::size_t size)
156{
157 return vtkStringToken(vtkStringToken::StringHash(data, size));
158}
159
160VTK_ABI_NAMESPACE_END
161} // namespace literals
162} // namespace vtk
163#endif // __VTK_WRAP__
164
165VTK_ABI_NAMESPACE_BEGIN
166bool VTKCOMMONCORE_EXPORT operator==(const std::string& a, const vtkStringToken& b);
167bool VTKCOMMONCORE_EXPORT operator!=(const std::string& a, const vtkStringToken& b);
168bool VTKCOMMONCORE_EXPORT operator>(const std::string& a, const vtkStringToken& b);
169bool VTKCOMMONCORE_EXPORT operator<(const std::string& a, const vtkStringToken& b);
170bool VTKCOMMONCORE_EXPORT operator>=(const std::string& a, const vtkStringToken& b);
171bool VTKCOMMONCORE_EXPORT operator<=(const std::string& a, const vtkStringToken& b);
172
173bool VTKCOMMONCORE_EXPORT operator==(const vtkStringToken& a, const std::string& b);
174bool VTKCOMMONCORE_EXPORT operator!=(const vtkStringToken& a, const std::string& b);
175bool VTKCOMMONCORE_EXPORT operator>(const vtkStringToken& a, const std::string& b);
176bool VTKCOMMONCORE_EXPORT operator<(const vtkStringToken& a, const std::string& b);
177bool VTKCOMMONCORE_EXPORT operator>=(const vtkStringToken& a, const std::string& b);
178bool VTKCOMMONCORE_EXPORT operator<=(const vtkStringToken& a, const std::string& b);
179
180bool VTKCOMMONCORE_EXPORT operator==(const char* a, const vtkStringToken& b);
181bool VTKCOMMONCORE_EXPORT operator!=(const char* a, const vtkStringToken& b);
182bool VTKCOMMONCORE_EXPORT operator>(const char* a, const vtkStringToken& b);
183bool VTKCOMMONCORE_EXPORT operator<(const char* a, const vtkStringToken& b);
184bool VTKCOMMONCORE_EXPORT operator>=(const char* a, const vtkStringToken& b);
185bool VTKCOMMONCORE_EXPORT operator<=(const char* a, const vtkStringToken& b);
186
187bool VTKCOMMONCORE_EXPORT operator==(const vtkStringToken& a, const char* b);
188bool VTKCOMMONCORE_EXPORT operator!=(const vtkStringToken& a, const char* b);
189bool VTKCOMMONCORE_EXPORT operator>(const vtkStringToken& a, const char* b);
190bool VTKCOMMONCORE_EXPORT operator<(const vtkStringToken& a, const char* b);
191bool VTKCOMMONCORE_EXPORT operator>=(const vtkStringToken& a, const char* b);
192bool VTKCOMMONCORE_EXPORT operator<=(const vtkStringToken& a, const char* b);
193VTK_ABI_NAMESPACE_END
194
195namespace std
196{
199template <>
200struct VTKCOMMONCORE_EXPORT hash<vtkStringToken>
201{
202 std::size_t operator()(const vtkStringToken& t) const { return t.GetId(); }
203};
204} // namespace std
205#endif // vtkStringToken_h
Hold a reference to a vtkObjectBase instance.
Manage string-token mappings.
Represent a string by its integer hash.
bool operator<=(const vtkStringToken &other) const
bool operator<(const vtkStringToken &other) const
Slow, but unsurprising string comparison (preserves lexical string ordering).
static vtkSmartPointer< vtkStringManager > Manager
static constexpr Hash StringHash(const char *data, std::size_t size) noexcept
Return the hash of a string This is used internally but also by the ""_token() literal operator.
constexpr vtkStringToken(Hash tokenId) noexcept
Construct a token given its hash value.
static const vtkStringManager * GetManager()
Return the database of strings and their tokens (hashes).
vtkStringToken(const char *data=nullptr, std::size_t size=std::string::npos)
Construct a token from a string literal.
bool operator!=(const vtkStringToken &other) const
Fast inequality comparison (compares hashes, not strings).
std::uint32_t Hash
const std::string & Data() const
Return the string corresponding to the token.
bool operator>=(const vtkStringToken &other) const
static constexpr uint32_t hash_32_fnv1a_const(const char *const str, std::size_t size, const uint32_t value=hash32a_const) noexcept
vtkStringToken(const std::string &data)
Construct a token from a std::string.
Hash GetId() const
Return the token's ID (usually its hash but possibly not in the case of collisions).
bool operator>(const vtkStringToken &other) const
bool operator==(const vtkStringToken &other) const
Fast equality comparison (compares hashes, not strings).
static vtkStringManager * GetManagerInternal()
Specialization of tuple ranges and iterators for vtkAOSDataArrayTemplate.
std::size_t operator()(const vtkStringToken &t) const
bool VTKCOMMONCORE_EXPORT operator>=(const std::string &a, const vtkStringToken &b)
bool VTKCOMMONCORE_EXPORT operator==(const std::string &a, const vtkStringToken &b)
bool VTKCOMMONCORE_EXPORT operator>(const std::string &a, const vtkStringToken &b)
bool VTKCOMMONCORE_EXPORT operator<=(const std::string &a, const vtkStringToken &b)
bool VTKCOMMONCORE_EXPORT operator<(const std::string &a, const vtkStringToken &b)
bool VTKCOMMONCORE_EXPORT operator!=(const std::string &a, const vtkStringToken &b)