Line | Branch | Exec | Source |
---|---|---|---|
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 { | ||
22 | OPENVDB_USE_VERSION_NAMESPACE | ||
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 | ||
36 | class AttributeBindings | ||
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 | 1 | AttributeBindings(const std::vector<std::pair<std::string, std::string>>& bindings) | |
46 |
1/2✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
|
1 | { |
47 |
1/2✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
|
1 | set(bindings); |
48 | 1 | } | |
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 | 7 | AttributeBindings(const std::initializer_list<std::pair<std::string, std::string>>& bindings) | |
55 |
1/2✓ Branch 1 taken 7 times.
✗ Branch 2 not taken.
|
7 | { |
56 |
2/4✓ Branch 1 taken 7 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 7 times.
✗ Branch 5 not taken.
|
7 | set(bindings); |
57 | 7 | } | |
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 | 10222 | inline void set(const std::string& axname, const std::string& dataname) | |
66 | { | ||
67 |
2/2✓ Branch 0 taken 36 times.
✓ Branch 1 taken 10186 times.
|
10222 | auto axToData = mAXToDataMap.find(axname); |
68 |
2/2✓ Branch 0 taken 36 times.
✓ Branch 1 taken 10186 times.
|
10222 | if (axToData != mAXToDataMap.end()) { |
69 | // the dataname is already mapped, so update it | ||
70 | // and remove corresponding map entry in opposite direction | ||
71 |
1/2✓ Branch 1 taken 36 times.
✗ Branch 2 not taken.
|
36 | auto dataToAX = mDataToAXMap.find(axToData->second); |
72 |
1/2✓ Branch 0 taken 36 times.
✗ Branch 1 not taken.
|
36 | if (dataToAX != mDataToAXMap.end()) { |
73 | 36 | mAXToDataMap.erase(dataToAX->second); | |
74 | 36 | mDataToAXMap.erase(dataToAX->first); | |
75 | } | ||
76 | } | ||
77 |
2/2✓ Branch 0 taken 12 times.
✓ Branch 1 taken 10210 times.
|
10222 | auto dataToAX = mDataToAXMap.find(dataname); |
78 |
2/2✓ Branch 0 taken 12 times.
✓ Branch 1 taken 10210 times.
|
10222 | if (dataToAX != mDataToAXMap.end()) { |
79 | 12 | mAXToDataMap.erase(dataToAX->second); | |
80 | } | ||
81 | |||
82 | 10222 | mAXToDataMap[axname] = dataname; | |
83 | 10222 | mDataToAXMap[dataname] = axname; | |
84 | 10222 | } | |
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 | 8 | inline void set(const std::vector<std::pair<std::string, std::string>>& bindings) { | |
91 |
2/2✓ Branch 0 taken 10 times.
✓ Branch 1 taken 8 times.
|
18 | for (const auto& binding : bindings) { |
92 | 10 | this->set(binding.first, binding.second); | |
93 | } | ||
94 | 8 | } | |
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 |
15/24✓ Branch 0 taken 1 times.
✓ Branch 1 taken 5073 times.
✓ Branch 2 taken 3 times.
✓ Branch 3 taken 824 times.
✓ Branch 4 taken 1 times.
✓ Branch 5 taken 5089 times.
✓ Branch 6 taken 1 times.
✗ Branch 7 not taken.
✓ Branch 8 taken 1 times.
✗ Branch 9 not taken.
✗ Branch 10 not taken.
✓ Branch 11 taken 1 times.
✓ Branch 12 taken 1 times.
✗ Branch 13 not taken.
✓ Branch 14 taken 1 times.
✗ Branch 15 not taken.
✓ Branch 16 taken 1 times.
✗ Branch 17 not taken.
✓ Branch 18 taken 1 times.
✗ Branch 19 not taken.
✓ Branch 20 taken 1 times.
✗ Branch 21 not taken.
✓ Branch 22 taken 1 times.
✗ Branch 23 not taken.
|
11000 | const auto iter = mAXToDataMap.find(axname); |
101 |
15/24✓ Branch 0 taken 1 times.
✓ Branch 1 taken 5073 times.
✓ Branch 2 taken 3 times.
✓ Branch 3 taken 824 times.
✓ Branch 4 taken 1 times.
✓ Branch 5 taken 5089 times.
✓ Branch 6 taken 1 times.
✗ Branch 7 not taken.
✓ Branch 8 taken 1 times.
✗ Branch 9 not taken.
✗ Branch 10 not taken.
✓ Branch 11 taken 1 times.
✓ Branch 12 taken 1 times.
✗ Branch 13 not taken.
✓ Branch 14 taken 1 times.
✗ Branch 15 not taken.
✓ Branch 16 taken 1 times.
✗ Branch 17 not taken.
✓ Branch 18 taken 1 times.
✗ Branch 19 not taken.
✓ Branch 20 taken 1 times.
✗ Branch 21 not taken.
✓ Branch 22 taken 1 times.
✗ Branch 23 not taken.
|
11000 | if (iter != mAXToDataMap.cend()) { |
102 |
3/6✗ Branch 0 not taken.
✓ Branch 1 taken 15 times.
✓ Branch 2 taken 824 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 5089 times.
|
10997 | 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 |
13/26✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 1 times.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✓ Branch 7 taken 1 times.
✓ Branch 8 taken 1 times.
✗ Branch 9 not taken.
✓ Branch 10 taken 1 times.
✗ Branch 11 not taken.
✓ Branch 12 taken 1 times.
✗ Branch 13 not taken.
✗ Branch 14 not taken.
✓ Branch 15 taken 1 times.
✓ Branch 16 taken 1 times.
✗ Branch 17 not taken.
✓ Branch 18 taken 1 times.
✗ Branch 19 not taken.
✓ Branch 20 taken 1 times.
✗ Branch 21 not taken.
✓ Branch 22 taken 1 times.
✗ Branch 23 not taken.
✓ Branch 24 taken 1 times.
✗ Branch 25 not taken.
|
15 | const auto iter = mDataToAXMap.find(name); |
113 |
13/26✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 1 times.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✓ Branch 7 taken 1 times.
✓ Branch 8 taken 1 times.
✗ Branch 9 not taken.
✓ Branch 10 taken 1 times.
✗ Branch 11 not taken.
✓ Branch 12 taken 1 times.
✗ Branch 13 not taken.
✗ Branch 14 not taken.
✓ Branch 15 taken 1 times.
✓ Branch 16 taken 1 times.
✗ Branch 17 not taken.
✓ Branch 18 taken 1 times.
✗ Branch 19 not taken.
✓ Branch 20 taken 1 times.
✗ Branch 21 not taken.
✓ Branch 22 taken 1 times.
✗ Branch 23 not taken.
✓ Branch 24 taken 1 times.
✗ Branch 25 not taken.
|
15 | if (iter != mDataToAXMap.cend()) { |
114 | 13 | 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 |
3/6✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 1 times.
✗ Branch 5 not taken.
✓ Branch 7 taken 1 times.
✗ Branch 8 not taken.
|
3 | 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 |
2/4✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 1 times.
✗ Branch 5 not taken.
|
2 | 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 | |||
156 |