1// <experimental/iterator> -*- C++ -*-
2
3// Copyright (C) 2015-2016 Free Software Foundation, Inc.
4//
5// This file is part of the GNU ISO C++ Library.  This library is free
6// software; you can redistribute it and/or modify it under the
7// terms of the GNU General Public License as published by the
8// Free Software Foundation; either version 3, or (at your option)
9// any later version.
10
11// This library is distributed in the hope that it will be useful,
12// but WITHOUT ANY WARRANTY; without even the implied warranty of
13// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14// GNU General Public License for more details.
15
16// Under Section 7 of GPL version 3, you are granted additional
17// permissions described in the GCC Runtime Library Exception, version
18// 3.1, as published by the Free Software Foundation.
19
20// You should have received a copy of the GNU General Public License and
21// a copy of the GCC Runtime Library Exception along with this program;
22// see the files COPYING3 and COPYING.RUNTIME respectively.  If not, see
23// <http://www.gnu.org/licenses/>.
24
25/** @file experimental/iterator
26 *  This is a TS C++ Library header.
27 */
28
29//
30// N4336 Working Draft, C++ Extensions for Library Fundamentals, Version 2
31//
32
33#ifndef _GLIBCXX_EXPERIMENTAL_ITERATOR
34#define _GLIBCXX_EXPERIMENTAL_ITERATOR 1
35
36#pragma GCC system_header
37
38#if __cplusplus <= 201103L
39# include <bits/c++14_warning.h>
40#else
41
42#include <iterator>
43#include <iosfwd>
44#include <experimental/type_traits>
45
46namespace std _GLIBCXX_VISIBILITY(default)
47{
48namespace experimental
49{
50inline namespace fundamentals_v2
51{
52_GLIBCXX_BEGIN_NAMESPACE_VERSION
53
54#define __cpp_lib_experimental_ostream_joiner 201411
55
56  /// Output iterator that inserts a delimiter between elements.
57  template<typename _DelimT, typename _CharT = char,
58	   typename _Traits = char_traits<_CharT>>
59  class ostream_joiner
60  {
61  public:
62    typedef _CharT				char_type;
63    typedef _Traits				traits_type;
64    typedef basic_ostream<_CharT, _Traits>	ostream_type;
65    typedef output_iterator_tag			iterator_category;
66    typedef void				value_type;
67    typedef void				difference_type;
68    typedef void				pointer;
69    typedef void				reference;
70
71    ostream_joiner(ostream_type& __os, const _DelimT& __delimiter)
72    noexcept(is_nothrow_copy_constructible_v<_DelimT>)
73    : _M_out(std::__addressof(__os)), _M_delim(__delimiter)
74    { }
75
76    ostream_joiner(ostream_type& __os, _DelimT&& __delimiter)
77    noexcept(is_nothrow_move_constructible_v<_DelimT>)
78    : _M_out(std::__addressof(__os)), _M_delim(std::move(__delimiter))
79    { }
80
81    template<typename _Tp>
82      ostream_joiner&
83      operator=(const _Tp& __value)
84      {
85	if (!_M_first)
86	  *_M_out << _M_delim;
87	_M_first = false;
88	*_M_out << __value;
89	return *this;
90      }
91
92    ostream_joiner& operator*() noexcept { return *this; }
93    ostream_joiner& operator++() noexcept { return *this; }
94    ostream_joiner& operator++(int) noexcept { return *this; }
95
96  private:
97    ostream_type* _M_out;
98    _DelimT _M_delim;
99    bool _M_first = true;
100  };
101
102  /// Object generator for ostream_joiner.
103  template<typename _CharT, typename _Traits, typename _DelimT>
104    inline ostream_joiner<decay_t<_DelimT>, _CharT, _Traits>
105    make_ostream_joiner(basic_ostream<_CharT, _Traits>& __os,
106			_DelimT&& __delimiter)
107    { return { __os, std::forward<_DelimT>(__delimiter) }; }
108
109_GLIBCXX_END_NAMESPACE_VERSION
110} // namespace fundamentals_v2
111} // namespace experimental
112} // namespace std
113
114#endif // __cplusplus <= 201103L
115
116#endif // _GLIBCXX_EXPERIMENTAL_ITERATOR
117