OpenVDB 7.0.0

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