1// -*- C++ -*-
2//===----------------------------------------------------------------------===//
3//
4// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
5// See https://llvm.org/LICENSE.txt for license information.
6// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7//
8//===----------------------------------------------------------------------===//
9
10#ifndef _LIBCPP___ITERATOR_BACK_INSERT_ITERATOR_H
11#define _LIBCPP___ITERATOR_BACK_INSERT_ITERATOR_H
12
13#include <__config>
14#include <__cstddef/ptrdiff_t.h>
15#include <__iterator/iterator.h>
16#include <__iterator/iterator_traits.h>
17#include <__memory/addressof.h>
18#include <__utility/move.h>
19
20#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
21# pragma GCC system_header
22#endif
23
24_LIBCPP_PUSH_MACROS
25#include <__undef_macros>
26
27_LIBCPP_BEGIN_NAMESPACE_STD
28
29template <class _Container>
30class back_insert_iterator
31 : public __iterator_base<back_insert_iterator<_Container>, output_iterator_tag, void, void, void, void> {
32protected:
33 _Container* container;
34
35public:
36 typedef output_iterator_tag iterator_category;
37 typedef void value_type;
38#if _LIBCPP_STD_VER >= 20
39 typedef ptrdiff_t difference_type;
40#else
41 typedef void difference_type;
42#endif
43 typedef void pointer;
44 typedef void reference;
45 typedef _Container container_type;
46
47 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 explicit back_insert_iterator(_Container& __x)
48 : container(std::addressof(__x)) {}
49 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 back_insert_iterator&
50 operator=(const typename _Container::value_type& __value) {
51 container->push_back(__value);
52 return *this;
53 }
54#ifndef _LIBCPP_CXX03_LANG
55 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 back_insert_iterator&
56 operator=(typename _Container::value_type&& __value) {
57 container->push_back(std::move(__value));
58 return *this;
59 }
60#endif // _LIBCPP_CXX03_LANG
61 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 back_insert_iterator& operator*() { return *this; }
62 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 back_insert_iterator& operator++() { return *this; }
63 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 back_insert_iterator operator++(int) { return *this; }
64
65 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 _Container* __get_container() const { return container; }
66};
67_LIBCPP_CTAD_SUPPORTED_FOR_TYPE(back_insert_iterator);
68
69template <class _Container>
70inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 back_insert_iterator<_Container>
71back_inserter(_Container& __x) {
72 return back_insert_iterator<_Container>(__x);
73}
74
75_LIBCPP_END_NAMESPACE_STD
76
77_LIBCPP_POP_MACROS
78
79#endif // _LIBCPP___ITERATOR_BACK_INSERT_ITERATOR_H