Skip to content

Commit 6f25613

Browse files
committed
feat(core): add set_current_to_first method to ContainerGroup
1 parent 9aef1d2 commit 6f25613

3 files changed

Lines changed: 50 additions & 0 deletions

File tree

‎Meta/include/meta/core/container_group.hpp‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,12 @@ class ContainerGroup : public MetaObject
9292
*/
9393
void set_current(const std::string &key);
9494

95+
/**
96+
* @brief Set the active container to the first container in the list.
97+
* @throws std::runtime_error if there are no containers in the group.
98+
*/
99+
void set_current_to_first();
100+
95101
/// Clear all containers.
96102
void clear();
97103

‎Meta/src/container_group.cpp‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -246,6 +246,19 @@ void ContainerGroup::set_current(const std::string &key)
246246
Logger::log()->trace("ContainerGroup::set_current: success = {}", key);
247247
}
248248

249+
void ContainerGroup::set_current_to_first()
250+
{
251+
Logger::log()->trace("ContainerGroup::set_current_to_first");
252+
253+
if (insertion_order_.empty())
254+
{
255+
Logger::log()->trace("ContainerGroup::set_current_to_first: no containers");
256+
throw std::runtime_error("No containers in group");
257+
}
258+
259+
set_current(insertion_order_.front());
260+
}
261+
249262
void ContainerGroup::clear()
250263
{
251264
Logger::log()->trace("ContainerGroup::clear");

‎tests/unittests/test_container_group_sync.cpp‎

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -274,3 +274,34 @@ TEST(ContainerGroupSyncTest, JsonRoundTripPreservesSync)
274274
EXPECT_FLOAT_EQ(dst_c1->value<float>("scale"), 88.0f);
275275
EXPECT_FLOAT_EQ(dst_c2->value<float>("scale"), 88.0f);
276276
}
277+
278+
TEST(ContainerGroupSyncTest, SetCurrentToFirst)
279+
{
280+
meta::ContainerGroup group;
281+
282+
// Expect exception when group is empty
283+
EXPECT_THROW(group.set_current_to_first(), std::runtime_error);
284+
285+
group.add("Feature1");
286+
group.add("Feature2");
287+
group.add("Feature3");
288+
289+
// Initial current is Feature1
290+
EXPECT_EQ(group.current_container_name(), "Feature1");
291+
292+
// Switch to Feature3
293+
group.set_current("Feature3");
294+
EXPECT_EQ(group.current_container_name(), "Feature3");
295+
296+
// Reset to first
297+
group.set_current_to_first();
298+
EXPECT_EQ(group.current_container_name(), "Feature1");
299+
300+
// Erase the first one, now Feature2 is first
301+
group.erase("Feature1");
302+
group.set_current("Feature3");
303+
EXPECT_EQ(group.current_container_name(), "Feature3");
304+
305+
group.set_current_to_first();
306+
EXPECT_EQ(group.current_container_name(), "Feature2");
307+
}

0 commit comments

Comments
 (0)