OpenVDB 12.1.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
19 namespace openvdb {
20 OPENVDB_USE_VERSION_NAMESPACE
21 namespace OPENVDB_VERSION_NAME {
22
23/// @brief Base class for storing metadata information in a grid.
24 class OPENVDB_API Metadata
25{
26 public:
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
79 protected:
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{
95 public:
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
110 protected:
111 void readValue(std::istream&, Index32 numBytes) override;
112 void writeValue(std::ostream&) const override;
113
114 private:
115 Name mTypeName;
116 ByteVec mBytes;
117};
118
119
120/// @brief Templated metadata class to hold specific types.
121 template<typename T>
122 class TypedMetadata: public Metadata
123{
124 public:
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
157 protected:
158 void readValue(std::istream&, Index32 numBytes) override;
159 void writeValue(std::ostream&) const override;
160
161 private:
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
172 inline void
173 Metadata::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
180 inline Index32
181 Metadata::readSize(std::istream& is)
182{
183 Index32 n = 0;
184 is.read(reinterpret_cast<char*>(&n), sizeof(Index32));
185 return n;
186}
187
188
189 inline void
190 Metadata::read(std::istream& is)
191{
192 const Index32 numBytes = this->readSize(is);
193 this->readValue(is, numBytes);
194}
195
196
197 inline void
198 Metadata::write(std::ostream& os) const
199{
200 this->writeSize(os);
201 this->writeValue(os);
202}
203
204
205////////////////////////////////////////
206
207
208 template <typename T>
209 inline
210 TypedMetadata<T>::TypedMetadata() : mValue(T())
211{
212}
213
214 template <typename T>
215 inline
216 TypedMetadata<T>::TypedMetadata(const T &value) : mValue(value)
217{
218}
219
220 template <typename T>
221 inline
222 TypedMetadata<T>::TypedMetadata(const TypedMetadata<T> &other) :
223 Metadata(),
224 mValue(other.mValue)
225{
226}
227
228 template <typename T>
229 inline
230 TypedMetadata<T>::~TypedMetadata()
231{
232}
233
234 template <typename T>
235 inline Name
236 TypedMetadata<T>::typeName() const
237{
238 return TypedMetadata<T>::staticTypeName();
239}
240
241 template <typename T>
242 inline void
243 TypedMetadata<T>::setValue(const T& val)
244{
245 mValue = val;
246}
247
248 template <typename T>
249 inline T&
250 TypedMetadata<T>::value()
251{
252 return mValue;
253}
254
255 template <typename T>
256 inline const T&
257 TypedMetadata<T>::value() const
258{
259 return mValue;
260}
261
262 template <typename T>
263 inline Metadata::Ptr
264 TypedMetadata<T>::copy() const
265{
266 Metadata::Ptr metadata(new TypedMetadata<T>());
267 metadata->copy(*this);
268 return metadata;
269}
270
271 template <typename T>
272 inline 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
281 template<typename T>
282 inline void
283 TypedMetadata<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
289 template<typename T>
290 inline void
291 TypedMetadata<T>::writeValue(std::ostream& os) const
292{
293 os.write(reinterpret_cast<const char*>(&mValue), this->size());
294}
295
296 template <typename T>
297 inline std::string
298 TypedMetadata<T>::str() const
299{
300 std::ostringstream ostr;
301 ostr << mValue;
302 return ostr.str();
303}
304
305 template<typename T>
306 inline bool
307 TypedMetadata<T>::asBool() const
308{
309 return !math::isZero(mValue);
310}
311
312 template <typename T>
313 inline Metadata::Ptr
314 TypedMetadata<T>::createMetadata()
315{
316 Metadata::Ptr ret(new TypedMetadata<T>());
317 return ret;
318}
319
320 template <typename T>
321 inline void
322 TypedMetadata<T>::registerType()
323{
324 Metadata::registerType(TypedMetadata<T>::staticTypeName(),
325 TypedMetadata<T>::createMetadata);
326}
327
328 template <typename T>
329 inline void
330 TypedMetadata<T>::unregisterType()
331{
332 Metadata::unregisterType(TypedMetadata<T>::staticTypeName());
333}
334
335 template <typename T>
336 inline bool
337 TypedMetadata<T>::isRegisteredType()
338{
339 return Metadata::isRegisteredType(TypedMetadata<T>::staticTypeName());
340}
341
342
343 template<>
344 inline 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 Int32Metadata = TypedMetadata<int32_t>;
363 using Int64Metadata = TypedMetadata<int64_t>;
364 using StringMetadata = TypedMetadata<std::string>;
365 using Vec2DMetadata = TypedMetadata<Vec2d>;
366 using Vec2IMetadata = TypedMetadata<Vec2i>;
367 using Vec2SMetadata = TypedMetadata<Vec2s>;
368 using Vec3DMetadata = TypedMetadata<Vec3d>;
369 using Vec3IMetadata = TypedMetadata<Vec3i>;
370 using Vec3SMetadata = TypedMetadata<Vec3s>;
371 using Vec4DMetadata = TypedMetadata<Vec4d>;
372 using Vec4IMetadata = TypedMetadata<Vec4i>;
373 using Vec4SMetadata = TypedMetadata<Vec4s>;
374 using Mat4SMetadata = TypedMetadata<Mat4s>;
375 using Mat4DMetadata = TypedMetadata<Mat4d>;
376
377
378////////////////////////////////////////
379
380
381 template<>
382 inline Index32
383 StringMetadata::size() const
384{
385 return static_cast<Index32>(mValue.size());
386}
387
388
389 template<>
390 inline std::string
391 StringMetadata::str() const
392{
393 return mValue;
394}
395
396
397 template<>
398 inline void
399 StringMetadata::readValue(std::istream& is, Index32 size)
400{
401 mValue.resize(size, '\0');
402 is.read(&mValue[0], size);
403}
404
405 template<>
406 inline void
407 StringMetadata::writeValue(std::ostream& os) const
408{
409 os.write(reinterpret_cast<const char*>(&mValue[0]), this->size());
410}
411
412} // namespace OPENVDB_VERSION_NAME
413} // namespace openvdb
414
415#endif // OPENVDB_METADATA_HAS_BEEN_INCLUDED
Assert.h
OPENVDB_ASSERT
#define OPENVDB_ASSERT(X)
Definition Assert.h:41
operator<<
OPENVDB_API std::ostream & operator<<(std::ostream &os, half h)
Output h to os, formatted as a float.
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::v12_1::Metadata
Base class for storing metadata information in a grid.
Definition Metadata.h:25
openvdb::v12_1::Metadata::ConstPtr
SharedPtr< const Metadata > ConstPtr
Definition Metadata.h:28
openvdb::v12_1::Metadata::copy
virtual void copy(const Metadata &other)=0
Copy the given metadata into this metadata.
openvdb::v12_1::Metadata::operator!=
bool operator!=(const Metadata &other) const
Return true if the given metadata is different from this metadata.
Definition Metadata.h:56
openvdb::v12_1::Metadata::asBool
virtual bool asBool() const =0
openvdb::v12_1::Metadata::typeName
virtual Name typeName() const =0
Return the type name of the metadata.
openvdb::v12_1::Metadata::Metadata
Metadata()
Definition Metadata.h:30
openvdb::v12_1::Metadata::operator=
Metadata & operator=(const Metadata &)=delete
openvdb::v12_1::Metadata::Metadata
Metadata(const Metadata &)=delete
openvdb::v12_1::Metadata::write
void write(std::ostream &) const
Serialize this metadata to a stream.
Definition Metadata.h:198
openvdb::v12_1::Metadata::createMetadata
static Metadata::Ptr createMetadata(const Name &typeName)
Create new metadata of the given type.
openvdb::v12_1::Metadata::writeValue
virtual void writeValue(std::ostream &) const =0
Write the metadata to a stream.
openvdb::v12_1::Metadata::str
virtual std::string str() const =0
Return a textual representation of this metadata.
openvdb::v12_1::Metadata::registerType
static void registerType(const Name &typeName, Metadata::Ptr(*createMetadata)())
Register the given metadata type along with a factory function.
openvdb::v12_1::Metadata::read
void read(std::istream &)
Unserialize this metadata from a stream.
Definition Metadata.h:190
openvdb::v12_1::Metadata::isRegisteredType
static bool isRegisteredType(const Name &typeName)
Return true if the given type is known by the metadata type registry.
openvdb::v12_1::Metadata::copy
virtual Metadata::Ptr copy() const =0
Return a copy of the metadata.
openvdb::v12_1::Metadata::unregisterType
static void unregisterType(const Name &typeName)
openvdb::v12_1::Metadata::readSize
static Index32 readSize(std::istream &)
Read the size of the metadata from a stream.
Definition Metadata.h:181
openvdb::v12_1::Metadata::operator==
bool operator==(const Metadata &other) const
Return true if the given metadata is equivalent to this metadata.
openvdb::v12_1::Metadata::Ptr
SharedPtr< Metadata > Ptr
Definition Metadata.h:27
openvdb::v12_1::Metadata::clearRegistry
static void clearRegistry()
Clear out the metadata registry.
openvdb::v12_1::Metadata::writeSize
void writeSize(std::ostream &) const
Write the size of the metadata to a stream.
Definition Metadata.h:173
openvdb::v12_1::Metadata::size
virtual Index32 size() const =0
Return the size of this metadata in bytes.
openvdb::v12_1::Metadata::readValue
virtual void readValue(std::istream &, Index32 numBytes)=0
Read the metadata from a stream.
openvdb::v12_1::Metadata::~Metadata
virtual ~Metadata()
Definition Metadata.h:31
openvdb::v12_1::TypeError
Definition Exceptions.h:64
openvdb::v12_1::TypedMetadata
Templated metadata class to hold specific types.
Definition Metadata.h:123
openvdb::v12_1::TypedMetadata::TypedMetadata
TypedMetadata(const T &value)
Definition Metadata.h:216
openvdb::v12_1::TypedMetadata::copy
void copy(const Metadata &other) override
Copy the given metadata into this metadata.
Definition Metadata.h:273
openvdb::v12_1::TypedMetadata::size
Index32 size() const override
Return the size of this metadata in bytes.
Definition Metadata.h:138
openvdb::v12_1::TypedMetadata::str
std::string str() const override
Return a textual representation of this metadata.
Definition Metadata.h:298
openvdb::v12_1::TypedMetadata::asBool
bool asBool() const override
Definition Metadata.h:307
openvdb::v12_1::TypedMetadata::typeName
Name typeName() const override
Return the type name of the metadata.
Definition Metadata.h:236
openvdb::v12_1::TypedMetadata::registerType
static void registerType()
Definition Metadata.h:322
openvdb::v12_1::TypedMetadata::unregisterType
static void unregisterType()
Definition Metadata.h:330
openvdb::v12_1::TypedMetadata::TypedMetadata
TypedMetadata(const TypedMetadata< T > &other)
Definition Metadata.h:222
openvdb::v12_1::TypedMetadata::isRegisteredType
static bool isRegisteredType()
Definition Metadata.h:337
openvdb::v12_1::TypedMetadata::createMetadata
static Metadata::Ptr createMetadata()
Create new metadata of this type.
Definition Metadata.h:314
openvdb::v12_1::TypedMetadata::TypedMetadata
TypedMetadata()
Definition Metadata.h:210
openvdb::v12_1::TypedMetadata::~TypedMetadata
~TypedMetadata() override
Definition Metadata.h:230
openvdb::v12_1::TypedMetadata::writeValue
void writeValue(std::ostream &) const override
Write the metadata to a stream.
Definition Metadata.h:291
openvdb::v12_1::TypedMetadata::Ptr
SharedPtr< TypedMetadata< T > > Ptr
Definition Metadata.h:125
openvdb::v12_1::TypedMetadata::copy
Metadata::Ptr copy() const override
Return a copy of the metadata.
Definition Metadata.h:264
openvdb::v12_1::TypedMetadata::ConstPtr
SharedPtr< const TypedMetadata< T > > ConstPtr
Definition Metadata.h:126
openvdb::v12_1::TypedMetadata< bool >::value
bool & value()
openvdb::v12_1::TypedMetadata::value
const T & value() const
Definition Metadata.h:257
openvdb::v12_1::TypedMetadata::staticTypeName
static Name staticTypeName()
Definition Metadata.h:148
openvdb::v12_1::TypedMetadata::readValue
void readValue(std::istream &, Index32 numBytes) override
Read the metadata from a stream.
Definition Metadata.h:283
openvdb::v12_1::TypedMetadata::setValue
void setValue(const T &)
Set this metadata's value.
Definition Metadata.h:243
openvdb::v12_1::UnknownMetadata::setValue
void setValue(const ByteVec &bytes)
Definition Metadata.h:107
openvdb::v12_1::UnknownMetadata::size
Index32 size() const override
Return the size of this metadata in bytes.
Definition Metadata.h:105
openvdb::v12_1::UnknownMetadata::copy
void copy(const Metadata &) override
Copy the given metadata into this metadata.
openvdb::v12_1::UnknownMetadata::value
const ByteVec & value() const
Definition Metadata.h:108
openvdb::v12_1::UnknownMetadata::str
std::string str() const override
Return a textual representation of this metadata.
Definition Metadata.h:103
openvdb::v12_1::UnknownMetadata::asBool
bool asBool() const override
Definition Metadata.h:104
openvdb::v12_1::UnknownMetadata::typeName
Name typeName() const override
Return the type name of the metadata.
Definition Metadata.h:100
openvdb::v12_1::UnknownMetadata::ByteVec
std::vector< uint8_t > ByteVec
Definition Metadata.h:96
openvdb::v12_1::UnknownMetadata::writeValue
void writeValue(std::ostream &) const override
Write the metadata to a stream.
openvdb::v12_1::UnknownMetadata::copy
Metadata::Ptr copy() const override
Return a copy of the metadata.
openvdb::v12_1::UnknownMetadata::UnknownMetadata
UnknownMetadata(const Name &typ="<unknown>")
Definition Metadata.h:98
openvdb::v12_1::UnknownMetadata::readValue
void readValue(std::istream &, Index32 numBytes) override
Read the metadata from a stream.
openvdb::v12_1::ax::TypedMetadata::TypedMetadata
TypedMetadata()
Definition Metadata.h:210
openvdb::v12_1::math::isZero
bool isZero(const Type &x)
Return true if x is exactly equal to zero.
Definition Math.h:337
openvdb::v12_1::Name
std::string Name
Definition Name.h:19
openvdb::v12_1::Vec4DMetadata
TypedMetadata< Vec4d > Vec4DMetadata
Definition Metadata.h:371
openvdb::v12_1::FloatMetadata
TypedMetadata< float > FloatMetadata
Definition Metadata.h:361
openvdb::v12_1::Vec2DMetadata
TypedMetadata< Vec2d > Vec2DMetadata
Definition Metadata.h:365
openvdb::v12_1::Vec3DMetadata
TypedMetadata< Vec3d > Vec3DMetadata
Definition Metadata.h:368
openvdb::v12_1::Int32Metadata
TypedMetadata< int32_t > Int32Metadata
Definition Metadata.h:362
openvdb::v12_1::Mat4DMetadata
TypedMetadata< Mat4d > Mat4DMetadata
Definition Metadata.h:375
openvdb::v12_1::operator<<
std::ostream & operator<<(std::ostream &ostr, const Metadata &metadata)
Write a Metadata to an output stream.
Definition Metadata.h:352
openvdb::v12_1::Vec2IMetadata
TypedMetadata< Vec2i > Vec2IMetadata
Definition Metadata.h:366
openvdb::v12_1::Index32
uint32_t Index32
Definition Types.h:52
openvdb::v12_1::StringMetadata
TypedMetadata< std::string > StringMetadata
Definition Metadata.h:364
openvdb::v12_1::Vec4SMetadata
TypedMetadata< Vec4s > Vec4SMetadata
Definition Metadata.h:373
openvdb::v12_1::Vec3SMetadata
TypedMetadata< Vec3s > Vec3SMetadata
Definition Metadata.h:370
openvdb::v12_1::SharedPtr
std::shared_ptr< T > SharedPtr
Definition Types.h:114
openvdb::v12_1::Vec2SMetadata
TypedMetadata< Vec2s > Vec2SMetadata
Definition Metadata.h:367
openvdb::v12_1::Vec4IMetadata
TypedMetadata< Vec4i > Vec4IMetadata
Definition Metadata.h:372
openvdb::v12_1::Mat4SMetadata
TypedMetadata< Mat4s > Mat4SMetadata
Definition Metadata.h:374
openvdb::v12_1::Vec3IMetadata
TypedMetadata< Vec3i > Vec3IMetadata
Definition Metadata.h:369
openvdb::v12_1::BoolMetadata
TypedMetadata< bool > BoolMetadata
Definition Metadata.h:359
openvdb::v12_1::Int64Metadata
TypedMetadata< int64_t > Int64Metadata
Definition Metadata.h:363
openvdb::v12_1::DoubleMetadata
TypedMetadata< double > DoubleMetadata
Definition Metadata.h:360
openvdb::v12_1::typeNameAsString
const char * typeNameAsString()
Definition Types.h:587
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