Added interface move assignment.

This commit is contained in:
John Wellbelove 2019-01-01 14:02:43 +00:00
parent 48aabaad54
commit 6b165aee54
2 changed files with 64 additions and 7 deletions

View File

@ -40,7 +40,6 @@ SOFTWARE.
#include "platform.h"
#include "algorithm.h"
#include "type_traits.h"
#include "parameter_type.h"
#include "error_handler.h"
#include "memory.h"
#include "container.h"
@ -86,6 +85,7 @@ namespace etl
typedef T value_type;
typedef T& reference;
typedef const T& const_reference;
typedef T&& rvalue_reference;
typedef T* pointer;
typedef const T* const_pointer;
typedef T* iterator;
@ -405,7 +405,7 @@ namespace etl
/// If asserts or exceptions are enabled, emits vector_full if the vector is already full.
///\param value The value to add.
//*********************************************************************
void push_back(const T& value)
void push_back(const_reference value)
{
#if defined(ETL_CHECK_PUSH_POP)
ETL_ASSERT(size() != CAPACITY, ETL_ERROR(vector_full));
@ -419,7 +419,7 @@ namespace etl
/// If asserts or exceptions are enabled, emits vector_full if the vector is already full.
///\param value The value to add.
//*********************************************************************
void push_back(T&& value)
void push_back(rvalue_reference value)
{
#if defined(ETL_CHECK_PUSH_POP)
ETL_ASSERT(size() != CAPACITY, ETL_ERROR(vector_full));
@ -528,7 +528,7 @@ namespace etl
///\param position The position to insert before.
///\param value The value to insert.
//*********************************************************************
iterator insert(iterator position, const T& value)
iterator insert(iterator position, const_reference value)
{
ETL_ASSERT(size() + 1 <= CAPACITY, ETL_ERROR(vector_full));
@ -553,7 +553,7 @@ namespace etl
///\param position The position to insert before.
///\param value The value to insert.
//*********************************************************************
iterator insert(iterator position, T&& value)
iterator insert(iterator position, rvalue_reference value)
{
ETL_ASSERT(size() + 1 <= CAPACITY, ETL_ERROR(vector_full));
@ -866,6 +866,29 @@ namespace etl
return *this;
}
#if ETL_CPP11_SUPPORTED
//*************************************************************************
/// Move assignment operator.
//*************************************************************************
ivector& operator = (ivector&& rhs)
{
if (&rhs != this)
{
clear();
iterator itr = rhs.begin();
while (itr != rhs.end())
{
push_back(std::move(*itr));
++itr;
}
rhs.initialise();
}
return *this;
}
#endif
//*************************************************************************
/// Gets the current size of the vector.
///\return The current size of the vector.
@ -961,7 +984,7 @@ namespace etl
//*********************************************************************
/// Create a new element with a value at the back
//*********************************************************************
inline void create_back(const T& value)
inline void create_back(const_reference value)
{
etl::create_copy_at(p_end, value);
ETL_INCREMENT_DEBUG_COUNT
@ -973,7 +996,7 @@ namespace etl
//*********************************************************************
/// Create a new element with a value at the back
//*********************************************************************
inline void create_back(T&& value)
inline void create_back(rvalue_reference value)
{
etl::create_copy_at(p_end, std::move(value));
ETL_INCREMENT_DEBUG_COUNT

View File

@ -279,6 +279,40 @@ namespace
CHECK(is_equal);
}
//*************************************************************************
TEST(test_move_assignment_interface)
{
const size_t SIZE = 10U;
typedef etl::vector<std::unique_ptr<uint32_t>, SIZE> Data;
typedef etl::ivector<std::unique_ptr<uint32_t>> IData;
std::unique_ptr<uint32_t> p1(new uint32_t(1U));
std::unique_ptr<uint32_t> p2(new uint32_t(2U));
std::unique_ptr<uint32_t> p3(new uint32_t(3U));
std::unique_ptr<uint32_t> p4(new uint32_t(4U));
Data data1;
data1.push_back(std::move(p1));
data1.push_back(std::move(p2));
data1.push_back(std::move(p3));
data1.push_back(std::move(p4));
Data data2;
IData& idata1 = data1;
IData& idata2 = data2;
idata2 = std::move(idata1);
CHECK_EQUAL(0U, data1.size());
CHECK_EQUAL(4U, data2.size());
CHECK_EQUAL(1U, *data2[0]);
CHECK_EQUAL(2U, *data2[1]);
CHECK_EQUAL(3U, *data2[2]);
CHECK_EQUAL(4U, *data2[3]);
}
//*************************************************************************
TEST_FIXTURE(SetupFixture, test_self_assignment)
{