VTK
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 
55 #ifndef NDEBUG // a debug implementation
56 
57 class VTKRENDERINGOPENGL2_EXPORT vtkStateStorage
58 {
59 public:
61 
62  // clear the storage
63  void Clear() {
64  this->Storage.clear();
65  this->StorageOffsets.clear();
66  this->StorageNames.clear();
67  }
68 
69  // append a data item to the state
70  template <class T>
71  void Append(const T &value, const char *name);
72 
73  bool operator !=(const vtkStateStorage &b) const {
74  // for debug we also lookup the name of what was different
75  this->WhatWasDifferent = "";
76  if (this->Storage.size() != b.Storage.size())
77  {
78  this->WhatWasDifferent = "Different state sizes";
79  return true;
80  }
81  for (size_t i = 0; i < this->Storage.size(); ++i)
82  {
83  if (this->Storage[i] != b.Storage[i])
84  {
85  size_t block = 0;
86  while (this->StorageOffsets.size() > block + 1 && this->StorageOffsets[block+1] >= i)
87  {
88  block++;
89  }
90  this->WhatWasDifferent = this->StorageNames[block] + " was different";
91  return true;
92  }
93  }
94  return false;
95  }
96 
98  this->Storage = b.Storage;
99  this->StorageNames = b.StorageNames;
100  this->StorageOffsets = b.StorageOffsets;
101  return *this;
102  }
103 
104 protected:
105  std::vector<unsigned char> Storage;
106  std::vector<std::string> StorageNames;
107  std::vector<size_t> StorageOffsets;
109 
110  private:
111  vtkStateStorage(const vtkStateStorage&) = delete;
112 };
113 
114 template <class T>
115 inline void vtkStateStorage::Append(const T &value, const char *name)
116 {
117  this->StorageOffsets.push_back(this->Storage.size());
118  this->StorageNames.push_back(name);
119  const char *start = reinterpret_cast<const char *>(&value);
120  this->Storage.insert(this->Storage.end(), start, start + sizeof(T));
121 }
122 
123 #else // release implementation
124 
125 class VTKRENDERINGOPENGL2_EXPORT vtkStateStorage
126 {
127 public:
128  vtkStateStorage() {}
129 
130  // clear the storage
131  void Clear() { this->Storage.clear(); }
132 
133  // append a data item to the state
134  template <class T>
135  void Append(const T &value, const char *name);
136 
137  bool operator !=(const vtkStateStorage &b) const {
138  return this->Storage != b.Storage;
139  }
140 
141  vtkStateStorage& operator=(const vtkStateStorage&b) {
142  this->Storage = b.Storage;
143  return *this;
144  }
145 
146 protected:
147  std::vector<unsigned char> Storage;
148 
149  private:
150  vtkStateStorage(const vtkStateStorage&) = delete;
151 };
152 
153 template <class T>
154 inline void vtkStateStorage::Append(const T &value, const char *)
155 {
156  const char *start = reinterpret_cast<const char *>(&value);
157  this->Storage.insert(this->Storage.end(), start, start + sizeof(T));
158 }
159 
160 #endif // Release implementation
161 
162 #endif // vtkStateStorage_h
163 
164 // VTK-HeaderTest-Exclude: vtkStateStorage.h
std::string WhatWasDifferent
std::vector< std::string > StorageNames
vtkStateStorage & operator=(const vtkStateStorage &b)
std::vector< size_t > StorageOffsets
std::vector< unsigned char > Storage
void Append(const T &value, const char *name)
VTKCOMMONCORE_EXPORT bool operator!=(const vtkUnicodeString &lhs, const vtkUnicodeString &rhs)
Class to make storing and comparing state quick and easy.