OpenVDB  9.0.1
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 
21 namespace openvdb {
23 namespace OPENVDB_VERSION_NAME {
24 
25 namespace 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
37 {
38 public:
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 public:
60 
61  /// @brief Set up a binding. If a data binding exists for this AX name, it will be replaced.
62  /// If another binding exists for the supplied dataname that will be removed.
63  /// @param axname The name of the attribute in AX
64  /// @param dataname The name of the attribute in the context data
65  inline void set(const std::string& axname, const std::string& dataname)
66  {
67  auto axToData = mAXToDataMap.find(axname);
68  if (axToData != mAXToDataMap.end()) {
69  // the dataname is already mapped, so update it
70  // and remove corresponding map entry in opposite direction
71  auto dataToAX = mDataToAXMap.find(axToData->second);
72  if (dataToAX != mDataToAXMap.end()) {
73  mAXToDataMap.erase(dataToAX->second);
74  mDataToAXMap.erase(dataToAX->first);
75  }
76  }
77  auto dataToAX = mDataToAXMap.find(dataname);
78  if (dataToAX != mDataToAXMap.end()) {
79  mAXToDataMap.erase(dataToAX->second);
80  }
81 
82  mAXToDataMap[axname] = dataname;
83  mDataToAXMap[dataname] = axname;
84  }
85 
86  /// @brief Set up multiple bindings from a vector of {ax name, data name} pairs.
87  /// If a data binding exists for any AX name, it will be replaced.
88  /// If another binding exists for the supplied dataname that will be removed.
89  /// @param bindings Vector of AX name data name pairs
90  inline void set(const std::vector<std::pair<std::string, std::string>>& bindings) {
91  for (const auto& binding : bindings) {
92  this->set(binding.first, binding.second);
93  }
94  }
95  /// @brief Returns a pointer to the data attribute name string that the input AX attribute name
96  /// is bound to, or nullptr if unbound.
97  /// @param axname The name of the attribute in AX
98  inline const std::string* dataNameBoundTo(const std::string& axname) const
99  {
100  const auto iter = mAXToDataMap.find(axname);
101  if (iter != mAXToDataMap.cend()) {
102  return &iter->second;
103  }
104  return nullptr;
105  }
106 
107  /// @brief Returns a pointer to the AX attribute name string that a data attribute name
108  /// is bound to, or nullptr if unbound.
109  /// @param name The name of the attribute in the context data
110  inline const std::string* axNameBoundTo(const std::string& name) const
111  {
112  const auto iter = mDataToAXMap.find(name);
113  if (iter != mDataToAXMap.cend()) {
114  return &iter->second;
115  }
116  return nullptr;
117  }
118 
119  /// @brief Returns whether the data attribute has been bound to an AX attribute
120  /// @param name The name of the attribute in the context data
121  inline bool isBoundDataName(const std::string& name) const
122  {
123  return mDataToAXMap.count(name);
124  }
125 
126  /// @brief Returns whether the AX attribute has been bound to a data attribute
127  /// @param name The name of the attribute in AX
128  inline bool isBoundAXName(const std::string& name) const
129  {
130  return mAXToDataMap.count(name);
131  }
132 
133  /// @brief Returns the map of AX attribute names to data attribute names
134  inline const std::map<std::string, std::string>& axToDataMap() const {
135  return mAXToDataMap;
136  }
137 
138  /// @brief Returns the map of data attribute names to AX attribute names
139  inline const std::map<std::string, std::string>& dataToAXMap() const {
140  return mDataToAXMap;
141  }
142 
143 private:
144 
145  std::map<std::string, std::string> mAXToDataMap;
146  std::map<std::string, std::string> mDataToAXMap;
147 };
148 
149 
150 } // namespace ax
151 } // namespace OPENVDB_VERSION_NAME
152 } // namespace openvdb
153 
154 #endif // OPENVDB_AX_COMPILER_ATTRIBUTE_BINDINGS_HAS_BEEN_INCLUDED
155 
const std::map< std::string, std::string > & axToDataMap() const
Returns the map of AX attribute names to data attribute names.
Definition: AttributeBindings.h:134
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:110
const std::map< std::string, std::string > & dataToAXMap() const
Returns the map of data attribute names to AX attribute names.
Definition: AttributeBindings.h:139
bool isBoundAXName(const std::string &name) const
Returns whether the AX attribute has been bound to a data attribute.
Definition: AttributeBindings.h:128
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:98
bool isBoundDataName(const std::string &name) const
Returns whether the data attribute has been bound to an AX attribute.
Definition: AttributeBindings.h:121
Definition: Exceptions.h:13
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
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
This class wraps an interface for a map of attribute bindings. These map attributes in AX code to con...
Definition: AttributeBindings.h:36
#define OPENVDB_VERSION_NAME
The version namespace name for this library version.
Definition: version.h.in:116
#define OPENVDB_USE_VERSION_NAMESPACE
Definition: version.h.in:202