-
Notifications
You must be signed in to change notification settings - Fork 0
/
ostream_joiner
76 lines (63 loc) · 1.92 KB
/
ostream_joiner
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
// N4257 - Delimited iterators (Rev. 4)
#include <type_traits> // For decay_t
namespace std
{
namespace experimental
{
inline namespace fundamentals_v2
{
#define __cpp_lib_experimental_ostream_joiner 201411
template<typename _DelimT, typename _CharT = char,
typename _Traits = char_traits<_CharT>>
class ostream_joiner
{
public:
using char_type = _CharT;
using traits_type = _Traits;
using ostream_type = basic_ostream<_CharT, _Traits>;
using iterator_category = output_iterator_tag;
using value_type = void;
using difference_type = void;
using pointer = void;
using reference = void;
ostream_joiner(ostream_type& __os, _DelimT&& __delim)
: _M_os{&__os}, _M_delim{std::move(__delim)}, _M_first{true}
{ }
ostream_joiner(ostream_type& __os, const _DelimT& __delim)
: _M_os{&__os}, _M_delim{__delim}, _M_first{true}
{ }
template<typename _Tp>
ostream_joiner<_DelimT, _CharT, _Traits>&
operator=(const _Tp& __value)
{
if (!_M_first)
*_M_os << _M_delim;
_M_first = false;
*_M_os << __value;
return *this;
}
ostream_joiner<_DelimT, _CharT, _Traits>&
operator*()
{ return *this; }
ostream_joiner<_DelimT, _CharT, _Traits>&
operator++()
{ return *this; }
ostream_joiner<_DelimT, _CharT, _Traits>&
operator++(int)
{ return *this; }
private:
basic_ostream<_CharT, _Traits>* _M_os;
_DelimT _M_delim;
bool _M_first;
};
template<typename _CharT, typename _Traits, typename _DelimT>
ostream_joiner<decay_t<_DelimT>, _CharT, _Traits>
make_ostream_joiner(basic_ostream<_CharT, _Traits>& __os,
_DelimT&& __delim)
{
using __joiner_t = ostream_joiner<decay_t<_DelimT>, _CharT, _Traits>;
return __joiner_t(__os, forward<_DelimT>(__delim));
}
} // inline namespace fundamentals_v2
} // namespace experimental
} // namespace std