OpenVDB 3.2.0

metadata/Metadata.h
Go to the documentation of this file.
1 //
3 // Copyright (c) 2012-2016 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_METADATA_HAS_BEEN_INCLUDED
32 #define OPENVDB_METADATA_METADATA_HAS_BEEN_INCLUDED
33
34 #include <iostream>
35 #include <string>
36 #include <openvdb/Types.h>
37 #include <openvdb/math/Math.h> // for math::isZero()
38 #include <openvdb/util/Name.h>
39 #include <openvdb/Exceptions.h>
40 #include <boost/shared_ptr.hpp>
41 #include <boost/cstdint.hpp>
42
43
44 namespace openvdb {
45 OPENVDB_USE_VERSION_NAMESPACE
46 namespace OPENVDB_VERSION_NAME {
47
49 class OPENVDB_API Metadata
50 {
51 public:
52 typedef boost::shared_ptr<Metadata> Ptr;
53 typedef boost::shared_ptr<const Metadata> ConstPtr;
54
55 Metadata() {}
56 virtual ~Metadata() {}
57
59 virtual Name typeName() const = 0;
60
62 virtual Metadata::Ptr copy() const = 0;
63
65 virtual void copy(const Metadata& other) = 0;
66
68 virtual std::string str() const = 0;
69
72 virtual bool asBool() const = 0;
73
75 bool operator==(const Metadata& other) const;
77 bool operator!=(const Metadata& other) const { return !(*this == other); }
78
80 virtual Index32 size() const = 0;
81
83 void read(std::istream&);
85 void write(std::ostream&) const;
86
88 static Metadata::Ptr createMetadata(const Name& typeName);
89
91 static bool isRegisteredType(const Name& typeName);
92
94 static void clearRegistry();
95
97 static void registerType(const Name& typeName, Metadata::Ptr (*createMetadata)());
98 static void unregisterType(const Name& typeName);
99
100 protected:
102 static Index32 readSize(std::istream&);
104 void writeSize(std::ostream&) const;
105
107 virtual void readValue(std::istream&, Index32 numBytes) = 0;
109 virtual void writeValue(std::ostream&) const = 0;
110
111 private:
112 // Disallow copying of instances of this class.
113 Metadata(const Metadata&);
114 Metadata& operator=(const Metadata&);
115 };
116
117
119 class OPENVDB_API UnknownMetadata: public Metadata
120 {
121 public:
122 UnknownMetadata() {}
123 virtual ~UnknownMetadata() {}
124 virtual Name typeName() const { return "<unknown>"; }
125 virtual Metadata::Ptr copy() const { OPENVDB_THROW(TypeError, "Metadata has unknown type"); }
126 virtual void copy(const Metadata&) { OPENVDB_THROW(TypeError, "Destination has unknown type"); }
127 virtual std::string str() const { return "<unknown>"; }
128 virtual bool asBool() const { return false; }
129 virtual Index32 size() const { return 0; }
130
131 protected:
132 virtual void readValue(std::istream&s, Index32 numBytes);
133 virtual void writeValue(std::ostream&) const;
134 };
135
136
138 template<typename T>
139 class TypedMetadata: public Metadata
140 {
141 public:
142 typedef boost::shared_ptr<TypedMetadata<T> > Ptr;
143 typedef boost::shared_ptr<const TypedMetadata<T> > ConstPtr;
144
145 TypedMetadata();
146 TypedMetadata(const T& value);
147 TypedMetadata(const TypedMetadata<T>& other);
148 virtual ~TypedMetadata();
149
150 virtual Name typeName() const;
151 virtual Metadata::Ptr copy() const;
152 virtual void copy(const Metadata& other);
153 virtual std::string str() const;
154 virtual bool asBool() const;
155 virtual Index32 size() const { return static_cast<Index32>(sizeof(T)); }
156
158 void setValue(const T&);
160 T& value();
161 const T& value() const;
162
163 // Static specialized function for the type name. This function must be
164 // template specialized for each type T.
165 static Name staticTypeName() { return typeNameAsString<T>(); }
166
168 static Metadata::Ptr createMetadata();
169
170 static void registerType();
171 static void unregisterType();
172 static bool isRegisteredType();
173
174 protected:
175 virtual void readValue(std::istream&, Index32 numBytes);
176 virtual void writeValue(std::ostream&) const;
177
178 private:
179 T mValue;
180 };
181
183 std::ostream& operator<<(std::ostream& ostr, const Metadata& metadata);
184
185
187
188
189 inline void
190 Metadata::writeSize(std::ostream& os) const
191 {
192 const Index32 n = this->size();
193 os.write(reinterpret_cast<const char*>(&n), sizeof(Index32));
194 }
195
196
197 inline Index32
198 Metadata::readSize(std::istream& is)
199 {
200 Index32 n = 0;
201 is.read(reinterpret_cast<char*>(&n), sizeof(Index32));
202 return n;
203 }
204
205
206 inline void
207 Metadata::read(std::istream& is)
208 {
209 const Index32 numBytes = this->readSize(is);
210 this->readValue(is, numBytes);
211 }
212
213
214 inline void
215 Metadata::write(std::ostream& os) const
216 {
217 this->writeSize(os);
218 this->writeValue(os);
219 }
220
221
223
224
225 template <typename T>
226 inline
227 TypedMetadata<T>::TypedMetadata() : mValue(T())
228 {
229 }
230
231 template <typename T>
232 inline
233 TypedMetadata<T>::TypedMetadata(const T &value) : mValue(value)
234 {
235 }
236
237 template <typename T>
238 inline
239 TypedMetadata<T>::TypedMetadata(const TypedMetadata<T> &other) :
240 Metadata(),
241 mValue(other.mValue)
242 {
243 }
244
245 template <typename T>
246 inline
247 TypedMetadata<T>::~TypedMetadata()
248 {
249 }
250
251 template <typename T>
252 inline Name
253 TypedMetadata<T>::typeName() const
254 {
255 return TypedMetadata<T>::staticTypeName();
256 }
257
258 template <typename T>
259 inline void
260 TypedMetadata<T>::setValue(const T& val)
261 {
262 mValue = val;
263 }
264
265 template <typename T>
266 inline T&
267 TypedMetadata<T>::value()
268 {
269 return mValue;
270 }
271
272 template <typename T>
273 inline const T&
274 TypedMetadata<T>::value() const
275 {
276 return mValue;
277 }
278
279 template <typename T>
280 inline Metadata::Ptr
281 TypedMetadata<T>::copy() const
282 {
283 Metadata::Ptr metadata(new TypedMetadata<T>());
284 metadata->copy(*this);
285 return metadata;
286 }
287
288 template <typename T>
289 inline void
290 TypedMetadata<T>::copy(const Metadata &other)
291 {
292 const TypedMetadata<T>* t = dynamic_cast<const TypedMetadata<T>*>(&other);
293 if (t == NULL) OPENVDB_THROW(TypeError, "Incompatible type during copy");
294 mValue = t->mValue;
295 }
296
297
298 template<typename T>
299 inline void
300 TypedMetadata<T>::readValue(std::istream& is, Index32 /*numBytes*/)
301 {
302 //assert(this->size() == numBytes);
303 is.read(reinterpret_cast<char*>(&mValue), this->size());
304 }
305
306 template<typename T>
307 inline void
308 TypedMetadata<T>::writeValue(std::ostream& os) const
309 {
310 os.write(reinterpret_cast<const char*>(&mValue), this->size());
311 }
312
313 template <typename T>
314 inline std::string
315 TypedMetadata<T>::str() const
316 {
317 std::ostringstream ostr;
318 ostr << mValue;
319 return ostr.str();
320 }
321
322 template<typename T>
323 inline bool
324 TypedMetadata<T>::asBool() const
325 {
326 return !math::isZero(mValue);
327 }
328
329 template <typename T>
330 inline Metadata::Ptr
331 TypedMetadata<T>::createMetadata()
332 {
333 Metadata::Ptr ret(new TypedMetadata<T>());
334 return ret;
335 }
336
337 template <typename T>
338 inline void
339 TypedMetadata<T>::registerType()
340 {
341 Metadata::registerType(TypedMetadata<T>::staticTypeName(),
342 TypedMetadata<T>::createMetadata);
343 }
344
345 template <typename T>
346 inline void
347 TypedMetadata<T>::unregisterType()
348 {
349 Metadata::unregisterType(TypedMetadata<T>::staticTypeName());
350 }
351
352 template <typename T>
353 inline bool
354 TypedMetadata<T>::isRegisteredType()
355 {
356 return Metadata::isRegisteredType(TypedMetadata<T>::staticTypeName());
357 }
358
359
360 template<>
361 inline std::string
362 TypedMetadata<bool>::str() const
363 {
364 return (mValue ? "true" : "false");
365 }
366
367
368 inline std::ostream&
369 operator<<(std::ostream& ostr, const Metadata& metadata)
370 {
371 ostr << metadata.str();
372 return ostr;
373 }
374
375
376 typedef TypedMetadata<bool> BoolMetadata;
377 typedef TypedMetadata<double> DoubleMetadata;
378 typedef TypedMetadata<float> FloatMetadata;
379 typedef TypedMetadata<boost::int32_t> Int32Metadata;
380 typedef TypedMetadata<boost::int64_t> Int64Metadata;
381 typedef TypedMetadata<Vec2d> Vec2DMetadata;
382 typedef TypedMetadata<Vec2i> Vec2IMetadata;
383 typedef TypedMetadata<Vec2s> Vec2SMetadata;
384 typedef TypedMetadata<Vec3d> Vec3DMetadata;
385 typedef TypedMetadata<Vec3i> Vec3IMetadata;
386 typedef TypedMetadata<Vec3s> Vec3SMetadata;
387 typedef TypedMetadata<Mat4s> Mat4SMetadata;
388 typedef TypedMetadata<Mat4d> Mat4DMetadata;
389
390 } // namespace OPENVDB_VERSION_NAME
391 } // namespace openvdb
392
393 #endif // OPENVDB_METADATA_METADATA_HAS_BEEN_INCLUDED
394
395 // Copyright (c) 2012-2016 DreamWorks Animation LLC
396 // All rights reserved. This software is distributed under the
397 // Mozilla Public License 2.0 ( http://www.mozilla.org/MPL/2.0/ )
openvdb::v3_2_0::TypedMetadata::ConstPtr
boost::shared_ptr< const TypedMetadata< T > > ConstPtr
Definition: metadata/Metadata.h:143
openvdb::v3_2_0::TypedMetadata::registerType
static void registerType()
Definition: metadata/Metadata.h:339
openvdb::v3_2_0::TypedMetadata::createMetadata
static Metadata::Ptr createMetadata()
Create new metadata of this type.
Definition: metadata/Metadata.h:331
OPENVDB_API
#define OPENVDB_API
Helper macros for defining library symbol visibility.
Definition: Platform.h:195
openvdb::v3_2_0::TypedMetadata
Templated metadata class to hold specific types.
Definition: metadata/Metadata.h:139
openvdb::v3_2_0::Metadata::unregisterType
static void unregisterType(const Name &typeName)
openvdb::v3_2_0::TypedMetadata::typeName
virtual Name typeName() const
Return the type name of the metadata.
Definition: metadata/Metadata.h:253
openvdb::v3_2_0::Vec2SMetadata
TypedMetadata< Vec2s > Vec2SMetadata
Definition: metadata/Metadata.h:383
Types.h
openvdb::v3_2_0::Vec3IMetadata
TypedMetadata< Vec3i > Vec3IMetadata
Definition: metadata/Metadata.h:385
openvdb::v3_2_0::Metadata
Base class for storing metadata information in a grid.
Definition: metadata/Metadata.h:49
openvdb::v3_2_0::UnknownMetadata::asBool
virtual bool asBool() const
Definition: metadata/Metadata.h:128
openvdb::v3_2_0::Metadata::ConstPtr
boost::shared_ptr< const Metadata > ConstPtr
Definition: metadata/Metadata.h:53
openvdb::v3_2_0::TypedMetadata::unregisterType
static void unregisterType()
Definition: metadata/Metadata.h:347
openvdb::v3_2_0::DoubleMetadata
TypedMetadata< double > DoubleMetadata
Definition: metadata/Metadata.h:377
openvdb::v3_2_0::TypedMetadata::readValue
virtual void readValue(std::istream &, Index32 numBytes)
Read the metadata from a stream.
Definition: metadata/Metadata.h:300
Math.h
General-purpose arithmetic and comparison routines, most of which accept arbitrary value types (or at...
openvdb::v3_2_0::Vec3SMetadata
TypedMetadata< Vec3s > Vec3SMetadata
Definition: metadata/Metadata.h:386
openvdb::v3_2_0::TypedMetadata::~TypedMetadata
virtual ~TypedMetadata()
Definition: metadata/Metadata.h:247
openvdb::v3_2_0::Vec2IMetadata
TypedMetadata< Vec2i > Vec2IMetadata
Definition: metadata/Metadata.h:382
openvdb::v3_2_0::UnknownMetadata::str
virtual std::string str() const
Return a textual representation of this metadata.
Definition: metadata/Metadata.h:127
openvdb::v3_2_0::TypedMetadata::size
virtual Index32 size() const
Return the size of this metadata in bytes.
Definition: metadata/Metadata.h:155
openvdb::v3_2_0::TypedMetadata::writeValue
virtual void writeValue(std::ostream &) const
Write the metadata to a stream.
Definition: metadata/Metadata.h:308
OPENVDB_THROW
#define OPENVDB_THROW(exception, message)
Definition: Exceptions.h:97
openvdb::v3_2_0::Name
std::string Name
Definition: Name.h:44
openvdb::v3_2_0::Metadata::read
void read(std::istream &)
Unserialize this metadata from a stream.
Definition: metadata/Metadata.h:207
openvdb::v3_2_0::TypedMetadata::isRegisteredType
static bool isRegisteredType()
Definition: metadata/Metadata.h:354
openvdb::v3_2_0::math::isZero
bool isZero(const Type &x)
Return true if x is exactly equal to zero.
Definition: Math.h:324
openvdb::v3_2_0::UnknownMetadata::copy
virtual Metadata::Ptr copy() const
Return a copy of the metadata.
Definition: metadata/Metadata.h:125
openvdb::v3_2_0::Metadata::write
void write(std::ostream &) const
Serialize this metadata to a stream.
Definition: metadata/Metadata.h:215
openvdb::v3_2_0::TypedMetadata::asBool
virtual bool asBool() const
Definition: metadata/Metadata.h:324
openvdb::v3_2_0::Mat4DMetadata
TypedMetadata< Mat4d > Mat4DMetadata
Definition: metadata/Metadata.h:388
openvdb::v3_2_0::UnknownMetadata::typeName
virtual Name typeName() const
Return the type name of the metadata.
Definition: metadata/Metadata.h:124
openvdb::v3_2_0::Metadata::readSize
static Index32 readSize(std::istream &)
Read the size of the metadata from a stream.
Definition: metadata/Metadata.h:198
openvdb::v3_2_0::UnknownMetadata
Subclass to read (and ignore) data of an unregistered type.
Definition: metadata/Metadata.h:119
openvdb::v3_2_0::TypedMetadata::copy
virtual Metadata::Ptr copy() const
Return a copy of the metadata.
Definition: metadata/Metadata.h:281
openvdb::v3_2_0::TypedMetadata::Ptr
boost::shared_ptr< TypedMetadata< T > > Ptr
Definition: metadata/Metadata.h:142
openvdb::v3_2_0::Int64Metadata
TypedMetadata< boost::int64_t > Int64Metadata
Definition: metadata/Metadata.h:380
openvdb::v3_2_0::Int32Metadata
TypedMetadata< boost::int32_t > Int32Metadata
Definition: metadata/Metadata.h:379
openvdb::v3_2_0::Metadata::isRegisteredType
static bool isRegisteredType(const Name &typeName)
Return true if the given type is known by the metadata type registry.
openvdb::v3_2_0::Metadata::Ptr
boost::shared_ptr< Metadata > Ptr
Definition: metadata/Metadata.h:52
openvdb::v3_2_0::Metadata::registerType
static void registerType(const Name &typeName, Metadata::Ptr(*createMetadata)())
Register the given metadata type along with a factory function.
openvdb::v3_2_0::TypedMetadata::staticTypeName
static Name staticTypeName()
Definition: metadata/Metadata.h:165
openvdb::v3_2_0::FloatMetadata
TypedMetadata< float > FloatMetadata
Definition: metadata/Metadata.h:378
Exceptions.h
openvdb::v3_2_0::TypedMetadata::TypedMetadata
TypedMetadata()
Definition: metadata/Metadata.h:227
OPENVDB_VERSION_NAME
#define OPENVDB_VERSION_NAME
Definition: version.h:43
openvdb::v3_2_0::operator<<
std::ostream & operator<<(std::ostream &ostr, const Metadata &metadata)
Write a Metadata to an output stream.
Definition: metadata/Metadata.h:369
openvdb::v3_2_0::TypedMetadata::setValue
void setValue(const T &)
Set this metadata&#39;s value.
Definition: metadata/Metadata.h:260
openvdb::v3_2_0::Vec2DMetadata
TypedMetadata< Vec2d > Vec2DMetadata
Definition: metadata/Metadata.h:381
openvdb
Definition: Exceptions.h:39
openvdb::v3_2_0::Index32
uint32_t Index32
Definition: Types.h:56
openvdb::v3_2_0::UnknownMetadata::~UnknownMetadata
virtual ~UnknownMetadata()
Definition: metadata/Metadata.h:123
openvdb::v3_2_0::Metadata::operator!=
bool operator!=(const Metadata &other) const
Return true if the given metadata is different from this metadata.
Definition: metadata/Metadata.h:77
openvdb::v3_2_0::TypedMetadata::str
virtual std::string str() const
Return a textual representation of this metadata.
Definition: metadata/Metadata.h:315
openvdb::v3_2_0::UnknownMetadata::copy
virtual void copy(const Metadata &)
Copy the given metadata into this metadata.
Definition: metadata/Metadata.h:126
openvdb::v3_2_0::Metadata::writeSize
void writeSize(std::ostream &) const
Write the size of the metadata to a stream.
Definition: metadata/Metadata.h:190
openvdb::v3_2_0::Mat4SMetadata
TypedMetadata< Mat4s > Mat4SMetadata
Definition: metadata/Metadata.h:387
openvdb::v3_2_0::TypeError
Definition: Exceptions.h:87
openvdb::v3_2_0::UnknownMetadata::UnknownMetadata
UnknownMetadata()
Definition: metadata/Metadata.h:122
openvdb::v3_2_0::UnknownMetadata::size
virtual Index32 size() const
Return the size of this metadata in bytes.
Definition: metadata/Metadata.h:129
openvdb::v3_2_0::math::operator==
bool operator==(const Vec3< T0 > &v0, const Vec3< T1 > &v1)
Equality operator, does exact floating point comparisons.
Definition: Vec3.h:450
openvdb::v3_2_0::Vec3DMetadata
TypedMetadata< Vec3d > Vec3DMetadata
Definition: metadata/Metadata.h:384
openvdb::v3_2_0::BoolMetadata
TypedMetadata< bool > BoolMetadata
Definition: metadata/Metadata.h:376
openvdb::v3_2_0::TypedMetadata::value
T & value()
Return this metadata&#39;s value.
Definition: metadata/Metadata.h:267
openvdb::v3_2_0::Metadata::Metadata
Metadata()
Definition: metadata/Metadata.h:55
OPENVDB_USE_VERSION_NAMESPACE
#define OPENVDB_USE_VERSION_NAMESPACE
Definition: version.h:71
openvdb::v3_2_0::Metadata::~Metadata
virtual ~Metadata()
Definition: metadata/Metadata.h:56
openvdb::v3_2_0::Metadata::str
virtual std::string str() const =0
Return a textual representation of this metadata.
Name.h