VTK  9.3.1
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
5 
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 
25 VTK_ABI_NAMESPACE_BEGIN
26 
27 class VTKCOMMONCORE_EXPORT vtkStringManager : public vtkObject
28 {
29 public:
30  vtkTypeMacro(vtkStringManager, vtkObject);
31  static vtkStringManager* New();
32  void PrintSelf(ostream& os, vtkIndent indent) override;
33 
35  enum Visit
36  {
37  Halt,
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 
120 protected:
121  vtkStringManager() = default;
122 
123 private:
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 
139 VTK_ABI_NAMESPACE_END
140 #endif // vtkStringManager_h
abstract base class for most VTK objects
Definition: vtkObject.h:51
void PrintSelf(ostream &os, vtkIndent indent) override
Methods invoked by print to print information about the object including superclasses.
bool Empty() const
Return true if the manager is empty (i.e., managing no hashes) and false otherwise.
a simple class to control print indentation
Definition: vtkIndent.h:28
Terminate visitation.
std::function< Visit(Hash entry)> Visitor
Signature for functions visiting strings in the manager or in a set held by the manager.
std::uint32_t Hash
The type of integer used to hash strings.
Manage string-token mappings.
Visit
An enumerant visitors return to terminate early (or not).
static vtkObject * New()
Create an object with Debug turned off, modified time initialized to zero, and reference counting on...
bool Contains(Hash hh) const