Commit 4db37327 by Maarten L. Hekkelman

move construction and operators =

parent 07131e8b
...@@ -147,8 +147,15 @@ class category_t ...@@ -147,8 +147,15 @@ class category_t
insert_impl(cend(), clone_row(*r)); insert_impl(cend(), clone_row(*r));
} }
category_t(category_t &&) category_t(category_t &&rhs)
: m_allocator(std::move(rhs.m_allocator))
, m_name(std::move(rhs.m_name))
, m_columns(std::move(rhs.m_columns))
, m_head(rhs.m_head)
, m_tail(rhs.m_tail)
{ {
rhs.m_head = nullptr;
rhs.m_tail = nullptr;
} }
template <typename Alloc2> template <typename Alloc2>
...@@ -165,31 +172,47 @@ class category_t ...@@ -165,31 +172,47 @@ class category_t
{ {
} }
category_t &operator=(const category_t &) category_t &operator=(const category_t &rhs)
{ {
if (this != &rhs)
{
if (not empty())
clear();
m_allocator = std::allocator_traits<allocator_type>::select_on_container_copy_construction(rhs.get_allocator());
m_name = rhs.m_name;
m_columns = rhs.m_columns;
for (auto r = rhs.m_head; r != nullptr; r = r->m_next)
insert_impl(cend(), clone_row(*r));
} }
category_t &operator=(category_t &&) return *this;
{
} }
~category_t() category_t &operator=(category_t &&rhs)
{
auto r = m_head;
while (r != nullptr)
{ {
auto i = r->m_head; if (this != &rhs)
while (i != nullptr)
{ {
auto ti = i->m_next; if (not empty())
delete_item(i); clear();
i = ti;
m_allocator = std::move(rhs.m_allocator);
m_name = std::move(rhs.m_name);
m_columns = std::move(rhs.m_columns);
m_head = rhs.m_head;
m_tail = rhs.m_tail;
rhs.m_head = rhs.m_tail = nullptr;
} }
auto t = r->m_next; return *this;
delete_row(r);
r = t;
} }
~category_t()
{
clear();
} }
// -------------------------------------------------------------------- // --------------------------------------------------------------------
...@@ -365,6 +388,19 @@ class category_t ...@@ -365,6 +388,19 @@ class category_t
// mIndex->insert(nr); // mIndex->insert(nr);
} }
void clear()
{
auto i = m_head;
while (i != nullptr)
{
auto t = i;
i = i->m_next;
delete_row(t);
}
m_head = m_tail = nullptr;
}
// -------------------------------------------------------------------- // --------------------------------------------------------------------
/// \brief Return the index number for \a column_name /// \brief Return the index number for \a column_name
...@@ -508,10 +544,21 @@ class category_t ...@@ -508,10 +544,21 @@ class category_t
void delete_row(row *r) void delete_row(row *r)
{ {
if (r != nullptr)
{
auto i = r->m_head;
while (i != nullptr)
{
auto t = i;
i = i->m_next;
delete_item(t);
}
row_allocator_type ra(get_allocator()); row_allocator_type ra(get_allocator());
row_allocator_traits::destroy(ra, r); row_allocator_traits::destroy(ra, r);
row_allocator_traits::deallocate(ra, r, 1); row_allocator_traits::deallocate(ra, r, 1);
} }
}
struct item_column struct item_column
{ {
......
...@@ -221,6 +221,23 @@ BOOST_AUTO_TEST_CASE(c_2) ...@@ -221,6 +221,23 @@ BOOST_AUTO_TEST_CASE(c_2)
BOOST_CHECK(not c2.empty()); BOOST_CHECK(not c2.empty());
BOOST_CHECK_EQUAL(c2.size(), 3); BOOST_CHECK_EQUAL(c2.size(), 3);
cif::v2::category c3(std::move(c));
BOOST_CHECK(not c3.empty());
BOOST_CHECK_EQUAL(c3.size(), 3);
BOOST_CHECK(c.empty());
BOOST_CHECK_EQUAL(c.size(), 0);
c = c3;
BOOST_CHECK(not c.empty());
BOOST_CHECK_EQUAL(c.size(), 3);
c = std::move(c2);
BOOST_CHECK(not c.empty());
BOOST_CHECK_EQUAL(c.size(), 3);
} }
// -------------------------------------------------------------------- // --------------------------------------------------------------------
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment