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