summaryrefslogtreecommitdiff
path: root/asl/types/array.hpp
blob: 5fb300a3fd0ac3748860182046b35c032a39f3e0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
// Copyright 2025 Steven Le Rouzic
//
// SPDX-License-Identifier: BSD-3-Clause

#pragma once

#include "asl/base/assert.hpp"
#include "asl/base/meta.hpp"
#include "asl/base/utility.hpp"
#include "asl/types/span.hpp"

namespace asl
{

template<is_object T, int64_t kSize>
requires (kSize > 0)
struct array
{
    T m_data[kSize];

    [[nodiscard]] constexpr bool is_empty() const { return false; }
    
    [[nodiscard]] constexpr int64_t size() const { return kSize; }

    constexpr auto data(this auto&& self)
    {
        using return_type = un_ref_t<copy_cref_t<decltype(self), T>>*;
        return static_cast<return_type>(self.m_data);
    }

    constexpr auto begin(this auto&& self)
    {
        return contiguous_iterator{self.data()};
    }

    constexpr auto end(this auto&& self)
    {
        return contiguous_iterator{self.data() + kSize};
    }

    template<int64_t kSpanSize>
    requires (kSpanSize == kSize || kSpanSize == dynamic_size)
    constexpr operator span<const T, kSpanSize>() const // NOLINT(*explicit*)
    {
        return as_span();
    }

    template<int64_t kSpanSize>
    requires (kSpanSize == kSize || kSpanSize == dynamic_size)
    constexpr operator span<T, kSpanSize>() // NOLINT(*explicit*)
    {
        return as_span();
    }

    constexpr auto as_span(this auto&& self)
    {
        using type = un_ref_t<copy_cref_t<decltype(self), T>>;
        return span<type, kSize>{self.data(), self.size()};
    }

    constexpr auto&& operator[](this auto&& self, isize_t i)
    {
        ASL_ASSERT(i >= 0 && i <= self.size());
        return std::forward_like<decltype(self)>(std::forward<decltype(self)>(self).data()[i]);
    }
};


} // namespace asl