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