blob: 5a68874da87f51d763a9f2cf5c7ecaf82e44766c [file] [log] [blame]
Vladimir Marko089142c2014-06-05 10:57:05 +01001/*
2 * Copyright (C) 2014 The Android Open Source Project
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
17#ifndef ART_COMPILER_UTILS_ARRAY_REF_H_
18#define ART_COMPILER_UTILS_ARRAY_REF_H_
19
20#include <type_traits>
21#include <vector>
22
23#include "base/logging.h"
24
25namespace art {
26
27template <typename T, bool ok> struct ArrayRefHelper;
28
29/**
30 * @brief A container that references an array.
31 *
32 * @details The template class ArrayRef provides a container that references
33 * an external array. This external array must remain alive while the ArrayRef
34 * object is in use. The external array may be a std::vector<>-backed storage
35 * or any other contiguous chunk of memory but that memory must remain valid,
36 * i.e. the std::vector<> must not be resized for example.
37 *
38 * Except for copy/assign and insert/erase/capacity functions, the interface
39 * is essentially the same as std::vector<>. Since we don't want to throw
40 * exceptions, at() is also excluded.
41 */
42template <typename T>
43class ArrayRef {
44 public:
45 typedef T value_type;
46 typedef T& reference;
47 typedef const T& const_reference;
48 typedef T* pointer;
49 typedef const T* const_pointer;
50 typedef T* iterator;
51 typedef const T* const_iterator;
52 typedef std::reverse_iterator<iterator> reverse_iterator;
53 typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
54 typedef ptrdiff_t difference_type;
55 typedef size_t size_type;
56
57 // Constructors.
58
59 constexpr ArrayRef()
60 : array_(nullptr), size_(0u) {
61 }
62
63 template <size_t size>
64 constexpr ArrayRef(T (&array)[size])
65 : array_(array), size_(size) {
66 }
67
68 template <typename U, size_t size>
69 constexpr ArrayRef(U (&array)[size],
70 typename ArrayRefHelper<T, std::is_same<T, const U>::value>::tag t = tag())
71 : array_(array), size_(size) {
72 }
73
74 constexpr ArrayRef(T* array, size_t size)
75 : array_(array), size_(size) {
76 }
77
78 template <typename U>
79 constexpr ArrayRef(U* array, size_t size,
80 typename ArrayRefHelper<T, std::is_same<T, const U>::value>::tag t = tag())
81 : array_(array), size_(size) {
82 }
83
84 explicit ArrayRef(std::vector<T>& v)
85 : array_(v.data()), size_(v.size()) {
86 }
87
88 template <typename U>
89 ArrayRef(const std::vector<U>& v,
90 typename ArrayRefHelper<T, std::is_same<T, const U>::value>::tag t = tag())
91 : array_(v.data()), size_(v.size()) {
92 }
93
94 // Assignment operators.
95
96 ArrayRef& operator=(const ArrayRef& other) {
97 array_ = other.array_;
98 size_ = other.size_;
99 return *this;
100 }
101
102 template <typename U>
103 typename ArrayRefHelper<T, std::is_same<T, const U>::value>::type&
104 operator=(const ArrayRef<U>& other) {
105 return *this = ArrayRef(other);
106 }
107
108 // Destructor.
109 ~ArrayRef() = default;
110
111 // Iterators.
112 iterator begin() { return array_; }
113 const_iterator begin() const { return array_; }
114 const_iterator cbegin() const { return array_; }
115 iterator end() { return array_ + size_; }
116 const_iterator end() const { return array_ + size_; }
117 const_iterator cend() const { return array_ + size_; }
118 reverse_iterator rbegin() { return reverse_iterator(end()); }
119 const_reverse_iterator rbegin() const { return const_reverse_iterator(end()); }
120 const_reverse_iterator crbegin() const { return const_reverse_iterator(cend()); }
121 reverse_iterator rend() { return reverse_iterator(begin()); }
122 const_reverse_iterator rend() const { return const_reverse_iterator(begin()); }
123 const_reverse_iterator crend() const { return const_reverse_iterator(cbegin()); }
124
125 // Size.
126 size_type size() const { return size_; }
127 bool empty() const { return size() == 0u; }
128
129 // Element access. NOTE: Not providing at().
130
131 reference operator[](size_type n) {
132 DCHECK_LT(n, size_);
133 return array_[n];
134 }
135
136 const_reference operator[](size_type n) const {
137 DCHECK_LT(n, size_);
138 return array_[n];
139 }
140
141 reference front() {
142 DCHECK_NE(size_, 0u);
143 return array_[0];
144 }
145
146 const_reference front() const {
147 DCHECK_NE(size_, 0u);
148 return array_[0];
149 }
150
151 reference back() {
152 DCHECK_NE(size_, 0u);
153 return array_[size_ - 1u];
154 }
155
156 const_reference back() const {
157 DCHECK_NE(size_, 0u);
158 return array_[size_ - 1u];
159 }
160
161 value_type* data() { return array_; }
162 const value_type* data() const { return array_; }
163
164 private:
165 struct tag { };
166 friend struct ArrayRefHelper<T, true>;
167
168 T* array_;
169 size_t size_;
170};
171
172template <typename T> struct ArrayRefHelper<T, true> {
173 typedef typename ArrayRef<T>::tag tag;
174 typedef ArrayRef<T> type;
175};
176
177template <typename T> struct ArrayRefHelper<T, false> {
178 // SFINAE: No "tag" or "type" typedef.
179};
180
181} // namespace art
182
183
184#endif // ART_COMPILER_UTILS_ARRAY_REF_H_