OpenVDB: openvdb/Metadata.h Source File

OpenVDB 13.0.0

Loading...
Searching...
No Matches
Metadata.h
Go to the documentation of this file.
1// Copyright Contributors to the OpenVDB Project
2// SPDX-License-Identifier: Apache-2.0
3
4#ifndef OPENVDB_METADATA_HAS_BEEN_INCLUDED
5#define OPENVDB_METADATA_HAS_BEEN_INCLUDED
6
7#include "version.h"
8#include "Exceptions.h"
9#include "Types.h"
10#include "math/Math.h" // for math::isZero()
11#include "util/Name.h"
12#include "util/Assert.h"
13#include <cstdint>
14#include <iostream>
15#include <string>
16#include <vector>
17
18
19namespace openvdb {
20 OPENVDB_USE_VERSION_NAMESPACE
21namespace OPENVDB_VERSION_NAME {
22
23/// @brief Base class for storing metadata information in a grid.
24 class OPENVDB_API Metadata
25{
26public:
27 using Ptr = SharedPtr<Metadata>;
28 using ConstPtr = SharedPtr<const Metadata>;
29
30 Metadata() {}
31 virtual ~Metadata() {}
32
33 // Disallow copying of instances of this class.
34 Metadata(const Metadata&) = delete;
35 Metadata& operator=(const Metadata&) = delete;
36
37 /// Return the type name of the metadata.
38 virtual Name typeName() const = 0;
39
40 /// Return a copy of the metadata.
41 virtual Metadata::Ptr copy() const = 0;
42
43 /// Copy the given metadata into this metadata.
44 virtual void copy(const Metadata& other) = 0;
45
46 /// Return a textual representation of this metadata.
47 virtual std::string str() const = 0;
48
49 /// Return the boolean representation of this metadata (empty strings
50 /// and zeroVals evaluate to false; most other values evaluate to true).
51 virtual bool asBool() const = 0;
52
53 /// Return @c true if the given metadata is equivalent to this metadata.
54 bool operator==(const Metadata& other) const;
55 /// Return @c true if the given metadata is different from this metadata.
56 bool operator!=(const Metadata& other) const { return !(*this == other); }
57
58 /// Return the size of this metadata in bytes.
59 virtual Index32 size() const = 0;
60
61 /// Unserialize this metadata from a stream.
62 void read(std::istream&);
63 /// Serialize this metadata to a stream.
64 void write(std::ostream&) const;
65
66 /// Create new metadata of the given type.
67 static Metadata::Ptr createMetadata(const Name& typeName);
68
69 /// Return @c true if the given type is known by the metadata type registry.
70 static bool isRegisteredType(const Name& typeName);
71
72 /// Clear out the metadata registry.
73 static void clearRegistry();
74
75 /// Register the given metadata type along with a factory function.
76 static void registerType(const Name& typeName, Metadata::Ptr (*createMetadata)());
77 static void unregisterType(const Name& typeName);
78
79protected:
80 /// Read the size of the metadata from a stream.
81 static Index32 readSize(std::istream&);
82 /// Write the size of the metadata to a stream.
83 void writeSize(std::ostream&) const;
84
85 /// Read the metadata from a stream.
86 virtual void readValue(std::istream&, Index32 numBytes) = 0;
87 /// Write the metadata to a stream.
88 virtual void writeValue(std::ostream&) const = 0;
89};
90
91
92/// @brief Subclass to hold raw data of an unregistered type
93 class OPENVDB_API UnknownMetadata: public Metadata
94{
95public:
96 using ByteVec = std::vector<uint8_t>;
97
98 explicit UnknownMetadata(const Name& typ = "<unknown>"): mTypeName(typ) {}
99
100 Name typeName() const override { return mTypeName; }
101 Metadata::Ptr copy() const override;
102 void copy(const Metadata&) override;
103 std::string str() const override { return (mBytes.empty() ? "" : "<binary data>"); }
104 bool asBool() const override { return !mBytes.empty(); }
105 Index32 size() const override { return static_cast<Index32>(mBytes.size()); }
106
107 void setValue(const ByteVec& bytes) { mBytes = bytes; }
108 const ByteVec& value() const { return mBytes; }
109
110protected:
111 void readValue(std::istream&, Index32 numBytes) override;
112 void writeValue(std::ostream&) const override;
113
114private:
115 Name mTypeName;
116 ByteVec mBytes;
117};
118
119
120/// @brief Templated metadata class to hold specific types.
121template<typename T>
122 class TypedMetadata: public Metadata
123{
124public:
125 using Ptr = SharedPtr<TypedMetadata<T>>;
126 using ConstPtr = SharedPtr<const TypedMetadata<T>>;
127
128 TypedMetadata();
129 TypedMetadata(const T& value);
130 TypedMetadata(const TypedMetadata<T>& other);
131 ~TypedMetadata() override;
132
133 Name typeName() const override;
134 Metadata::Ptr copy() const override;
135 void copy(const Metadata& other) override;
136 std::string str() const override;
137 bool asBool() const override;
138 Index32 size() const override { return static_cast<Index32>(sizeof(T)); }
139
140 /// Set this metadata's value.
141 void setValue(const T&);
142 /// Return this metadata's value.
143 T& value();
144 const T& value() const;
145
146 // Static specialized function for the type name. This function must be
147 // template specialized for each type T.
148 static Name staticTypeName() { return typeNameAsString<T>(); }
149
150 /// Create new metadata of this type.
151 static Metadata::Ptr createMetadata();
152
153 static void registerType();
154 static void unregisterType();
155 static bool isRegisteredType();
156
157protected:
158 void readValue(std::istream&, Index32 numBytes) override;
159 void writeValue(std::ostream&) const override;
160
161private:
162 T mValue;
163};
164
165/// Write a Metadata to an output stream
166std::ostream& operator<<(std::ostream& ostr, const Metadata& metadata);
167
168
169////////////////////////////////////////
170
171
172inline void
173Metadata::writeSize(std::ostream& os) const
174{
175 const Index32 n = this->size();
176 os.write(reinterpret_cast<const char*>(&n), sizeof(Index32));
177}
178
179
180inline Index32
181Metadata::readSize(std::istream& is)
182{
183 Index32 n = 0;
184 is.read(reinterpret_cast<char*>(&n), sizeof(Index32));
185 return n;
186}
187
188
189inline void
190Metadata::read(std::istream& is)
191{
192 const Index32 numBytes = this->readSize(is);
193 this->readValue(is, numBytes);
194}
195
196
197inline void
198Metadata::write(std::ostream& os) const
199{
200 this->writeSize(os);
201 this->writeValue(os);
202}
203
204
205////////////////////////////////////////
206
207
208template <typename T>
209inline
210TypedMetadata<T>::TypedMetadata() : mValue(T())
211{
212}
213
214template <typename T>
215inline
216 TypedMetadata<T>::TypedMetadata(const T &value) : mValue(value)
217{
218}
219
220template <typename T>
221inline
222 TypedMetadata<T>::TypedMetadata(const TypedMetadata<T> &other) :
223 Metadata(),
224 mValue(other.mValue)
225{
226}
227
228template <typename T>
229inline
230TypedMetadata<T>::~TypedMetadata()
231{
232}
233
234template <typename T>
235inline Name
236TypedMetadata<T>::typeName() const
237{
238 return TypedMetadata<T>::staticTypeName();
239}
240
241template <typename T>
242inline void
243TypedMetadata<T>::setValue(const T& val)
244{
245 mValue = val;
246}
247
248template <typename T>
249inline T&
250TypedMetadata<T>::value()
251{
252 return mValue;
253}
254
255template <typename T>
256inline const T&
257 TypedMetadata<T>::value() const
258{
259 return mValue;
260}
261
262template <typename T>
263inline Metadata::Ptr
264TypedMetadata<T>::copy() const
265{
266 Metadata::Ptr metadata(new TypedMetadata<T>());
267 metadata->copy(*this);
268 return metadata;
269}
270
271template <typename T>
272inline void
273 TypedMetadata<T>::copy(const Metadata &other)
274{
275 const TypedMetadata<T>* t = dynamic_cast<const TypedMetadata<T>*>(&other);
276 if (t == nullptr) OPENVDB_THROW(TypeError, "Incompatible type during copy");
277 mValue = t->mValue;
278}
279
280
281template<typename T>
282inline void
283TypedMetadata<T>::readValue(std::istream& is, [[maybe_unused]] Index32 numBytes)
284{
285 OPENVDB_ASSERT(this->size() == numBytes);
286 is.read(reinterpret_cast<char*>(&mValue), this->size());
287}
288
289template<typename T>
290inline void
291TypedMetadata<T>::writeValue(std::ostream& os) const
292{
293 os.write(reinterpret_cast<const char*>(&mValue), this->size());
294}
295
296template <typename T>
297inline std::string
298TypedMetadata<T>::str() const
299{
300 std::ostringstream ostr;
301 ostr << mValue;
302 return ostr.str();
303}
304
305template<typename T>
306inline bool
307TypedMetadata<T>::asBool() const
308{
309 return !math::isZero(mValue);
310}
311
312template <typename T>
313inline Metadata::Ptr
314TypedMetadata<T>::createMetadata()
315{
316 Metadata::Ptr ret(new TypedMetadata<T>());
317 return ret;
318}
319
320template <typename T>
321inline void
322TypedMetadata<T>::registerType()
323{
324 Metadata::registerType(TypedMetadata<T>::staticTypeName(),
325 TypedMetadata<T>::createMetadata);
326}
327
328template <typename T>
329inline void
330TypedMetadata<T>::unregisterType()
331{
332 Metadata::unregisterType(TypedMetadata<T>::staticTypeName());
333}
334
335template <typename T>
336inline bool
337TypedMetadata<T>::isRegisteredType()
338{
339 return Metadata::isRegisteredType(TypedMetadata<T>::staticTypeName());
340}
341
342
343template<>
344inline std::string
345 TypedMetadata<bool>::str() const
346{
347 return (mValue ? "true" : "false");
348}
349
350
351 inline std::ostream&
352 operator<<(std::ostream& ostr, const Metadata& metadata)
353{
354 ostr << metadata.str();
355 return ostr;
356}
357
358
359 using BoolMetadata = TypedMetadata<bool>;
360 using DoubleMetadata = TypedMetadata<double>;
361 using FloatMetadata = TypedMetadata<float>;
362 using HalfMetadata = TypedMetadata<math::half>;
363 using Int32Metadata = TypedMetadata<int32_t>;
364 using Int64Metadata = TypedMetadata<int64_t>;
365 using StringMetadata = TypedMetadata<std::string>;
366 using Vec2DMetadata = TypedMetadata<Vec2d>;
367 using Vec2IMetadata = TypedMetadata<Vec2i>;
368 using Vec2SMetadata = TypedMetadata<Vec2s>;
369 using Vec3DMetadata = TypedMetadata<Vec3d>;
370 using Vec3IMetadata = TypedMetadata<Vec3i>;
371 using Vec3SMetadata = TypedMetadata<Vec3s>;
372 using Vec4DMetadata = TypedMetadata<Vec4d>;
373 using Vec4IMetadata = TypedMetadata<Vec4i>;
374 using Vec4SMetadata = TypedMetadata<Vec4s>;
375 using Mat4SMetadata = TypedMetadata<Mat4s>;
376 using Mat4DMetadata = TypedMetadata<Mat4d>;
377
378
379////////////////////////////////////////
380
381
382template<>
383inline Index32
384 StringMetadata::size() const
385{
386 return static_cast<Index32>(mValue.size());
387}
388
389
390template<>
391inline std::string
392StringMetadata::str() const
393{
394 return mValue;
395}
396
397
398template<>
399inline void
400StringMetadata::readValue(std::istream& is, Index32 size)
401{
402 mValue.resize(size, '\0');
403 is.read(&mValue[0], size);
404}
405
406template<>
407inline void
408StringMetadata::writeValue(std::ostream& os) const
409{
410 os.write(reinterpret_cast<const char*>(&mValue[0]), this->size());
411}
412
413} // namespace OPENVDB_VERSION_NAME
414} // namespace openvdb
415
416#endif // OPENVDB_METADATA_HAS_BEEN_INCLUDED
Assert.h
OPENVDB_ASSERT
#define OPENVDB_ASSERT(X)
Definition Assert.h:41
Math.h
General-purpose arithmetic and comparison routines, most of which accept arbitrary value types (or at...
Name.h
OPENVDB_API
#define OPENVDB_API
Definition Platform.h:291
openvdb::v13_0::Metadata
Base class for storing metadata information in a grid.
Definition Metadata.h:25
openvdb::v13_0::Metadata::ConstPtr
SharedPtr< const Metadata > ConstPtr
Definition Metadata.h:28
openvdb::v13_0::Metadata::copy
virtual void copy(const Metadata &other)=0
Copy the given metadata into this metadata.
openvdb::v13_0::Metadata::operator!=
bool operator!=(const Metadata &other) const
Return true if the given metadata is different from this metadata.
Definition Metadata.h:56
openvdb::v13_0::Metadata::asBool
virtual bool asBool() const =0
openvdb::v13_0::Metadata::typeName
virtual Name typeName() const =0
Return the type name of the metadata.
openvdb::v13_0::Metadata::Metadata
Metadata()
Definition Metadata.h:30
openvdb::v13_0::Metadata::operator=
Metadata & operator=(const Metadata &)=delete
openvdb::v13_0::Metadata::Metadata
Metadata(const Metadata &)=delete
openvdb::v13_0::Metadata::write
void write(std::ostream &) const
Serialize this metadata to a stream.
Definition Metadata.h:198
openvdb::v13_0::Metadata::createMetadata
static Metadata::Ptr createMetadata(const Name &typeName)
Create new metadata of the given type.
openvdb::v13_0::Metadata::writeValue
virtual void writeValue(std::ostream &) const =0
Write the metadata to a stream.
openvdb::v13_0::Metadata::str
virtual std::string str() const =0
Return a textual representation of this metadata.
openvdb::v13_0::Metadata::registerType
static void registerType(const Name &typeName, Metadata::Ptr(*createMetadata)())
Register the given metadata type along with a factory function.
openvdb::v13_0::Metadata::read
void read(std::istream &)
Unserialize this metadata from a stream.
Definition Metadata.h:190
openvdb::v13_0::Metadata::isRegisteredType
static bool isRegisteredType(const Name &typeName)
Return true if the given type is known by the metadata type registry.
openvdb::v13_0::Metadata::copy
virtual Metadata::Ptr copy() const =0
Return a copy of the metadata.
openvdb::v13_0::Metadata::unregisterType
static void unregisterType(const Name &typeName)
openvdb::v13_0::Metadata::readSize
static Index32 readSize(std::istream &)
Read the size of the metadata from a stream.
Definition Metadata.h:181
openvdb::v13_0::Metadata::operator==
bool operator==(const Metadata &other) const
Return true if the given metadata is equivalent to this metadata.
openvdb::v13_0::Metadata::Ptr
SharedPtr< Metadata > Ptr
Definition Metadata.h:27
openvdb::v13_0::Metadata::clearRegistry
static void clearRegistry()
Clear out the metadata registry.
openvdb::v13_0::Metadata::writeSize
void writeSize(std::ostream &) const
Write the size of the metadata to a stream.
Definition Metadata.h:173
openvdb::v13_0::Metadata::size
virtual Index32 size() const =0
Return the size of this metadata in bytes.
openvdb::v13_0::Metadata::readValue
virtual void readValue(std::istream &, Index32 numBytes)=0
Read the metadata from a stream.
openvdb::v13_0::Metadata::~Metadata
virtual ~Metadata()
Definition Metadata.h:31
openvdb::v13_0::TypeError
Definition Exceptions.h:64
openvdb::v13_0::TypedMetadata
Templated metadata class to hold specific types.
Definition Metadata.h:123
openvdb::v13_0::TypedMetadata::TypedMetadata
TypedMetadata(const T &value)
Definition Metadata.h:216
openvdb::v13_0::TypedMetadata::copy
void copy(const Metadata &other) override
Copy the given metadata into this metadata.
Definition Metadata.h:273
openvdb::v13_0::TypedMetadata::size
Index32 size() const override
Return the size of this metadata in bytes.
Definition Metadata.h:138
openvdb::v13_0::TypedMetadata::str
std::string str() const override
Return a textual representation of this metadata.
Definition Metadata.h:298
openvdb::v13_0::TypedMetadata::asBool
bool asBool() const override
Definition Metadata.h:307
openvdb::v13_0::TypedMetadata::typeName
Name typeName() const override
Return the type name of the metadata.
Definition Metadata.h:236
openvdb::v13_0::TypedMetadata::registerType
static void registerType()
Definition Metadata.h:322
openvdb::v13_0::TypedMetadata::unregisterType
static void unregisterType()
Definition Metadata.h:330
openvdb::v13_0::TypedMetadata::TypedMetadata
TypedMetadata(const TypedMetadata< T > &other)
Definition Metadata.h:222
openvdb::v13_0::TypedMetadata::isRegisteredType
static bool isRegisteredType()
Definition Metadata.h:337
openvdb::v13_0::TypedMetadata::createMetadata
static Metadata::Ptr createMetadata()
Create new metadata of this type.
Definition Metadata.h:314
openvdb::v13_0::TypedMetadata::TypedMetadata
TypedMetadata()
Definition Metadata.h:210
openvdb::v13_0::TypedMetadata::~TypedMetadata
~TypedMetadata() override
Definition Metadata.h:230
openvdb::v13_0::TypedMetadata::writeValue
void writeValue(std::ostream &) const override
Write the metadata to a stream.
Definition Metadata.h:291
openvdb::v13_0::TypedMetadata::Ptr
SharedPtr< TypedMetadata< T > > Ptr
Definition Metadata.h:125
openvdb::v13_0::TypedMetadata::copy
Metadata::Ptr copy() const override
Return a copy of the metadata.
Definition Metadata.h:264
openvdb::v13_0::TypedMetadata::ConstPtr
SharedPtr< const TypedMetadata< T > > ConstPtr
Definition Metadata.h:126
openvdb::v13_0::TypedMetadata< bool >::value
bool & value()
openvdb::v13_0::TypedMetadata::value
const T & value() const
Definition Metadata.h:257
openvdb::v13_0::TypedMetadata::staticTypeName
static Name staticTypeName()
Definition Metadata.h:148
openvdb::v13_0::TypedMetadata::readValue
void readValue(std::istream &, Index32 numBytes) override
Read the metadata from a stream.
Definition Metadata.h:283
openvdb::v13_0::TypedMetadata::setValue
void setValue(const T &)
Set this metadata's value.
Definition Metadata.h:243
openvdb::v13_0::UnknownMetadata::setValue
void setValue(const ByteVec &bytes)
Definition Metadata.h:107
openvdb::v13_0::UnknownMetadata::size
Index32 size() const override
Return the size of this metadata in bytes.
Definition Metadata.h:105
openvdb::v13_0::UnknownMetadata::copy
void copy(const Metadata &) override
Copy the given metadata into this metadata.
openvdb::v13_0::UnknownMetadata::value
const ByteVec & value() const
Definition Metadata.h:108
openvdb::v13_0::UnknownMetadata::str
std::string str() const override
Return a textual representation of this metadata.
Definition Metadata.h:103
openvdb::v13_0::UnknownMetadata::asBool
bool asBool() const override
Definition Metadata.h:104
openvdb::v13_0::UnknownMetadata::typeName
Name typeName() const override
Return the type name of the metadata.
Definition Metadata.h:100
openvdb::v13_0::UnknownMetadata::ByteVec
std::vector< uint8_t > ByteVec
Definition Metadata.h:96
openvdb::v13_0::UnknownMetadata::writeValue
void writeValue(std::ostream &) const override
Write the metadata to a stream.
openvdb::v13_0::UnknownMetadata::copy
Metadata::Ptr copy() const override
Return a copy of the metadata.
openvdb::v13_0::UnknownMetadata::UnknownMetadata
UnknownMetadata(const Name &typ="<unknown>")
Definition Metadata.h:98
openvdb::v13_0::UnknownMetadata::readValue
void readValue(std::istream &, Index32 numBytes) override
Read the metadata from a stream.
openvdb::v13_0::ax::TypedMetadata::TypedMetadata
TypedMetadata()
Definition Metadata.h:210
openvdb::v13_0::math::isZero
bool isZero(const Type &x)
Return true if x is exactly equal to zero.
Definition Math.h:350
openvdb::v13_0::Name
std::string Name
Definition Name.h:19
openvdb::v13_0::HalfMetadata
TypedMetadata< math::half > HalfMetadata
Definition Metadata.h:362
openvdb::v13_0::Vec4DMetadata
TypedMetadata< Vec4d > Vec4DMetadata
Definition Metadata.h:372
openvdb::v13_0::FloatMetadata
TypedMetadata< float > FloatMetadata
Definition Metadata.h:361
openvdb::v13_0::Vec2DMetadata
TypedMetadata< Vec2d > Vec2DMetadata
Definition Metadata.h:366
openvdb::v13_0::Vec3DMetadata
TypedMetadata< Vec3d > Vec3DMetadata
Definition Metadata.h:369
openvdb::v13_0::Int32Metadata
TypedMetadata< int32_t > Int32Metadata
Definition Metadata.h:363
openvdb::v13_0::Mat4DMetadata
TypedMetadata< Mat4d > Mat4DMetadata
Definition Metadata.h:376
openvdb::v13_0::operator<<
std::ostream & operator<<(std::ostream &ostr, const Metadata &metadata)
Write a Metadata to an output stream.
Definition Metadata.h:352
openvdb::v13_0::Vec2IMetadata
TypedMetadata< Vec2i > Vec2IMetadata
Definition Metadata.h:367
openvdb::v13_0::Index32
uint32_t Index32
Definition Types.h:32
openvdb::v13_0::StringMetadata
TypedMetadata< std::string > StringMetadata
Definition Metadata.h:365
openvdb::v13_0::Vec4SMetadata
TypedMetadata< Vec4s > Vec4SMetadata
Definition Metadata.h:374
openvdb::v13_0::Vec3SMetadata
TypedMetadata< Vec3s > Vec3SMetadata
Definition Metadata.h:371
openvdb::v13_0::SharedPtr
std::shared_ptr< T > SharedPtr
Definition Types.h:95
openvdb::v13_0::Vec2SMetadata
TypedMetadata< Vec2s > Vec2SMetadata
Definition Metadata.h:368
openvdb::v13_0::Vec4IMetadata
TypedMetadata< Vec4i > Vec4IMetadata
Definition Metadata.h:373
openvdb::v13_0::Mat4SMetadata
TypedMetadata< Mat4s > Mat4SMetadata
Definition Metadata.h:375
openvdb::v13_0::Vec3IMetadata
TypedMetadata< Vec3i > Vec3IMetadata
Definition Metadata.h:370
openvdb::v13_0::BoolMetadata
TypedMetadata< bool > BoolMetadata
Definition Metadata.h:359
openvdb::v13_0::Int64Metadata
TypedMetadata< int64_t > Int64Metadata
Definition Metadata.h:364
openvdb::v13_0::DoubleMetadata
TypedMetadata< double > DoubleMetadata
Definition Metadata.h:360
openvdb::v13_0::typeNameAsString
const char * typeNameAsString()
Definition Types.h:583
openvdb
Definition Exceptions.h:13
Exceptions.h
OPENVDB_THROW
#define OPENVDB_THROW(exception, message)
Definition Exceptions.h:74
Types.h
OPENVDB_VERSION_NAME
#define OPENVDB_VERSION_NAME
The version namespace name for this library version.
Definition version.h.in:121
OPENVDB_USE_VERSION_NAMESPACE
#define OPENVDB_USE_VERSION_NAMESPACE
Definition version.h.in:218