From 6b165aee54a0626ed0c14578b3e04299d5af9ec1 Mon Sep 17 00:00:00 2001 From: John Wellbelove Date: Tue, 1 Jan 2019 14:02:43 +0000 Subject: [PATCH] Added interface move assignment. --- include/etl/vector.h | 37 ++++++++++++++++++++++++++------ test/test_vector_non_trivial.cpp | 34 +++++++++++++++++++++++++++++ 2 files changed, 64 insertions(+), 7 deletions(-) diff --git a/include/etl/vector.h b/include/etl/vector.h index ab975fbd..8b3cf069 100644 --- a/include/etl/vector.h +++ b/include/etl/vector.h @@ -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 diff --git a/test/test_vector_non_trivial.cpp b/test/test_vector_non_trivial.cpp index 1ffdd5fb..530779f2 100644 --- a/test/test_vector_non_trivial.cpp +++ b/test/test_vector_non_trivial.cpp @@ -279,6 +279,40 @@ namespace CHECK(is_equal); } + //************************************************************************* + TEST(test_move_assignment_interface) + { + const size_t SIZE = 10U; + typedef etl::vector, SIZE> Data; + typedef etl::ivector> IData; + + std::unique_ptr p1(new uint32_t(1U)); + std::unique_ptr p2(new uint32_t(2U)); + std::unique_ptr p3(new uint32_t(3U)); + std::unique_ptr 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) {