• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2020 The Dawn Authors
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 //     http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #ifndef COMMON_ITYP_VECTOR_H_
16 #define COMMON_ITYP_VECTOR_H_
17 
18 #include "common/TypedInteger.h"
19 #include "common/UnderlyingType.h"
20 
21 #include <type_traits>
22 #include <vector>
23 
24 namespace ityp {
25 
26     // ityp::vector is a helper class that wraps std::vector with the restriction that
27     // indices must be a particular type |Index|.
28     template <typename Index, typename Value>
29     class vector : public std::vector<Value> {
30         using I = UnderlyingType<Index>;
31         using Base = std::vector<Value>;
32 
33       private:
34         // Disallow access to base constructors and untyped index/size-related operators.
35         using Base::Base;
36         using Base::operator=;
37         using Base::operator[];
38         using Base::at;
39         using Base::reserve;
40         using Base::resize;
41         using Base::size;
42 
43       public:
vector()44         vector() : Base() {
45         }
46 
vector(Index size)47         explicit vector(Index size) : Base(static_cast<I>(size)) {
48         }
49 
vector(Index size,const Value & init)50         vector(Index size, const Value& init) : Base(static_cast<I>(size), init) {
51         }
52 
vector(const vector & rhs)53         vector(const vector& rhs) : Base(static_cast<const Base&>(rhs)) {
54         }
55 
vector(vector && rhs)56         vector(vector&& rhs) : Base(static_cast<Base&&>(rhs)) {
57         }
58 
vector(std::initializer_list<Value> init)59         vector(std::initializer_list<Value> init) : Base(init) {
60         }
61 
62         vector& operator=(const vector& rhs) {
63             Base::operator=(static_cast<const Base&>(rhs));
64             return *this;
65         }
66 
67         vector& operator=(vector&& rhs) noexcept {
68             Base::operator=(static_cast<Base&&>(rhs));
69             return *this;
70         }
71 
72         Value& operator[](Index i) {
73             ASSERT(i >= Index(0) && i < size());
74             return Base::operator[](static_cast<I>(i));
75         }
76 
77         constexpr const Value& operator[](Index i) const {
78             ASSERT(i >= Index(0) && i < size());
79             return Base::operator[](static_cast<I>(i));
80         }
81 
at(Index i)82         Value& at(Index i) {
83             ASSERT(i >= Index(0) && i < size());
84             return Base::at(static_cast<I>(i));
85         }
86 
at(Index i)87         constexpr const Value& at(Index i) const {
88             ASSERT(i >= Index(0) && i < size());
89             return Base::at(static_cast<I>(i));
90         }
91 
size()92         constexpr Index size() const {
93             ASSERT(std::numeric_limits<I>::max() >= Base::size());
94             return Index(static_cast<I>(Base::size()));
95         }
96 
resize(Index size)97         void resize(Index size) {
98             Base::resize(static_cast<I>(size));
99         }
100 
reserve(Index size)101         void reserve(Index size) {
102             Base::reserve(static_cast<I>(size));
103         }
104     };
105 
106 }  // namespace ityp
107 
108 #endif  // COMMON_ITYP_VECTOR_H_
109