OpenVDB  8.1.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 {
24 namespace OPENVDB_VERSION_NAME {
25 
26 namespace ax {
27 
34 {
35 public:
36 
37  using Ptr = std::shared_ptr<CustomData>;
38  using ConstPtr = std::shared_ptr<const CustomData>;
39  using UniquePtr = std::unique_ptr<CustomData>;
40 
41  CustomData() : mData() {}
42 
43  static UniquePtr create()
44  {
45  UniquePtr data(new CustomData);
46  return data;
47  }
48 
52  inline void reset()
53  {
54  mData.clear();
55  }
56 
58  inline bool
59  hasData(const Name& name)
60  {
61  const auto iter = mData.find(name);
62  return (iter != mData.end());
63  }
64 
66  template <typename TypedDataCacheT>
67  inline bool
68  hasData(const Name& name)
69  {
70  const auto iter = mData.find(name);
71  if (iter == mData.end()) return false;
72  const TypedDataCacheT* const typed =
73  dynamic_cast<const TypedDataCacheT* const>(iter->second.get());
74  return typed != nullptr;
75  }
76 
79  inline const Metadata::ConstPtr
80  getData(const Name& name) const
81  {
82  const auto iter = mData.find(name);
83  if (iter == mData.end()) return Metadata::ConstPtr();
84  return iter->second;
85  }
86 
91  template <typename TypedDataCacheT>
92  inline const TypedDataCacheT*
93  getData(const Name& name) const
94  {
95  Metadata::ConstPtr data = getData(name);
96  if (!data) return nullptr;
97  const TypedDataCacheT* const typed =
98  dynamic_cast<const TypedDataCacheT* const>(data.get());
99  return typed;
100  }
101 
106  template <typename TypedDataCacheT>
107  inline TypedDataCacheT*
108  getOrInsertData(const Name& name)
109  {
110  const auto iter = mData.find(name);
111  if (iter == mData.end()) {
112  Metadata::Ptr data(new TypedDataCacheT());
113  mData[name] = data;
114  return static_cast<TypedDataCacheT* const>(data.get());
115  }
116  else {
117  return dynamic_cast<TypedDataCacheT* const>(iter->second.get());
118  }
119  }
120 
126  template <typename TypedDataCacheT>
127  inline void
128  insertData(const Name& name,
129  const typename TypedDataCacheT::Ptr data)
130  {
131  if (hasData(name)) {
132  TypedDataCacheT* const dataToSet =
133  getOrInsertData<TypedDataCacheT>(name);
134  if (!dataToSet) {
135  OPENVDB_THROW(TypeError, "Custom data \"" + name +
136  "\" already exists with a different type.");
137  }
138  dataToSet->value() = data->value();
139  }
140  else {
141  mData[name] = data->copy();
142  }
143  }
144 
150  inline void
151  insertData(const Name& name,
152  const Metadata::Ptr data)
153  {
154  const auto iter = mData.find(name);
155  if (iter == mData.end()) {
156  mData[name] = data;
157  }
158  else {
159  iter->second->copy(*data);
160  }
161  }
162 
163 private:
164  std::unordered_map<Name, Metadata::Ptr> mData;
165 };
166 
167 // fwd declare the codegen::String and alias deprecated metadata type
168 namespace codegen { struct String; }
169 using AXStringMetadata [[deprecated("The ax::AXStringMetadata type has "
170  "been replaced with openvdb::TypedMetadata<ax::codegen::String>. The "
171  "new backend string definition can be found in ax/codegen/String.h")]] =
173 
174 } // namespace ax
175 } // namespace OPENVDB_VERSION_NAME
176 } // namespace openvdb
177 
178 #endif // OPENVDB_AX_COMPILER_CUSTOM_DATA_HAS_BEEN_INCLUDED
179 
Types.h
Metadata.h
openvdb::v8_1::ax::CustomData::CustomData
CustomData()
Definition: CustomData.h:41
openvdb::v8_1::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:93
openvdb::v8_1::TypedMetadata
Templated metadata class to hold specific types.
Definition: Metadata.h:121
OPENVDB_VERSION_NAME
#define OPENVDB_VERSION_NAME
The version namespace name for this library version.
Definition: version.h.in:116
OPENVDB_USE_VERSION_NAMESPACE
#define OPENVDB_USE_VERSION_NAMESPACE
Definition: version.h.in:178
openvdb::v8_1::ax::CustomData::create
static UniquePtr create()
Definition: CustomData.h:43
openvdb::v8_1::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:80
openvdb::v8_1::ax::CustomData::Ptr
std::shared_ptr< CustomData > Ptr
Definition: CustomData.h:37
openvdb::v8_1::ax::CustomData::reset
void reset()
Reset the custom data. This will clear and delete all previously added data. This will invalidated an...
Definition: CustomData.h:52
OPENVDB_THROW
#define OPENVDB_THROW(exception, message)
Definition: openvdb/Exceptions.h:74
openvdb::v8_1::ax::CustomData::ConstPtr
std::shared_ptr< const CustomData > ConstPtr
Definition: CustomData.h:38
openvdb::v8_1::ax::CustomData::insertData
void insertData(const Name &name, const typename TypedDataCacheT::Ptr data)
Inserts data of specified type with given name.
Definition: CustomData.h:128
openvdb::v8_1::Metadata::ConstPtr
SharedPtr< const Metadata > ConstPtr
Definition: Metadata.h:27
openvdb::v8_1::ax::CustomData::hasData
bool hasData(const Name &name)
Checks whether or not data of given name has been inserted.
Definition: CustomData.h:59
openvdb::v8_1::ax::CustomData
The custom data class is a simple container for named openvdb metadata. Its primary use case is passi...
Definition: CustomData.h:33
openvdb::v8_1::ax::CustomData::UniquePtr
std::unique_ptr< CustomData > UniquePtr
Definition: CustomData.h:39
openvdb::v8_1::ax::CustomData::insertData
void insertData(const Name &name, const Metadata::Ptr data)
Inserts data with given name.
Definition: CustomData.h:151
openvdb::v8_1::TypeError
Definition: openvdb/Exceptions.h:64
openvdb::v8_1::ax::AXStringMetadata
TypedMetadata< ax::codegen::String > AXStringMetadata
Definition: CustomData.h:172
openvdb::v8_1::Name
std::string Name
Definition: Name.h:17
openvdb::v8_1::Metadata::Ptr
SharedPtr< Metadata > Ptr
Definition: Metadata.h:26
openvdb
Definition: openvdb/Exceptions.h:13
openvdb::v8_1::ax::CustomData::getOrInsertData
TypedDataCacheT * getOrInsertData(const Name &name)
Retrieves or inserts typed metadata. If the data exists, it is dynamic-casted to the expected type,...
Definition: CustomData.h:108
openvdb::v8_1::ax::CustomData::hasData
bool hasData(const Name &name)
Checks whether or not data of given name and type has been inserted.
Definition: CustomData.h:68