fixed_size_vector.h revision 4849d809e7b9691e5968923d546c8be7547e4386
1/*
2 * Copyright (C) 2016 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 CHRE_UTIL_FIXED_SIZE_VECTOR_H_
18#define CHRE_UTIL_FIXED_SIZE_VECTOR_H_
19
20#include <type_traits>
21
22#include "chre/util/non_copyable.h"
23
24namespace chre {
25
26template<typename ElementType, size_t kCapacity>
27class FixedSizeVector : public NonCopyable {
28 public:
29  /**
30   * Destructs the objects
31   */
32  ~FixedSizeVector();
33
34  /**
35   * @return A reference to the last element in the vector
36   */
37  ElementType& back();
38  const ElementType& back() const;
39
40  /**
41   * @return A reference to the first element in the vector
42   */
43  ElementType& front();
44  const ElementType& front() const;
45
46  /**
47   * Obtains a pointer to the underlying storage for the vector.
48   *
49   * @return A pointer to the storage used for elements in this vector.
50   */
51  ElementType *data();
52
53  /**
54   * Obtains a const pointer to the underlying storage for the vector.
55   *
56   * @return A const pointer to the storage used for elements in this vector.
57   */
58  const ElementType *data() const;
59
60  /**
61   * Obtains the number of elements currently stored in the static vector.
62   *
63   * @return The number of elements currently stored in the vector.
64   */
65  size_t size() const;
66
67  /**
68   * Obtains the maximum number of elements that can be stored in the static
69   * vector.
70   *
71   * @return The maximum capacity of the vector as defined by the template
72   * argument.
73   */
74  size_t capacity() const;
75
76  /**
77   * Determines whether the vector is empty or not.
78   *
79   * @return true if the vector is empty.
80   */
81  bool empty() const;
82
83  /**
84   * Determines whether the vector is full or not.
85   *
86   * @return true if the vector is full.
87   */
88  bool full() const;
89
90  /**
91   * Pushes an element onto the back of the vector. It is illegal to push an
92   * item onto a full vector. The user of the API must check the return of the
93   * full() function prior to pushing another element. All iterators and
94   * references are unaffected.
95   *
96   * @param The element to push onto the vector.
97   */
98  void push_back(const ElementType& element);
99
100  /**
101   * Constructs an element onto the back of the vector. It is illegal to
102   * construct an item onto a full vector. The user of the API must check the
103   * return of the full() function prior to constructing another element. All
104   * iterators and references are unaffected.
105   *
106   * @param The arguments to the constructor
107   */
108  template<typename... Args>
109  void emplace_back(Args&&... args);
110
111  /**
112   * Obtains an element of the vector given an index. It is illegal to index
113   * this vector out of bounds and the user of the API must check the size()
114   * function prior to indexing this vector to ensure that they will not read
115   * out of bounds.
116   *
117   * @param The index of the element.
118   * @return The element.
119   */
120  ElementType& operator[](size_t index);
121
122  /**
123   * Obtains a const element of the vector given an index. It is illegal to
124   * index this vector out of bounds and the user of the API must check the
125   * size() function prior to indexing this vector to ensure that they will not
126   * read out of bounds.
127   *
128   * @param The index of the element.
129   * @return The element.
130   */
131  const ElementType& operator[](size_t index) const;
132
133  /**
134   * Removes an element from the vector given an index. All elements after the
135   * indexed one are moved forward one position. The destructor is invoked on
136   * on the invalid item left at the end of the vector. The index passed in
137   * must be less than the size() of the vector. If the index is greater than or
138   * equal to the size no operation is performed. All iterators and references
139   * to elements before the indexed one are unaffected.
140   *
141   * @param index The index to remove an element at.
142   */
143  void erase(size_t index);
144
145  /**
146   * Swaps the location of two elements stored in the vector. The indices
147   * passed in must be less than the size() of the vector. If the index is
148   * greater than or equal to the size, no operation is performed. All iterators
149   * and references to these two indexed elements are invalidated.
150   *
151   * @param index0 The index of the first element
152   * @param index1 The index of the second element
153   */
154  void swap(size_t index0, size_t index1);
155
156  /**
157   * Resizes the fixed size vector by default-constructing from the current
158   * size() to the newly requested size. If the new size is smaller than the
159   * current size(), the elements from the new size to the current size() are
160   * destructed and the vector is shrunk. A resize operation cannot be performed
161   * that is greater than kCapacity. This will result in an assertion failure
162   * and a resize to kCapacity if assertions are disabled. All iterators and
163   * references to elements before newSize are unaffected.
164   *
165   * @param newSize The new size of the vector.
166   */
167  void resize(size_t newSize);
168
169  /**
170   * Random-access iterator that points to some element in the container.
171   */
172  typedef ElementType* iterator;
173  typedef const ElementType* const_iterator;
174
175  /**
176   * @return A random-access iterator to the beginning.
177   */
178  typename FixedSizeVector<ElementType, kCapacity>::iterator begin();
179  typename FixedSizeVector<ElementType, kCapacity>::const_iterator begin() const;
180  typename FixedSizeVector<ElementType, kCapacity>::const_iterator cbegin() const;
181
182  /**
183   * @return A random-access iterator to the end.
184   */
185  typename FixedSizeVector<ElementType, kCapacity>::iterator end();
186  typename FixedSizeVector<ElementType, kCapacity>::const_iterator end() const;
187  typename FixedSizeVector<ElementType, kCapacity>::const_iterator cend() const;
188
189 private:
190  //! Storage for vector elements. To avoid static initialization of members,
191  //! std::aligned_storage is used.
192  typename std::aligned_storage<sizeof(ElementType),
193      alignof(ElementType)>::type mData[kCapacity];
194
195  //! The number of elements in the vector. This will never be more than
196  //! kCapacity.
197  size_t mSize = 0;
198};
199
200}  // namespace chre
201
202#include "chre/util/fixed_size_vector_impl.h"
203
204#endif  // CHRE_UTIL_FIXED_SIZE_VECTOR_H_
205