2017-06-14 17:45:43 +10:00
|
|
|
/*
|
2018-08-04 15:14:06 +10:00
|
|
|
* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
2017-06-14 17:45:43 +10:00
|
|
|
*
|
2019-03-27 15:51:57 +11:00
|
|
|
* Copyright 2017-2019 Danny Robson <danny@nerdcruft.net>
|
2017-06-14 17:45:43 +10:00
|
|
|
*/
|
|
|
|
|
2018-12-17 12:35:45 +11:00
|
|
|
#pragma once
|
2017-06-14 17:45:43 +10:00
|
|
|
|
2019-03-18 16:18:27 +11:00
|
|
|
#include "../view.hpp"
|
2017-06-14 17:45:43 +10:00
|
|
|
|
2019-03-27 15:51:57 +11:00
|
|
|
#include "../iterator/zip.hpp"
|
|
|
|
|
2017-06-14 17:45:43 +10:00
|
|
|
#include <cstdlib>
|
2017-06-15 13:19:39 +10:00
|
|
|
#include <stdexcept>
|
|
|
|
|
2017-06-14 17:45:43 +10:00
|
|
|
|
2018-08-05 14:42:02 +10:00
|
|
|
namespace cruft {
|
2018-12-17 12:35:45 +11:00
|
|
|
/// An array-like object with capacity fixed at instantiation time, and a
|
|
|
|
/// size which is fixed at construction time.
|
2017-06-14 17:45:43 +10:00
|
|
|
///
|
|
|
|
/// \tparam S maximum number of elements
|
|
|
|
/// \tparam T data type of elements
|
|
|
|
template <std::size_t S, typename T>
|
|
|
|
class sarray {
|
|
|
|
public:
|
2019-02-06 17:03:52 +11:00
|
|
|
sarray (sarray &&rhs) noexcept (std::is_nothrow_move_constructible_v<T>)
|
|
|
|
: m_size (rhs.m_size)
|
|
|
|
{
|
|
|
|
for (size_t i = 0; i < m_size; ++i)
|
|
|
|
::new (&m_data.objects[i]) (T) (std::move (rhs[i]));
|
|
|
|
}
|
|
|
|
|
2018-12-19 17:14:35 +11:00
|
|
|
//---------------------------------------------------------------------
|
|
|
|
sarray (sarray const &rhs)
|
|
|
|
: sarray (cruft::view (rhs))
|
|
|
|
{ ; }
|
|
|
|
|
|
|
|
|
2018-12-17 12:35:45 +11:00
|
|
|
//---------------------------------------------------------------------
|
|
|
|
sarray (T const &data)
|
|
|
|
: sarray<S,T> (&data, &data + 1)
|
|
|
|
{ ; }
|
|
|
|
|
|
|
|
|
|
|
|
//-------------------------------------------------------------------------
|
|
|
|
sarray (const T(&data)[S], std::size_t count = S):
|
|
|
|
sarray<S,T> (data, data + count)
|
|
|
|
{ ; }
|
|
|
|
|
|
|
|
|
2017-06-15 13:19:39 +10:00
|
|
|
template <typename InputIt>
|
|
|
|
sarray (InputIt first, InputIt last):
|
|
|
|
m_size (std::distance (first, last))
|
2017-06-14 17:45:43 +10:00
|
|
|
{
|
|
|
|
if (m_size > S)
|
2017-06-15 13:19:39 +10:00
|
|
|
throw std::length_error ("oversize sarray");
|
2017-06-14 17:45:43 +10:00
|
|
|
|
|
|
|
std::size_t i = 0;
|
2017-06-15 13:19:39 +10:00
|
|
|
for (auto cursor = first; cursor != last; ++cursor)
|
|
|
|
::new (&m_data.objects[i++]) (T) (*cursor);
|
2017-06-14 17:45:43 +10:00
|
|
|
}
|
|
|
|
|
2017-06-15 13:19:39 +10:00
|
|
|
//---------------------------------------------------------------------
|
|
|
|
template <typename ContainerT>
|
|
|
|
explicit
|
|
|
|
sarray (const ContainerT &_data):
|
|
|
|
sarray (std::begin (_data), std::end (_data))
|
|
|
|
{ ; }
|
|
|
|
|
2019-03-27 15:51:57 +11:00
|
|
|
|
|
|
|
//---------------------------------------------------------------------
|
|
|
|
sarray& operator= (sarray &&rhs) noexcept (std::is_nothrow_move_constructible_v<T>)
|
|
|
|
{
|
|
|
|
// Destroy the currently held objects.
|
|
|
|
for (auto &obj: *this)
|
|
|
|
obj.~T ();
|
|
|
|
|
|
|
|
// Move-construct the new objects
|
|
|
|
for (auto [idx, obj]: cruft::iterator::izip (rhs))
|
|
|
|
new (&m_data.objects[idx]) T (std::move (obj));
|
|
|
|
m_size = rhs.m_size;
|
|
|
|
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-06-15 13:19:39 +10:00
|
|
|
//---------------------------------------------------------------------
|
2017-06-14 17:45:43 +10:00
|
|
|
~sarray ()
|
|
|
|
{
|
|
|
|
for (std::size_t i = 0; i < m_size; ++i)
|
|
|
|
m_data.objects[i].~T ();
|
|
|
|
}
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------
|
|
|
|
std::size_t size (void) const
|
|
|
|
{
|
|
|
|
return m_size;
|
|
|
|
}
|
|
|
|
|
2020-07-22 13:27:32 +10:00
|
|
|
|
|
|
|
//---------------------------------------------------------------------
|
|
|
|
bool empty (void) const
|
|
|
|
{
|
|
|
|
return m_size == 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-06-14 17:45:43 +10:00
|
|
|
//---------------------------------------------------------------------
|
|
|
|
T& operator[] (std::size_t i)& { return m_data.objects[i]; }
|
|
|
|
const T& operator[] (std::size_t i) const& { return m_data.objects[i]; }
|
|
|
|
|
|
|
|
auto begin (void) { return std::begin (m_data.objects); };
|
|
|
|
auto end (void) { return begin () + m_size; }
|
|
|
|
|
|
|
|
auto begin (void) const { return std::begin (m_data.objects); };
|
|
|
|
auto end (void) const { return begin () + m_size; }
|
|
|
|
|
|
|
|
auto cbegin (void) const { return std::cbegin (m_data.objects); };
|
|
|
|
auto cend (void) const { return cbegin () + m_size; }
|
|
|
|
|
|
|
|
private:
|
|
|
|
union alignas (T) data_t {
|
|
|
|
// we manually define a trivial constructor/destructor to appease
|
|
|
|
// gcc which doesn't like us using non-trivial T as the stored
|
|
|
|
// object.
|
|
|
|
//
|
|
|
|
// we're explicitly controlling the lifetime ourselves so this
|
|
|
|
// isn't a functional issue.
|
|
|
|
data_t () { };
|
|
|
|
~data_t () { };
|
|
|
|
|
|
|
|
char store[sizeof (T) * S];
|
|
|
|
T objects[S];
|
|
|
|
} m_data;
|
|
|
|
|
2018-12-19 17:14:49 +11:00
|
|
|
std::size_t m_size;
|
2017-06-14 17:45:43 +10:00
|
|
|
};
|
|
|
|
}
|