summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDan Goodliffe <dan@randomdan.homeip.net>2025-03-23 13:56:24 +0000
committerDan Goodliffe <dan@randomdan.homeip.net>2025-03-23 13:56:24 +0000
commit422b466caa4bcd4b30f08d9a24535dad8ed20f0d (patch)
treeb2a7f431b874b947515b58206168ccbfd33085fd
parentPopulate typed collection of pointers (diff)
downloadilt-422b466caa4bcd4b30f08d9a24535dad8ed20f0d.tar.bz2
ilt-422b466caa4bcd4b30f08d9a24535dad8ed20f0d.tar.xz
ilt-422b466caa4bcd4b30f08d9a24535dad8ed20f0d.zip
Other objects support in operator=
-rw-r--r--lib/collection.h8
-rw-r--r--test/test-collection.cpp65
2 files changed, 71 insertions, 2 deletions
diff --git a/lib/collection.h b/lib/collection.h
index e68a8db..329b681 100644
--- a/lib/collection.h
+++ b/lib/collection.h
@@ -18,6 +18,10 @@ public:
operator=(Objects && other)
{
objects = std::move(other);
+ ((std::get<OtherObjects<Others>>(otherObjects).clear()), ...);
+ for (const auto & other : objects) {
+ addOthersPtr(other.get());
+ }
return *this;
}
@@ -138,10 +142,10 @@ public:
return objects.empty();
}
- auto
+ decltype(auto)
emplace(Ptr && ptr)
{
- auto object = objects.emplace_back(std::move(ptr));
+ const auto & object = objects.emplace_back(std::move(ptr));
addOthersPtr(object.get());
return object;
}
diff --git a/test/test-collection.cpp b/test/test-collection.cpp
index 5c67a8c..0cd08d5 100644
--- a/test/test-collection.cpp
+++ b/test/test-collection.cpp
@@ -43,6 +43,7 @@ BOOST_FIXTURE_TEST_SUITE(tc, TestCollection)
BOOST_AUTO_TEST_CASE(empty)
{
+ BOOST_CHECK(TestCollection::empty());
BOOST_REQUIRE(!apply(&Base::add));
const auto i = applyOne(&Base::add);
BOOST_CHECK_EQUAL(i, end());
@@ -90,6 +91,70 @@ BOOST_AUTO_TEST_CASE(a_sub)
BOOST_CHECK_EQUAL(*i, s);
}
+BOOST_AUTO_TEST_CASE(begin_end)
+{
+ BOOST_CHECK_EQUAL(0, std::distance(begin(), end()));
+ create<Sub>();
+ create<Base>();
+ BOOST_CHECK_EQUAL(2, std::distance(begin(), end()));
+}
+
+BOOST_AUTO_TEST_CASE(rbegin_rend)
+{
+ BOOST_CHECK_EQUAL(0, std::distance(rbegin(), rend()));
+ create<Sub>();
+ create<Base>();
+ BOOST_CHECK_EQUAL(2, std::distance(rbegin(), rend()));
+}
+
+BOOST_AUTO_TEST_SUITE_END()
+
+using TestUniqueCollection = UniqueCollection<Base, Sub>;
+BOOST_TEST_DONT_PRINT_LOG_VALUE(TestUniqueCollection::Objects::const_iterator)
+BOOST_TEST_DONT_PRINT_LOG_VALUE(TestUniqueCollection::Objects::const_reverse_iterator)
+
+BOOST_FIXTURE_TEST_SUITE(utc, TestUniqueCollection)
+
+BOOST_AUTO_TEST_CASE(unique_create)
+{
+ create<Base>();
+ BOOST_CHECK_EQUAL(objects.size(), 1);
+ BOOST_CHECK(std::get<OtherObjects<Sub>>(otherObjects).empty());
+ create<Sub>();
+ BOOST_CHECK_EQUAL(objects.size(), 2);
+ BOOST_CHECK_EQUAL(std::get<OtherObjects<Sub>>(otherObjects).size(), 1);
+}
+
+BOOST_AUTO_TEST_CASE(move_assign)
+{
+ create<Base>();
+ create<Sub>();
+
+ TestUniqueCollection::Objects other;
+ TestUniqueCollection::operator=(std::move(other));
+ BOOST_CHECK(objects.empty());
+ BOOST_CHECK(std::get<OtherObjects<Sub>>(otherObjects).empty());
+
+ other.push_back(std::make_unique<Sub>());
+ other.push_back(std::make_unique<Base>());
+ TestUniqueCollection::operator=(std::move(other));
+ BOOST_CHECK_EQUAL(objects.size(), 2);
+ BOOST_CHECK_EQUAL(std::get<OtherObjects<Sub>>(otherObjects).size(), 1);
+ BOOST_CHECK(other.empty());
+}
+
+BOOST_AUTO_TEST_SUITE_END()
+
+BOOST_FIXTURE_TEST_SUITE(btc, UniqueCollection<Base>)
+
+BOOST_AUTO_TEST_CASE(no_others)
+{
+ create<Base>();
+ create<Sub>();
+ emplace(std::make_unique<Base>());
+ emplace(std::make_unique<Sub>());
+}
+
BOOST_AUTO_TEST_SUITE_END()
BOOST_AUTO_TEST_CASE(wrapped_ptr_file_cons)