VTK  9.0.3
vtkStateStorage.h
Go to the documentation of this file.
1 /*=========================================================================
2 
3  Program: Visualization Toolkit
4 
5  Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
6  All rights reserved.
7  See Copyright.txt or http://www.kitware.com/Copyright.htm for details.
8 
9  This software is distributed WITHOUT ANY WARRANTY; without even
10  the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
11  PURPOSE. See the above copyright notice for more information.
12 
13 =========================================================================*/
47 #ifndef vtkStateStorage_h
48 #define vtkStateStorage_h
49 
50 #include "vtkRenderingOpenGL2Module.h" // for export macro
51 
52 #include <algorithm>
53 #include <string>
54 #include <vector>
55 
56 // uncomment the following line to add in state debugging information
57 //#define USE_STATE_DEBUGGING 1
58 #ifdef USE_STATE_DEBUGGING
59 
60 class VTKRENDERINGOPENGL2_EXPORT vtkStateStorage
61 {
62 public:
63  vtkStateStorage() {}
64 
65  // clear the storage
66  void Clear()
67  {
68  this->Storage.clear();
69  this->StorageOffsets.clear();
70  this->StorageNames.clear();
71  }
72 
73  // append a data item to the state
74  template <class T>
75  void Append(const T& value, const char* name);
76 
77  bool operator!=(const vtkStateStorage& b) const
78  {
79  // for debug we also lookup the name of what was different
80  this->WhatWasDifferent = "";
81  if (this->Storage.size() != b.Storage.size())
82  {
83  this->WhatWasDifferent = "Different state sizes";
84  return true;
85  }
86  for (size_t i = 0; i < this->Storage.size(); ++i)
87  {
88  if (this->Storage[i] != b.Storage[i])
89  {
90  size_t block = 0;
91  while (this->StorageOffsets.size() > block + 1 && this->StorageOffsets[block + 1] >= i)
92  {
93  block++;
94  }
95  this->WhatWasDifferent = this->StorageNames[block] + " was different";
96  return true;
97  }
98  }
99  return false;
100  }
101 
102  vtkStateStorage& operator=(const vtkStateStorage& b)
103  {
104  this->Storage = b.Storage;
105  this->StorageNames = b.StorageNames;
106  this->StorageOffsets = b.StorageOffsets;
107  return *this;
108  }
109 
110 protected:
111  std::vector<unsigned char> Storage;
112  std::vector<std::string> StorageNames;
113  std::vector<size_t> StorageOffsets;
114  mutable std::string WhatWasDifferent;
115 
116 private:
117  vtkStateStorage(const vtkStateStorage&) = delete;
118 };
119 
120 template <class T>
121 inline void vtkStateStorage::Append(const T& value, const char* name)
122 {
123  this->StorageOffsets.push_back(this->Storage.size());
124  this->StorageNames.push_back(name);
125  const char* start = reinterpret_cast<const char*>(&value);
126  this->Storage.insert(this->Storage.end(), start, start + sizeof(T));
127 }
128 
129 #else // normal implementation
130 
131 class VTKRENDERINGOPENGL2_EXPORT vtkStateStorage
132 {
133 public:
135 
136  // clear the storage
137  void Clear() { this->Storage.clear(); }
138 
139  // append a data item to the state
140  template <class T>
141  void Append(const T& value, const char* name);
142 
143  bool operator!=(const vtkStateStorage& b) const { return this->Storage != b.Storage; }
144 
146  {
147  this->Storage = b.Storage;
148  return *this;
149  }
150 
151 protected:
152  std::vector<unsigned char> Storage;
153 
154 private:
155  vtkStateStorage(const vtkStateStorage&) = delete;
156 };
157 
158 template <class T>
159 inline void vtkStateStorage::Append(const T& value, const char*)
160 {
161  const char* start = reinterpret_cast<const char*>(&value);
162  this->Storage.insert(this->Storage.end(), start, start + sizeof(T));
163 }
164 
165 #endif // normal implementation
166 
167 #endif // vtkStateStorage_h
168 
169 // 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)
bool operator!=(const vtkStateStorage &b) const
vtkStateStorage & operator=(const vtkStateStorage &b)
@ value
Definition: vtkX3D.h:226
@ name
Definition: vtkX3D.h:225
@ string
Definition: vtkX3D.h:496
VTKCOMMONCORE_EXPORT bool operator!=(const vtkUnicodeString &lhs, const vtkUnicodeString &rhs)