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