VTK  9.4.20250303
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
29#include <token/Token.h>
30
31#include "vtkCompiler.h" // for VTK_COMPILER_GCC
32#include "vtkSmartPointer.h" // for ivar
33
34//clang-format off
35#include <vtk_nlohmannjson.h>
36#include VTK_NLOHMANN_JSON(json.hpp)
37//clang-format on
38
39#include <cstdint> // for `std::uint*_t`
40#include <unordered_set> // for membership API
41
42VTK_ABI_NAMESPACE_BEGIN
43
44class VTKCOMMONCORE_EXPORT vtkStringToken
45{
46public:
47 using Hash = std::uint32_t;
48
50 VTK_WRAPEXCLUDE vtkStringToken(const char* data = nullptr, std::size_t size = std::string::npos);
52 vtkStringToken(const std::string& data);
55 constexpr vtkStringToken(Hash tokenId) noexcept
56 : Id(tokenId)
57 {
58 }
59
61 Hash GetId() const { return this->Id; }
63 unsigned int GetHash() const { return static_cast<unsigned int>(this->Id); }
65 const std::string& Data() const;
66
70 bool IsValid() const;
72 bool HasData() const;
73
75 bool operator==(const vtkStringToken& other) const;
77 bool operator!=(const vtkStringToken& other) const;
78
80 bool operator<(const vtkStringToken& other) const;
81 bool operator>(const vtkStringToken& other) const;
82 bool operator<=(const vtkStringToken& other) const;
83 bool operator>=(const vtkStringToken& other) const;
84
87 static constexpr Hash StringHash(const char* data, std::size_t size) noexcept
88 {
89 return token_NAMESPACE::Token::stringHash(data, size);
90 }
91
93 static Hash InvalidHash();
94
116 std::unordered_set<vtkStringToken> Children(bool recursive = true);
118 static std::unordered_set<vtkStringToken> AllGroups();
120
121protected:
123};
124
130inline void VTKCOMMONCORE_EXPORT from_json(const nlohmann::json& jj, vtkStringToken& tt)
131{
132 if (jj.is_number_integer())
133 {
134 tt = vtkStringToken(jj.get<vtkStringToken::Hash>());
135 }
136 else if (jj.is_string())
137 {
138 tt = jj.get<std::string>();
139 }
140 else
141 {
142 throw std::runtime_error("String tokens must be JSON integers or strings.");
143 }
144}
145
150inline void VTKCOMMONCORE_EXPORT to_json(nlohmann::json& jj, const vtkStringToken& tt)
151{
152 if (tt.HasData())
153 {
154 jj = tt.Data();
155 }
156 else
157 {
158 jj = tt.GetId();
159 }
160}
161
162VTK_ABI_NAMESPACE_END
163
164namespace vtk
165{
166namespace literals
167{
168VTK_ABI_NAMESPACE_BEGIN
169
190constexpr VTKCOMMONCORE_EXPORT vtkStringToken::Hash operator""_hash(
191 const char* data, std::size_t size)
192{
193 return vtkStringToken::StringHash(data, size);
194}
195
205constexpr VTKCOMMONCORE_EXPORT vtkStringToken operator""_token(const char* data, std::size_t size)
206{
207 return vtkStringToken(vtkStringToken::StringHash(data, size));
208}
209
210VTK_ABI_NAMESPACE_END
211} // namespace literals
212} // namespace vtk
213
214VTK_ABI_NAMESPACE_BEGIN
215bool VTKCOMMONCORE_EXPORT operator==(const std::string& a, const vtkStringToken& b);
216bool VTKCOMMONCORE_EXPORT operator!=(const std::string& a, const vtkStringToken& b);
217bool VTKCOMMONCORE_EXPORT operator>(const std::string& a, const vtkStringToken& b);
218bool VTKCOMMONCORE_EXPORT operator<(const std::string& a, const vtkStringToken& b);
219bool VTKCOMMONCORE_EXPORT operator>=(const std::string& a, const vtkStringToken& b);
220bool VTKCOMMONCORE_EXPORT operator<=(const std::string& a, const vtkStringToken& b);
221
222bool VTKCOMMONCORE_EXPORT operator==(const vtkStringToken& a, const std::string& b);
223bool VTKCOMMONCORE_EXPORT operator!=(const vtkStringToken& a, const std::string& b);
224bool VTKCOMMONCORE_EXPORT operator>(const vtkStringToken& a, const std::string& b);
225bool VTKCOMMONCORE_EXPORT operator<(const vtkStringToken& a, const std::string& b);
226bool VTKCOMMONCORE_EXPORT operator>=(const vtkStringToken& a, const std::string& b);
227bool VTKCOMMONCORE_EXPORT operator<=(const vtkStringToken& a, const std::string& b);
228
229bool VTKCOMMONCORE_EXPORT operator==(const char* a, const vtkStringToken& b);
230bool VTKCOMMONCORE_EXPORT operator!=(const char* a, const vtkStringToken& b);
231bool VTKCOMMONCORE_EXPORT operator>(const char* a, const vtkStringToken& b);
232bool VTKCOMMONCORE_EXPORT operator<(const char* a, const vtkStringToken& b);
233bool VTKCOMMONCORE_EXPORT operator>=(const char* a, const vtkStringToken& b);
234bool VTKCOMMONCORE_EXPORT operator<=(const char* a, const vtkStringToken& b);
235
236bool VTKCOMMONCORE_EXPORT operator==(const vtkStringToken& a, const char* b);
237bool VTKCOMMONCORE_EXPORT operator!=(const vtkStringToken& a, const char* b);
238bool VTKCOMMONCORE_EXPORT operator>(const vtkStringToken& a, const char* b);
239bool VTKCOMMONCORE_EXPORT operator<(const vtkStringToken& a, const char* b);
240bool VTKCOMMONCORE_EXPORT operator>=(const vtkStringToken& a, const char* b);
241bool VTKCOMMONCORE_EXPORT operator<=(const vtkStringToken& a, const char* b);
242VTK_ABI_NAMESPACE_END
243
244namespace std
245{
248template <>
249struct VTKCOMMONCORE_EXPORT hash<vtkStringToken>
250{
251 std::size_t operator()(const vtkStringToken& t) const { return t.GetId(); }
252};
253} // namespace std
254
255#endif // vtkStringToken_h
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 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.
VTK_WRAPEXCLUDE vtkStringToken(const char *data=nullptr, std::size_t size=std::string::npos)
Construct a token from a string literal.
bool IsValid() const
Return whether the token is valid or not.
bool HasData() const
Return whether a string is available for the token's hash ID.
bool operator!=(const vtkStringToken &other) const
Fast inequality comparison (compares hashes, not strings).
bool AddChild(vtkStringToken member)
Methods to manage groups of tokens underneath a parent.
std::uint32_t Hash
const std::string & Data() const
Return the string corresponding to the token.
bool operator>=(const vtkStringToken &other) const
bool RemoveChild(vtkStringToken member)
Remove a member from this token's children.
std::unordered_set< vtkStringToken > Children(bool recursive=true)
Return all the children of this token.
static std::unordered_set< vtkStringToken > AllGroups()
Return all the tokens that have children.
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).
unsigned int GetHash() const
A Python-wrappable (but less strongly typed) alternative to GetId()
static Hash InvalidHash()
Return the hash code used to indicate an invalid (empty) token.
Specialization of tuple ranges and iterators for vtkAOSDataArrayTemplate.
std::size_t operator()(const vtkStringToken &t) const
void VTKCOMMONCORE_EXPORT to_json(nlohmann::json &jj, const vtkStringToken &tt)
Convert a string token into a JSON value.
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)
void VTKCOMMONCORE_EXPORT from_json(const nlohmann::json &jj, vtkStringToken &tt)
Convert a JSON value into a string token.
#define VTK_WRAPEXCLUDE