fixed_size_vector.h revision 4927ee586656424c827920876673228fbdcf27c3
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 22namespace chre { 23 24template<typename ElementType, size_t kCapacity> 25class FixedSizeVector { 26 public: 27 /** 28 * Obtains a pointer to the underlying storage for the vector. 29 * 30 * @return A pointer to the storage used for elements in this vector. 31 */ 32 ElementType *data(); 33 34 /** 35 * Obtains a const pointer to the underlying storage for the vector. 36 * 37 * @return A const pointer to the storage used for elements in this vector. 38 */ 39 const ElementType *data() const; 40 41 /** 42 * Obtains the number of elements currently stored in the static vector. 43 * 44 * @return The number of elements currently stored in the vector. 45 */ 46 size_t size() const; 47 48 /** 49 * Obtains the maximum number of elements that can be stored in the static 50 * vector. 51 * 52 * @return The maximum capacity of the vector as defined by the template 53 * argument. 54 */ 55 size_t capacity() const; 56 57 /** 58 * Determines whether the vector is empty or not. 59 * 60 * @return Returns true if the vector is empty. 61 */ 62 bool empty() const; 63 64 /** 65 * Determins whether the vector is full or not. 66 * 67 * @return Returns true if the vector is full. 68 */ 69 bool full() const; 70 71 /** 72 * Pushes an element onto the back of the vector. It is illegal to push an 73 * item onto a full vector. The user of the API must check the return of the 74 * full() function prior to pushing another element. 75 * 76 * @param The element to push onto the vector. 77 */ 78 void push_back(const ElementType& element); 79 80 /** 81 * Constructs an element onto the back of the vector. It is illegal to 82 * construct an item onto a full vector. The user of the API must check the 83 * return of the full() function prior to constructing another element. 84 * 85 * @param The arguments to the constructor 86 */ 87 template<typename... Args> 88 void emplace_back(Args&&... args); 89 90 /** 91 * Obtains an element of the vector given an index. It is illegal to index 92 * this vector out of bounds and the user of the API must check the size() 93 * function prior to indexing this vector to ensure that they will not read 94 * out of bounds. 95 * 96 * @param The index of the element. 97 * @return The element. 98 */ 99 ElementType& operator[](size_t index); 100 101 /** 102 * Obtains a const element of the vector given an index. It is illegal to 103 * index this vector out of bounds and the user of the API must check the 104 * size() function prior to indexing this vector to ensure that they will not 105 * read out of bounds. 106 * 107 * @param The index of the element. 108 * @return The element. 109 */ 110 const ElementType& operator[](size_t index) const; 111 112 private: 113 //! Storage for vector elements. To avoid static initialization of members, 114 //! std::aligned_storage is used. 115 typename std::aligned_storage<sizeof(ElementType), 116 alignof(ElementType)>::type mData[kCapacity]; 117 118 //! The number of elements in the vector. This will never be more than 119 //! kCapacity. 120 size_t mSize = 0; 121}; 122 123} // namespace chre 124 125#include "chre/util/fixed_size_vector_impl.h" 126 127#endif // CHRE_UTIL_FIXED_SIZE_VECTOR_H_ 128