Files

98 lines
1.9 KiB
C++

#ifndef FREERTOS_CPP_VECTOR_V1_HPP
#define FREERTOS_CPP_VECTOR_V1_HPP
#include <stddef.h>
namespace freertos
{
/*
* VectorV1 deliberately owns exactly one allocation. It is small enough to
* expose the Rule of Five without hiding the transfer behind a hosted STL
* container. Bounds checking and growth belong to later container lessons.
*/
class VectorV1 final
{
public:
explicit VectorV1( size_t size )
: elements_ { size != 0U ? new double[ size ] : nullptr },
size_ { size }
{
}
~VectorV1() noexcept
{
release();
}
VectorV1( const VectorV1 & ) = delete;
VectorV1 & operator=( const VectorV1 & ) = delete;
VectorV1( VectorV1 && other ) noexcept
: elements_ { other.elements_ }, size_ { other.size_ }
{
other.elements_ = nullptr;
other.size_ = 0U;
}
VectorV1 & operator=( VectorV1 && other ) noexcept
{
if( this != &other )
{
/* Release the old destination before taking the source buffer. */
release();
elements_ = other.elements_;
size_ = other.size_;
other.elements_ = nullptr;
other.size_ = 0U;
}
return *this;
}
double & operator[]( size_t index ) noexcept
{
return elements_[ index ];
}
const double & operator[]( size_t index ) const noexcept
{
return elements_[ index ];
}
double * data() noexcept
{
return elements_;
}
const double * data() const noexcept
{
return elements_;
}
size_t size() const noexcept
{
return size_;
}
bool empty() const noexcept
{
return size_ == 0U;
}
private:
void release() noexcept
{
delete[] elements_;
elements_ = nullptr;
size_ = 0U;
}
double * elements_;
size_t size_;
};
} // namespace freertos
#endif