1 /*
2  * Copyright (C) 2018 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_DYNAMIC_VECTOR_BASE_H_
18 #define CHRE_UTIL_DYNAMIC_VECTOR_BASE_H_
19 
20 #include <cstddef>
21 
22 #include "chre/util/non_copyable.h"
23 
24 namespace chre {
25 
26 class DynamicVectorBase : public NonCopyable {
27  protected:
28   DynamicVectorBase() = default;
29 
30   /**
31    * Move-constructs a dynamic vector from another. The other dynamic vector is
32    * left in an empty state.
33    *
34    * @param other The other vector to move from.
35    */
36   DynamicVectorBase(DynamicVectorBase &&other);
37 
38   /**
39    * Performs a reserve operation for DynamicVector when the underlying type is
40    * trivial. See {@link DynamicVector::reserve} for further details.
41    *
42    * @param elementSize The size of the element used to determine the effective
43    *        size of the underlying data.
44    */
45   bool doReserve(size_t newCapacity, size_t elementSize);
46 
47   /**
48    * Performs a prepare for push operation for DynamicVector when the underlying
49    * type is trivial. See {@link DynamicVector::prepareForPush} for further
50    * details.
51    *
52    * @param elementSize The size of the element used to determine the effective
53    *        size of the underlying data.
54    */
55   bool doPrepareForPush(size_t elementSize);
56 
57   /**
58    * @return the next size of allocation to perform when growing the size of
59    *         this vector. If no growth is required (mSize is less than
60    *         mCapacity), the current capacity is returned.
61    */
62   size_t getNextGrowthCapacity() const;
63 
64   /*
65    * Performs an erase operation for DynamicVector when the underlying type is
66    * trivial. See {@link DynamicVector::erase} for further details.
67    *
68    * @param elementSize The size of the element used to determine the effective
69    *        size of the underlying data.
70    */
71   void doErase(size_t index, size_t elementSize);
72 
73   /**
74    * Performs a push back operation for DynamicVector when the underlying type
75    * is trivial. See {@link DynamicVector::push_back} for further details.
76    *
77    * @param elementSize The size of the element used to determine the effective
78    *        size of the underlying data.
79    */
80   bool doPushBack(const void *element, size_t elementSize);
81 
82   //! A pointer to the underlying data buffer.
83   void *mData = nullptr;
84 
85   //! The current size of the vector, as in the number of elements stored.
86   size_t mSize = 0;
87 
88   //! The current capacity of the vector, as in the maximum number of elements
89   //! that can be stored.
90   size_t mCapacity = 0;
91 };
92 
93 }  // namespace chre
94 
95 #endif  // CHRE_UTIL_DYNAMIC_VECTOR_BASE_H_
96