tetl 0.1.0
Embedded Template Library
Loading...
Searching...
No Matches
dynamic_array.hpp
Go to the documentation of this file.
1// SPDX-License-Identifier: BSL-1.0
2
3#ifndef TETL_ARRAY_DYNAMIC_ARRAY_HPP
4#define TETL_ARRAY_DYNAMIC_ARRAY_HPP
5
6#include <etl/_config/all.hpp>
7
16#include <etl/_utility/move.hpp>
17
18namespace etl {
19
22template <typename T, typename Allocator>
24 using value_type = T;
25 using allocator_type = Allocator;
28 using pointer = T*;
29 using const_pointer = T const*;
30
33 = default;
34
35 explicit dynamic_array(Allocator alloc)
36 : _alloc{etl::move(alloc)}
37 {
38 }
39
40 dynamic_array(etl::size_t n, T const& value, Allocator alloc = Allocator())
41 : _size{n}
42 , _alloc{etl::move(alloc)}
43 {
46 }
47
48 explicit dynamic_array(etl::size_t n, Allocator alloc = Allocator())
49 : dynamic_array{n, T(), etl::move(alloc)}
50 {
51 }
52
53 dynamic_array(dynamic_array const& other) = delete;
54 auto operator=(dynamic_array const& other) -> dynamic_array& = delete;
55
56 dynamic_array(dynamic_array&& other) noexcept
57 : _ptr{etl::exchange(other._ptr, nullptr)}
58 , _size{etl::exchange(other._size, etl::size_t(0))}
59 , _alloc{etl::exchange(other._alloc, Allocator{})}
60 {
61 }
62
63 auto operator=(dynamic_array&& other) noexcept -> dynamic_array&
64 {
65 _ptr = etl::exchange(other._ptr, nullptr);
66 _size = etl::exchange(other._size, etl::size_t(0));
67 _alloc = etl::exchange(other._alloc, Allocator{});
68 return *this;
69 }
70
76
77 [[nodiscard]] auto size() -> etl::size_t { return _size; }
78
79 [[nodiscard]] auto size() const -> etl::size_t { return _size; }
80
81 [[nodiscard]] auto data() -> T* { return _ptr; }
82
83 [[nodiscard]] auto data() const -> T const* { return _ptr; }
84
85 [[nodiscard]] auto begin() -> T* { return _ptr; }
86
87 [[nodiscard]] auto begin() const -> T const* { return _ptr; }
88
89 [[nodiscard]] auto end() -> T* { return etl::next(_ptr, static_cast<etl::ptrdiff_t>(size())); }
90
91 [[nodiscard]] auto end() const -> T const* { return etl::next(_ptr, static_cast<etl::ptrdiff_t>(size())); }
92
93private:
94 T* _ptr{nullptr};
95 etl::size_t _size{0};
96 TETL_NO_UNIQUE_ADDRESS Allocator _alloc;
97};
98
99} // namespace etl
100
101#endif // TETL_ARRAY_DYNAMIC_ARRAY_HPP
#define TETL_NO_UNIQUE_ADDRESS
Definition attributes.hpp:41
constexpr auto move(InputIt first, InputIt last, OutputIt destination) -> OutputIt
Moves the elements in the range [first, last), to another range beginning at destination,...
Definition move.hpp:26
constexpr auto next(InputIt it, typename iterator_traits< InputIt >::difference_type n=1) -> InputIt
Return the nth successor of iterator it.
Definition next.hpp:14
constexpr struct etl::ranges::destroy_fn destroy
Definition adjacent_find.hpp:8
constexpr bool is_default_constructible_v
Definition is_default_constructible.hpp:26
constexpr auto exchange(T &obj, U &&newValue) noexcept(etl::is_nothrow_move_constructible_v< T > and etl::is_nothrow_assignable_v< T &, U >) -> T
Replaces the value of obj with new_value and returns the old value of obj.
Definition exchange.hpp:16
TETL_BUILTIN_PTRDIFF ptrdiff_t
etl::ptrdiff_t is the signed integer type of the result of subtracting two pointers.
Definition ptrdiff_t.hpp:14
constexpr auto uninitialized_fill(ForwardIt first, ForwardIt last, T const &value) -> void
Definition uninitialized_fill.hpp:13
TETL_BUILTIN_SIZET size_t
etl::size_t is the unsigned integer type of the result of the sizeof operator.
Definition size_t.hpp:14
static constexpr auto allocate(Alloc &a, size_type n)
Definition allocator_traits.hpp:97
static constexpr void deallocate(Alloc &a, pointer p, size_type n)
Definition allocator_traits.hpp:99
auto data() -> T *
Definition dynamic_array.hpp:81
auto begin() const -> T const *
Definition dynamic_array.hpp:87
auto end() const -> T const *
Definition dynamic_array.hpp:91
dynamic_array(dynamic_array const &other)=delete
Allocator allocator_type
Definition dynamic_array.hpp:25
etl::ptrdiff_t difference_type
Definition dynamic_array.hpp:27
etl::size_t size_type
Definition dynamic_array.hpp:26
dynamic_array()=default
auto operator=(dynamic_array const &other) -> dynamic_array &=delete
~dynamic_array()
Definition dynamic_array.hpp:71
auto data() const -> T const *
Definition dynamic_array.hpp:83
auto operator=(dynamic_array &&other) noexcept -> dynamic_array &
Definition dynamic_array.hpp:63
T * pointer
Definition dynamic_array.hpp:28
auto size() -> etl::size_t
Definition dynamic_array.hpp:77
dynamic_array(etl::size_t n, T const &value, Allocator alloc=Allocator())
Definition dynamic_array.hpp:40
auto end() -> T *
Definition dynamic_array.hpp:89
T const * const_pointer
Definition dynamic_array.hpp:29
auto begin() -> T *
Definition dynamic_array.hpp:85
dynamic_array(dynamic_array &&other) noexcept
Definition dynamic_array.hpp:56
auto size() const -> etl::size_t
Definition dynamic_array.hpp:79
dynamic_array(Allocator alloc)
Definition dynamic_array.hpp:35
dynamic_array(etl::size_t n, Allocator alloc=Allocator())
Definition dynamic_array.hpp:48
T value_type
Definition dynamic_array.hpp:24