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 {
20 namespace OPENVDB_VERSION_NAME {
21 
24 {
25 public:
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 
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:
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
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
245  Metadata(),
246  mValue(other.mValue)
247 {
248 }
249 
250 template <typename T>
251 inline
253 {
254 }
255 
256 template <typename T>
257 inline Name
259 {
261 }
262 
263 template <typename T>
264 inline void
266 {
267  mValue = val;
268 }
269 
270 template <typename T>
271 inline T&
273 {
274  return mValue;
275 }
276 
277 template <typename T>
278 inline const T&
280 {
281  return mValue;
282 }
283 
284 template <typename T>
285 inline Metadata::Ptr
287 {
288  Metadata::Ptr metadata(new TypedMetadata<T>());
289  metadata->copy(*this);
290  return metadata;
291 }
292 
293 template <typename T>
294 inline void
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
321 {
322  std::ostringstream ostr;
323  ostr << mValue;
324  return ostr.str();
325 }
326 
327 template<typename T>
328 inline bool
330 {
331  return !math::isZero(mValue);
332 }
333 
334 template <typename T>
335 inline Metadata::Ptr
337 {
338  Metadata::Ptr ret(new TypedMetadata<T>());
339  return ret;
340 }
341 
342 template <typename T>
343 inline void
345 {
348 }
349 
350 template <typename T>
351 inline void
353 {
355 }
356 
357 template <typename T>
358 inline bool
360 {
362 }
363 
364 
365 template<>
366 inline std::string
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 
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
virtual std::string str() const =0
Return a textual representation of this metadata.
#define OPENVDB_API
Helper macros for defining library symbol visibility.
Definition: Platform.h:230
bool isZero(const Type &x)
Return true if x is exactly equal to zero.
Definition: Math.h:281
TypedMetadata()
Definition: Metadata.h:232
std::vector< uint8_t > ByteVec
Definition: Metadata.h:97
static Name staticTypeName()
Definition: Metadata.h:170
Name typeName() const override
Return the type name of the metadata.
Definition: Metadata.h:101
bool operator!=(const Metadata &other) const
Return true if the given metadata is different from this metadata.
Definition: Metadata.h:55
UnknownMetadata(const Name &typ="<unknown>")
Definition: Metadata.h:99
static void unregisterType(const Name &typeName)
General-purpose arithmetic and comparison routines, most of which accept arbitrary value types (or at...
uint32_t Index32
Definition: Types.h:29
#define OPENVDB_THROW(exception, message)
Definition: Exceptions.h:82
std::string Name
Definition: Name.h:17
~TypedMetadata() override
Definition: Metadata.h:252
std::shared_ptr< T > SharedPtr
Definition: Types.h:91
void readValue(std::istream &, Index32 numBytes) override
Read the metadata from a stream.
Definition: Metadata.h:305
Name typeName() const override
Return the type name of the metadata.
Definition: Metadata.h:258
static void unregisterType()
Definition: Metadata.h:352
bool asBool() const override
Definition: Metadata.h:329
std::string str() const override
Return a textual representation of this metadata.
Definition: Metadata.h:320
Index32 size() const override
Return the size of this metadata in bytes.
Definition: Metadata.h:160
static void registerType(const Name &typeName, Metadata::Ptr(*createMetadata)())
Register the given metadata type along with a factory function.
void writeValue(std::ostream &) const override
Write the metadata to a stream.
Definition: Metadata.h:313
Metadata()
Definition: Metadata.h:29
Base class for storing metadata information in a grid.
Definition: Metadata.h:23
#define OPENVDB_VERSION_NAME
The version namespace name for this library version.
Definition: version.h:102
Subclass to hold raw data of an unregistered type.
Definition: Metadata.h:94
Templated metadata class to hold specific types.
Definition: Metadata.h:144
Metadata::Ptr copy() const override
Return a copy of the metadata.
Definition: Metadata.h:286
T & value()
Return this metadata&#39;s value.
Definition: Metadata.h:272
Definition: Exceptions.h:13
const ByteVec & value() const
Definition: Metadata.h:109
bool asBool() const override
Definition: Metadata.h:105
Library and file format version numbers.
static bool isRegisteredType()
Definition: Metadata.h:359
void writeSize(std::ostream &) const
Write the size of the metadata to a stream.
Definition: Metadata.h:195
bool operator==(const Vec3< T0 > &v0, const Vec3< T1 > &v1)
Equality operator, does exact floating point comparisons.
Definition: Vec3.h:471
static Index32 readSize(std::istream &)
Read the size of the metadata from a stream.
Definition: Metadata.h:203
static bool isRegisteredType(const Name &typeName)
Return true if the given type is known by the metadata type registry.
SharedPtr< Metadata > Ptr
Definition: Metadata.h:26
void write(std::ostream &) const
Serialize this metadata to a stream.
Definition: Metadata.h:220
void setValue(const T &)
Set this metadata&#39;s value.
Definition: Metadata.h:265
std::ostream & operator<<(std::ostream &ostr, const Metadata &metadata)
Write a Metadata to an output stream.
Definition: Metadata.h:374
static Metadata::Ptr createMetadata()
Create new metadata of this type.
Definition: Metadata.h:336
void setValue(const ByteVec &bytes)
Definition: Metadata.h:108
#define OPENVDB_USE_VERSION_NAMESPACE
Definition: version.h:154
Index32 size() const override
Return the size of this metadata in bytes.
Definition: Metadata.h:106
virtual ~Metadata()
Definition: Metadata.h:30
void read(std::istream &)
Unserialize this metadata from a stream.
Definition: Metadata.h:212
Definition: Exceptions.h:64
SharedPtr< const Metadata > ConstPtr
Definition: Metadata.h:27
std::string str() const override
Return a textual representation of this metadata.
Definition: Metadata.h:104
static void registerType()
Definition: Metadata.h:344