xref: /openbsd-src/gnu/llvm/libcxx/include/__iterator/ostreambuf_iterator.h (revision a0747c9f67a4ae71ccb71e62a28d1ea19e06a63c)
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_OSTREAMBUF_ITERATOR_H
11 #define _LIBCPP___ITERATOR_OSTREAMBUF_ITERATOR_H
12 
13 #include <__config>
14 #include <__iterator/iterator.h>
15 #include <__iterator/iterator_traits.h>
16 #include <iosfwd> // for forward declaration of basic_streambuf
17 
18 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
19 #pragma GCC system_header
20 #endif
21 
22 _LIBCPP_PUSH_MACROS
23 #include <__undef_macros>
24 
25 _LIBCPP_BEGIN_NAMESPACE_STD
26 
27 _LIBCPP_SUPPRESS_DEPRECATED_PUSH
28 template <class _CharT, class _Traits>
29 class _LIBCPP_TEMPLATE_VIS ostreambuf_iterator
30 #if _LIBCPP_STD_VER <= 14 || !defined(_LIBCPP_ABI_NO_ITERATOR_BASES)
31     : public iterator<output_iterator_tag, void, void, void, void>
32 #endif
33 {
34 _LIBCPP_SUPPRESS_DEPRECATED_POP
35 public:
36     typedef output_iterator_tag                 iterator_category;
37     typedef void                                value_type;
38 #if _LIBCPP_STD_VER > 17
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 _CharT                              char_type;
46     typedef _Traits                             traits_type;
47     typedef basic_streambuf<_CharT, _Traits>    streambuf_type;
48     typedef basic_ostream<_CharT, _Traits>      ostream_type;
49 
50 private:
51     streambuf_type* __sbuf_;
52 public:
53     _LIBCPP_INLINE_VISIBILITY ostreambuf_iterator(ostream_type& __s) _NOEXCEPT
54         : __sbuf_(__s.rdbuf()) {}
55     _LIBCPP_INLINE_VISIBILITY ostreambuf_iterator(streambuf_type* __s) _NOEXCEPT
56         : __sbuf_(__s) {}
57     _LIBCPP_INLINE_VISIBILITY ostreambuf_iterator& operator=(_CharT __c)
58         {
59             if (__sbuf_ && traits_type::eq_int_type(__sbuf_->sputc(__c), traits_type::eof()))
60                 __sbuf_ = nullptr;
61             return *this;
62         }
63     _LIBCPP_INLINE_VISIBILITY ostreambuf_iterator& operator*()     {return *this;}
64     _LIBCPP_INLINE_VISIBILITY ostreambuf_iterator& operator++()    {return *this;}
65     _LIBCPP_INLINE_VISIBILITY ostreambuf_iterator& operator++(int) {return *this;}
66     _LIBCPP_INLINE_VISIBILITY bool failed() const _NOEXCEPT {return __sbuf_ == nullptr;}
67 
68     template <class _Ch, class _Tr>
69     friend
70     _LIBCPP_HIDDEN
71     ostreambuf_iterator<_Ch, _Tr>
72     __pad_and_output(ostreambuf_iterator<_Ch, _Tr> __s,
73                      const _Ch* __ob, const _Ch* __op, const _Ch* __oe,
74                      ios_base& __iob, _Ch __fl);
75 };
76 
77 _LIBCPP_END_NAMESPACE_STD
78 
79 _LIBCPP_POP_MACROS
80 
81 #endif // _LIBCPP___ITERATOR_OSTREAMBUF_ITERATOR_H
82