iceoryx_doc  1.0.1
vector.hpp
1 // Copyright (c) 2019 by Robert Bosch GmbH. All rights reserved.
2 // Copyright (c) 2021 by Apex.AI Inc. All rights reserved.
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 // SPDX-License-Identifier: Apache-2.0
17 #ifndef IOX_UTILS_CXX_VECTOR_HPP
18 #define IOX_UTILS_CXX_VECTOR_HPP
19 
20 #include <algorithm>
21 #include <cstdint>
22 #include <cstdio>
23 #include <utility>
24 
25 namespace iox
26 {
27 namespace cxx
28 {
32 template <typename T, uint64_t Capacity>
33 class vector
34 {
35  public:
36  using value_type = T;
37 
38  using iterator = T*;
39  using const_iterator = const T*;
40 
42  vector() noexcept = default;
43 
47  vector(const uint64_t count, const T& value) noexcept;
48 
51  vector(const uint64_t count) noexcept;
52 
54  vector(const vector& rhs) noexcept;
55 
57  vector(vector&& rhs) noexcept;
58 
61  ~vector() noexcept;
62 
66  vector& operator=(const vector& rhs) noexcept;
67 
71  vector& operator=(vector&& rhs) noexcept;
72 
76  iterator begin() noexcept;
77 
81  const_iterator begin() const noexcept;
82 
85  iterator end() noexcept;
86 
89  const_iterator end() const noexcept;
90 
93  T* data() noexcept;
94 
97  const T* data() const noexcept;
98 
100  // is undefined if the element at index does not exist.
101  T& at(const uint64_t index) noexcept;
102 
105  const T& at(const uint64_t index) const noexcept;
106 
108  // is undefined if the element at index does not exist.
109  T& operator[](const uint64_t index) noexcept;
110 
113  const T& operator[](const uint64_t index) const noexcept;
114 
117  T& front() noexcept;
118 
121  const T& front() const noexcept;
122 
125  T& back() noexcept;
126 
129  const T& back() const noexcept;
130 
133  uint64_t capacity() const noexcept;
134 
137  uint64_t size() const noexcept;
138 
140  bool empty() const noexcept;
141 
143  void clear() noexcept;
144 
154  template <typename... Targs>
155  bool resize(const uint64_t count, const Targs&... args) noexcept;
156 
160  template <typename... Targs>
161  bool emplace(const uint64_t position, Targs&&... args) noexcept;
162 
165  template <typename... Targs>
166  bool emplace_back(Targs&&... args) noexcept;
167 
170  bool push_back(const T& value) noexcept;
171 
174  bool push_back(T&& value) noexcept;
175 
178  bool pop_back() noexcept;
179 
183  iterator erase(iterator position) noexcept;
184 
185  private:
186  using element_t = uint8_t[sizeof(T)];
187  alignas(T) element_t m_data[Capacity];
188  uint64_t m_size = 0u;
189 };
190 } // namespace cxx
191 } // namespace iox
192 
193 template <typename T, uint64_t CapacityLeft, uint64_t CapacityRight>
194 bool operator==(const iox::cxx::vector<T, CapacityLeft>& lhs, const iox::cxx::vector<T, CapacityRight>& rhs) noexcept;
195 
196 template <typename T, uint64_t CapacityLeft, uint64_t CapacityRight>
197 bool operator!=(const iox::cxx::vector<T, CapacityLeft>& lhs, const iox::cxx::vector<T, CapacityRight>& rhs) noexcept;
198 
199 #include "iceoryx_utils/internal/cxx/vector.inl"
200 
201 #endif // IOX_UTILS_CXX_VECTOR_HPP
C++11 compatible vector implementation. We needed to do some adjustments in the API since we do not u...
Definition: vector.hpp:34
bool push_back(const T &value) noexcept
appends the given element at the end of the vector
Definition: vector.inl:201
T & front() noexcept
returns a reference to the first element; terminates if the vector is empty
Definition: vector.inl:292
T * data() noexcept
return the pointer to the underlying array
Definition: vector.inl:250
void clear() noexcept
calls the destructor of all contained elements and removes them
Definition: vector.inl:157
bool emplace_back(Targs &&... args) noexcept
forwards all arguments to the constructor of the contained element and performs a placement new at th...
Definition: vector.inl:166
iterator end() noexcept
returns an iterator to the element which comes after the last element (the first element which is out...
Definition: vector.inl:348
bool pop_back() noexcept
removes the last element of the vector; calling pop_back on an empty container does nothing
Definition: vector.inl:213
vector() noexcept=default
creates an empty vector
T & at(const uint64_t index) noexcept
returns a reference to the element stored at index. the behavior
Definition: vector.inl:262
bool resize(const uint64_t count, const Targs &... args) noexcept
resizes the vector. If the vector size increases new elements will be constructed with the given argu...
Definition: vector.inl:225
iterator begin() noexcept
returns an iterator to the first element of the vector, if the vector is empty it returns the same it...
Definition: vector.inl:336
iterator erase(iterator position) noexcept
removes an element at the given position. if this element is in the middle of the vector every elemen...
Definition: vector.inl:360
uint64_t size() const noexcept
returns the number of elements which are currently stored in the vector
Definition: vector.inl:145
bool emplace(const uint64_t position, Targs &&... args) noexcept
forwards all arguments to the constructor of the contained element and performs a placement new at th...
Definition: vector.inl:178
uint64_t capacity() const noexcept
returns the capacity of the vector which was given via the template argument
Definition: vector.inl:151
T & back() noexcept
returns a reference to the last element; terminates if the vector is empty
Definition: vector.inl:314
bool empty() const noexcept
returns true if the vector is emtpy, otherwise false
Definition: vector.inl:139
building block to easily create free function for logging in a library context
Definition: lockfree_queue.hpp:28