tetl 0.1.0
Embedded Template Library
Loading...
Searching...
No Matches
front_insert_iterator.hpp
Go to the documentation of this file.
1// SPDX-License-Identifier: BSL-1.0
2// SPDX-FileCopyrightText: Copyright (C) 2021 Tobias Hienzsch
3#ifndef TETL_ITERATOR_FRONT_INSERT_ITERATOR_HPP
4#define TETL_ITERATOR_FRONT_INSERT_ITERATOR_HPP
5
6#include <etl/_iterator/tags.hpp>
7#include <etl/_memory/addressof.hpp>
8#include <etl/_utility/move.hpp>
9
10namespace etl {
11
12/// front_insert_iterator is an LegacyOutputIterator that prepends
13/// elements to a container for which it was constructed.
14///
15/// The container's push_front() member function is called whenever the
16/// iterator (whether dereferenced or not) is assigned to. Incrementing the
17/// front_insert_iterator is a no-op.
18///
19/// \ingroup iterator
20template <typename Container>
22protected:
23 Container* _container = nullptr;
24
25public:
26 using iterator_category = output_iterator_tag;
27 using value_type = void;
28 using difference_type = void;
29 using pointer = void;
30 using reference = void;
31 using container_type = Container;
32
33 /// Initializes the underlying pointer to container with nullptr.
34 constexpr front_insert_iterator() noexcept = default;
35
36 /// Initializes the underlying pointer to the container to addressof(c).
37 constexpr explicit front_insert_iterator(Container& container)
38 : _container{etl::addressof(container)}
39 {
40 }
41
42 /// Inserts the given value value to the container.
43 constexpr auto operator=(typename Container::value_type const& value) -> front_insert_iterator&
44 {
45 _container->push_front(value);
46 return *this;
47 }
48
49 /// Inserts the given value value to the container.
50 constexpr auto operator=(typename Container::value_type&& value) -> front_insert_iterator&
51 {
52 _container->push_front(etl::move(value));
53 return *this;
54 }
55
56 /// Does nothing, this member function is provided to satisfy the
57 /// requirements of LegacyOutputIterator. It returns the iterator itself,
58 /// which makes it possible to use code such as *iter = value to output
59 /// (insert) the value into the underlying container.
60 constexpr auto operator*() -> front_insert_iterator&
61 {
62 return *this;
63 }
64
65 /// Does nothing. These operator overloads are provided to satisfy
66 /// the requirements of LegacyOutputIterator. They make it possible for the
67 /// expressions *iter++=value and *++iter=value to be used to output
68 /// (insert) a value into the underlying container.
69 constexpr auto operator++() -> front_insert_iterator&
70 {
71 return *this;
72 }
73
74 /// Does nothing. These operator overloads are provided to satisfy
75 /// the requirements of LegacyOutputIterator. They make it possible for the
76 /// expressions *iter++=value and *++iter=value to be used to output
77 /// (insert) a value into the underlying container.
78 constexpr auto operator++(int) -> front_insert_iterator
79 {
80 return *this;
81 }
82};
83
84/// front_inserter is a convenience function template that constructs a
85/// front_insert_iterator for the container c with the type deduced from
86/// the type of the argument.
87template <typename Container>
88[[nodiscard]] constexpr auto front_inserter(Container& c) -> front_insert_iterator<Container>
89{
90 return front_insert_iterator<Container>(c);
91}
92
93} // namespace etl
94
95#endif // TETL_ITERATOR_FRONT_INSERT_ITERATOR_HPP
Definition adjacent_find.hpp:9
constexpr auto front_inserter(Container &c) -> front_insert_iterator< Container >
front_inserter is a convenience function template that constructs a front_insert_iterator for the con...
Definition front_insert_iterator.hpp:88
front_insert_iterator is an LegacyOutputIterator that prepends elements to a container for which it w...
Definition front_insert_iterator.hpp:21
constexpr front_insert_iterator() noexcept=default
Initializes the underlying pointer to container with nullptr.
Container * _container
Definition front_insert_iterator.hpp:23
constexpr auto operator*() -> front_insert_iterator &
Does nothing, this member function is provided to satisfy the requirements of LegacyOutputIterator....
Definition front_insert_iterator.hpp:60
constexpr auto operator++(int) -> front_insert_iterator
Does nothing. These operator overloads are provided to satisfy the requirements of LegacyOutputIterat...
Definition front_insert_iterator.hpp:78
constexpr auto operator=(typename Container::value_type const &value) -> front_insert_iterator &
Inserts the given value value to the container.
Definition front_insert_iterator.hpp:43
constexpr auto operator=(typename Container::value_type &&value) -> front_insert_iterator &
Inserts the given value value to the container.
Definition front_insert_iterator.hpp:50
constexpr front_insert_iterator(Container &container)
Initializes the underlying pointer to the container to addressof(c).
Definition front_insert_iterator.hpp:37
constexpr auto operator++() -> front_insert_iterator &
Does nothing. These operator overloads are provided to satisfy the requirements of LegacyOutputIterat...
Definition front_insert_iterator.hpp:69
Defines the category of an iterator. Each tag is an empty type and corresponds to one of the five (un...
Definition tags.hpp:19