OpenVDB  0.104.0
MetaMap.h
Go to the documentation of this file.
1 
2 //
3 // Copyright (c) 2012 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 
49 {
50 public:
51  typedef boost::shared_ptr<MetaMap> Ptr;
52  typedef boost::shared_ptr<const MetaMap> ConstPtr;
53 
54  typedef std::map<Name, Metadata::Ptr> MetadataMap;
55  typedef MetadataMap::iterator MetaIterator;
56  typedef MetadataMap::const_iterator ConstMetaIterator;
58 
60  MetaMap() {}
61  MetaMap(const MetaMap& other);
62 
64  virtual ~MetaMap() {}
65 
67  MetaMap::Ptr copyMeta() const;
69  MetaMap::Ptr deepCopyMeta() const;
70 
72  MetaMap& operator=(const MetaMap&);
73 
75  void readMeta(std::istream&);
76 
78  void writeMeta(std::ostream&) const;
79 
87  void insertMeta(const Name& name, const Metadata& metadata);
88 
93  void removeMeta(const Name &name);
94 
96 
97 
98  Metadata::Ptr operator[](const Name&);
99  Metadata::ConstPtr operator[](const Name&) const;
101 
103 
104  template<typename T> typename T::Ptr getMetadata(const Name &name);
105  template<typename T> typename T::ConstPtr getMetadata(const Name &name) const;
107 
111  template<typename T> T& metaValue(const Name &name);
112  template<typename T> const T& metaValue(const Name &name) const;
113 
115  MetaIterator beginMeta() { return mMeta.begin(); }
116  MetaIterator endMeta() { return mMeta.end(); }
117  ConstMetaIterator beginMeta() const { return mMeta.begin(); }
118  ConstMetaIterator endMeta() const { return mMeta.end(); }
119 
120  void clearMetadata() { mMeta.clear(); }
121 
122  size_t metaCount() const { return mMeta.size(); }
123 
124  bool empty() const { return mMeta.empty(); }
125 
127  std::string str() const;
128 
129 private:
133  template<typename T>
134  typename TypedMetadata<T>::Ptr getValidTypedMetadata(const Name&) const;
135 
136  MetadataMap mMeta;
137 };
138 
140 std::ostream& operator<<(std::ostream&, const MetaMap&);
141 
142 
144 
145 
146 inline Metadata::Ptr
148 {
149  MetaIterator iter = mMeta.find(name);
150  return (iter == mMeta.end() ? Metadata::Ptr() : iter->second);
151 }
152 
153 inline Metadata::ConstPtr
154 MetaMap::operator[](const Name &name) const
155 {
156  ConstMetaIterator iter = mMeta.find(name);
157  return (iter == mMeta.end() ? Metadata::Ptr() : iter->second);
158 }
159 
160 
162 
163 
164 template <typename T>
165 inline typename T::Ptr
167 {
168  ConstMetaIterator iter = mMeta.find(name);
169  if(iter == mMeta.end()) {
170  return typename T::Ptr();
171  }
172 
173  // To ensure that we get valid conversion if the metadata pointers cross dso
174  // boundaries, we have to check the qualified typename and then do a static
175  // cast. This is slower than doing a dynamic_pointer_cast, but is safer when
176  // pointers cross dso boundaries.
177  if (iter->second->typeName() == T::staticTypeName()) {
178  return boost::static_pointer_cast<T, Metadata>(iter->second);
179  } // else
180  return typename T::Ptr();
181 }
182 
183 template <typename T>
184 inline typename T::ConstPtr
185 MetaMap::getMetadata(const Name &name) const
186 {
187  ConstMetaIterator iter = mMeta.find(name);
188  if(iter == mMeta.end()) {
189  return typename T::ConstPtr();
190  }
191  // To ensure that we get valid conversion if the metadata pointers cross dso
192  // boundaries, we have to check the qualified typename and then do a static
193  // cast. This is slower than doing a dynamic_pointer_cast, but is safer when
194  // pointers cross dso boundaries.
195  if (iter->second->typeName() == T::staticTypeName()) {
196  return boost::static_pointer_cast<const T, const Metadata>(iter->second);
197  } // else
198  return typename T::ConstPtr();
199 }
200 
201 
203 
204 
205 template <typename T>
206 inline typename TypedMetadata<T>::Ptr
207 MetaMap::getValidTypedMetadata(const Name &name) const
208 {
209  ConstMetaIterator iter = mMeta.find(name);
210  if (iter == mMeta.end()) OPENVDB_THROW(LookupError, "Cannot find metadata " << name);
211 
212  // To ensure that we get valid conversion if the metadata pointers cross dso
213  // boundaries, we have to check the qualified typename and then do a static
214  // cast. This is slower than doing a dynamic_pointer_cast, but is safer when
215  // pointers cross dso boundaries.
216  typename TypedMetadata<T>::Ptr m;
217  if (iter->second->typeName() == TypedMetadata<T>::staticTypeName()) {
218  m = boost::static_pointer_cast<TypedMetadata<T>, Metadata>(iter->second);
219  }
220  if (!m) OPENVDB_THROW(TypeError, "Invalid type for metadata " << name);
221  return m;
222 }
223 
224 
226 
227 
228 template <typename T>
229 inline T&
231 {
232  typename TypedMetadata<T>::Ptr m = getValidTypedMetadata<T>(name);
233  return m->value();
234 }
235 
236 
237 template <typename T>
238 inline const T&
239 MetaMap::metaValue(const Name &name) const
240 {
241  typename TypedMetadata<T>::Ptr m = getValidTypedMetadata<T>(name);
242  return m->value();
243 }
244 
245 } // namespace OPENVDB_VERSION_NAME
246 } // namespace openvdb
247 
248 #endif // OPENVDB_METADATA_METAMAP_HAS_BEEN_INCLUDED
249 
250 // Copyright (c) 2012 DreamWorks Animation LLC
251 // All rights reserved. This software is distributed under the
252 // Mozilla Public License 2.0 ( http://www.mozilla.org/MPL/2.0/ )