aboutsummaryrefslogtreecommitdiff
path: root/reference_model/src/array_proxy.h
blob: f6b31059062879f3b397daa21c19c30825fa0701 (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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98

// Copyright (c) 2022, ARM Limited.
//
//    Licensed under the Apache License, Version 2.0 (the "License");
//    you may not use this file except in compliance with the License.
//    You may obtain a copy of the License at
//
//         http://www.apache.org/licenses/LICENSE-2.0
//
//    Unless required by applicable law or agreed to in writing, software
//    distributed under the License is distributed on an "AS IS" BASIS,
//    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
//    See the License for the specific language governing permissions and
//    limitations under the License.

#ifndef ARRAY_PROXY_H_
#define ARRAY_PROXY_H_

#include <cstddef>
#include <type_traits>

template <typename T>
class ArrayProxy
{
public:
    ArrayProxy(size_t n, T* ptr) noexcept
        : _n(n)
        , _ptr(ptr)
    {}

    template <typename U = T, std::enable_if_t<std::is_const<U>::value, int> = 0>
    ArrayProxy(size_t n, typename std::remove_const_t<T>* ptr) noexcept
        : _n(n)
        , _ptr(ptr)
    {}

    template <std::size_t S>
    ArrayProxy(T (&ptr)[S]) noexcept
        : _n(S)
        , _ptr(ptr)
    {}

    template <std::size_t S, typename U = T, std::enable_if_t<std::is_const<U>::value, int> = 0>
    ArrayProxy(typename std::remove_const_t<T> (&ptr)[S]) noexcept
        : _n(S)
        , _ptr(ptr)
    {}

    template <typename O,
              std::enable_if_t<std::is_convertible_v<decltype(std::declval<O>().data()), T*> &&
                                   std::is_convertible_v<decltype(std::declval<O>().size()), std::size_t>,
                               int> = 0>
    ArrayProxy(O& obj) noexcept
        : _n(obj.size())
        , _ptr(obj.data())
    {}

    size_t size() const noexcept
    {
        return _n;
    }

    T* data() const noexcept
    {
        return _ptr;
    }

    bool empty() const noexcept
    {
        return _n == 0;
    }

    const T* begin() const noexcept
    {
        return _ptr;
    }

    const T* end() const noexcept
    {
        return _ptr + _n;
    }

    T& operator[](size_t idx) noexcept
    {
        return *(_ptr + idx);
    }

    const T& operator[](size_t idx) const noexcept
    {
        return *(_ptr + idx);
    }

private:
    size_t _n;
    T* _ptr;
};

#endif    // ARRAY_PROXY_H_