iterator 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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 <__utility/forward.h>
  47. #include <__utility/move.h>
  48. #include <experimental/__config>
  49. #include <iterator>
  50. #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
  51. # pragma GCC system_header
  52. #endif
  53. #if _LIBCPP_STD_VER > 11
  54. _LIBCPP_BEGIN_NAMESPACE_LFTS
  55. template <class _Delim, class _CharT = char, class _Traits = char_traits<_CharT>>
  56. class ostream_joiner {
  57. public:
  58. typedef _CharT char_type;
  59. typedef _Traits traits_type;
  60. typedef basic_ostream<char_type,traits_type> ostream_type;
  61. typedef output_iterator_tag iterator_category;
  62. typedef void value_type;
  63. typedef void difference_type;
  64. typedef void pointer;
  65. typedef void reference;
  66. ostream_joiner(ostream_type& __os, _Delim&& __d)
  67. : __output_iter(_VSTD::addressof(__os)), __delim(_VSTD::move(__d)), __first(true) {}
  68. ostream_joiner(ostream_type& __os, const _Delim& __d)
  69. : __output_iter(_VSTD::addressof(__os)), __delim(__d), __first(true) {}
  70. template<typename _Tp>
  71. ostream_joiner& operator=(const _Tp& __v)
  72. {
  73. if (!__first)
  74. *__output_iter << __delim;
  75. __first = false;
  76. *__output_iter << __v;
  77. return *this;
  78. }
  79. ostream_joiner& operator*() _NOEXCEPT { return *this; }
  80. ostream_joiner& operator++() _NOEXCEPT { return *this; }
  81. ostream_joiner& operator++(int) _NOEXCEPT { return *this; }
  82. private:
  83. ostream_type* __output_iter;
  84. _Delim __delim;
  85. bool __first;
  86. };
  87. template <class _CharT, class _Traits, class _Delim>
  88. ostream_joiner<typename decay<_Delim>::type, _CharT, _Traits>
  89. make_ostream_joiner(basic_ostream<_CharT, _Traits>& __os, _Delim && __d)
  90. { return ostream_joiner<typename decay<_Delim>::type, _CharT, _Traits>(__os, _VSTD::forward<_Delim>(__d)); }
  91. _LIBCPP_END_NAMESPACE_LFTS
  92. #endif // _LIBCPP_STD_VER > 11
  93. #endif // _LIBCPP_EXPERIMENTAL_ITERATOR