OpenVDB: Metadata.h Source File

OpenVDB 5.0.0

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 "version.h"
35 #include "Exceptions.h"
36 #include "Types.h"
37 #include "math/Math.h" // for math::isZero()
38 #include "util/Name.h"
39 #include <cstdint>
40 #include <iostream>
41 #include <string>
42 #include <vector>
43
44
45 namespace openvdb {
46 OPENVDB_USE_VERSION_NAMESPACE
47 namespace OPENVDB_VERSION_NAME {
48
50 class OPENVDB_API Metadata
51 {
52 public:
53 using Ptr = SharedPtr<Metadata>;
54 using ConstPtr = SharedPtr<const Metadata>;
55
56 Metadata() {}
57 virtual ~Metadata() {}
58
59 // Disallow copying of instances of this class.
60 Metadata(const Metadata&) = delete;
61 Metadata& operator=(const Metadata&) = delete;
62
64 virtual Name typeName() const = 0;
65
67 virtual Metadata::Ptr copy() const = 0;
68
70 virtual void copy(const Metadata& other) = 0;
71
73 virtual std::string str() const = 0;
74
77 virtual bool asBool() const = 0;
78
80 bool operator==(const Metadata& other) const;
82 bool operator!=(const Metadata& other) const { return !(*this == other); }
83
85 virtual Index32 size() const = 0;
86
88 void read(std::istream&);
90 void write(std::ostream&) const;
91
93 static Metadata::Ptr createMetadata(const Name& typeName);
94
96 static bool isRegisteredType(const Name& typeName);
97
99 static void clearRegistry();
100
102 static void registerType(const Name& typeName, Metadata::Ptr (*createMetadata)());
103 static void unregisterType(const Name& typeName);
104
105 protected:
107 static Index32 readSize(std::istream&);
109 void writeSize(std::ostream&) const;
110
112 virtual void readValue(std::istream&, Index32 numBytes) = 0;
114 virtual void writeValue(std::ostream&) const = 0;
115 };
116
117
118 #if OPENVDB_ABI_VERSION_NUMBER >= 5
119
121 class OPENVDB_API UnknownMetadata: public Metadata
122 {
123 public:
124 using ByteVec = std::vector<uint8_t>;
125
126 explicit UnknownMetadata(const Name& typ = "<unknown>"): mTypeName(typ) {}
127
128 Name typeName() const override { return mTypeName; }
129 Metadata::Ptr copy() const override;
130 void copy(const Metadata&) override;
131 std::string str() const override { return (mBytes.empty() ? "" : "<binary data>"); }
132 bool asBool() const override { return !mBytes.empty(); }
133 Index32 size() const override { return static_cast<Index32>(mBytes.size()); }
134
135 void setValue(const ByteVec& bytes) { mBytes = bytes; }
136 const ByteVec& value() const { return mBytes; }
137
138 protected:
139 void readValue(std::istream&, Index32 numBytes) override;
140 void writeValue(std::ostream&) const override;
141
142 private:
143 Name mTypeName;
144 ByteVec mBytes;
145 };
146
147 #else // if OPENVDB_ABI_VERSION_NUMBER < 5
148
150 class OPENVDB_API UnknownMetadata: public Metadata
151 {
152 public:
153 UnknownMetadata() {}
154 Name typeName() const override { return "<unknown>"; }
155 Metadata::Ptr copy() const override { OPENVDB_THROW(TypeError, "Metadata has unknown type"); }
156 void copy(const Metadata&) override {OPENVDB_THROW(TypeError, "Destination has unknown type");}
157 std::string str() const override { return "<unknown>"; }
158 bool asBool() const override { return false; }
159 Index32 size() const override { return 0; }
160
161 protected:
162 void readValue(std::istream&, Index32 numBytes) override;
163 void writeValue(std::ostream&) const override;
164 };
165
166 #endif
167
168
170 template<typename T>
171 class TypedMetadata: public Metadata
172 {
173 public:
174 using Ptr = SharedPtr<TypedMetadata<T>>;
175 using ConstPtr = SharedPtr<const TypedMetadata<T>>;
176
177 TypedMetadata();
178 TypedMetadata(const T& value);
179 TypedMetadata(const TypedMetadata<T>& other);
180 ~TypedMetadata() override;
181
182 Name typeName() const override;
183 Metadata::Ptr copy() const override;
184 void copy(const Metadata& other) override;
185 std::string str() const override;
186 bool asBool() const override;
187 Index32 size() const override { return static_cast<Index32>(sizeof(T)); }
188
190 void setValue(const T&);
192 T& value();
193 const T& value() const;
194
195 // Static specialized function for the type name. This function must be
196 // template specialized for each type T.
197 static Name staticTypeName() { return typeNameAsString<T>(); }
198
200 static Metadata::Ptr createMetadata();
201
202 static void registerType();
203 static void unregisterType();
204 static bool isRegisteredType();
205
206 protected:
207 void readValue(std::istream&, Index32 numBytes) override;
208 void writeValue(std::ostream&) const override;
209
210 private:
211 T mValue;
212 };
213
215 std::ostream& operator<<(std::ostream& ostr, const Metadata& metadata);
216
217
219
220
221 inline void
222 Metadata::writeSize(std::ostream& os) const
223 {
224 const Index32 n = this->size();
225 os.write(reinterpret_cast<const char*>(&n), sizeof(Index32));
226 }
227
228
229 inline Index32
230 Metadata::readSize(std::istream& is)
231 {
232 Index32 n = 0;
233 is.read(reinterpret_cast<char*>(&n), sizeof(Index32));
234 return n;
235 }
236
237
238 inline void
239 Metadata::read(std::istream& is)
240 {
241 const Index32 numBytes = this->readSize(is);
242 this->readValue(is, numBytes);
243 }
244
245
246 inline void
247 Metadata::write(std::ostream& os) const
248 {
249 this->writeSize(os);
250 this->writeValue(os);
251 }
252
253
255
256
257 template <typename T>
258 inline
259 TypedMetadata<T>::TypedMetadata() : mValue(T())
260 {
261 }
262
263 template <typename T>
264 inline
265 TypedMetadata<T>::TypedMetadata(const T &value) : mValue(value)
266 {
267 }
268
269 template <typename T>
270 inline
271 TypedMetadata<T>::TypedMetadata(const TypedMetadata<T> &other) :
272 Metadata(),
273 mValue(other.mValue)
274 {
275 }
276
277 template <typename T>
278 inline
279 TypedMetadata<T>::~TypedMetadata()
280 {
281 }
282
283 template <typename T>
284 inline Name
285 TypedMetadata<T>::typeName() const
286 {
287 return TypedMetadata<T>::staticTypeName();
288 }
289
290 template <typename T>
291 inline void
292 TypedMetadata<T>::setValue(const T& val)
293 {
294 mValue = val;
295 }
296
297 template <typename T>
298 inline T&
299 TypedMetadata<T>::value()
300 {
301 return mValue;
302 }
303
304 template <typename T>
305 inline const T&
306 TypedMetadata<T>::value() const
307 {
308 return mValue;
309 }
310
311 template <typename T>
312 inline Metadata::Ptr
313 TypedMetadata<T>::copy() const
314 {
315 Metadata::Ptr metadata(new TypedMetadata<T>());
316 metadata->copy(*this);
317 return metadata;
318 }
319
320 template <typename T>
321 inline void
322 TypedMetadata<T>::copy(const Metadata &other)
323 {
324 const TypedMetadata<T>* t = dynamic_cast<const TypedMetadata<T>*>(&other);
325 if (t == nullptr) OPENVDB_THROW(TypeError, "Incompatible type during copy");
326 mValue = t->mValue;
327 }
328
329
330 template<typename T>
331 inline void
332 TypedMetadata<T>::readValue(std::istream& is, Index32 /*numBytes*/)
333 {
334 //assert(this->size() == numBytes);
335 is.read(reinterpret_cast<char*>(&mValue), this->size());
336 }
337
338 template<typename T>
339 inline void
340 TypedMetadata<T>::writeValue(std::ostream& os) const
341 {
342 os.write(reinterpret_cast<const char*>(&mValue), this->size());
343 }
344
345 template <typename T>
346 inline std::string
347 TypedMetadata<T>::str() const
348 {
349 std::ostringstream ostr;
350 ostr << mValue;
351 return ostr.str();
352 }
353
354 template<typename T>
355 inline bool
356 TypedMetadata<T>::asBool() const
357 {
358 return !math::isZero(mValue);
359 }
360
361 template <typename T>
362 inline Metadata::Ptr
363 TypedMetadata<T>::createMetadata()
364 {
365 Metadata::Ptr ret(new TypedMetadata<T>());
366 return ret;
367 }
368
369 template <typename T>
370 inline void
371 TypedMetadata<T>::registerType()
372 {
373 Metadata::registerType(TypedMetadata<T>::staticTypeName(),
374 TypedMetadata<T>::createMetadata);
375 }
376
377 template <typename T>
378 inline void
379 TypedMetadata<T>::unregisterType()
380 {
381 Metadata::unregisterType(TypedMetadata<T>::staticTypeName());
382 }
383
384 template <typename T>
385 inline bool
386 TypedMetadata<T>::isRegisteredType()
387 {
388 return Metadata::isRegisteredType(TypedMetadata<T>::staticTypeName());
389 }
390
391
392 template<>
393 inline std::string
394 TypedMetadata<bool>::str() const
395 {
396 return (mValue ? "true" : "false");
397 }
398
399
400 inline std::ostream&
401 operator<<(std::ostream& ostr, const Metadata& metadata)
402 {
403 ostr << metadata.str();
404 return ostr;
405 }
406
407
408 using BoolMetadata = TypedMetadata<bool>;
409 using DoubleMetadata = TypedMetadata<double>;
410 using FloatMetadata = TypedMetadata<float>;
411 using Int32Metadata = TypedMetadata<int32_t>;
412 using Int64Metadata = TypedMetadata<int64_t>;
413 using StringMetadata = TypedMetadata<std::string>;
414 using Vec2DMetadata = TypedMetadata<Vec2d>;
415 using Vec2IMetadata = TypedMetadata<Vec2i>;
416 using Vec2SMetadata = TypedMetadata<Vec2s>;
417 using Vec3DMetadata = TypedMetadata<Vec3d>;
418 using Vec3IMetadata = TypedMetadata<Vec3i>;
419 using Vec3SMetadata = TypedMetadata<Vec3s>;
420 using Mat4SMetadata = TypedMetadata<Mat4s>;
421 using Mat4DMetadata = TypedMetadata<Mat4d>;
422
423
425
426
427 template<>
428 inline Index32
429 StringMetadata::size() const
430 {
431 return static_cast<Index32>(mValue.size());
432 }
433
434
435 template<>
436 inline std::string
437 StringMetadata::str() const
438 {
439 return mValue;
440 }
441
442
443 template<>
444 inline void
445 StringMetadata::readValue(std::istream& is, Index32 size)
446 {
447 mValue.resize(size, '\0');
448 is.read(&mValue[0], size);
449 }
450
451 template<>
452 inline void
453 StringMetadata::writeValue(std::ostream& os) const
454 {
455 os.write(reinterpret_cast<const char*>(&mValue[0]), this->size());
456 }
457
458 } // namespace OPENVDB_VERSION_NAME
459 } // namespace openvdb
460
461 #endif // OPENVDB_METADATA_HAS_BEEN_INCLUDED
462
463 // Copyright (c) 2012-2017 DreamWorks Animation LLC
464 // All rights reserved. This software is distributed under the
465 // Mozilla Public License 2.0 ( http://www.mozilla.org/MPL/2.0/ )
openvdb::v5_0::Metadata::Ptr
SharedPtr< Metadata > Ptr
Definition: Metadata.h:53
OPENVDB_API
#define OPENVDB_API
Helper macros for defining library symbol visibility.
Definition: Platform.h:194
openvdb::v5_0::Metadata::registerType
static void registerType(const Name &typeName, Metadata::Ptr(*createMetadata)())
Register the given metadata type along with a factory function.
openvdb::v5_0::UnknownMetadata::ByteVec
std::vector< uint8_t > ByteVec
Definition: Metadata.h:124
openvdb::v5_0::operator<<
std::ostream & operator<<(std::ostream &ostr, const Metadata &metadata)
Write a Metadata to an output stream.
Definition: Metadata.h:401
Types.h
openvdb::v5_0::Metadata::Metadata
Metadata()
Definition: Metadata.h:56
openvdb::v5_0::UnknownMetadata::UnknownMetadata
UnknownMetadata(const Name &typ="<unknown>")
Definition: Metadata.h:126
Math.h
General-purpose arithmetic and comparison routines, most of which accept arbitrary value types (or at...
openvdb::v5_0::Metadata::operator!=
bool operator!=(const Metadata &other) const
Return true if the given metadata is different from this metadata.
Definition: Metadata.h:82
openvdb::v5_0::TypeError
Definition: Exceptions.h:90
openvdb::v5_0::TypedMetadata::value
T & value()
Return this metadata&#39;s value.
Definition: Metadata.h:299
openvdb::v5_0::math::operator==
bool operator==(const Vec3< T0 > &v0, const Vec3< T1 > &v1)
Equality operator, does exact floating point comparisons.
Definition: Vec3.h:488
OPENVDB_THROW
#define OPENVDB_THROW(exception, message)
Definition: Exceptions.h:108
openvdb::v5_0::TypedMetadata::readValue
void readValue(std::istream &, Index32 numBytes) override
Read the metadata from a stream.
Definition: Metadata.h:332
openvdb::v5_0::Metadata::readSize
static Index32 readSize(std::istream &)
Read the size of the metadata from a stream.
Definition: Metadata.h:230
openvdb::v5_0::Metadata::isRegisteredType
static bool isRegisteredType(const Name &typeName)
Return true if the given type is known by the metadata type registry.
openvdb::v5_0::UnknownMetadata::asBool
bool asBool() const override
Definition: Metadata.h:132
openvdb::v5_0::Metadata
Base class for storing metadata information in a grid.
Definition: Metadata.h:50
openvdb::v5_0::TypedMetadata::TypedMetadata
TypedMetadata()
Definition: Metadata.h:259
openvdb::v5_0::Metadata::write
void write(std::ostream &) const
Serialize this metadata to a stream.
Definition: Metadata.h:247
openvdb::v5_0::Metadata::writeSize
void writeSize(std::ostream &) const
Write the size of the metadata to a stream.
Definition: Metadata.h:222
openvdb::v5_0::UnknownMetadata::str
std::string str() const override
Return a textual representation of this metadata.
Definition: Metadata.h:131
openvdb::v5_0::TypedMetadata::registerType
static void registerType()
Definition: Metadata.h:371
openvdb::v5_0::TypedMetadata::isRegisteredType
static bool isRegisteredType()
Definition: Metadata.h:386
openvdb::v5_0::Index32
uint32_t Index32
Definition: Types.h:58
Exceptions.h
OPENVDB_VERSION_NAME
#define OPENVDB_VERSION_NAME
The version namespace name for this library version.
Definition: version.h:136
openvdb::v5_0::Metadata::ConstPtr
SharedPtr< const Metadata > ConstPtr
Definition: Metadata.h:54
openvdb::v5_0::TypedMetadata
Templated metadata class to hold specific types.
Definition: Metadata.h:171
openvdb::v5_0::TypedMetadata::setValue
void setValue(const T &)
Set this metadata&#39;s value.
Definition: Metadata.h:292
openvdb::v5_0::Metadata::str
virtual std::string str() const =0
Return a textual representation of this metadata.
openvdb::v5_0::TypedMetadata::staticTypeName
static Name staticTypeName()
Definition: Metadata.h:197
openvdb::v5_0::TypedMetadata::copy
Metadata::Ptr copy() const override
Return a copy of the metadata.
Definition: Metadata.h:313
openvdb
Definition: Exceptions.h:39
openvdb::v5_0::TypedMetadata::writeValue
void writeValue(std::ostream &) const override
Write the metadata to a stream.
Definition: Metadata.h:340
openvdb::v5_0::Metadata::~Metadata
virtual ~Metadata()
Definition: Metadata.h:57
openvdb::v5_0::Metadata::unregisterType
static void unregisterType(const Name &typeName)
openvdb::v5_0::TypedMetadata::~TypedMetadata
~TypedMetadata() override
Definition: Metadata.h:279
openvdb::v5_0::TypedMetadata::unregisterType
static void unregisterType()
Definition: Metadata.h:379
openvdb::v5_0::Metadata::read
void read(std::istream &)
Unserialize this metadata from a stream.
Definition: Metadata.h:239
version.h
Library and file format version numbers.
openvdb::v5_0::Name
std::string Name
Definition: Name.h:44
openvdb::v5_0::math::isZero
bool isZero(const Type &x)
Return true if x is exactly equal to zero.
Definition: Math.h:308
openvdb::v5_0::SharedPtr
std::shared_ptr< T > SharedPtr
Definition: Types.h:134
openvdb::v5_0::UnknownMetadata::size
Index32 size() const override
Return the size of this metadata in bytes.
Definition: Metadata.h:133
openvdb::v5_0::UnknownMetadata::value
const ByteVec & value() const
Definition: Metadata.h:136
openvdb::v5_0::UnknownMetadata::typeName
Name typeName() const override
Return the type name of the metadata.
Definition: Metadata.h:128
openvdb::v5_0::UnknownMetadata::setValue
void setValue(const ByteVec &bytes)
Definition: Metadata.h:135
OPENVDB_USE_VERSION_NAMESPACE
#define OPENVDB_USE_VERSION_NAMESPACE
Definition: version.h:188
openvdb::v5_0::TypedMetadata::str
std::string str() const override
Return a textual representation of this metadata.
Definition: Metadata.h:347
openvdb::v5_0::TypedMetadata::createMetadata
static Metadata::Ptr createMetadata()
Create new metadata of this type.
Definition: Metadata.h:363
openvdb::v5_0::UnknownMetadata
Subclass to hold raw data of an unregistered type.
Definition: Metadata.h:121
Name.h
openvdb::v5_0::TypedMetadata::size
Index32 size() const override
Return the size of this metadata in bytes.
Definition: Metadata.h:187
openvdb::v5_0::TypedMetadata::typeName
Name typeName() const override
Return the type name of the metadata.
Definition: Metadata.h:285
openvdb::v5_0::TypedMetadata::asBool
bool asBool() const override
Definition: Metadata.h:356