The open source OpenXR runtime
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

u/json: Implement rule of 5 in JSONNode

`operator=(JSONNode)` and `operator=(JSONNode &&)` conflict with each other
resulting in neither, and particularly the move operator, being usable.

Implement the missing traditional operators instead:
- `JSONNode(const JSONNode&)`,
- `operator=(const JSONNode&)`, and
- `operator=(JSONNode&&)`

Part-of: <https://gitlab.freedesktop.org/monado/monado/-/merge_requests/2426>

averyv c53a6904 06c75645

+51 -9
+17 -9
src/xrt/auxiliary/util/u_json.hpp
··· 114 114 parent = nullptr; 115 115 } 116 116 117 - JSONNode(JSONNode &&) = default; 117 + JSONNode(JSONNode &&node) 118 + { 119 + *this = std::move(node); 120 + } 118 121 119 122 JSONNode(const JSONNode &node) 120 123 { 124 + *this = node; 125 + }; 126 + 127 + JSONNode & 128 + operator=(JSONNode &&rhs) 129 + { 130 + swap(*this, rhs); 131 + return *this; 132 + } 133 + 134 + JSONNode & 135 + operator=(const JSONNode &node) 136 + { 121 137 is_owner = node.is_owner; 122 138 parent = node.parent; 123 139 if (node.is_owner) { ··· 125 141 } else { 126 142 cjson = node.cjson; // Shallow copy 127 143 } 128 - }; 129 144 130 - JSONNode & 131 - operator=(JSONNode &&) = default; 132 - 133 - JSONNode & 134 - operator=(JSONNode rhs) 135 - { 136 - swap(*this, rhs); 137 145 return *this; 138 146 }; 139 147
+34
tests/tests_json.cpp
··· 147 147 CHECK(stringToDouble.isDouble()); 148 148 CHECK(stringToDouble.asDouble() == Catch::Approx(0.5).margin(e)); 149 149 } 150 + 151 + SECTION("Copy construction duplicates the JSON") 152 + { 153 + JSONNode copy{json_node}; 154 + CHECK(copy.getCJSON() != json_node.getCJSON()); 155 + CHECK(copy.toString(false) == json_node.toString(false)); 156 + } 157 + 158 + SECTION("Copy assignment duplicates the JSON") 159 + { 160 + JSONNode copy; 161 + copy = json_node; 162 + CHECK(copy.getCJSON() != json_node.getCJSON()); 163 + CHECK(copy.toString(false) == json_node.toString(false)); 164 + } 165 + 166 + SECTION("Move construction doesn't duplicate the JSON") 167 + { 168 + JSONNode temp{json_node}; 169 + const auto cjson = temp.getCJSON(); 170 + JSONNode moved{std::move(temp)}; 171 + CHECK(moved.getCJSON() == cjson); 172 + CHECK(moved.toString(false) == json_node.toString(false)); 173 + } 174 + 175 + SECTION("Move assignment doesn't duplicate the JSON") 176 + { 177 + JSONNode temp{json_node}; 178 + const auto cjson = temp.getCJSON(); 179 + JSONNode moved; 180 + moved = std::move(temp); 181 + CHECK(moved.getCJSON() == cjson); 182 + CHECK(moved.toString(false) == json_node.toString(false)); 183 + } 150 184 }