OpenVDB: Metadata.h Source File

OpenVDB 4.0.2

Metadata.h
Go to the documentation of this file.
1 //
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 //
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 {
44 OPENVDB_USE_VERSION_NAMESPACE
45 namespace OPENVDB_VERSION_NAME {
46
48 class OPENVDB_API Metadata
49 {
50 public:
51 using Ptr = SharedPtr<Metadata>;
52 using ConstPtr = SharedPtr<const Metadata>;
53
54 Metadata() {}
55 virtual ~Metadata() {}
56
58 virtual Name typeName() const = 0;
59
61 virtual Metadata::Ptr copy() const = 0;
62
64 virtual void copy(const Metadata& other) = 0;
65
67 virtual std::string str() const = 0;
68
71 virtual bool asBool() const = 0;
72
74 bool operator==(const Metadata& other) const;
76 bool operator!=(const Metadata& other) const { return !(*this == other); }
77
79 virtual Index32 size() const = 0;
80
82 void read(std::istream&);
84 void write(std::ostream&) const;
85
87 static Metadata::Ptr createMetadata(const Name& typeName);
88
90 static bool isRegisteredType(const Name& typeName);
91
93 static void clearRegistry();
94
96 static void registerType(const Name& typeName, Metadata::Ptr (*createMetadata)());
97 static void unregisterType(const Name& typeName);
98
99 protected:
101 static Index32 readSize(std::istream&);
103 void writeSize(std::ostream&) const;
104
106 virtual void readValue(std::istream&, Index32 numBytes) = 0;
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
118 class OPENVDB_API UnknownMetadata: public Metadata
119 {
120 public:
121 UnknownMetadata() {}
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
137 template<typename T>
138 class TypedMetadata: public Metadata
139 {
140 public:
141 using Ptr = SharedPtr<TypedMetadata<T>>;
142 using ConstPtr = SharedPtr<const TypedMetadata<T>>;
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
157 void setValue(const T&);
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
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
182 std::ostream& operator<<(std::ostream& ostr, const Metadata& metadata);
183
184
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
222
223
224 template <typename T>
225 inline
226 TypedMetadata<T>::TypedMetadata() : mValue(T())
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
238 TypedMetadata<T>::TypedMetadata(const TypedMetadata<T> &other) :
239 Metadata(),
240 mValue(other.mValue)
241 {
242 }
243
244 template <typename T>
245 inline
246 TypedMetadata<T>::~TypedMetadata()
247 {
248 }
249
250 template <typename T>
251 inline Name
252 TypedMetadata<T>::typeName() const
253 {
254 return TypedMetadata<T>::staticTypeName();
255 }
256
257 template <typename T>
258 inline void
259 TypedMetadata<T>::setValue(const T& val)
260 {
261 mValue = val;
262 }
263
264 template <typename T>
265 inline T&
266 TypedMetadata<T>::value()
267 {
268 return mValue;
269 }
270
271 template <typename T>
272 inline const T&
273 TypedMetadata<T>::value() const
274 {
275 return mValue;
276 }
277
278 template <typename T>
279 inline Metadata::Ptr
280 TypedMetadata<T>::copy() const
281 {
282 Metadata::Ptr metadata(new TypedMetadata<T>());
283 metadata->copy(*this);
284 return metadata;
285 }
286
287 template <typename T>
288 inline void
289 TypedMetadata<T>::copy(const Metadata &other)
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
314 TypedMetadata<T>::str() const
315 {
316 std::ostringstream ostr;
317 ostr << mValue;
318 return ostr.str();
319 }
320
321 template<typename T>
322 inline bool
323 TypedMetadata<T>::asBool() const
324 {
325 return !math::isZero(mValue);
326 }
327
328 template <typename T>
329 inline Metadata::Ptr
330 TypedMetadata<T>::createMetadata()
331 {
332 Metadata::Ptr ret(new TypedMetadata<T>());
333 return ret;
334 }
335
336 template <typename T>
337 inline void
338 TypedMetadata<T>::registerType()
339 {
340 Metadata::registerType(TypedMetadata<T>::staticTypeName(),
341 TypedMetadata<T>::createMetadata);
342 }
343
344 template <typename T>
345 inline void
346 TypedMetadata<T>::unregisterType()
347 {
348 Metadata::unregisterType(TypedMetadata<T>::staticTypeName());
349 }
350
351 template <typename T>
352 inline bool
353 TypedMetadata<T>::isRegisteredType()
354 {
355 return Metadata::isRegisteredType(TypedMetadata<T>::staticTypeName());
356 }
357
358
359 template<>
360 inline std::string
361 TypedMetadata<bool>::str() const
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
375 using BoolMetadata = TypedMetadata<bool>;
376 using DoubleMetadata = TypedMetadata<double>;
377 using FloatMetadata = TypedMetadata<float>;
378 using Int32Metadata = TypedMetadata<int32_t>;
379 using Int64Metadata = TypedMetadata<int64_t>;
380 using StringMetadata = TypedMetadata<std::string>;
381 using Vec2DMetadata = TypedMetadata<Vec2d>;
382 using Vec2IMetadata = TypedMetadata<Vec2i>;
383 using Vec2SMetadata = TypedMetadata<Vec2s>;
384 using Vec3DMetadata = TypedMetadata<Vec3d>;
385 using Vec3IMetadata = TypedMetadata<Vec3i>;
386 using Vec3SMetadata = TypedMetadata<Vec3s>;
387 using Mat4SMetadata = TypedMetadata<Mat4s>;
388 using Mat4DMetadata = TypedMetadata<Mat4d>;
389
390
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/ )
openvdb::v4_0_2::Metadata::operator!=
bool operator!=(const Metadata &other) const
Return true if the given metadata is different from this metadata.
Definition: Metadata.h:76
OPENVDB_API
#define OPENVDB_API
Helper macros for defining library symbol visibility.
Definition: Platform.h:194
openvdb::v4_0_2::TypedMetadata::writeValue
virtual void writeValue(std::ostream &) const
Write the metadata to a stream.
Definition: Metadata.h:307
Types.h
openvdb::v4_0_2::Metadata::str
virtual std::string str() const =0
Return a textual representation of this metadata.
openvdb::v4_0_2::TypedMetadata::size
virtual Index32 size() const
Return the size of this metadata in bytes.
Definition: Metadata.h:154
openvdb::v4_0_2::SharedPtr
std::shared_ptr< T > SharedPtr
Definition: Types.h:130
Math.h
General-purpose arithmetic and comparison routines, most of which accept arbitrary value types (or at...
openvdb::v4_0_2::TypedMetadata::value
T & value()
Return this metadata&#39;s value.
Definition: Metadata.h:266
openvdb::v4_0_2::UnknownMetadata::UnknownMetadata
UnknownMetadata()
Definition: Metadata.h:121
openvdb::v4_0_2::TypedMetadata::copy
virtual Metadata::Ptr copy() const
Return a copy of the metadata.
Definition: Metadata.h:280
OPENVDB_THROW
#define OPENVDB_THROW(exception, message)
Definition: Exceptions.h:101
openvdb::v4_0_2::TypedMetadata::str
virtual std::string str() const
Return a textual representation of this metadata.
Definition: Metadata.h:314
openvdb::v4_0_2::Index32
uint32_t Index32
Definition: Types.h:55
openvdb::v4_0_2::TypedMetadata::TypedMetadata
TypedMetadata()
Definition: Metadata.h:226
openvdb::v4_0_2::UnknownMetadata::size
virtual Index32 size() const
Return the size of this metadata in bytes.
Definition: Metadata.h:128
openvdb::v4_0_2::Metadata::ConstPtr
SharedPtr< const Metadata > ConstPtr
Definition: Metadata.h:52
openvdb::v4_0_2::Metadata::write
void write(std::ostream &) const
Serialize this metadata to a stream.
Definition: Metadata.h:214
openvdb::v4_0_2::TypedMetadata::typeName
virtual Name typeName() const
Return the type name of the metadata.
Definition: Metadata.h:252
openvdb::v4_0_2::TypedMetadata::isRegisteredType
static bool isRegisteredType()
Definition: Metadata.h:353
openvdb::v4_0_2::Metadata::unregisterType
static void unregisterType(const Name &typeName)
openvdb::v4_0_2::Metadata::Metadata
Metadata()
Definition: Metadata.h:54
openvdb::v4_0_2::Name
std::string Name
Definition: Name.h:44
Exceptions.h
openvdb::v4_0_2::UnknownMetadata::asBool
virtual bool asBool() const
Definition: Metadata.h:127
openvdb::v4_0_2::TypedMetadata::readValue
virtual void readValue(std::istream &, Index32 numBytes)
Read the metadata from a stream.
Definition: Metadata.h:299
OPENVDB_VERSION_NAME
#define OPENVDB_VERSION_NAME
Definition: version.h:43
openvdb::v4_0_2::TypedMetadata
Templated metadata class to hold specific types.
Definition: Metadata.h:138
openvdb::v4_0_2::TypedMetadata::staticTypeName
static Name staticTypeName()
Definition: Metadata.h:164
openvdb::v4_0_2::math::isZero
bool isZero(const Type &x)
Return true if x is exactly equal to zero.
Definition: Math.h:308
openvdb::v4_0_2::Metadata::isRegisteredType
static bool isRegisteredType(const Name &typeName)
Return true if the given type is known by the metadata type registry.
openvdb
Definition: Exceptions.h:39
openvdb::v4_0_2::UnknownMetadata
Subclass to read (and ignore) data of an unregistered type.
Definition: Metadata.h:118
openvdb::v4_0_2::math::operator==
bool operator==(const Vec3< T0 > &v0, const Vec3< T1 > &v1)
Equality operator, does exact floating point comparisons.
Definition: Vec3.h:487
openvdb::v4_0_2::Metadata::readSize
static Index32 readSize(std::istream &)
Read the size of the metadata from a stream.
Definition: Metadata.h:197
openvdb::v4_0_2::TypedMetadata::createMetadata
static Metadata::Ptr createMetadata()
Create new metadata of this type.
Definition: Metadata.h:330
openvdb::v4_0_2::Metadata
Base class for storing metadata information in a grid.
Definition: Metadata.h:48
openvdb::v4_0_2::TypedMetadata::setValue
void setValue(const T &)
Set this metadata&#39;s value.
Definition: Metadata.h:259
openvdb::v4_0_2::Metadata::read
void read(std::istream &)
Unserialize this metadata from a stream.
Definition: Metadata.h:206
openvdb::v4_0_2::TypedMetadata::registerType
static void registerType()
Definition: Metadata.h:338
openvdb::v4_0_2::UnknownMetadata::~UnknownMetadata
virtual ~UnknownMetadata()
Definition: Metadata.h:122
openvdb::v4_0_2::TypedMetadata::~TypedMetadata
virtual ~TypedMetadata()
Definition: Metadata.h:246
openvdb::v4_0_2::TypedMetadata::unregisterType
static void unregisterType()
Definition: Metadata.h:346
openvdb::v4_0_2::UnknownMetadata::copy
virtual void copy(const Metadata &)
Copy the given metadata into this metadata.
Definition: Metadata.h:125
openvdb::v4_0_2::UnknownMetadata::str
virtual std::string str() const
Return a textual representation of this metadata.
Definition: Metadata.h:126
openvdb::v4_0_2::Metadata::registerType
static void registerType(const Name &typeName, Metadata::Ptr(*createMetadata)())
Register the given metadata type along with a factory function.
openvdb::v4_0_2::TypeError
Definition: Exceptions.h:91
OPENVDB_USE_VERSION_NAMESPACE
#define OPENVDB_USE_VERSION_NAMESPACE
Definition: version.h:71
openvdb::v4_0_2::TypedMetadata::asBool
virtual bool asBool() const
Definition: Metadata.h:323
openvdb::v4_0_2::Metadata::~Metadata
virtual ~Metadata()
Definition: Metadata.h:55
openvdb::v4_0_2::UnknownMetadata::typeName
virtual Name typeName() const
Return the type name of the metadata.
Definition: Metadata.h:123
size
static constexpr size_t size
The size of a LeafBuffer when LeafBuffer::mOutOfCore is atomic.
Definition: LeafBuffer.h:85
openvdb::v4_0_2::Metadata::writeSize
void writeSize(std::ostream &) const
Write the size of the metadata to a stream.
Definition: Metadata.h:189
Name.h
openvdb::v4_0_2::Metadata::Ptr
SharedPtr< Metadata > Ptr
Definition: Metadata.h:51
openvdb::v4_0_2::UnknownMetadata::copy
virtual Metadata::Ptr copy() const
Return a copy of the metadata.
Definition: Metadata.h:124
openvdb::v4_0_2::operator<<
std::ostream & operator<<(std::ostream &ostr, const Metadata &metadata)
Write a Metadata to an output stream.
Definition: Metadata.h:368