Files
llvm-project/libcxx/include/__iterator/ostreambuf_iterator.h
Nikolas Klauser b101f01382 [libc++] Optimize using std::copy with an ostreambuf_iterator (#181815)
```
Benchmark                                                  old             new    Difference    % Difference
----------------------------------------------  --------------  --------------  ------------  --------------
std::copy(CharT*,_CharT*,_ostreambuf_iterator)         8115.45          329.54      -7785.91         -95.94%
```
2026-02-26 12:01:07 +01:00

95 lines
3.5 KiB
C++

// -*- C++ -*-
//===----------------------------------------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
#ifndef _LIBCPP___ITERATOR_OSTREAMBUF_ITERATOR_H
#define _LIBCPP___ITERATOR_OSTREAMBUF_ITERATOR_H
#include <__algorithm/specialized_algorithms.h>
#include <__config>
#include <__cstddef/ptrdiff_t.h>
#include <__fwd/ios.h>
#include <__fwd/ostream.h>
#include <__fwd/streambuf.h>
#include <__iterator/iterator.h>
#include <__iterator/iterator_traits.h>
#include <__type_traits/is_same.h>
#include <__utility/pair.h>
#include <iosfwd> // for forward declaration of ostreambuf_iterator
#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
# pragma GCC system_header
#endif
_LIBCPP_BEGIN_NAMESPACE_STD
template <class _CharT, class _Traits>
class ostreambuf_iterator
: public __iterator_base<ostreambuf_iterator<_CharT, _Traits>, output_iterator_tag, void, void, void, void> {
public:
typedef output_iterator_tag iterator_category;
typedef void value_type;
#if _LIBCPP_STD_VER >= 20
typedef ptrdiff_t difference_type;
#else
typedef void difference_type;
#endif
typedef void pointer;
typedef void reference;
typedef _CharT char_type;
typedef _Traits traits_type;
typedef basic_streambuf<_CharT, _Traits> streambuf_type;
typedef basic_ostream<_CharT, _Traits> ostream_type;
private:
streambuf_type* __sbuf_;
public:
_LIBCPP_HIDE_FROM_ABI ostreambuf_iterator(ostream_type& __s) _NOEXCEPT : __sbuf_(__s.rdbuf()) {}
_LIBCPP_HIDE_FROM_ABI ostreambuf_iterator(streambuf_type* __s) _NOEXCEPT : __sbuf_(__s) {}
_LIBCPP_HIDE_FROM_ABI ostreambuf_iterator& operator=(_CharT __c) {
if (__sbuf_ && traits_type::eq_int_type(__sbuf_->sputc(__c), traits_type::eof()))
__sbuf_ = nullptr;
return *this;
}
_LIBCPP_HIDE_FROM_ABI ostreambuf_iterator& operator*() { return *this; }
_LIBCPP_HIDE_FROM_ABI ostreambuf_iterator& operator++() { return *this; }
_LIBCPP_HIDE_FROM_ABI ostreambuf_iterator& operator++(int) { return *this; }
_LIBCPP_HIDE_FROM_ABI bool failed() const _NOEXCEPT { return __sbuf_ == nullptr; }
#if _LIBCPP_HAS_LOCALIZATION
template <class _Ch, class _Tr>
friend _LIBCPP_HIDE_FROM_ABI ostreambuf_iterator<_Ch, _Tr> __pad_and_output(
ostreambuf_iterator<_Ch, _Tr> __s, const _Ch* __ob, const _Ch* __op, const _Ch* __oe, ios_base& __iob, _Ch __fl);
#endif // _LIBCPP_HAS_LOCALIZATION
template <class, class...>
friend struct __specialized_algorithm;
};
template <class _InCharT, class _CharT, class _Traits>
struct __specialized_algorithm<_Algorithm::__copy,
__iterator_pair<_InCharT*, _InCharT*>,
__single_iterator<ostreambuf_iterator<_CharT, _Traits> > > {
static const bool __has_algorithm = is_same<const _CharT, const _InCharT>::value;
_LIBCPP_HIDE_FROM_ABI static pair<_InCharT*, ostreambuf_iterator<_CharT, _Traits> >
operator()(_InCharT* __first, _InCharT* __last, ostreambuf_iterator<_CharT, _Traits> __result) {
auto __size = __last - __first;
if (__result.__sbuf_ && __size > 0) {
if (__result.__sbuf_->sputn(__first, __last - __first) != __size)
__result.__sbuf_ = nullptr;
}
return pair<_InCharT*, ostreambuf_iterator<_CharT, _Traits> >(__last, __result);
}
};
_LIBCPP_END_NAMESPACE_STD
#endif // _LIBCPP___ITERATOR_OSTREAMBUF_ITERATOR_H