Modified to use constructed/destructed elements

This commit is contained in:
jwellbelove 2015-01-24 19:42:12 +00:00
parent ab971ee2ca
commit adb0b9a5b5
2 changed files with 12 additions and 64 deletions

View File

@ -30,10 +30,13 @@ SOFTWARE.
#define __ETL_VECTOR__
#include <stddef.h>
#include <stdint.h>
#include <iterator>
#include "ivector.h"
#include "container.h"
#include "alignment.h"
#include "array.h"
//*****************************************************************************
///\defgroup vector vector
@ -62,7 +65,7 @@ namespace etl
/// Constructor.
//*************************************************************************
vector()
: ivector<T>(buffer, MAX_SIZE)
: ivector<T>(reinterpret_cast<T*>(&buffer.value[0]), MAX_SIZE)
{
}
@ -71,7 +74,7 @@ namespace etl
///\param initialSize The initial size of the vector.
//*************************************************************************
explicit vector(size_t initialSize)
: ivector<T>(buffer, MAX_SIZE)
: ivector<T>(reinterpret_cast<T*>(&buffer.value[0]), MAX_SIZE)
{
ivector<T>::resize(initialSize);
}
@ -82,7 +85,7 @@ namespace etl
///\param value The value to fill the vector with.
//*************************************************************************
vector(size_t initialSize, typename ivector<T>::parameter_t value)
: ivector<T>(buffer, MAX_SIZE)
: ivector<T>(reinterpret_cast<T*>(&buffer.value[0]), MAX_SIZE)
{
ivector<T>::resize(initialSize, value);
}
@ -95,7 +98,7 @@ namespace etl
//*************************************************************************
template <typename TIterator>
vector(TIterator first, TIterator last)
: ivector<T>(buffer, MAX_SIZE)
: ivector<T>(reinterpret_cast<T*>(&buffer.value[0]), MAX_SIZE)
{
ivector<T>::assign(first, last);
}
@ -105,33 +108,18 @@ namespace etl
//*************************************************************************
vector& operator = (const vector& rhs)
{
ivector<T>::assign(rhs.cbegin(), rhs.cend());
if (&rhs != this)
{
ivector<T>::assign(rhs.cbegin(), rhs.cend());
}
return *this;
}
//*************************************************************************
/// Swap
//*************************************************************************
void swap(vector& other)
{
std::swap_ranges(etl::begin(buffer), etl::end(buffer), etl::begin(other.buffer));
std::swap(this->current_size, other.current_size);
}
private:
T buffer[MAX_SIZE]; ///<The array that stores the elements.
etl::align_as<array<uint8_t, MAX_SIZE * sizeof(T)>, T> buffer;
};
//*************************************************************************
/// Swap
//*************************************************************************
template <typename T, const size_t MAX_SIZE>
void swap(etl::vector<T, MAX_SIZE>& first, etl::vector<T, MAX_SIZE>& second)
{
first.swap(second);
}
}
#endif

View File

@ -163,46 +163,6 @@ namespace etl
return max_size() - size();
}
//*************************************************************************
/// Clears the vector.
//*************************************************************************
void clear()
{
current_size = 0;
}
//*************************************************************************
/// Increases the size of the vector by one, but does not initialise the new element.
/// If ETL_THROW_EXCEPTIONS is defined, throws a vector_full if the vector is already full.
//*************************************************************************
void push_back()
{
#ifdef ETL_THROW_EXCEPTIONS
if (current_size == MAX_SIZE)
{
throw vector_full();
}
#else
{
error_handler::error(vector_full());
}
#endif
++current_size;
}
//*************************************************************************
/// Removes an element from the end of the vector.
/// Does nothing if the vector is empty.
//*************************************************************************
void pop_back()
{
if (current_size > 0)
{
--current_size;
}
}
protected:
//*************************************************************************