OpenVDB  3.1.0
metadata/Metadata.h
Go to the documentation of this file.
1 //
3 // Copyright (c) 2012-2015 DreamWorks Animation LLC
4 //
5 // All rights reserved. This software is distributed under the
6 // Mozilla Public License 2.0 ( http://www.mozilla.org/MPL/2.0/ )
7 //
8 // Redistributions of source code must retain the above copyright
9 // and license notice and the following restrictions and disclaimer.
10 //
11 // * Neither the name of DreamWorks Animation nor the names of
12 // its contributors may be used to endorse or promote products derived
13 // from this software without specific prior written permission.
14 //
15 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
16 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
17 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
18 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
19 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY INDIRECT, INCIDENTAL,
20 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
21 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
25 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 // IN NO EVENT SHALL THE COPYRIGHT HOLDERS' AND CONTRIBUTORS' AGGREGATE
27 // LIABILITY FOR ALL CLAIMS REGARDLESS OF THEIR BASIS EXCEED US$250.00.
28 //
30 
31 #ifndef OPENVDB_METADATA_METADATA_HAS_BEEN_INCLUDED
32 #define OPENVDB_METADATA_METADATA_HAS_BEEN_INCLUDED
33 
34 #include <iostream>
35 #include <string>
36 #include <openvdb/Types.h>
37 #include <openvdb/math/Math.h> // for math::isZero()
38 #include <openvdb/util/Name.h>
39 #include <openvdb/Exceptions.h>
40 #include <boost/shared_ptr.hpp>
41 #include <boost/cstdint.hpp>
42 
43 
44 namespace openvdb {
46 namespace OPENVDB_VERSION_NAME {
47 
50 {
51 public:
52  typedef boost::shared_ptr<Metadata> Ptr;
53  typedef boost::shared_ptr<const Metadata> ConstPtr;
54 
55  Metadata() {}
56  virtual ~Metadata() {}
57 
59  virtual Name typeName() const = 0;
60 
62  virtual Metadata::Ptr copy() const = 0;
63 
65  virtual void copy(const Metadata& other) = 0;
66 
68  virtual std::string str() const = 0;
69 
72  virtual bool asBool() const = 0;
73 
75  bool operator==(const Metadata& other) const;
77  bool operator!=(const Metadata& other) const { return !(*this == other); }
78 
80  virtual Index32 size() const = 0;
81 
83  void read(std::istream&);
85  void write(std::ostream&) const;
86 
88  static Metadata::Ptr createMetadata(const Name& typeName);
89 
91  static bool isRegisteredType(const Name& typeName);
92 
94  static void clearRegistry();
95 
97  static void registerType(const Name& typeName, Metadata::Ptr (*createMetadata)());
98  static void unregisterType(const Name& typeName);
99 
100 protected:
102  static Index32 readSize(std::istream&);
104  void writeSize(std::ostream&) const;
105 
107  virtual void readValue(std::istream&, Index32 numBytes) = 0;
109  virtual void writeValue(std::ostream&) const = 0;
110 
111 private:
112  // Disallow copying of instances of this class.
113  Metadata(const Metadata&);
114  Metadata& operator=(const Metadata&);
115 };
116 
117 
120 {
121 public:
123  virtual ~UnknownMetadata() {}
124  virtual Name typeName() const { return "<unknown>"; }
125  virtual Metadata::Ptr copy() const { OPENVDB_THROW(TypeError, "Metadata has unknown type"); }
126  virtual void copy(const Metadata&) { OPENVDB_THROW(TypeError, "Destination has unknown type"); }
127  virtual std::string str() const { return "<unknown>"; }
128  virtual bool asBool() const { return false; }
129  virtual Index32 size() const { return 0; }
130 
131 protected:
132  virtual void readValue(std::istream&s, Index32 numBytes);
133  virtual void writeValue(std::ostream&) const;
134 };
135 
136 
138 template<typename T>
139 class TypedMetadata: public Metadata
140 {
141 public:
142  typedef boost::shared_ptr<TypedMetadata<T> > Ptr;
143  typedef boost::shared_ptr<const TypedMetadata<T> > ConstPtr;
144 
145  TypedMetadata();
146  TypedMetadata(const T& value);
147  TypedMetadata(const TypedMetadata<T>& other);
148  virtual ~TypedMetadata();
149 
150  virtual Name typeName() const;
151  virtual Metadata::Ptr copy() const;
152  virtual void copy(const Metadata& other);
153  virtual std::string str() const;
154  virtual bool asBool() const;
155  virtual Index32 size() const { return static_cast<Index32>(sizeof(T)); }
156 
158  void setValue(const T&);
160  T& value();
161  const T& value() const;
162 
163  // Static specialized function for the type name. This function must be
164  // template specialized for each type T.
165  static Name staticTypeName() { return typeNameAsString<T>(); }
166 
168  static Metadata::Ptr createMetadata();
169 
170  static void registerType();
171  static void unregisterType();
172  static bool isRegisteredType();
173 
174 protected:
175  virtual void readValue(std::istream&, Index32 numBytes);
176  virtual void writeValue(std::ostream&) const;
177 
178 private:
179  T mValue;
180 };
181 
183 std::ostream& operator<<(std::ostream& ostr, const Metadata& metadata);
184 
185 
187 
188 
189 inline void
190 Metadata::writeSize(std::ostream& os) const
191 {
192  const Index32 n = this->size();
193  os.write(reinterpret_cast<const char*>(&n), sizeof(Index32));
194 }
195 
196 
197 inline Index32
198 Metadata::readSize(std::istream& is)
199 {
200  Index32 n = 0;
201  is.read(reinterpret_cast<char*>(&n), sizeof(Index32));
202  return n;
203 }
204 
205 
206 inline void
207 Metadata::read(std::istream& is)
208 {
209  const Index32 numBytes = this->readSize(is);
210  this->readValue(is, numBytes);
211 }
212 
213 
214 inline void
215 Metadata::write(std::ostream& os) const
216 {
217  this->writeSize(os);
218  this->writeValue(os);
219 }
220 
221 
223 
224 
225 template <typename T>
226 inline
228 {
229 }
230 
231 template <typename T>
232 inline
233 TypedMetadata<T>::TypedMetadata(const T &value) : mValue(value)
234 {
235 }
236 
237 template <typename T>
238 inline
240  Metadata(),
241  mValue(other.mValue)
242 {
243 }
244 
245 template <typename T>
246 inline
248 {
249 }
250 
251 template <typename T>
252 inline Name
254 {
256 }
257 
258 template <typename T>
259 inline void
261 {
262  mValue = val;
263 }
264 
265 template <typename T>
266 inline T&
268 {
269  return mValue;
270 }
271 
272 template <typename T>
273 inline const T&
275 {
276  return mValue;
277 }
278 
279 template <typename T>
280 inline Metadata::Ptr
282 {
283  Metadata::Ptr metadata(new TypedMetadata<T>());
284  metadata->copy(*this);
285  return metadata;
286 }
287 
288 template <typename T>
289 inline void
291 {
292  const TypedMetadata<T>* t = dynamic_cast<const TypedMetadata<T>*>(&other);
293  if (t == NULL) OPENVDB_THROW(TypeError, "Incompatible type during copy");
294  mValue = t->mValue;
295 }
296 
297 
298 template<typename T>
299 inline void
300 TypedMetadata<T>::readValue(std::istream& is, Index32 /*numBytes*/)
301 {
302  //assert(this->size() == numBytes);
303  is.read(reinterpret_cast<char*>(&mValue), this->size());
304 }
305 
306 template<typename T>
307 inline void
308 TypedMetadata<T>::writeValue(std::ostream& os) const
309 {
310  os.write(reinterpret_cast<const char*>(&mValue), this->size());
311 }
312 
313 template <typename T>
314 inline std::string
316 {
317  std::ostringstream ostr;
318  ostr << mValue;
319  return ostr.str();
320 }
321 
322 template<typename T>
323 inline bool
325 {
326  return !math::isZero(mValue);
327 }
328 
329 template <typename T>
330 inline Metadata::Ptr
332 {
333  Metadata::Ptr ret(new TypedMetadata<T>());
334  return ret;
335 }
336 
337 template <typename T>
338 inline void
340 {
343 }
344 
345 template <typename T>
346 inline void
348 {
350 }
351 
352 template <typename T>
353 inline bool
355 {
357 }
358 
359 
360 template<>
361 inline std::string
363 {
364  return (mValue ? "true" : "false");
365 }
366 
367 
368 inline std::ostream&
369 operator<<(std::ostream& ostr, const Metadata& metadata)
370 {
371  ostr << metadata.str();
372  return ostr;
373 }
374 
375 
389 
390 } // namespace OPENVDB_VERSION_NAME
391 } // namespace openvdb
392 
393 #endif // OPENVDB_METADATA_METADATA_HAS_BEEN_INCLUDED
394 
395 // Copyright (c) 2012-2015 DreamWorks Animation LLC
396 // All rights reserved. This software is distributed under the
397 // Mozilla Public License 2.0 ( http://www.mozilla.org/MPL/2.0/ )
static bool isRegisteredType(const Name &typeName)
Return true if the given type is known by the metadata type registry.
void writeSize(std::ostream &) const
Write the size of the metadata to a stream.
Definition: metadata/Metadata.h:190
boost::shared_ptr< Metadata > Ptr
Definition: metadata/Metadata.h:52
static void registerType(const Name &typeName, Metadata::Ptr(*createMetadata)())
Register the given metadata type along with a factory function.
Templated metadata class to hold specific types.
Definition: metadata/Metadata.h:139
#define OPENVDB_API
Helper macros for defining library symbol visibility.
Definition: Platform.h:195
TypedMetadata< boost::int64_t > Int64Metadata
Definition: metadata/Metadata.h:380
TypedMetadata< float > FloatMetadata
Definition: metadata/Metadata.h:378
void write(std::ostream &) const
Serialize this metadata to a stream.
Definition: metadata/Metadata.h:215
virtual ~TypedMetadata()
Definition: metadata/Metadata.h:247
virtual Index32 size() const
Return the size of this metadata in bytes.
Definition: metadata/Metadata.h:129
bool operator==(const Vec3< T0 > &v0, const Vec3< T1 > &v1)
Equality operator, does exact floating point comparisons.
Definition: Vec3.h:450
std::ostream & operator<<(std::ostream &ostr, const Metadata &metadata)
Write a Metadata to an output stream.
Definition: metadata/Metadata.h:369
virtual void readValue(std::istream &, Index32 numBytes)
Read the metadata from a stream.
Definition: metadata/Metadata.h:300
#define OPENVDB_THROW(exception, message)
Definition: Exceptions.h:97
uint32_t Index32
Definition: Types.h:56
virtual std::string str() const =0
Return a textual representation of this metadata.
virtual void writeValue(std::ostream &) const
Write the metadata to a stream.
Definition: metadata/Metadata.h:308
boost::shared_ptr< TypedMetadata< T > > Ptr
Definition: metadata/Metadata.h:142
T & value()
Return this metadata&#39;s value.
Definition: metadata/Metadata.h:267
virtual Name typeName() const
Return the type name of the metadata.
Definition: metadata/Metadata.h:124
TypedMetadata< Vec2s > Vec2SMetadata
Definition: metadata/Metadata.h:383
static Name staticTypeName()
Definition: metadata/Metadata.h:165
virtual bool asBool() const
Definition: metadata/Metadata.h:324
TypedMetadata< double > DoubleMetadata
Definition: metadata/Metadata.h:377
TypedMetadata< Vec3s > Vec3SMetadata
Definition: metadata/Metadata.h:386
virtual Metadata::Ptr copy() const
Return a copy of the metadata.
Definition: metadata/Metadata.h:125
TypedMetadata< Mat4s > Mat4SMetadata
Definition: metadata/Metadata.h:387
static void unregisterType(const Name &typeName)
TypedMetadata< Vec2i > Vec2IMetadata
Definition: metadata/Metadata.h:382
virtual Index32 size() const
Return the size of this metadata in bytes.
Definition: metadata/Metadata.h:155
TypedMetadata< Vec3i > Vec3IMetadata
Definition: metadata/Metadata.h:385
boost::shared_ptr< const Metadata > ConstPtr
Definition: metadata/Metadata.h:53
virtual Metadata::Ptr copy() const
Return a copy of the metadata.
Definition: metadata/Metadata.h:281
#define OPENVDB_VERSION_NAME
Definition: version.h:43
TypedMetadata< bool > BoolMetadata
Definition: metadata/Metadata.h:376
TypedMetadata()
Definition: metadata/Metadata.h:227
bool isZero(const Type &x)
Return true if x is exactly equal to zero.
Definition: Math.h:324
virtual std::string str() const
Return a textual representation of this metadata.
Definition: metadata/Metadata.h:315
static Metadata::Ptr createMetadata()
Create new metadata of this type.
Definition: metadata/Metadata.h:331
virtual std::string str() const
Return a textual representation of this metadata.
Definition: metadata/Metadata.h:127
std::string Name
Definition: Name.h:44
Definition: Exceptions.h:39
bool operator!=(const Metadata &other) const
Return true if the given metadata is different from this metadata.
Definition: metadata/Metadata.h:77
Metadata()
Definition: metadata/Metadata.h:55
virtual ~Metadata()
Definition: metadata/Metadata.h:56
static void unregisterType()
Definition: metadata/Metadata.h:347
static bool isRegisteredType()
Definition: metadata/Metadata.h:354
virtual ~UnknownMetadata()
Definition: metadata/Metadata.h:123
TypedMetadata< boost::int32_t > Int32Metadata
Definition: metadata/Metadata.h:379
virtual void copy(const Metadata &)
Copy the given metadata into this metadata.
Definition: metadata/Metadata.h:126
Subclass to read (and ignore) data of an unregistered type.
Definition: metadata/Metadata.h:119
UnknownMetadata()
Definition: metadata/Metadata.h:122
TypedMetadata< Vec2d > Vec2DMetadata
Definition: metadata/Metadata.h:381
#define OPENVDB_USE_VERSION_NAMESPACE
Definition: version.h:71
void setValue(const T &)
Set this metadata&#39;s value.
Definition: metadata/Metadata.h:260
Definition: Exceptions.h:87
TypedMetadata< Mat4d > Mat4DMetadata
Definition: metadata/Metadata.h:388
virtual bool asBool() const
Definition: metadata/Metadata.h:128
static Index32 readSize(std::istream &)
Read the size of the metadata from a stream.
Definition: metadata/Metadata.h:198
static void registerType()
Definition: metadata/Metadata.h:339
void read(std::istream &)
Unserialize this metadata from a stream.
Definition: metadata/Metadata.h:207
boost::shared_ptr< const TypedMetadata< T > > ConstPtr
Definition: metadata/Metadata.h:143
virtual Name typeName() const
Return the type name of the metadata.
Definition: metadata/Metadata.h:253
TypedMetadata< Vec3d > Vec3DMetadata
Definition: metadata/Metadata.h:384
Base class for storing metadata information in a grid.
Definition: metadata/Metadata.h:49