From ca813920756900c5c39318021a87ca092cc9d5e4 Mon Sep 17 00:00:00 2001 From: John Wellbelove Date: Sat, 29 Dec 2018 20:56:37 +0000 Subject: [PATCH] Added move constructor and assignment operator --- include/etl/vector.h | 49 +++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 48 insertions(+), 1 deletion(-) diff --git a/include/etl/vector.h b/include/etl/vector.h index 99641b0e..8836803c 100644 --- a/include/etl/vector.h +++ b/include/etl/vector.h @@ -1289,7 +1289,10 @@ namespace etl vector(const vector& other) : etl::ivector(reinterpret_cast(&buffer), MAX_SIZE) { - this->assign(other.begin(), other.end()); + if (this != &other) + { + this->assign(other.begin(), other.end()); + } } //************************************************************************* @@ -1305,6 +1308,50 @@ namespace etl return *this; } +#if ETL_CPP11_SUPPORTED + //************************************************************************* + /// Move constructor. + //************************************************************************* + vector(vector&& other) + : etl::ivector(reinterpret_cast(&buffer), MAX_SIZE) + { + if (this != &other) + { + this->initialise(); + + typename etl::ivector::iterator itr = other.begin(); + while (itr != other.end()) + { + this->push_back(std::move(*itr)); + ++itr; + } + + other.initialise(); + } + } + + //************************************************************************* + /// Assignment operator. + //************************************************************************* + vector& operator = (vector&& rhs) + { + if (&rhs != this) + { + this->clear(); + typename etl::ivector::iterator itr = rhs.begin(); + while (itr != rhs.end()) + { + this->push_back(std::move(*itr)); + ++itr; + } + + rhs.initialise(); + } + + return *this; + } +#endif + //************************************************************************* /// Fix the internal pointers after a low level memory copy. //*************************************************************************