rsl 1.1.0
ROS Support Library
Loading...
Searching...
No Matches
static_vector.hpp
Go to the documentation of this file.
1#pragma once
2
3#include <tcb_span/span.hpp>
4
5#include <array>
6#include <cassert>
7#include <vector>
8
9namespace rsl {
10
17template <typename T, size_t capacity>
19 std::array<T, capacity> data_{};
20 size_t size_{};
21
22 public:
26 StaticVector() = default;
27
31 template <typename Collection>
32 StaticVector(Collection const& collection) : size_(std::min(collection.size(), capacity)) {
33 assert(collection.size() <= capacity &&
34 "rsl::StaticVector::StaticVector: Input exceeds capacity");
35 std::copy(collection.cbegin(),
36 collection.cbegin() + typename Collection::difference_type(size_), data_.begin());
37 }
38
42 StaticVector(std::initializer_list<T> const& collection)
43 : StaticVector(std::vector<T>(collection)) {}
44
48 [[nodiscard]] auto begin() { return data_.begin(); }
49
53 [[nodiscard]] auto begin() const { return data_.cbegin(); }
54
58 [[nodiscard]] auto end() { return data_.begin() + size_; }
59
63 [[nodiscard]] auto end() const { return data_.cbegin() + size_; }
64
68 operator tcb::span<T>() { return tcb::span<T>(data_.data(), size_); }
69
73 operator tcb::span<T const>() const { return tcb::span<T const>(data_.data(), size_); }
74};
75
79template <typename T, size_t capacity>
80[[nodiscard]] auto to_vector(StaticVector<T, capacity> const& static_vector) {
81 return std::vector<T>(static_vector.begin(), static_vector.end());
82}
83
84} // namespace rsl
Fixed capacity vector with an implicit conversion to tcb::span. Capacity is specified as a template p...
Definition static_vector.hpp:18
auto begin() const
Get a const begin iterator.
Definition static_vector.hpp:53
StaticVector()=default
Construct an empty vector.
StaticVector(Collection const &collection)
Construct from another container.
Definition static_vector.hpp:32
auto begin()
Get a mutable begin iterator.
Definition static_vector.hpp:48
auto end() const
Get a const end iterator.
Definition static_vector.hpp:63
auto end()
Get a mutable end iterator.
Definition static_vector.hpp:58
StaticVector(std::initializer_list< T > const &collection)
Construct from a std::initializer_list.
Definition static_vector.hpp:42
auto to_vector(StaticVector< T, capacity > const &static_vector)
Explicit conversion to std::vector<T>
Definition static_vector.hpp:80