blob: f6b31059062879f3b397daa21c19c30825fa0701 [file] [log] [blame]
Grant Watson64285a12022-11-16 15:32:39 +00001
2// Copyright (c) 2022, ARM Limited.
3//
4// Licensed under the Apache License, Version 2.0 (the "License");
5// you may not use this file except in compliance with the License.
6// You may obtain a copy of the License at
7//
8// http://www.apache.org/licenses/LICENSE-2.0
9//
10// Unless required by applicable law or agreed to in writing, software
11// distributed under the License is distributed on an "AS IS" BASIS,
12// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13// See the License for the specific language governing permissions and
14// limitations under the License.
15
16#ifndef ARRAY_PROXY_H_
17#define ARRAY_PROXY_H_
18
19#include <cstddef>
20#include <type_traits>
21
22template <typename T>
23class ArrayProxy
24{
25public:
26 ArrayProxy(size_t n, T* ptr) noexcept
27 : _n(n)
28 , _ptr(ptr)
29 {}
30
31 template <typename U = T, std::enable_if_t<std::is_const<U>::value, int> = 0>
32 ArrayProxy(size_t n, typename std::remove_const_t<T>* ptr) noexcept
33 : _n(n)
34 , _ptr(ptr)
35 {}
36
37 template <std::size_t S>
38 ArrayProxy(T (&ptr)[S]) noexcept
39 : _n(S)
40 , _ptr(ptr)
41 {}
42
43 template <std::size_t S, typename U = T, std::enable_if_t<std::is_const<U>::value, int> = 0>
44 ArrayProxy(typename std::remove_const_t<T> (&ptr)[S]) noexcept
45 : _n(S)
46 , _ptr(ptr)
47 {}
48
49 template <typename O,
50 std::enable_if_t<std::is_convertible_v<decltype(std::declval<O>().data()), T*> &&
51 std::is_convertible_v<decltype(std::declval<O>().size()), std::size_t>,
52 int> = 0>
53 ArrayProxy(O& obj) noexcept
54 : _n(obj.size())
55 , _ptr(obj.data())
56 {}
57
58 size_t size() const noexcept
59 {
60 return _n;
61 }
62
63 T* data() const noexcept
64 {
65 return _ptr;
66 }
67
68 bool empty() const noexcept
69 {
70 return _n == 0;
71 }
72
73 const T* begin() const noexcept
74 {
75 return _ptr;
76 }
77
78 const T* end() const noexcept
79 {
80 return _ptr + _n;
81 }
82
83 T& operator[](size_t idx) noexcept
84 {
85 return *(_ptr + idx);
86 }
87
88 const T& operator[](size_t idx) const noexcept
89 {
90 return *(_ptr + idx);
91 }
92
93private:
94 size_t _n;
95 T* _ptr;
96};
97
98#endif // ARRAY_PROXY_H_