Skip to content

Commit 29e2f35

Browse files
committed
Container/MeshNodePtr: Branch::operator[]
1 parent 0d52e19 commit 29e2f35

2 files changed

Lines changed: 26 additions & 26 deletions

File tree

modules/Container/MeshNetwork.mpp

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,28 @@ export namespace CppUtils::Container
148148
node2Accessor.operator->()->detachChild(node1.get(), node1NetworkPtr);
149149
}
150150

151+
[[nodiscard]] auto operator[](std::size_t index) const -> std::expected<MeshNodePtr, std::string_view>
152+
{
153+
using namespace std::literals;
154+
155+
if (not m_self)
156+
return std::unexpected{"Invalid MeshNodePtr"sv};
157+
auto sharedLocker = m_self->sharedAccess();
158+
const auto& branches = sharedLocker->value.branches;
159+
if (auto it = branches.find(m_key); it != std::cend(branches))
160+
{
161+
const auto& children = it->second;
162+
if (index < std::size(children))
163+
if (auto sharedChild = children[index].lock())
164+
return MeshNodePtr{sharedChild};
165+
else
166+
return std::unexpected{"Failed to lock weak pointer for branch. Node might have been deleted"sv};
167+
else
168+
return std::unexpected{"Branch index out of bounds"sv};
169+
}
170+
return std::unexpected{"Branch key not found"sv};
171+
}
172+
151173
private:
152174
SharedPtr& m_self;
153175
const Key& m_key;
@@ -202,28 +224,6 @@ export namespace CppUtils::Container
202224
return sharedLocker->value.value;
203225
}
204226

205-
[[nodiscard]] auto getNode(const Key& key, std::size_t index = 0) const -> std::expected<MeshNodePtr, std::string_view>
206-
{
207-
using namespace std::literals;
208-
209-
if (not m_node)
210-
return std::unexpected{"Invalid MeshNodePtr"sv};
211-
auto sharedLocker = m_node->sharedAccess();
212-
const auto& branches = sharedLocker->value.branches;
213-
if (auto it = branches.find(key); it != std::cend(branches))
214-
{
215-
const auto& children = it->second;
216-
if (index < std::size(children))
217-
if (auto sharedChild = children[index].lock())
218-
return MeshNodePtr{sharedChild};
219-
else
220-
return std::unexpected{"Failed to lock weak pointer for branch. Node might have been deleted"sv};
221-
else
222-
return std::unexpected{"Branch index out of bounds"sv};
223-
}
224-
return std::unexpected{"Branch key not found"sv};
225-
}
226-
227227
[[nodiscard]] auto getNodes(const Key& key) const -> std::expected<std::vector<MeshNodePtr>, std::string_view>
228228
{
229229
using namespace std::literals;

tests/Container/MeshNetwork.mpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -390,7 +390,7 @@ export namespace CppUtils::UnitTest::Container::MeshNetwork
390390
auto child = StringMeshNodePtr::make("child");
391391
root["Children"] >> child;
392392

393-
auto childExpected = root.getNode("Children", 0);
393+
auto childExpected = root["Children"][0];
394394

395395
suite.expect(childExpected.has_value());
396396
suite.expectEqual(childExpected.value().getValue().value(), "child");
@@ -401,7 +401,7 @@ export namespace CppUtils::UnitTest::Container::MeshNetwork
401401
auto child = StringMeshNodePtr::make("child");
402402
root["Children"] >> child;
403403

404-
auto invalidKeyExpected = root.getNode("NonExistentBranch");
404+
auto invalidKeyExpected = root["NonExistentBranch"][0];
405405

406406
suite.expect(not invalidKeyExpected.has_value());
407407
suite.expectEqual(invalidKeyExpected.error(), "Branch key not found"sv);
@@ -412,7 +412,7 @@ export namespace CppUtils::UnitTest::Container::MeshNetwork
412412
auto child = StringMeshNodePtr::make("child");
413413
root["Children"] >> child;
414414

415-
auto invalidIndexExpected = root.getNode("Children", 1);
415+
auto invalidIndexExpected = root["Children"][1];
416416

417417
suite.expect(not invalidIndexExpected.has_value());
418418
suite.expectEqual(invalidIndexExpected.error(), "Branch index out of bounds"sv);
@@ -421,7 +421,7 @@ export namespace CppUtils::UnitTest::Container::MeshNetwork
421421
suite.addTest("Get node: Invalid node", [&] {
422422
auto emptyNode = StringMeshNodePtr{};
423423

424-
auto emptyBranchExpected = emptyNode.getNode("Branch");
424+
auto emptyBranchExpected = emptyNode["Branch"][0];
425425

426426
suite.expect(not emptyBranchExpected.has_value());
427427
suite.expectEqual(emptyBranchExpected.error(), "Invalid MeshNodePtr"sv);

0 commit comments

Comments
 (0)