OpenVDB  1.2.0
metadata/Metadata.h
Go to the documentation of this file.
1 //
3 // Copyright (c) 2012-2013 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 
56  Metadata() {}
57 
59  virtual ~Metadata() {}
60 
62  virtual Name typeName() const = 0;
63 
65  virtual Metadata::Ptr copy() const = 0;
66 
68  virtual void copy(const Metadata &other) = 0;
69 
71  virtual std::string str() const = 0;
72 
75  virtual bool asBool() const = 0;
76 
78  virtual Index32 size() const = 0;
79 
81  void read(std::istream&);
83  void write(std::ostream&) const;
84 
86  static Metadata::Ptr createMetadata(const Name &typeName);
87 
89  static bool isRegisteredType(const Name &typeName);
90 
92  static void clearRegistry();
93 
94 protected:
96  static Index32 readSize(std::istream&);
98  void writeSize(std::ostream&) const;
99 
101  virtual void readValue(std::istream&, Index32 numBytes) = 0;
103  virtual void writeValue(std::ostream&) const = 0;
104 
106  static void registerType(const Name& typeName, Metadata::Ptr (*createMetadata)());
107  static void unregisterType(const Name& typeName);
108 
109 private:
110  // Disallow copying of instances of this class.
111  Metadata(const Metadata&);
112  Metadata& operator=(const Metadata&);
113 };
114 
115 
118 {
119 public:
121  virtual ~UnknownMetadata() {}
122  virtual Name typeName() const { return "<unknown>"; }
123  virtual Metadata::Ptr copy() const { OPENVDB_THROW(TypeError, "Metadata has unknown type"); }
124  virtual void copy(const Metadata&) { OPENVDB_THROW(TypeError, "Destination has unknown type"); }
125  virtual std::string str() const { return "<unknown>"; }
126  virtual bool asBool() const { return false; }
127  virtual Index32 size() const { return 0; }
128 
129 protected:
130  virtual void readValue(std::istream&s, Index32 numBytes);
131  virtual void writeValue(std::ostream&) const;
132 };
133 
134 
136 template<typename T>
137 class TypedMetadata: public Metadata
138 {
139 public:
140  typedef boost::shared_ptr<TypedMetadata<T> > Ptr;
141  typedef boost::shared_ptr<const TypedMetadata<T> > ConstPtr;
142 
143  // Constructors & destructors
144  TypedMetadata();
145  TypedMetadata(const T &value);
146  TypedMetadata(const TypedMetadata<T> &other);
147  virtual ~TypedMetadata();
148 
150  virtual Name typeName() const;
151 
153  virtual Metadata::Ptr copy() const;
154 
156  virtual void copy(const Metadata &other);
157 
159  virtual std::string str() const;
160 
163  virtual bool asBool() const;
164 
166  virtual Index32 size() const { return static_cast<Index32>(sizeof(T)); }
167 
169  void setValue(const T&);
171  T& value();
172  const T& value() const;
173 
176  static Name staticTypeName() { return typeNameAsString<T>(); }
177 
179  static Metadata::Ptr createMetadata();
180 
184  static void registerType();
185  static void unregisterType();
186 
187  static bool isRegisteredType();
188 
189 protected:
191  virtual void readValue(std::istream&, Index32 numBytes);
193  virtual void writeValue(std::ostream&) const;
194 
195 private:
196  T mValue;
197 };
198 
200 std::ostream& operator<<(std::ostream& ostr, const Metadata& metadata);
201 
202 
204 
205 
206 inline void
207 Metadata::writeSize(std::ostream& os) const
208 {
209  const Index32 n = this->size();
210  os.write(reinterpret_cast<const char*>(&n), sizeof(Index32));
211 }
212 
213 
214 inline Index32
215 Metadata::readSize(std::istream& is)
216 {
217  Index32 n = 0;
218  is.read(reinterpret_cast<char*>(&n), sizeof(Index32));
219  return n;
220 }
221 
222 
223 inline void
224 Metadata::read(std::istream& is)
225 {
226  const Index32 numBytes = this->readSize(is);
227  this->readValue(is, numBytes);
228 }
229 
230 
231 inline void
232 Metadata::write(std::ostream& os) const
233 {
234  this->writeSize(os);
235  this->writeValue(os);
236 }
237 
238 
240 
241 
242 template <typename T>
243 inline
245 {
246 }
247 
248 template <typename T>
249 inline
250 TypedMetadata<T>::TypedMetadata(const T &value) : mValue(value)
251 {
252 }
253 
254 template <typename T>
255 inline
257  Metadata(),
258  mValue(other.mValue)
259 {
260 }
261 
262 template <typename T>
263 inline
265 {
266 }
267 
268 template <typename T>
269 inline Name
271 {
273 }
274 
275 template <typename T>
276 inline void
278 {
279  mValue = val;
280 }
281 
282 template <typename T>
283 inline T&
285 {
286  return mValue;
287 }
288 
289 template <typename T>
290 inline const T&
292 {
293  return mValue;
294 }
295 
296 template <typename T>
297 inline Metadata::Ptr
299 {
300  Metadata::Ptr metadata(new TypedMetadata<T>());
301  metadata->copy(*this);
302  return metadata;
303 }
304 
305 template <typename T>
306 inline void
308 {
309  const TypedMetadata<T>* t = dynamic_cast<const TypedMetadata<T>*>(&other);
310  if (t == NULL) OPENVDB_THROW(TypeError, "Incompatible type during copy");
311  mValue = t->mValue;
312 }
313 
314 
315 template<typename T>
316 inline void
317 TypedMetadata<T>::readValue(std::istream& is, Index32 /*numBytes*/)
318 {
319  //assert(this->size() == numBytes);
320  is.read(reinterpret_cast<char*>(&mValue), this->size());
321 }
322 
323 template<typename T>
324 inline void
325 TypedMetadata<T>::writeValue(std::ostream& os) const
326 {
327  os.write(reinterpret_cast<const char*>(&mValue), this->size());
328 }
329 
330 template <typename T>
331 inline std::string
333 {
334  std::ostringstream ostr;
335  ostr << mValue;
336  return ostr.str();
337 }
338 
339 template<typename T>
340 inline bool
342 {
343  return !math::isZero(mValue);
344 }
345 
346 template <typename T>
347 inline Metadata::Ptr
349 {
350  Metadata::Ptr ret(new TypedMetadata<T>());
351  return ret;
352 }
353 
354 template <typename T>
355 inline void
357 {
360 }
361 
362 template <typename T>
363 inline void
365 {
367 }
368 
369 template <typename T>
370 inline bool
372 {
374 }
375 
376 
377 template<>
378 inline std::string
380 {
381  return (mValue ? "true" : "false");
382 }
383 
384 
385 inline std::ostream&
386 operator<<(std::ostream& ostr, const Metadata& metadata)
387 {
388  ostr << metadata.str();
389  return ostr;
390 }
391 
392 
406 
407 } // namespace OPENVDB_VERSION_NAME
408 } // namespace openvdb
409 
410 #endif // OPENVDB_METADATA_METADATA_HAS_BEEN_INCLUDED
411 
412 // Copyright (c) 2012-2013 DreamWorks Animation LLC
413 // All rights reserved. This software is distributed under the
414 // Mozilla Public License 2.0 ( http://www.mozilla.org/MPL/2.0/ )