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