xref: /llvm-project/libcxx/include/experimental/iterator (revision b9a2658a3e8bd13b0f9e7a8a440832a95b377216)
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_EXPERIMENTAL_ITERATOR
11#define _LIBCPP_EXPERIMENTAL_ITERATOR
12
13/*
14namespace std {
15  namespace experimental {
16    inline namespace fundamentals_v2 {
17
18    template <class DelimT, class charT = char, class traits = char_traits<charT>>
19        class ostream_joiner {
20        public:
21         typedef charT                        char_type;
22         typedef traits                       traits_type;
23         typedef basic_ostream<charT, traits> ostream_type;
24         typedef output_iterator_tag          iterator_category;
25         typedef void                         value_type;
26         typedef void                         difference_type;
27         typedef void                         pointer;
28         typedef void                         reference;
29
30         ostream_joiner(ostream_type& s, const DelimT& delimiter);
31         ostream_joiner(ostream_type& s, DelimT&& delimiter);
32
33         template<typename T>
34         ostream_joiner& operator=(const T& value);
35
36         ostream_joiner& operator*() noexcept;
37         ostream_joiner& operator++() noexcept;
38         ostream_joiner& operator++(int) noexcept;
39   private:
40      ostream_type* out_stream;   // exposition only
41      DelimT delim;               // exposition only
42      bool first_element;         // exposition only
43   };
44
45  template <class charT, class traits, class DelimT>
46    ostream_joiner<decay_t<DelimT>, charT, traits>
47    make_ostream_joiner(basic_ostream<charT, traits>& os, DelimT&& delimiter);
48
49    } // inline namespace fundamentals_v2
50  } // namespace experimental
51} // namespace std
52
53*/
54
55#if __cplusplus < 201103L && defined(_LIBCPP_USE_FROZEN_CXX03_HEADERS)
56#  include <__cxx03/experimental/iterator>
57#else
58#  include <__config>
59#  include <__memory/addressof.h>
60#  include <__ostream/basic_ostream.h>
61#  include <__string/char_traits.h>
62#  include <__type_traits/decay.h>
63#  include <__utility/forward.h>
64#  include <__utility/move.h>
65#  include <iterator>
66
67#  if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
68#    pragma GCC system_header
69#  endif
70
71_LIBCPP_PUSH_MACROS
72#  include <__undef_macros>
73
74#  if _LIBCPP_STD_VER >= 14
75
76_LIBCPP_BEGIN_NAMESPACE_LFTS
77
78template <class _Delim, class _CharT = char, class _Traits = char_traits<_CharT>>
79class ostream_joiner {
80public:
81  typedef _CharT char_type;
82  typedef _Traits traits_type;
83  typedef basic_ostream<char_type, traits_type> ostream_type;
84  typedef output_iterator_tag iterator_category;
85  typedef void value_type;
86  typedef void difference_type;
87  typedef void pointer;
88  typedef void reference;
89
90  _LIBCPP_HIDE_FROM_ABI ostream_joiner(ostream_type& __os, _Delim&& __d)
91      : __output_iter_(std::addressof(__os)), __delim_(std::move(__d)), __first_(true) {}
92
93  _LIBCPP_HIDE_FROM_ABI ostream_joiner(ostream_type& __os, const _Delim& __d)
94      : __output_iter_(std::addressof(__os)), __delim_(__d), __first_(true) {}
95
96  template <typename _Tp>
97  _LIBCPP_HIDE_FROM_ABI ostream_joiner& operator=(const _Tp& __v) {
98    if (!__first_)
99      *__output_iter_ << __delim_;
100    __first_ = false;
101    *__output_iter_ << __v;
102    return *this;
103  }
104
105  _LIBCPP_HIDE_FROM_ABI ostream_joiner& operator*() _NOEXCEPT { return *this; }
106  _LIBCPP_HIDE_FROM_ABI ostream_joiner& operator++() _NOEXCEPT { return *this; }
107  _LIBCPP_HIDE_FROM_ABI ostream_joiner& operator++(int) _NOEXCEPT { return *this; }
108
109private:
110  ostream_type* __output_iter_;
111  _Delim __delim_;
112  bool __first_;
113};
114
115template <class _CharT, class _Traits, class _Delim>
116_LIBCPP_HIDE_FROM_ABI ostream_joiner<__decay_t<_Delim>, _CharT, _Traits>
117make_ostream_joiner(basic_ostream<_CharT, _Traits>& __os, _Delim&& __d) {
118  return ostream_joiner<__decay_t<_Delim>, _CharT, _Traits>(__os, std::forward<_Delim>(__d));
119}
120
121_LIBCPP_END_NAMESPACE_LFTS
122
123#  endif // _LIBCPP_STD_VER >= 14
124
125_LIBCPP_POP_MACROS
126
127#  if !defined(_LIBCPP_REMOVE_TRANSITIVE_INCLUDES) && _LIBCPP_STD_VER <= 20
128#    include <cstddef>
129#    include <iosfwd>
130#    include <type_traits>
131#  endif
132#endif // __cplusplus < 201103L && defined(_LIBCPP_USE_FROZEN_CXX03_HEADERS)
133
134#endif // _LIBCPP_EXPERIMENTAL_ITERATOR
135