iterator 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. // <experimental/iterator> -*- C++ -*-
  2. // Copyright (C) 2015-2018 Free Software Foundation, Inc.
  3. //
  4. // This file is part of the GNU ISO C++ Library. This library is free
  5. // software; you can redistribute it and/or modify it under the
  6. // terms of the GNU General Public License as published by the
  7. // Free Software Foundation; either version 3, or (at your option)
  8. // any later version.
  9. // This library is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU General Public License for more details.
  13. // Under Section 7 of GPL version 3, you are granted additional
  14. // permissions described in the GCC Runtime Library Exception, version
  15. // 3.1, as published by the Free Software Foundation.
  16. // You should have received a copy of the GNU General Public License and
  17. // a copy of the GCC Runtime Library Exception along with this program;
  18. // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
  19. // <http://www.gnu.org/licenses/>.
  20. /** @file experimental/iterator
  21. * This is a TS C++ Library header.
  22. */
  23. //
  24. // N4336 Working Draft, C++ Extensions for Library Fundamentals, Version 2
  25. //
  26. #ifndef _GLIBCXX_EXPERIMENTAL_ITERATOR
  27. #define _GLIBCXX_EXPERIMENTAL_ITERATOR 1
  28. #pragma GCC system_header
  29. #if __cplusplus >= 201402L
  30. #include <iterator>
  31. #include <iosfwd>
  32. #include <experimental/type_traits>
  33. namespace std _GLIBCXX_VISIBILITY(default)
  34. {
  35. _GLIBCXX_BEGIN_NAMESPACE_VERSION
  36. namespace experimental
  37. {
  38. inline namespace fundamentals_v2
  39. {
  40. #define __cpp_lib_experimental_ostream_joiner 201411
  41. /// Output iterator that inserts a delimiter between elements.
  42. template<typename _DelimT, typename _CharT = char,
  43. typename _Traits = char_traits<_CharT>>
  44. class ostream_joiner
  45. {
  46. public:
  47. typedef _CharT char_type;
  48. typedef _Traits traits_type;
  49. typedef basic_ostream<_CharT, _Traits> ostream_type;
  50. typedef output_iterator_tag iterator_category;
  51. typedef void value_type;
  52. typedef void difference_type;
  53. typedef void pointer;
  54. typedef void reference;
  55. ostream_joiner(ostream_type& __os, const _DelimT& __delimiter)
  56. noexcept(is_nothrow_copy_constructible_v<_DelimT>)
  57. : _M_out(std::__addressof(__os)), _M_delim(__delimiter)
  58. { }
  59. ostream_joiner(ostream_type& __os, _DelimT&& __delimiter)
  60. noexcept(is_nothrow_move_constructible_v<_DelimT>)
  61. : _M_out(std::__addressof(__os)), _M_delim(std::move(__delimiter))
  62. { }
  63. template<typename _Tp>
  64. ostream_joiner&
  65. operator=(const _Tp& __value)
  66. {
  67. if (!_M_first)
  68. *_M_out << _M_delim;
  69. _M_first = false;
  70. *_M_out << __value;
  71. return *this;
  72. }
  73. ostream_joiner& operator*() noexcept { return *this; }
  74. ostream_joiner& operator++() noexcept { return *this; }
  75. ostream_joiner& operator++(int) noexcept { return *this; }
  76. private:
  77. ostream_type* _M_out;
  78. _DelimT _M_delim;
  79. bool _M_first = true;
  80. };
  81. /// Object generator for ostream_joiner.
  82. template<typename _CharT, typename _Traits, typename _DelimT>
  83. inline ostream_joiner<decay_t<_DelimT>, _CharT, _Traits>
  84. make_ostream_joiner(basic_ostream<_CharT, _Traits>& __os,
  85. _DelimT&& __delimiter)
  86. { return { __os, std::forward<_DelimT>(__delimiter) }; }
  87. } // namespace fundamentals_v2
  88. } // namespace experimental
  89. _GLIBCXX_END_NAMESPACE_VERSION
  90. } // namespace std
  91. #endif // __cplusplus <= 201103L
  92. #endif // _GLIBCXX_EXPERIMENTAL_ITERATOR