fixed_size_vector_impl.h revision 926f1b91231f88c3669f10dec8f56e24ae16b957
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_IMPL_H_
18#define CHRE_UTIL_FIXED_SIZE_VECTOR_IMPL_H_
19
20#include "chre/platform/assert.h"
21#include "chre/util/fixed_size_vector.h"
22
23namespace chre {
24
25template<typename ElementType, size_t kCapacity>
26ElementType *FixedSizeVector<ElementType, kCapacity>::data() {
27  return reinterpret_cast<ElementType *>(mData);
28}
29
30template<typename ElementType, size_t kCapacity>
31const ElementType *FixedSizeVector<ElementType, kCapacity>::data() const {
32  return reinterpret_cast<const ElementType *>(mData);
33}
34
35template<typename ElementType, size_t kCapacity>
36size_t FixedSizeVector<ElementType, kCapacity>::size() const {
37  return mSize;
38}
39
40template<typename ElementType, size_t kCapacity>
41size_t FixedSizeVector<ElementType, kCapacity>::capacity() const {
42  return kCapacity;
43}
44
45template<typename ElementType, size_t kCapacity>
46bool FixedSizeVector<ElementType, kCapacity>::empty() const {
47  return (mSize == 0);
48}
49
50template<typename ElementType, size_t kCapacity>
51bool FixedSizeVector<ElementType, kCapacity>::full() const {
52  return (mSize == kCapacity);
53}
54
55template<typename ElementType, size_t kCapacity>
56void FixedSizeVector<ElementType, kCapacity>::push_back(
57    const ElementType& element) {
58  CHRE_ASSERT(!full());
59  if (!full()) {
60    data()[mSize++] = element;
61  }
62}
63
64template<typename ElementType, size_t kCapacity>
65template<typename... Args>
66void FixedSizeVector<ElementType, kCapacity>::emplace_back(Args&&... args) {
67  CHRE_ASSERT(!full());
68  if (!full()) {
69    new (&data()[mSize++]) ElementType(std::forward<Args>(args)...);
70  }
71}
72
73template<typename ElementType, size_t kCapacity>
74ElementType& FixedSizeVector<ElementType, kCapacity>::operator[](
75    size_t index) {
76  CHRE_ASSERT(index < mSize);
77  if (index >= mSize) {
78    index = mSize - 1;
79  }
80
81  return data()[index];
82}
83
84template<typename ElementType, size_t kCapacity>
85const ElementType& FixedSizeVector<ElementType, kCapacity>::operator[](
86    size_t index) const {
87  CHRE_ASSERT(index < mSize);
88  if (index >= mSize) {
89    index = mSize - 1;
90  }
91
92  return data()[index];
93}
94
95}  // namespace chre
96
97#endif  // CHRE_UTIL_FIXED_SIZE_VECTOR_IMPL_H_
98