HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
Metadata.h
Go to the documentation of this file.
1 ///////////////////////////////////////////////////////////////////////////
2 //
3 // Copyright (c) 2012-2017 DreamWorks Animation LLC
4 //
5 // All rights reserved. This software is distributed under the
6 // Mozilla Public License 2.0 ( http://www.mozilla.org/MPL/2.0/ )
7 //
8 // Redistributions of source code must retain the above copyright
9 // and license notice and the following restrictions and disclaimer.
10 //
11 // * Neither the name of DreamWorks Animation nor the names of
12 // its contributors may be used to endorse or promote products derived
13 // from this software without specific prior written permission.
14 //
15 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
16 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
17 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
18 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
19 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY INDIRECT, INCIDENTAL,
20 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
21 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
25 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 // IN NO EVENT SHALL THE COPYRIGHT HOLDERS' AND CONTRIBUTORS' AGGREGATE
27 // LIABILITY FOR ALL CLAIMS REGARDLESS OF THEIR BASIS EXCEED US$250.00.
28 //
29 ///////////////////////////////////////////////////////////////////////////
30 
31 #ifndef OPENVDB_METADATA_HAS_BEEN_INCLUDED
32 #define OPENVDB_METADATA_HAS_BEEN_INCLUDED
33 
34 #include "Exceptions.h"
35 #include "Types.h"
36 #include "math/Math.h" // for math::isZero()
37 #include "util/Name.h"
38 #include <cstdint>
39 #include <iostream>
40 #include <string>
41 
42 
43 namespace openvdb {
45 namespace OPENVDB_VERSION_NAME {
46 
47 /// @brief Base class for storing metadata information in a grid.
49 {
50 public:
53 
54  Metadata() {}
55  virtual ~Metadata() {}
56 
57  /// Return the type name of the metadata.
58  virtual Name typeName() const = 0;
59 
60  /// Return a copy of the metadata.
61  virtual Metadata::Ptr copy() const = 0;
62 
63  /// Copy the given metadata into this metadata.
64  virtual void copy(const Metadata& other) = 0;
65 
66  /// Return a textual representation of this metadata.
67  virtual std::string str() const = 0;
68 
69  /// Return the boolean representation of this metadata (empty strings
70  /// and zeroVals evaluate to false; most other values evaluate to true).
71  virtual bool asBool() const = 0;
72 
73  /// Return @c true if the given metadata is equivalent to this metadata.
74  bool operator==(const Metadata& other) const;
75  /// Return @c true if the given metadata is different from this metadata.
76  bool operator!=(const Metadata& other) const { return !(*this == other); }
77 
78  /// Return the size of this metadata in bytes.
79  virtual Index32 size() const = 0;
80 
81  /// Unserialize this metadata from a stream.
82  void read(std::istream&);
83  /// Serialize this metadata to a stream.
84  void write(std::ostream&) const;
85 
86  /// Create new metadata of the given type.
87  static Metadata::Ptr createMetadata(const Name& typeName);
88 
89  /// Return @c true if the given type is known by the metadata type registry.
90  static bool isRegisteredType(const Name& typeName);
91 
92  /// Clear out the metadata registry.
93  static void clearRegistry();
94 
95  /// Register the given metadata type along with a factory function.
96  static void registerType(const Name& typeName, Metadata::Ptr (*createMetadata)());
97  static void unregisterType(const Name& typeName);
98 
99 protected:
100  /// Read the size of the metadata from a stream.
101  static Index32 readSize(std::istream&);
102  /// Write the size of the metadata to a stream.
103  void writeSize(std::ostream&) const;
104 
105  /// Read the metadata from a stream.
106  virtual void readValue(std::istream&, Index32 numBytes) = 0;
107  /// Write the metadata to a stream.
108  virtual void writeValue(std::ostream&) const = 0;
109 
110 private:
111  // Disallow copying of instances of this class.
112  Metadata(const Metadata&);
113  Metadata& operator=(const Metadata&);
114 };
115 
116 
117 /// @brief Subclass to read (and ignore) data of an unregistered type
119 {
120 public:
122  virtual ~UnknownMetadata() {}
123  virtual Name typeName() const { return "<unknown>"; }
124  virtual Metadata::Ptr copy() const { OPENVDB_THROW(TypeError, "Metadata has unknown type"); }
125  virtual void copy(const Metadata&) { OPENVDB_THROW(TypeError, "Destination has unknown type"); }
126  virtual std::string str() const { return "<unknown>"; }
127  virtual bool asBool() const { return false; }
128  virtual Index32 size() const { return 0; }
129 
130 protected:
131  virtual void readValue(std::istream&s, Index32 numBytes);
132  virtual void writeValue(std::ostream&) const;
133 };
134 
135 
136 /// @brief Templated metadata class to hold specific types.
137 template<typename T>
138 class TypedMetadata: public Metadata
139 {
140 public:
143 
144  TypedMetadata();
145  TypedMetadata(const T& value);
146  TypedMetadata(const TypedMetadata<T>& other);
147  virtual ~TypedMetadata();
148 
149  virtual Name typeName() const;
150  virtual Metadata::Ptr copy() const;
151  virtual void copy(const Metadata& other);
152  virtual std::string str() const;
153  virtual bool asBool() const;
154  virtual Index32 size() const { return static_cast<Index32>(sizeof(T)); }
155 
156  /// Set this metadata's value.
157  void setValue(const T&);
158  /// Return this metadata's value.
159  T& value();
160  const T& value() const;
161 
162  // Static specialized function for the type name. This function must be
163  // template specialized for each type T.
164  static Name staticTypeName() { return typeNameAsString<T>(); }
165 
166  /// Create new metadata of this type.
167  static Metadata::Ptr createMetadata();
168 
169  static void registerType();
170  static void unregisterType();
171  static bool isRegisteredType();
172 
173 protected:
174  virtual void readValue(std::istream&, Index32 numBytes);
175  virtual void writeValue(std::ostream&) const;
176 
177 private:
178  T mValue;
179 };
180 
181 /// Write a Metadata to an output stream
182 std::ostream& operator<<(std::ostream& ostr, const Metadata& metadata);
183 
184 
185 ////////////////////////////////////////
186 
187 
188 inline void
189 Metadata::writeSize(std::ostream& os) const
190 {
191  const Index32 n = this->size();
192  os.write(reinterpret_cast<const char*>(&n), sizeof(Index32));
193 }
194 
195 
196 inline Index32
197 Metadata::readSize(std::istream& is)
198 {
199  Index32 n = 0;
200  is.read(reinterpret_cast<char*>(&n), sizeof(Index32));
201  return n;
202 }
203 
204 
205 inline void
206 Metadata::read(std::istream& is)
207 {
208  const Index32 numBytes = this->readSize(is);
209  this->readValue(is, numBytes);
210 }
211 
212 
213 inline void
214 Metadata::write(std::ostream& os) const
215 {
216  this->writeSize(os);
217  this->writeValue(os);
218 }
219 
220 
221 ////////////////////////////////////////
222 
223 
224 template <typename T>
225 inline
227 {
228 }
229 
230 template <typename T>
231 inline
232 TypedMetadata<T>::TypedMetadata(const T &value) : mValue(value)
233 {
234 }
235 
236 template <typename T>
237 inline
239  Metadata(),
240  mValue(other.mValue)
241 {
242 }
243 
244 template <typename T>
245 inline
247 {
248 }
249 
250 template <typename T>
251 inline Name
253 {
255 }
256 
257 template <typename T>
258 inline void
260 {
261  mValue = val;
262 }
263 
264 template <typename T>
265 inline T&
267 {
268  return mValue;
269 }
270 
271 template <typename T>
272 inline const T&
274 {
275  return mValue;
276 }
277 
278 template <typename T>
279 inline Metadata::Ptr
281 {
282  Metadata::Ptr metadata(new TypedMetadata<T>());
283  metadata->copy(*this);
284  return metadata;
285 }
286 
287 template <typename T>
288 inline void
290 {
291  const TypedMetadata<T>* t = dynamic_cast<const TypedMetadata<T>*>(&other);
292  if (t == nullptr) OPENVDB_THROW(TypeError, "Incompatible type during copy");
293  mValue = t->mValue;
294 }
295 
296 
297 template<typename T>
298 inline void
299 TypedMetadata<T>::readValue(std::istream& is, Index32 /*numBytes*/)
300 {
301  //assert(this->size() == numBytes);
302  is.read(reinterpret_cast<char*>(&mValue), this->size());
303 }
304 
305 template<typename T>
306 inline void
307 TypedMetadata<T>::writeValue(std::ostream& os) const
308 {
309  os.write(reinterpret_cast<const char*>(&mValue), this->size());
310 }
311 
312 template <typename T>
313 inline std::string
315 {
316  std::ostringstream ostr;
317  ostr << mValue;
318  return ostr.str();
319 }
320 
321 template<typename T>
322 inline bool
324 {
325  return !math::isZero(mValue);
326 }
327 
328 template <typename T>
329 inline Metadata::Ptr
331 {
332  Metadata::Ptr ret(new TypedMetadata<T>());
333  return ret;
334 }
335 
336 template <typename T>
337 inline void
339 {
342 }
343 
344 template <typename T>
345 inline void
347 {
349 }
350 
351 template <typename T>
352 inline bool
354 {
356 }
357 
358 
359 template<>
360 inline std::string
362 {
363  return (mValue ? "true" : "false");
364 }
365 
366 
367 inline std::ostream&
368 operator<<(std::ostream& ostr, const Metadata& metadata)
369 {
370  ostr << metadata.str();
371  return ostr;
372 }
373 
374 
389 
390 
391 ////////////////////////////////////////
392 
393 
394 template<>
395 inline Index32
396 StringMetadata::size() const
397 {
398  return static_cast<Index32>(mValue.size());
399 }
400 
401 
402 template<>
403 inline std::string
404 StringMetadata::str() const
405 {
406  return mValue;
407 }
408 
409 
410 template<>
411 inline void
412 StringMetadata::readValue(std::istream& is, Index32 size)
413 {
414  mValue.resize(size, '\0');
415  is.read(&mValue[0], size);
416 }
417 
418 template<>
419 inline void
420 StringMetadata::writeValue(std::ostream& os) const
421 {
422  os.write(reinterpret_cast<const char*>(&mValue[0]), this->size());
423 }
424 
425 } // namespace OPENVDB_VERSION_NAME
426 } // namespace openvdb
427 
428 #endif // OPENVDB_METADATA_HAS_BEEN_INCLUDED
429 
430 // Copyright (c) 2012-2017 DreamWorks Animation LLC
431 // All rights reserved. This software is distributed under the
432 // Mozilla Public License 2.0 ( http://www.mozilla.org/MPL/2.0/ )
virtual Index32 size() const
Return the size of this metadata in bytes.
Definition: Metadata.h:154
void read(std::istream &)
Unserialize this metadata from a stream.
Definition: Metadata.h:206
Definition: ImfName.h:53
std::ostream & operator<<(std::ostream &ostr, const Metadata &metadata)
Write a Metadata to an output stream.
Definition: Metadata.h:368
T & value()
Return this metadata's value.
Definition: Metadata.h:266
GLsizei const GLchar *const * string
Definition: glcorearb.h:813
virtual Index32 size() const =0
Return the size of this metadata in bytes.
virtual std::string str() const =0
Return a textual representation of this metadata.
virtual std::string str() const
Return a textual representation of this metadata.
Definition: Metadata.h:314
static void unregisterType(const Name &typeName)
static Index32 readSize(std::istream &)
Read the size of the metadata from a stream.
Definition: Metadata.h:197
bool operator!=(const Metadata &other) const
Return true if the given metadata is different from this metadata.
Definition: Metadata.h:76
virtual Metadata::Ptr copy() const
Return a copy of the metadata.
Definition: Metadata.h:124
SharedPtr< const Metadata > ConstPtr
Definition: Metadata.h:52
void read(T &in, bool &v)
Definition: ImfXdr.h:611
GLsizeiptr size
Definition: glcorearb.h:663
void write(std::ostream &) const
Serialize this metadata to a stream.
Definition: Metadata.h:214
std::shared_ptr< T > SharedPtr
Definition: Types.h:130
static bool isRegisteredType(const Name &typeName)
Return true if the given type is known by the metadata type registry.
bool operator==(const BaseDimensions< T > &a, const BaseDimensions< Y > &b)
Definition: Dimensions.h:137
virtual void readValue(std::istream &, Index32 numBytes)
Read the metadata from a stream.
Definition: Metadata.h:299
GLdouble n
Definition: glcorearb.h:2007
virtual Name typeName() const
Return the type name of the metadata.
Definition: Metadata.h:123
#define OPENVDB_API
Helper macros for defining library symbol visibility.
Definition: Platform.h:194
Subclass to read (and ignore) data of an unregistered type.
Definition: Metadata.h:118
#define OPENVDB_VERSION_NAME
Definition: version.h:43
Templated metadata class to hold specific types.
Definition: Metadata.h:138
General-purpose arithmetic and comparison routines, most of which accept arbitrary value types (or at...
virtual Metadata::Ptr copy() const
Return a copy of the metadata.
Definition: Metadata.h:280
virtual Index32 size() const
Return the size of this metadata in bytes.
Definition: Metadata.h:128
void setValue(const T &)
Set this metadata's value.
Definition: Metadata.h:259
virtual void writeValue(std::ostream &) const =0
Write the metadata to a stream.
static void registerType(const Name &typeName, Metadata::Ptr(*createMetadata)())
Register the given metadata type along with a factory function.
virtual void copy(const Metadata &)
Copy the given metadata into this metadata.
Definition: Metadata.h:125
GLsizei const GLfloat * value
Definition: glcorearb.h:823
virtual void readValue(std::istream &, Index32 numBytes)=0
Read the metadata from a stream.
virtual void writeValue(std::ostream &) const
Write the metadata to a stream.
Definition: Metadata.h:307
Base class for storing metadata information in a grid.
Definition: Metadata.h:48
GLuint GLfloat * val
Definition: glcorearb.h:1607
#define OPENVDB_USE_VERSION_NAMESPACE
Definition: version.h:71
void write(T &out, bool v)
Definition: ImfXdr.h:332
virtual std::string str() const
Return a textual representation of this metadata.
Definition: Metadata.h:126
static Metadata::Ptr createMetadata()
Create new metadata of this type.
Definition: Metadata.h:330
void writeSize(std::ostream &) const
Write the size of the metadata to a stream.
Definition: Metadata.h:189
bool isZero(const Type &x)
Return true if x is exactly equal to zero.
Definition: Math.h:324
#define OPENVDB_THROW(exception, message)
Definition: Exceptions.h:101
virtual Name typeName() const
Return the type name of the metadata.
Definition: Metadata.h:252