OpenVDB: CustomData.h Source File

OpenVDB 8.0.0

CustomData.h
Go to the documentation of this file.
1 // Copyright Contributors to the OpenVDB Project
2 // SPDX-License-Identifier: MPL-2.0
3
11
12 #ifndef OPENVDB_AX_COMPILER_CUSTOM_DATA_HAS_BEEN_INCLUDED
13 #define OPENVDB_AX_COMPILER_CUSTOM_DATA_HAS_BEEN_INCLUDED
14
15 #include <openvdb/version.h>
16 #include <openvdb/Metadata.h>
17 #include <openvdb/Types.h>
18
19 #include <unordered_map>
20 #include <memory>
21
22 namespace openvdb {
23 OPENVDB_USE_VERSION_NAMESPACE
24 namespace OPENVDB_VERSION_NAME {
25
26 namespace ax {
27
30 struct AXString
31 {
32 // usually size_t. Used to match the implementation of std:string
33 using SizeType = std::allocator<char>::size_type;
34 const char* ptr = nullptr;
35 SizeType size = 0;
36 };
37
42 class CustomData
43 {
44 public:
45
46 using Ptr = std::shared_ptr<CustomData>;
47 using ConstPtr = std::shared_ptr<const CustomData>;
48 using UniquePtr = std::unique_ptr<CustomData>;
49
50 CustomData() : mData() {}
51
52 static UniquePtr create()
53 {
54 UniquePtr data(new CustomData);
55 return data;
56 }
57
61 inline void reset()
62 {
63 mData.clear();
64 }
65
67 inline bool
68 hasData(const Name& name)
69 {
70 const auto iter = mData.find(name);
71 return (iter != mData.end());
72 }
73
75 template <typename TypedDataCacheT>
76 inline bool
77 hasData(const Name& name)
78 {
79 const auto iter = mData.find(name);
80 if (iter == mData.end()) return false;
81 const TypedDataCacheT* const typed =
82 dynamic_cast<const TypedDataCacheT* const>(iter->second.get());
83 return typed != nullptr;
84 }
85
88 inline const Metadata::ConstPtr
89 getData(const Name& name) const
90 {
91 const auto iter = mData.find(name);
92 if (iter == mData.end()) return Metadata::ConstPtr();
93 return iter->second;
94 }
95
100 template <typename TypedDataCacheT>
101 inline const TypedDataCacheT*
102 getData(const Name& name) const
103 {
104 Metadata::ConstPtr data = getData(name);
105 if (!data) return nullptr;
106 const TypedDataCacheT* const typed =
107 dynamic_cast<const TypedDataCacheT* const>(data.get());
108 return typed;
109 }
110
114 template <typename TypedDataCacheT>
115 inline TypedDataCacheT*
116 getOrInsertData(const Name& name)
117 {
118 const auto iter = mData.find(name);
119 if (iter == mData.end()) {
120 Metadata::Ptr data(new TypedDataCacheT());
121 mData[name] = data;
122 return static_cast<TypedDataCacheT* const>(data.get());
123 }
124 else {
125 return dynamic_cast<TypedDataCacheT* const>(iter->second.get());
126 }
127 }
128
134 template <typename TypedDataCacheT>
135 inline void
136 insertData(const Name& name,
137 const typename TypedDataCacheT::Ptr data)
138 {
139 if (hasData(name)) {
140 TypedDataCacheT* const dataToSet =
141 getOrInsertData<TypedDataCacheT>(name);
142 if (!dataToSet) {
143 OPENVDB_THROW(TypeError, "Custom data \"" + name + "\" already exists with a different type.");
144 }
145 dataToSet->value() = data->value();
146 }
147 else {
148 mData[name] = data->copy();
149 }
150 }
151
157 inline void
158 insertData(const Name& name,
159 const Metadata::Ptr data)
160 {
161 const auto iter = mData.find(name);
162 if (iter == mData.end()) {
163 mData[name] = data;
164 }
165 else {
166 iter->second->copy(*data);
167 }
168 }
169
170 private:
171 std::unordered_map<Name, Metadata::Ptr> mData;
172 };
173
174
175 struct AXStringMetadata : public StringMetadata
176 {
177 using Ptr = openvdb::SharedPtr<AXStringMetadata>;
178 using ConstPtr = openvdb::SharedPtr<const AXStringMetadata>;
179
180 AXStringMetadata(const std::string& string)
181 : StringMetadata(string)
182 , mData()
183 {
184 this->initialize();
185 }
186
187 // delegate, ensure valid string initialization
188 AXStringMetadata() : AXStringMetadata("") {}
189 AXStringMetadata(const AXStringMetadata& other)
190 : StringMetadata(other)
191 , mData()
192 {
193 this->initialize();
194 }
195
196 ~AXStringMetadata() override {}
197
198 openvdb::Metadata::Ptr copy() const override {
199 openvdb::Metadata::Ptr metadata(new AXStringMetadata());
200 metadata->copy(*this);
201 return metadata;
202 }
203
204 void copy(const openvdb::Metadata& other) override {
205 const AXStringMetadata* t = dynamic_cast<const AXStringMetadata*>(&other);
206 if (t == nullptr) OPENVDB_THROW(openvdb::TypeError, "Incompatible type during copy");
207 this->StringMetadata::setValue(t->StringMetadata::value());
208 this->initialize();
209 }
210
211 void setValue(const std::string& string)
212 {
213 this->StringMetadata::setValue(string);
214 this->initialize();
215 }
216
217 ax::AXString& value() { return mData; }
218 const ax::AXString& value() const { return mData; }
219
220 protected:
221
222 void readValue(std::istream& is, openvdb::Index32 size) override {
223 StringMetadata::readValue(is, size);
224 this->initialize();
225 }
226
227 private:
228 void initialize()
229 {
230 mData.ptr = StringMetadata::value().c_str();
231 mData.size = StringMetadata::value().size();
232 }
233
234 ax::AXString mData;
235 };
236
237
238 } // namespace ax
239 } // namespace OPENVDB_VERSION_NAME
240 } // namespace openvdb
241
242 #endif // OPENVDB_AX_COMPILER_CUSTOM_DATA_HAS_BEEN_INCLUDED
243
Types.h
openvdb::v8_2::ax::CustomData::ConstPtr
std::shared_ptr< const CustomData > ConstPtr
Definition: CustomData.h:47
Metadata.h
openvdb::v8_2::ax::CustomData::getOrInsertData
TypedDataCacheT * getOrInsertData(const Name &name)
Retrieves or inserts typed metadata. If thedata exists, it is dynamic-casted to the expected type,...
Definition: CustomData.h:116
openvdb::v8_2::ax::AXString
The backend representation of strings in AX. This is also how strings are passed from the AX code gen...
Definition: CustomData.h:31
openvdb::v8_2::initialize
OPENVDB_IMPORT void initialize()
Global registration of basic types.
openvdb::v8_2::ax::AXStringMetadata::copy
openvdb::Metadata::Ptr copy() const override
Return a copy of the metadata.
Definition: CustomData.h:198
openvdb::v8_2::ax::AXStringMetadata::readValue
void readValue(std::istream &is, openvdb::Index32 size) override
Read the metadata from a stream.
Definition: CustomData.h:222
openvdb::v8_2::TypedMetadata::value
T & value()
Return this metadata's value.
Definition: Metadata.h:249
version.h
Library and file format version numbers.
openvdb::v8_2::ax::CustomData::hasData
bool hasData(const Name &name)
Checks whether or not data of given name and type has been inserted.
Definition: CustomData.h:77
openvdb::v8_2::ax::AXStringMetadata::value
const ax::AXString & value() const
Definition: CustomData.h:218
OPENVDB_THROW
#define OPENVDB_THROW(exception, message)
Definition: openvdb/Exceptions.h:74
openvdb::v8_2::ax::CustomData::hasData
bool hasData(const Name &name)
Checks whether or not data of given name has been inserted.
Definition: CustomData.h:68
openvdb::v8_2::ax::AXStringMetadata::AXStringMetadata
AXStringMetadata(const std::string &string)
Definition: CustomData.h:180
openvdb::v8_2::ax::CustomData::CustomData
CustomData()
Definition: CustomData.h:50
openvdb::v8_2::TypeError
Definition: openvdb/Exceptions.h:64
openvdb::v8_2::ax::AXStringMetadata
Definition: CustomData.h:176
openvdb::v8_2::ax::CustomData::reset
void reset()
Reset the custom data. This will clear and delete all previously added data.
Definition: CustomData.h:61
openvdb::v8_2::ax::AXStringMetadata::AXStringMetadata
AXStringMetadata(const AXStringMetadata &other)
Definition: CustomData.h:189
openvdb::v8_2::ax::AXStringMetadata::~AXStringMetadata
~AXStringMetadata() override
Definition: CustomData.h:196
openvdb::v8_2::ax::AXStringMetadata::copy
void copy(const openvdb::Metadata &other) override
Copy the given metadata into this metadata.
Definition: CustomData.h:204
openvdb::v8_2::Metadata
Base class for storing metadata information in a grid.
Definition: Metadata.h:24
openvdb::v8_2::SharedPtr
std::shared_ptr< T > SharedPtr
Definition: openvdb/Types.h:92
openvdb::v8_2::ax::CustomData
The custom data class is a simple container for named openvdb metadata. Its primary use case is passi...
Definition: CustomData.h:43
openvdb::v8_2::ax::CustomData::insertData
void insertData(const Name &name, const Metadata::Ptr data)
Inserts data with given name.
Definition: CustomData.h:158
openvdb::v8_2::TypedMetadata::setValue
void setValue(const T &)
Set this metadata's value.
Definition: Metadata.h:242
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::ax::CustomData::insertData
void insertData(const Name &name, const typename TypedDataCacheT::Ptr data)
Inserts data of specified type with given name.
Definition: CustomData.h:136
openvdb::v8_2::Metadata::ConstPtr
SharedPtr< const Metadata > ConstPtr
Definition: Metadata.h:27
openvdb::v8_2::ax::CustomData::getData
const TypedDataCacheT * getData(const Name &name) const
Retrieves a const pointer to data of given name and type. If it does not exist, returns nullptr.
Definition: CustomData.h:102
openvdb::v8_2::Index32
uint32_t Index32
Definition: openvdb/Types.h:30
openvdb::v8_2::ax::AXStringMetadata::AXStringMetadata
AXStringMetadata()
Definition: CustomData.h:188
openvdb::v8_2::TypedMetadata::readValue
void readValue(std::istream &, Index32 numBytes) override
Read the metadata from a stream.
Definition: Metadata.h:282
openvdb::v8_2::Metadata::Ptr
SharedPtr< Metadata > Ptr
Definition: Metadata.h:26
openvdb::v8_2::ax::AXString::SizeType
std::allocator< char >::size_type SizeType
Definition: CustomData.h:33
OPENVDB_VERSION_NAME
#define OPENVDB_VERSION_NAME
The version namespace name for this library version.
Definition: version.h:101
openvdb
Definition: openvdb/Exceptions.h:13
openvdb::v8_2::ax::CustomData::UniquePtr
std::unique_ptr< CustomData > UniquePtr
Definition: CustomData.h:48
openvdb::v8_2::ax::CustomData::create
static UniquePtr create()
Definition: CustomData.h:52
openvdb::v8_2::ax::AXStringMetadata::setValue
void setValue(const std::string &string)
Definition: CustomData.h:211
openvdb::v8_2::ax::AXStringMetadata::value
ax::AXString & value()
Definition: CustomData.h:217
openvdb::v8_2::ax::CustomData::getData
const Metadata::ConstPtr getData(const Name &name) const
Retrieves a const pointer to data of given name. If it does not exist, returns nullptr.
Definition: CustomData.h:89
openvdb::v8_2::ax::CustomData::Ptr
std::shared_ptr< CustomData > Ptr
Definition: CustomData.h:46
openvdb::v8_2::Name
std::string Name
Definition: Name.h:17