iterator 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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. #ifndef _LIBCPP_EXPERIMENTAL_ITERATOR
  10. #define _LIBCPP_EXPERIMENTAL_ITERATOR
  11. /*
  12. namespace std {
  13. namespace experimental {
  14. inline namespace fundamentals_v2 {
  15. template <class DelimT, class charT = char, class traits = char_traits<charT>>
  16. class ostream_joiner {
  17. public:
  18. typedef charT char_type;
  19. typedef traits traits_type;
  20. typedef basic_ostream<charT, traits> ostream_type;
  21. typedef output_iterator_tag iterator_category;
  22. typedef void value_type;
  23. typedef void difference_type;
  24. typedef void pointer;
  25. typedef void reference;
  26. ostream_joiner(ostream_type& s, const DelimT& delimiter);
  27. ostream_joiner(ostream_type& s, DelimT&& delimiter);
  28. template<typename T>
  29. ostream_joiner& operator=(const T& value);
  30. ostream_joiner& operator*() noexcept;
  31. ostream_joiner& operator++() noexcept;
  32. ostream_joiner& operator++(int) noexcept;
  33. private:
  34. ostream_type* out_stream; // exposition only
  35. DelimT delim; // exposition only
  36. bool first_element; // exposition only
  37. };
  38. template <class charT, class traits, class DelimT>
  39. ostream_joiner<decay_t<DelimT>, charT, traits>
  40. make_ostream_joiner(basic_ostream<charT, traits>& os, DelimT&& delimiter);
  41. } // inline namespace fundamentals_v2
  42. } // namespace experimental
  43. } // namespace std
  44. */
  45. #include <__memory/addressof.h>
  46. #include <__type_traits/decay.h>
  47. #include <__utility/forward.h>
  48. #include <__utility/move.h>
  49. #include <experimental/__config>
  50. #include <iterator>
  51. #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
  52. # pragma GCC system_header
  53. #endif
  54. _LIBCPP_PUSH_MACROS
  55. #include <__undef_macros>
  56. #if _LIBCPP_STD_VER >= 14
  57. _LIBCPP_BEGIN_NAMESPACE_LFTS
  58. template <class _Delim, class _CharT = char, class _Traits = char_traits<_CharT>>
  59. class ostream_joiner {
  60. public:
  61. typedef _CharT char_type;
  62. typedef _Traits traits_type;
  63. typedef basic_ostream<char_type, traits_type> ostream_type;
  64. typedef output_iterator_tag iterator_category;
  65. typedef void value_type;
  66. typedef void difference_type;
  67. typedef void pointer;
  68. typedef void reference;
  69. _LIBCPP_HIDE_FROM_ABI ostream_joiner(ostream_type& __os, _Delim&& __d)
  70. : __output_iter_(std::addressof(__os)), __delim_(std::move(__d)), __first_(true) {}
  71. _LIBCPP_HIDE_FROM_ABI ostream_joiner(ostream_type& __os, const _Delim& __d)
  72. : __output_iter_(std::addressof(__os)), __delim_(__d), __first_(true) {}
  73. template <typename _Tp>
  74. _LIBCPP_HIDE_FROM_ABI ostream_joiner& operator=(const _Tp& __v) {
  75. if (!__first_)
  76. *__output_iter_ << __delim_;
  77. __first_ = false;
  78. *__output_iter_ << __v;
  79. return *this;
  80. }
  81. _LIBCPP_HIDE_FROM_ABI ostream_joiner& operator*() _NOEXCEPT { return *this; }
  82. _LIBCPP_HIDE_FROM_ABI ostream_joiner& operator++() _NOEXCEPT { return *this; }
  83. _LIBCPP_HIDE_FROM_ABI ostream_joiner& operator++(int) _NOEXCEPT { return *this; }
  84. private:
  85. ostream_type* __output_iter_;
  86. _Delim __delim_;
  87. bool __first_;
  88. };
  89. template <class _CharT, class _Traits, class _Delim>
  90. _LIBCPP_HIDE_FROM_ABI ostream_joiner<__decay_t<_Delim>, _CharT, _Traits>
  91. make_ostream_joiner(basic_ostream<_CharT, _Traits>& __os, _Delim&& __d) {
  92. return ostream_joiner<__decay_t<_Delim>, _CharT, _Traits>(__os, std::forward<_Delim>(__d));
  93. }
  94. _LIBCPP_END_NAMESPACE_LFTS
  95. #endif // _LIBCPP_STD_VER >= 14
  96. _LIBCPP_POP_MACROS
  97. #if !defined(_LIBCPP_REMOVE_TRANSITIVE_INCLUDES) && _LIBCPP_STD_VER <= 20
  98. # include <iosfwd>
  99. # include <type_traits>
  100. #endif
  101. #endif // _LIBCPP_EXPERIMENTAL_ITERATOR