Add util::spatial_array asserts in case of wrong number of indices

This commit is contained in:
Nikita Lisitsa 2022-08-05 16:12:13 +03:00
parent 2421dee3b7
commit 8f6bd5b5fc

View file

@ -50,6 +50,8 @@ namespace psemek::util
template <typename ... Ix>
T & spatial_array<T, N, Index>::get(Ix ... index)
{
static_assert(sizeof...(Ix) == N, "wrong number of indices");
if (!contains(index...))
expand(index...);
@ -63,6 +65,8 @@ namespace psemek::util
template <typename ... Ix>
T * spatial_array<T, N, Index>::get_if(Ix ... index)
{
static_assert(sizeof...(Ix) == N, "wrong number of indices");
if (!contains(index...))
return nullptr;
@ -76,6 +80,8 @@ namespace psemek::util
template <typename ... Ix>
T const * spatial_array<T, N, Index>::get_if(Ix ... index) const
{
static_assert(sizeof...(Ix) == N, "wrong number of indices");
if (!contains(index...))
return nullptr;
@ -89,6 +95,8 @@ namespace psemek::util
template <typename ... Ix>
T const & spatial_array<T, N, Index>::at(Ix ... index) const
{
static_assert(sizeof...(Ix) == N, "wrong number of indices");
auto const size = array_.dims();
Index ix[N] {index...};
for (std::size_t i = 0; i < N; ++i)
@ -104,6 +112,8 @@ namespace psemek::util
template <typename ... Ix>
bool spatial_array<T, N, Index>::contains(Ix ... index) const
{
static_assert(sizeof...(Ix) == N, "wrong number of indices");
auto const size = array_.dims();
Index ix[N] {index...};
for (std::size_t i = 0; i < N; ++i)
@ -116,6 +126,8 @@ namespace psemek::util
template <typename ... Ix>
void spatial_array<T, N, Index>::expand(Ix ... index)
{
static_assert(sizeof...(Ix) == N, "wrong number of indices");
Index ix[N] {index...};
if (array_.empty())