OpenVDB: MetaMap.h Source File

OpenVDB 8.0.0

MetaMap.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_METAMAP_HAS_BEEN_INCLUDED
5 #define OPENVDB_METADATA_METAMAP_HAS_BEEN_INCLUDED
6
7 #include "Metadata.h"
8 #include "Types.h"
9 #include "Exceptions.h"
10 #include <iosfwd>
11 #include <map>
12
13
14 namespace openvdb {
15 OPENVDB_USE_VERSION_NAMESPACE
16 namespace OPENVDB_VERSION_NAME {
17
19 class OPENVDB_API MetaMap
20 {
21 public:
22 using Ptr = SharedPtr<MetaMap>;
23 using ConstPtr = SharedPtr<const MetaMap>;
24
25 using MetadataMap = std::map<Name, Metadata::Ptr>;
26 using MetaIterator = MetadataMap::iterator;
27 using ConstMetaIterator = MetadataMap::const_iterator;
29
30 MetaMap() {}
31 MetaMap(const MetaMap& other);
32 virtual ~MetaMap() {}
33
35 MetaMap::Ptr copyMeta() const;
37 MetaMap::Ptr deepCopyMeta() const;
38
40 MetaMap& operator=(const MetaMap&);
41
43 void readMeta(std::istream&);
45 void writeMeta(std::ostream&) const;
46
54 void insertMeta(const Name&, const Metadata& value);
58 void insertMeta(const MetaMap&);
59
61 void removeMeta(const Name&);
62
64 Metadata::Ptr operator[](const Name&);
67 Metadata::ConstPtr operator[](const Name&) const;
69
71 template<typename T> typename T::Ptr getMetadata(const Name&);
74 template<typename T> typename T::ConstPtr getMetadata(const Name&) const;
76
80 template<typename T> T& metaValue(const Name&);
81 template<typename T> const T& metaValue(const Name&) const;
82
83 // Functions for iterating over the metadata
84 MetaIterator beginMeta() { return mMeta.begin(); }
85 MetaIterator endMeta() { return mMeta.end(); }
86 ConstMetaIterator beginMeta() const { return mMeta.begin(); }
87 ConstMetaIterator endMeta() const { return mMeta.end(); }
88
89 void clearMetadata() { mMeta.clear(); }
90
91 size_t metaCount() const { return mMeta.size(); }
92
94 std::string str(const std::string& indent = "") const;
95
97 bool operator==(const MetaMap& other) const;
99 bool operator!=(const MetaMap& other) const { return !(*this == other); }
100
101 private:
105 template<typename T>
106 typename TypedMetadata<T>::Ptr getValidTypedMetadata(const Name&) const;
107
108 MetadataMap mMeta;
109 };
110
112 std::ostream& operator<<(std::ostream&, const MetaMap&);
113
114
116
117
118 inline Metadata::Ptr
119 MetaMap::operator[](const Name& name)
120 {
121 MetaIterator iter = mMeta.find(name);
122 return (iter == mMeta.end() ? Metadata::Ptr() : iter->second);
123 }
124
125 inline Metadata::ConstPtr
126 MetaMap::operator[](const Name &name) const
127 {
128 ConstMetaIterator iter = mMeta.find(name);
129 return (iter == mMeta.end() ? Metadata::Ptr() : iter->second);
130 }
131
132
134
135
136 template<typename T>
137 inline typename T::Ptr
138 MetaMap::getMetadata(const Name &name)
139 {
140 ConstMetaIterator iter = mMeta.find(name);
141 if (iter == mMeta.end()) return typename T::Ptr{};
142
143 // To ensure that we get valid conversion if the metadata pointers cross dso
144 // boundaries, we have to check the qualified typename and then do a static
145 // cast. This is slower than doing a dynamic_pointer_cast, but is safer when
146 // pointers cross dso boundaries.
147 if (iter->second->typeName() == T::staticTypeName()) {
148 return StaticPtrCast<T, Metadata>(iter->second);
149 } // else
150 return typename T::Ptr{};
151 }
152
153 template<typename T>
154 inline typename T::ConstPtr
155 MetaMap::getMetadata(const Name &name) const
156 {
157 ConstMetaIterator iter = mMeta.find(name);
158 if (iter == mMeta.end()) return typename T::ConstPtr{};
159
160 // To ensure that we get valid conversion if the metadata pointers cross dso
161 // boundaries, we have to check the qualified typename and then do a static
162 // cast. This is slower than doing a dynamic_pointer_cast, but is safer when
163 // pointers cross dso boundaries.
164 if (iter->second->typeName() == T::staticTypeName()) {
165 return StaticPtrCast<const T, const Metadata>(iter->second);
166 } // else
167 return typename T::ConstPtr{};
168 }
169
170
172
173
174 template<typename T>
175 inline typename TypedMetadata<T>::Ptr
176 MetaMap::getValidTypedMetadata(const Name &name) const
177 {
178 ConstMetaIterator iter = mMeta.find(name);
179 if (iter == mMeta.end()) OPENVDB_THROW(LookupError, "Cannot find metadata " << name);
180
181 // To ensure that we get valid conversion if the metadata pointers cross dso
182 // boundaries, we have to check the qualified typename and then do a static
183 // cast. This is slower than doing a dynamic_pointer_cast, but is safer when
184 // pointers cross dso boundaries.
185 typename TypedMetadata<T>::Ptr m;
186 if (iter->second->typeName() == TypedMetadata<T>::staticTypeName()) {
187 m = StaticPtrCast<TypedMetadata<T>, Metadata>(iter->second);
188 }
189 if (!m) OPENVDB_THROW(TypeError, "Invalid type for metadata " << name);
190 return m;
191 }
192
193
195
196
197 template<typename T>
198 inline T&
199 MetaMap::metaValue(const Name &name)
200 {
201 typename TypedMetadata<T>::Ptr m = getValidTypedMetadata<T>(name);
202 return m->value();
203 }
204
205
206 template<typename T>
207 inline const T&
208 MetaMap::metaValue(const Name &name) const
209 {
210 typename TypedMetadata<T>::Ptr m = getValidTypedMetadata<T>(name);
211 return m->value();
212 }
213
214 } // namespace OPENVDB_VERSION_NAME
215 } // namespace openvdb
216
217 #endif // OPENVDB_METADATA_METAMAP_HAS_BEEN_INCLUDED
openvdb::v8_2::MetaMap::removeMeta
void removeMeta(const Name &)
Remove the given metadata field if it exists.
openvdb::v8_2::MetaMap::metaValue
T & metaValue(const Name &)
Return a reference to the value of type T stored in the given metadata field.
Definition: MetaMap.h:199
Types.h
Consolidated llvm types for most supported types.
openvdb::v8_2::MetaMap::~MetaMap
virtual ~MetaMap()
Definition: MetaMap.h:32
Metadata.h
openvdb::v8_2::MetaMap::beginMeta
MetaIterator beginMeta()
Definition: MetaMap.h:84
openvdb::v8_2::MetaMap::operator=
MetaMap & operator=(const MetaMap &)
Assign a deep copy of another map to this map.
openvdb::v8_2::MetaMap::MetadataMap
std::map< Name, Metadata::Ptr > MetadataMap
Definition: MetaMap.h:25
openvdb::v8_2::MetaMap::MetaMap
MetaMap()
Definition: MetaMap.h:30
openvdb::v8_2::MetaMap::operator[]
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:119
openvdb::v8_2::MetaMap::metaCount
size_t metaCount() const
Definition: MetaMap.h:91
openvdb::v8_2::MetaMap::insertMeta
void insertMeta(const MetaMap &)
Deep copy all of the metadata fields from the given map into this map.
openvdb::v8_2::TypedMetadata::value
T & value()
Return this metadata's value.
Definition: Metadata.h:249
openvdb::v8_2::MetaMap::endMeta
MetaIterator endMeta()
Definition: MetaMap.h:85
OPENVDB_THROW
#define OPENVDB_THROW(exception, message)
Definition: openvdb/Exceptions.h:74
openvdb::v8_2::MetaMap::MetaMap
MetaMap(const MetaMap &other)
openvdb::v8_2::MetaMap
Container that maps names (strings) to values of arbitrary types.
Definition: MetaMap.h:20
openvdb::v8_2::TypedMetadata::Ptr
SharedPtr< TypedMetadata< T > > Ptr
Definition: Metadata.h:124
openvdb::v8_2::TypeError
Definition: openvdb/Exceptions.h:64
OPENVDB_API
#define OPENVDB_API
Helper macros for defining library symbol visibility.
Definition: Platform.h:208
openvdb::v8_2::MetaMap::operator==
bool operator==(const MetaMap &other) const
Return true if the given map is equivalent to this map.
openvdb::v8_2::MetaMap::MetaIterator
MetadataMap::iterator MetaIterator
Definition: MetaMap.h:26
openvdb::v8_2::MetaMap::clearMetadata
void clearMetadata()
Definition: MetaMap.h:89
openvdb::v8_2::Metadata
Base class for storing metadata information in a grid.
Definition: Metadata.h:24
openvdb::v8_2::MetaMap::beginMeta
ConstMetaIterator beginMeta() const
Definition: MetaMap.h:86
openvdb::v8_2::MetaMap::operator!=
bool operator!=(const MetaMap &other) const
Return true if the given map is different from this map.
Definition: MetaMap.h:99
openvdb::v8_2::SharedPtr
std::shared_ptr< T > SharedPtr
Definition: openvdb/Types.h:92
openvdb::v8_2::operator<<
std::ostream & operator<<(std::ostream &, const MetaMap &)
Write a MetaMap to an output stream.
openvdb::v8_2::TypedMetadata
Templated metadata class to hold specific types.
Definition: Metadata.h:122
OPENVDB_USE_VERSION_NAMESPACE
#define OPENVDB_USE_VERSION_NAMESPACE
Definition: version.h:153
openvdb::v8_2::Metadata::ConstPtr
SharedPtr< const Metadata > ConstPtr
Definition: Metadata.h:27
openvdb::v8_2::MetaMap::insertMeta
void insertMeta(const Name &, const Metadata &value)
Insert a new metadata field or overwrite the value of an existing field.
openvdb::v8_2::MetaMap::deepCopyMeta
MetaMap::Ptr deepCopyMeta() const
Return a deep copy of this map that shares no data with this map.
openvdb::v8_2::MetaMap::writeMeta
void writeMeta(std::ostream &) const
Serialize metadata to the given stream.
openvdb::v8_2::MetaMap::str
std::string str(const std::string &indent="") const
Return a string describing this metadata map. Prefix each line with indent.
openvdb::v8_2::MetaMap::ConstMetaIterator
MetadataMap::const_iterator ConstMetaIterator
Definition: MetaMap.h:28
openvdb::v8_2::MetaMap::readMeta
void readMeta(std::istream &)
Unserialize metadata from the given stream.
openvdb::v8_2::Metadata::Ptr
SharedPtr< Metadata > Ptr
Definition: Metadata.h:26
OPENVDB_VERSION_NAME
#define OPENVDB_VERSION_NAME
The version namespace name for this library version.
Definition: version.h:101
openvdb::v8_2::MetaMap::Ptr
SharedPtr< MetaMap > Ptr
Definition: MetaMap.h:22
openvdb::v8_2::LookupError
Definition: openvdb/Exceptions.h:60
openvdb::v8_2::MetaMap::getMetadata
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:138
Exceptions.h
OpenVDB AX Exceptions.
openvdb
Definition: openvdb/Exceptions.h:13
openvdb::v8_2::MetaMap::copyMeta
MetaMap::Ptr copyMeta() const
Return a copy of this map whose fields are shared with this map.
openvdb::v8_2::MetaMap::ConstPtr
SharedPtr< const MetaMap > ConstPtr
Definition: MetaMap.h:23
openvdb::v8_2::MetaMap::endMeta
ConstMetaIterator endMeta() const
Definition: MetaMap.h:87
openvdb::v8_2::Name
std::string Name
Definition: Name.h:17