VTK  9.3.0
vtkStringManager.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 vtkStringManager_h
4#define vtkStringManager_h
17#include "vtkObject.h"
18#include "vtkStringToken.h" // for vtkStringToken::Hash type-alias.
19
20#include <functional>
21#include <mutex>
22#include <unordered_map>
23#include <unordered_set>
24
25VTK_ABI_NAMESPACE_BEGIN
26
27class VTKCOMMONCORE_EXPORT vtkStringManager : public vtkObject
28{
29public:
32 void PrintSelf(ostream& os, vtkIndent indent) override;
33
35 enum Visit
36 {
38 Continue
39 };
40
42 using Hash = std::uint32_t;
43
45 using Visitor = std::function<Visit(Hash entry)>;
47 static constexpr Hash Invalid = 0;
48
50 Hash Manage(const std::string& ss);
53 std::size_t Unmanage(Hash hh);
54
56 const std::string& Value(Hash hh) const;
59 Hash Find(const std::string& s) const;
67 Hash Compute(const std::string& ss) const;
68
77 Hash Insert(const std::string& ss, Hash hh);
78 bool Insert(Hash ss, Hash hh);
82 bool Remove(const std::string& ss, Hash hh);
83 bool Remove(Hash ss, Hash hh);
84
90 bool Contains(const std::string& ss, Hash hh) const;
91 bool Contains(Hash ss, Hash hh) const;
92 bool Contains(Hash hh) const { return this->Contains(Invalid, hh); }
93
95 bool Empty() const { return this->Data.empty(); }
96
105 Visit VisitMembers(Visitor visitor, Hash set = Invalid) const;
106
115 Visit VisitSets(Visitor visitor) const;
116
118 void Reset();
119
120protected:
121 vtkStringManager() = default;
122
123private:
124 using LockGuard = std::lock_guard<std::mutex>;
126 std::pair<Hash, bool> ComputeInternal(const std::string& s, const LockGuard& proofOfLock) const;
127 std::pair<Hash, bool> ComputeInternalAndInsert(
128 const std::string& s, const LockGuard& proofOfLock);
129 std::size_t UnmanageInternal(Hash hh, const LockGuard& proofOfLock);
130
131 std::unordered_map<Hash, std::string> Data;
132 std::unordered_map<Hash, std::unordered_set<Hash>> Sets;
133 mutable std::mutex WriteLock;
134
135 vtkStringManager(const vtkStringManager&) = delete;
136 void operator=(const vtkStringManager&) = delete;
137};
138
139VTK_ABI_NAMESPACE_END
140#endif // vtkStringManager_h
a simple class to control print indentation
Definition vtkIndent.h:29
abstract base class for most VTK objects
Definition vtkObject.h:49
Manage string-token mappings.
const std::string & Value(Hash hh) const
Look up a string from its hashed value, hh.
bool Contains(const std::string &ss, Hash hh) const
Return true if the set ss exists and contains hash hh ; and false otherwise.
void PrintSelf(ostream &os, vtkIndent indent) override
Methods invoked by print to print information about the object including superclasses.
bool Contains(Hash ss, Hash hh) const
bool Empty() const
Return true if the manager is empty (i.e., managing no hashes) and false otherwise.
Visit
An enumerant visitors return to terminate early (or not).
@ Halt
Terminate visitation.
std::size_t Unmanage(Hash hh)
Remove a hash from the manager.
Hash Find(const std::string &s) const
Look up a hash from a string value (without inserting it).
Hash Manage(const std::string &ss)
Insert a string into the manager by computing a unique hash (the returned value).
Hash Compute(const std::string &ss) const
Compute a hash from a string value (without inserting it into the manager).
Hash Insert(const std::string &ss, Hash hh)
Add the hash hh to the set ss.
bool Remove(Hash ss, Hash hh)
static vtkStringManager * New()
bool Contains(Hash hh) const
vtkStringManager()=default
Visit VisitSets(Visitor visitor) const
Visit all set names in the manager.
bool Insert(Hash ss, Hash hh)
Visit VisitMembers(Visitor visitor, Hash set=Invalid) const
Visit all members of the set (or the entire Manager if passed the Invalid hash).
std::uint32_t Hash
The type of integer used to hash strings.
bool Remove(const std::string &ss, Hash hh)
Remove the hash h from the set s.
std::function< Visit(Hash entry)> Visitor
Signature for functions visiting strings in the manager or in a set held by the manager.
void Reset()
Reset the manager to an empty state, clearing both members and sets.