Added variadic constructors

This commit is contained in:
John Wellbelove 2025-03-01 13:39:51 +00:00
parent 5327b5584b
commit 5d6771f607
4 changed files with 149 additions and 0 deletions

View File

@ -629,6 +629,20 @@ namespace etl
this->assign(first, last);
}
#if ETL_USING_CPP11
//*************************************************************************
/// Constructor from variadic list of nodes.
//*************************************************************************
template <typename... TLinks>
intrusive_forward_list(link_type& first, TLinks&... links)
{
current_size = 0;
this->start.etl_next = &first;
link_type* last = make_linked_list(current_size, first, static_cast<link_type&>(links)...);
last->etl_next = &this->terminator;
}
#endif
//*************************************************************************
/// Gets the beginning of the intrusive_forward_list.
//*************************************************************************
@ -1192,6 +1206,41 @@ namespace etl
private:
#if ETL_USING_CPP17
//***************************************************************************
/// Create a linked list from a number of forward_link nodes.
//***************************************************************************
template <typename TLink, typename... TLinks>
TLink* make_linked_list(size_t& count, TLink& first, TLinks&... links)
{
TLink* current = &first;
++count;
((current->etl_next = &links, current = &links, ++count), ...);
return current;
}
#elif ETL_USING_CPP11
//***************************************************************************
/// Create a counted linked list from a number of forward_link nodes.
//***************************************************************************
link_type* make_linked_list(size_t& count, link_type& first)
{
++count;
return &first;
}
//***************************************************************************
/// Create a counted linked list from a number of forward_link nodes.
//***************************************************************************
template <typename... TLinks>
link_type* make_linked_list(size_t& count, link_type& first, link_type& next, TLinks&... links)
{
++count;
first.etl_next = &next;
return make_linked_list(count, next, static_cast<link_type&>(links)...);
}
#endif
//*************************************************************************
/// Get the next value.
//*************************************************************************

View File

@ -685,6 +685,22 @@ namespace etl
this->assign(first, last);
}
#if ETL_USING_CPP11
//*************************************************************************
/// Constructor from variadic list of nodes.
//*************************************************************************
template <typename... TLinks>
intrusive_list(link_type& first, TLinks&... links)
{
current_size = 0;
this->terminal_link.etl_next = &first;
link_type* last = make_linked_list(current_size, first, static_cast<link_type&>(links)...);
first.etl_previous = &this->terminal_link;
last->etl_next = &this->terminal_link;
this->terminal_link.etl_previous = last;
}
#endif
//*************************************************************************
/// Gets the beginning of the intrusive_list.
//*************************************************************************
@ -1194,6 +1210,44 @@ namespace etl
private:
#if ETL_USING_CPP17
//***************************************************************************
/// Create a linked list from a number of bidirectional_link nodes.
//***************************************************************************
template <typename... TLinks>
link_type* make_linked_list(size_t& count, link_type& first, TLinks&... links)
{
TLink* current = &first;
++count;
((current->etl_next = &links, static_cast<TLink&>(links).etl_previous = current, current = &links, ++count), ...);
return current;
}
#elif ETL_USING_CPP11
//***************************************************************************
/// Create a linked list from a number of bidirectional_link nodes.
//***************************************************************************
link_type* make_linked_list(size_t& count, link_type& first)
{
++count;
return &first;
}
//***************************************************************************
/// Create a linked list from a number of bidirectional_link nodes.
//***************************************************************************
template <typename... TLinks>
link_type* make_linked_list(size_t& count, link_type& first, link_type& next, TLinks&... links)
{
++count;
first.etl_next = &next;
next.etl_previous = &first;
return make_linked_list(count, next, static_cast<link_type&>(links)...);
}
#endif
// Disabled.
intrusive_list(const intrusive_list& other);
intrusive_list& operator = (const intrusive_list& rhs);

View File

@ -183,6 +183,29 @@ namespace
CHECK_EQUAL(sorted_data.size(), data0.size());
}
//*************************************************************************
TEST_FIXTURE(SetupFixture, test_constructor_variadic_list_single)
{
DataNDC0 data0(sorted_data[0]);
CHECK(!data0.empty());
CHECK_EQUAL(1, data0.size());
CHECK_EQUAL(sorted_data[0], data0.front());
}
//*************************************************************************
TEST_FIXTURE(SetupFixture, test_constructor_variadic_list_multiple)
{
DataNDC0 data0(sorted_data[0], sorted_data[1], sorted_data[2], sorted_data[3], sorted_data[4],
sorted_data[5], sorted_data[6], sorted_data[7], sorted_data[8], sorted_data[9]);
CHECK(!data0.empty());
CHECK_EQUAL(10, data0.size());
bool are_equal = std::equal(data0.begin(), data0.end(), sorted_data.begin());
CHECK(are_equal);
}
//*************************************************************************
TEST_FIXTURE(SetupFixture, test_empty_begin_end)
{

View File

@ -196,6 +196,29 @@ namespace
CHECK_EQUAL(sorted_data.size(), data0.size());
}
//*************************************************************************
TEST_FIXTURE(SetupFixture, test_constructor_variadic_list_single)
{
DataNDC0 data0(sorted_data[0]);
CHECK(!data0.empty());
CHECK_EQUAL(1, data0.size());
CHECK_EQUAL(sorted_data[0], data0.front());
}
//*************************************************************************
TEST_FIXTURE(SetupFixture, test_constructor_variadic_list_multiple)
{
DataNDC0 data0(sorted_data[0], sorted_data[1], sorted_data[2], sorted_data[3], sorted_data[4],
sorted_data[5], sorted_data[6], sorted_data[7], sorted_data[8], sorted_data[9]);
CHECK(!data0.empty());
CHECK_EQUAL(10, data0.size());
bool are_equal = std::equal(data0.begin(), data0.end(), sorted_data.begin());
CHECK(are_equal);
}
//*************************************************************************
TEST_FIXTURE(SetupFixture, test_empty_begin_end)
{