OpenVDB: openvdb_ax/compiler/AttributeBindings.h Source File

OpenVDB 11.0.0

Loading...
Searching...
No Matches
AttributeBindings.h
Go to the documentation of this file.
1// Copyright Contributors to the OpenVDB Project
2// SPDX-License-Identifier: MPL-2.0/
3
4/// @file compiler/AttributeBindings.h
5///
6/// @authors Richard Jones
7///
8/// @brief The Attribute Bindings class is used by the compiled Executables
9/// to handle the mapping of AX Attribute names to context dependent data
10/// names, i.e point attribute and volume grid names.
11///
12
13#ifndef OPENVDB_AX_COMPILER_ATTRIBUTE_BINDINGS_HAS_BEEN_INCLUDED
14#define OPENVDB_AX_COMPILER_ATTRIBUTE_BINDINGS_HAS_BEEN_INCLUDED
15
16#include <openvdb/version.h>
17#include <openvdb/util/Name.h>
18
19#include <map>
20
21namespace openvdb {
22 OPENVDB_USE_VERSION_NAMESPACE
23namespace OPENVDB_VERSION_NAME {
24
25namespace ax {
26
27/// @brief This class wraps an interface for a map of attribute bindings. These map
28/// attributes in AX code to context data. These mappings are one-to-one i.e.
29/// each AX name can only map to one data name, however each name can appear as either
30/// an AX name or data name or both, i.e. the following sets of bindings are valid:
31/// axname: a -> dataname: a
32/// axname: b -> dataname: c
33/// or
34/// axname: a -> dataname: b
35/// axname: b -> dataname: a
36 class AttributeBindings
37{
38public:
39
40 AttributeBindings() = default;
41
42 /// @brief Construct a set of attribute bindings from a vector of {ax name, data name} pairs
43 /// @param bindings A vector of ax name data name pairs where the first element is the name
44 /// in the AX code, and the second is the name in the context data
45 AttributeBindings(const std::vector<std::pair<std::string, std::string>>& bindings)
46 {
47 set(bindings);
48 }
49
50 /// @brief Construct a set of attribute bindings from a vector of {ax name, data name} pairs
51 /// using an initializer list i.e. {{"axname0", "dataname0"}, {"axname1", "dataname0"}}
52 /// @param bindings A initializer list of ax name data name pairs where the first element is the
53 /// name in the AX code, and the second is the name in the context data
54 AttributeBindings(const std::initializer_list<std::pair<std::string, std::string>>& bindings)
55 {
56 set(bindings);
57 }
58
59 bool operator==(const AttributeBindings& other) const
60 {
61 return mAXToDataMap == other.mAXToDataMap &&
62 mDataToAXMap == other.mDataToAXMap;
63 }
64
65 /// @brief Set up a binding. If a data binding exists for this AX name, it will be replaced.
66 /// If another binding exists for the supplied dataname that will be removed.
67 /// @param axname The name of the attribute in AX
68 /// @param dataname The name of the attribute in the context data
69 inline void set(const std::string& axname, const std::string& dataname)
70 {
71 auto axToData = mAXToDataMap.find(axname);
72 if (axToData != mAXToDataMap.end()) {
73 // the dataname is already mapped, so update it
74 // and remove corresponding map entry in opposite direction
75 auto dataToAX = mDataToAXMap.find(axToData->second);
76 if (dataToAX != mDataToAXMap.end()) {
77 mAXToDataMap.erase(dataToAX->second);
78 mDataToAXMap.erase(dataToAX->first);
79 }
80 }
81 auto dataToAX = mDataToAXMap.find(dataname);
82 if (dataToAX != mDataToAXMap.end()) {
83 mAXToDataMap.erase(dataToAX->second);
84 }
85
86 mAXToDataMap[axname] = dataname;
87 mDataToAXMap[dataname] = axname;
88 }
89
90 /// @brief Set up multiple bindings from a vector of {ax name, data name} pairs.
91 /// If a data binding exists for any AX name, it will be replaced.
92 /// If another binding exists for the supplied dataname that will be removed.
93 /// @param bindings Vector of AX name data name pairs
94 inline void set(const std::vector<std::pair<std::string, std::string>>& bindings) {
95 for (const auto& binding : bindings) {
96 this->set(binding.first, binding.second);
97 }
98 }
99 /// @brief Returns a pointer to the data attribute name string that the input AX attribute name
100 /// is bound to, or nullptr if unbound.
101 /// @param axname The name of the attribute in AX
102 inline const std::string* dataNameBoundTo(const std::string& axname) const
103 {
104 const auto iter = mAXToDataMap.find(axname);
105 if (iter != mAXToDataMap.cend()) {
106 return &iter->second;
107 }
108 return nullptr;
109 }
110
111 /// @brief Returns a pointer to the AX attribute name string that a data attribute name
112 /// is bound to, or nullptr if unbound.
113 /// @param name The name of the attribute in the context data
114 inline const std::string* axNameBoundTo(const std::string& name) const
115 {
116 const auto iter = mDataToAXMap.find(name);
117 if (iter != mDataToAXMap.cend()) {
118 return &iter->second;
119 }
120 return nullptr;
121 }
122
123 /// @brief Returns whether the data attribute has been bound to an AX attribute
124 /// @param name The name of the attribute in the context data
125 inline bool isBoundDataName(const std::string& name) const
126 {
127 return mDataToAXMap.count(name);
128 }
129
130 /// @brief Returns whether the AX attribute has been bound to a data attribute
131 /// @param name The name of the attribute in AX
132 inline bool isBoundAXName(const std::string& name) const
133 {
134 return mAXToDataMap.count(name);
135 }
136
137 /// @brief Returns the map of AX attribute names to data attribute names
138 inline const std::map<std::string, std::string>& axToDataMap() const {
139 return mAXToDataMap;
140 }
141
142 /// @brief Returns the map of data attribute names to AX attribute names
143 inline const std::map<std::string, std::string>& dataToAXMap() const {
144 return mDataToAXMap;
145 }
146
147private:
148
149 std::map<std::string, std::string> mAXToDataMap;
150 std::map<std::string, std::string> mDataToAXMap;
151};
152
153
154 inline std::ostream& operator<<(std::ostream& os, const AttributeBindings& bindings)
155{
156 os << "ax->data map:\n";
157 for (const auto& m : bindings.axToDataMap()) {
158 os << " [" << m.first << " -> " << m.second << ']' << '\n';
159 }
160 os << "data->ax map:\n";
161 for (const auto& m : bindings.dataToAXMap()) {
162 os << " [" << m.first << " -> " << m.second << ']' << '\n';
163 }
164 return os;
165}
166
167} // namespace ax
168} // namespace OPENVDB_VERSION_NAME
169} // namespace openvdb
170
171#endif // OPENVDB_AX_COMPILER_ATTRIBUTE_BINDINGS_HAS_BEEN_INCLUDED
172
Name.h
openvdb::v11_0::ax::AttributeBindings
This class wraps an interface for a map of attribute bindings. These map attributes in AX code to con...
Definition AttributeBindings.h:37
openvdb::v11_0::ax::AttributeBindings::AttributeBindings
AttributeBindings()=default
openvdb::v11_0::ax::AttributeBindings::axNameBoundTo
const std::string * axNameBoundTo(const std::string &name) const
Returns a pointer to the AX attribute name string that a data attribute name is bound to,...
Definition AttributeBindings.h:114
openvdb::v11_0::ax::AttributeBindings::set
void set(const std::string &axname, const std::string &dataname)
Set up a binding. If a data binding exists for this AX name, it will be replaced. If another binding ...
Definition AttributeBindings.h:69
openvdb::v11_0::ax::AttributeBindings::AttributeBindings
AttributeBindings(const std::vector< std::pair< std::string, std::string > > &bindings)
Construct a set of attribute bindings from a vector of {ax name, data name} pairs.
Definition AttributeBindings.h:45
openvdb::v11_0::ax::AttributeBindings::isBoundDataName
bool isBoundDataName(const std::string &name) const
Returns whether the data attribute has been bound to an AX attribute.
Definition AttributeBindings.h:125
openvdb::v11_0::ax::AttributeBindings::dataNameBoundTo
const std::string * dataNameBoundTo(const std::string &axname) const
Returns a pointer to the data attribute name string that the input AX attribute name is bound to,...
Definition AttributeBindings.h:102
openvdb::v11_0::ax::AttributeBindings::operator==
bool operator==(const AttributeBindings &other) const
Definition AttributeBindings.h:59
openvdb::v11_0::ax::AttributeBindings::axToDataMap
const std::map< std::string, std::string > & axToDataMap() const
Returns the map of AX attribute names to data attribute names.
Definition AttributeBindings.h:138
openvdb::v11_0::ax::AttributeBindings::isBoundAXName
bool isBoundAXName(const std::string &name) const
Returns whether the AX attribute has been bound to a data attribute.
Definition AttributeBindings.h:132
openvdb::v11_0::ax::AttributeBindings::set
void set(const std::vector< std::pair< std::string, std::string > > &bindings)
Set up multiple bindings from a vector of {ax name, data name} pairs. If a data binding exists for an...
Definition AttributeBindings.h:94
openvdb::v11_0::ax::AttributeBindings::AttributeBindings
AttributeBindings(const std::initializer_list< std::pair< std::string, std::string > > &bindings)
Construct a set of attribute bindings from a vector of {ax name, data name} pairs using an initialize...
Definition AttributeBindings.h:54
openvdb::v11_0::ax::AttributeBindings::dataToAXMap
const std::map< std::string, std::string > & dataToAXMap() const
Returns the map of data attribute names to AX attribute names.
Definition AttributeBindings.h:143
openvdb::v11_0::operator<<
std::ostream & operator<<(std::ostream &ostr, const Metadata &metadata)
Write a Metadata to an output stream.
Definition Metadata.h:351
openvdb
Definition Exceptions.h:13
OPENVDB_VERSION_NAME
#define OPENVDB_VERSION_NAME
The version namespace name for this library version.
Definition version.h.in:121
OPENVDB_USE_VERSION_NAMESPACE
#define OPENVDB_USE_VERSION_NAMESPACE
Definition version.h.in:212