OpenVDB  3.0.0
MetaMap.h
Go to the documentation of this file.
1 //
3 // Copyright (c) 2012-2014 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_METAMAP_HAS_BEEN_INCLUDED
32 #define OPENVDB_METADATA_METAMAP_HAS_BEEN_INCLUDED
33 
34 #include <iosfwd>
35 #include <map>
36 #include <openvdb/metadata/Metadata.h>
37 #include <openvdb/Types.h>
38 #include <openvdb/Exceptions.h>
39 
40 
41 namespace openvdb {
43 namespace OPENVDB_VERSION_NAME {
44 
47 {
48 public:
49  typedef boost::shared_ptr<MetaMap> Ptr;
50  typedef boost::shared_ptr<const MetaMap> ConstPtr;
51 
52  typedef std::map<Name, Metadata::Ptr> MetadataMap;
53  typedef MetadataMap::iterator MetaIterator;
54  typedef MetadataMap::const_iterator ConstMetaIterator;
56 
57  MetaMap() {}
58  MetaMap(const MetaMap& other);
59  virtual ~MetaMap() {}
60 
62  MetaMap::Ptr copyMeta() const;
64  MetaMap::Ptr deepCopyMeta() const;
65 
67  MetaMap& operator=(const MetaMap&);
68 
70  void readMeta(std::istream&);
72  void writeMeta(std::ostream&) const;
73 
81  void insertMeta(const Name&, const Metadata& value);
85  void insertMeta(const MetaMap&);
86 
88  void removeMeta(const Name&);
89 
91  Metadata::Ptr operator[](const Name&);
94  Metadata::ConstPtr operator[](const Name&) const;
96 
98  template<typename T> typename T::Ptr getMetadata(const Name&);
101  template<typename T> typename T::ConstPtr getMetadata(const Name&) const;
103 
107  template<typename T> T& metaValue(const Name&);
108  template<typename T> const T& metaValue(const Name&) const;
109 
110  // Functions for iterating over the metadata
111  MetaIterator beginMeta() { return mMeta.begin(); }
112  MetaIterator endMeta() { return mMeta.end(); }
113  ConstMetaIterator beginMeta() const { return mMeta.begin(); }
114  ConstMetaIterator endMeta() const { return mMeta.end(); }
115 
116  void clearMetadata() { mMeta.clear(); }
117 
118  size_t metaCount() const { return mMeta.size(); }
119 
121  std::string str(const std::string& indent = "") const;
122 
123 private:
127  template<typename T>
128  typename TypedMetadata<T>::Ptr getValidTypedMetadata(const Name&) const;
129 
130  MetadataMap mMeta;
131 };
132 
134 std::ostream& operator<<(std::ostream&, const MetaMap&);
135 
136 
138 
139 
140 inline Metadata::Ptr
142 {
143  MetaIterator iter = mMeta.find(name);
144  return (iter == mMeta.end() ? Metadata::Ptr() : iter->second);
145 }
146 
147 inline Metadata::ConstPtr
148 MetaMap::operator[](const Name &name) const
149 {
150  ConstMetaIterator iter = mMeta.find(name);
151  return (iter == mMeta.end() ? Metadata::Ptr() : iter->second);
152 }
153 
154 
156 
157 
158 template <typename T>
159 inline typename T::Ptr
161 {
162  ConstMetaIterator iter = mMeta.find(name);
163  if(iter == mMeta.end()) {
164  return typename T::Ptr();
165  }
166 
167  // To ensure that we get valid conversion if the metadata pointers cross dso
168  // boundaries, we have to check the qualified typename and then do a static
169  // cast. This is slower than doing a dynamic_pointer_cast, but is safer when
170  // pointers cross dso boundaries.
171  if (iter->second->typeName() == T::staticTypeName()) {
172  return boost::static_pointer_cast<T, Metadata>(iter->second);
173  } // else
174  return typename T::Ptr();
175 }
176 
177 template <typename T>
178 inline typename T::ConstPtr
179 MetaMap::getMetadata(const Name &name) const
180 {
181  ConstMetaIterator iter = mMeta.find(name);
182  if(iter == mMeta.end()) {
183  return typename T::ConstPtr();
184  }
185  // To ensure that we get valid conversion if the metadata pointers cross dso
186  // boundaries, we have to check the qualified typename and then do a static
187  // cast. This is slower than doing a dynamic_pointer_cast, but is safer when
188  // pointers cross dso boundaries.
189  if (iter->second->typeName() == T::staticTypeName()) {
190  return boost::static_pointer_cast<const T, const Metadata>(iter->second);
191  } // else
192  return typename T::ConstPtr();
193 }
194 
195 
197 
198 
199 template <typename T>
200 inline typename TypedMetadata<T>::Ptr
201 MetaMap::getValidTypedMetadata(const Name &name) const
202 {
203  ConstMetaIterator iter = mMeta.find(name);
204  if (iter == mMeta.end()) OPENVDB_THROW(LookupError, "Cannot find metadata " << name);
205 
206  // To ensure that we get valid conversion if the metadata pointers cross dso
207  // boundaries, we have to check the qualified typename and then do a static
208  // cast. This is slower than doing a dynamic_pointer_cast, but is safer when
209  // pointers cross dso boundaries.
210  typename TypedMetadata<T>::Ptr m;
211  if (iter->second->typeName() == TypedMetadata<T>::staticTypeName()) {
212  m = boost::static_pointer_cast<TypedMetadata<T>, Metadata>(iter->second);
213  }
214  if (!m) OPENVDB_THROW(TypeError, "Invalid type for metadata " << name);
215  return m;
216 }
217 
218 
220 
221 
222 template <typename T>
223 inline T&
225 {
226  typename TypedMetadata<T>::Ptr m = getValidTypedMetadata<T>(name);
227  return m->value();
228 }
229 
230 
231 template <typename T>
232 inline const T&
233 MetaMap::metaValue(const Name &name) const
234 {
235  typename TypedMetadata<T>::Ptr m = getValidTypedMetadata<T>(name);
236  return m->value();
237 }
238 
239 } // namespace OPENVDB_VERSION_NAME
240 } // namespace openvdb
241 
242 #endif // OPENVDB_METADATA_METAMAP_HAS_BEEN_INCLUDED
243 
244 // Copyright (c) 2012-2014 DreamWorks Animation LLC
245 // All rights reserved. This software is distributed under the
246 // Mozilla Public License 2.0 ( http://www.mozilla.org/MPL/2.0/ )
Base class for storing metadata information in a grid.
Definition: metadata/Metadata.h:49
Templated metadata class to hold specific types.
Definition: metadata/Metadata.h:137
MetaMap()
Definition: MetaMap.h:57
void clearMetadata()
Definition: MetaMap.h:116
#define OPENVDB_API
Helper macros for defining library symbol visibility.
Definition: Platform.h:187
boost::shared_ptr< TypedMetadata< T > > Ptr
Definition: metadata/Metadata.h:140
T & value()
Definition: metadata/Metadata.h:284
std::string Name
Definition: Name.h:44
ConstMetaIterator beginMeta() const
Definition: MetaMap.h:113
std::map< Name, Metadata::Ptr > MetadataMap
Definition: MetaMap.h:52
#define OPENVDB_THROW(exception, message)
Definition: Exceptions.h:97
Container that maps names (strings) to values of arbitrary types.
Definition: MetaMap.h:46
ConstMetaIterator endMeta() const
Definition: MetaMap.h:114
MetaIterator endMeta()
Definition: MetaMap.h:112
T & metaValue(const Name &)
Return a reference to the value of type T stored in the given metadata field.
Definition: MetaMap.h:224
boost::shared_ptr< MetaMap > Ptr
Definition: MetaMap.h:49
#define OPENVDB_VERSION_NAME
Definition: version.h:43
Metadata::Ptr operator[](const Name &)
Return a pointer to the metadata with the given name. If no such field exists, return a null pointer...
Definition: MetaMap.h:141
Definition: Exceptions.h:83
Definition: Exceptions.h:87
virtual ~MetaMap()
Definition: MetaMap.h:59
static Name staticTypeName()
Definition: metadata/Metadata.h:176
Definition: Exceptions.h:39
T::Ptr getMetadata(const Name &)
Return a pointer to a TypedMetadata object of type T and with the given name. If no such field exists...
Definition: MetaMap.h:160
boost::shared_ptr< const MetaMap > ConstPtr
Definition: MetaMap.h:50
boost::shared_ptr< Metadata > Ptr
Definition: metadata/Metadata.h:52
boost::shared_ptr< const Metadata > ConstPtr
Definition: metadata/Metadata.h:53
MetadataMap::const_iterator ConstMetaIterator
Definition: MetaMap.h:54
MetadataMap::iterator MetaIterator
Definition: MetaMap.h:53
#define OPENVDB_USE_VERSION_NAMESPACE
Definition: version.h:71
std::ostream & operator<<(std::ostream &, const MetaMap &)
Write a MetaMap to an output stream.
size_t metaCount() const
Definition: MetaMap.h:118
MetaIterator beginMeta()
Definition: MetaMap.h:111