VTK  9.4.20241222
vtkStateStorage.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
36#ifndef vtkStateStorage_h
37#define vtkStateStorage_h
38
39#include "vtkABINamespace.h"
40
41#include <algorithm>
42#include <string>
43#include <vector>
44
45// uncomment the following line to add in state debugging information
46// #define USE_STATE_DEBUGGING 1
47#ifdef USE_STATE_DEBUGGING
48
49VTK_ABI_NAMESPACE_BEGIN
51{
52public:
54
55 // clear the storage
56 void Clear()
57 {
58 this->Storage.clear();
59 this->StorageOffsets.clear();
60 this->StorageNames.clear();
61 }
62
63 // append a data item to the state
64 template <class T>
65 void Append(const T& value, const char* name);
66
67 bool operator!=(const vtkStateStorage& b) const
68 {
69 // for debug we also lookup the name of what was different
70 this->WhatWasDifferent = "";
71 if (this->Storage.size() != b.Storage.size())
72 {
73 this->WhatWasDifferent = "Different state sizes";
74 return true;
75 }
76 for (size_t i = 0; i < this->Storage.size(); ++i)
77 {
78 if (this->Storage[i] != b.Storage[i])
79 {
80 size_t block = 0;
81 while (this->StorageOffsets.size() > block + 1 && this->StorageOffsets[block + 1] >= i)
82 {
83 block++;
84 }
85 this->WhatWasDifferent = this->StorageNames[block] + " was different";
86 return true;
87 }
88 }
89 return false;
90 }
91
93 {
94 this->Storage = b.Storage;
95 this->StorageNames = b.StorageNames;
96 this->StorageOffsets = b.StorageOffsets;
97 return *this;
98 }
99
100protected:
101 std::vector<unsigned char> Storage;
102 std::vector<std::string> StorageNames;
103 std::vector<size_t> StorageOffsets;
104 mutable std::string WhatWasDifferent;
105
106private:
107 vtkStateStorage(const vtkStateStorage&) = delete;
108};
109
110template <class T>
111inline void vtkStateStorage::Append(const T& value, const char* name)
112{
113 this->StorageOffsets.push_back(this->Storage.size());
114 this->StorageNames.push_back(name);
115 const char* start = reinterpret_cast<const char*>(&value);
116 this->Storage.insert(this->Storage.end(), start, start + sizeof(T));
117}
118
119VTK_ABI_NAMESPACE_END
120#else // normal implementation
121
122VTK_ABI_NAMESPACE_BEGIN
124{
125public:
126 vtkStateStorage() = default;
127
128 // clear the storage
129 void Clear() { this->Storage.clear(); }
130
131 // append a data item to the state
132 template <class T>
133 void Append(const T& value, const char* name);
134
135 bool operator!=(const vtkStateStorage& b) const { return this->Storage != b.Storage; }
136
138
139protected:
140 std::vector<unsigned char> Storage;
141
142private:
143 vtkStateStorage(const vtkStateStorage&) = delete;
144};
145
146template <class T>
147inline void vtkStateStorage::Append(const T& value, const char*)
148{
149 const char* start = reinterpret_cast<const char*>(&value);
150 this->Storage.insert(this->Storage.end(), start, start + sizeof(T));
151}
152
153VTK_ABI_NAMESPACE_END
154#endif // normal implementation
155
156#endif // vtkStateStorage_h
157
158// VTK-HeaderTest-Exclude: vtkStateStorage.h
Class to make storing and comparing state quick and easy.
std::vector< unsigned char > Storage
void Append(const T &value, const char *name)
vtkStateStorage()=default
bool operator!=(const vtkStateStorage &b) const
vtkStateStorage & operator=(const vtkStateStorage &b)=default
@ value
Definition vtkX3D.h:220