ostream-joiner.hpp
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * Copyright (c) 2013-2023 Regents of the University of California.
4  *
5  * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
6  *
7  * ndn-cxx library is free software: you can redistribute it and/or modify it under the
8  * terms of the GNU Lesser General Public License as published by the Free Software
9  * Foundation, either version 3 of the License, or (at your option) any later version.
10  *
11  * ndn-cxx library is distributed in the hope that it will be useful, but WITHOUT ANY
12  * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
13  * PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
14  *
15  * You should have received copies of the GNU General Public License and GNU Lesser
16  * General Public License along with ndn-cxx, e.g., in COPYING.md file. If not, see
17  * <http://www.gnu.org/licenses/>.
18  *
19  * See AUTHORS.md for complete list of ndn-cxx authors and contributors.
20  */
21 
28 #ifndef NDN_CXX_UTIL_OSTREAM_JOINER_HPP
29 #define NDN_CXX_UTIL_OSTREAM_JOINER_HPP
30 
32 
33 #if __has_include(<experimental/iterator>)
34 # include <experimental/iterator>
35 # if __cpp_lib_experimental_ostream_joiner >= 201411
36 # define NDN_CXX_HAVE_EXPERIMENTAL_OSTREAM_JOINER
37 # endif
38 #endif
39 
40 #ifdef NDN_CXX_HAVE_EXPERIMENTAL_OSTREAM_JOINER
41 
42 namespace ndn {
43 using std::experimental::ostream_joiner;
45 } // namespace ndn
46 
47 #else
48 
49 #include <iterator>
50 
51 namespace ndn {
52 
53 template<typename DelimT,
54  typename CharT = char,
55  typename Traits = std::char_traits<CharT>>
57 {
58 public:
59  typedef CharT char_type;
60  typedef Traits traits_type;
61  typedef std::basic_ostream<CharT, Traits> ostream_type;
62  typedef std::output_iterator_tag iterator_category;
63  typedef void value_type;
64  typedef void difference_type;
65  typedef void pointer;
66  typedef void reference;
67 
68  ostream_joiner(ostream_type& os, const DelimT& delimiter)
69  noexcept(std::is_nothrow_copy_constructible_v<DelimT>)
70  : m_os(std::addressof(os)), m_delim(delimiter)
71  {
72  }
73 
74  ostream_joiner(ostream_type& os, DelimT&& delimiter)
75  noexcept(std::is_nothrow_move_constructible_v<DelimT>)
76  : m_os(std::addressof(os)), m_delim(std::move(delimiter))
77  {
78  }
79 
80  template<typename T>
82  operator=(const T& value)
83  {
84  if (!m_isFirst) {
85  *m_os << m_delim;
86  }
87  m_isFirst = false;
88  *m_os << value;
89  return *this;
90  }
91 
93  operator*() noexcept
94  {
95  return *this;
96  }
97 
99  operator++() noexcept
100  {
101  return *this;
102  }
103 
105  operator++(int) noexcept
106  {
107  return *this;
108  }
109 
110 private:
111  ostream_type* m_os;
112  DelimT m_delim;
113  bool m_isFirst = true;
114 };
115 
116 template<typename CharT, typename Traits, typename DelimT>
117 inline ostream_joiner<std::decay_t<DelimT>, CharT, Traits>
118 make_ostream_joiner(std::basic_ostream<CharT, Traits>& os, DelimT&& delimiter)
119 {
120  return {os, std::forward<DelimT>(delimiter)};
121 }
122 
123 } // namespace ndn
124 
125 #endif // NDN_CXX_HAVE_EXPERIMENTAL_OSTREAM_JOINER
126 #endif // NDN_CXX_UTIL_OSTREAM_JOINER_HPP
std::basic_ostream< CharT, Traits > ostream_type
ostream_joiner(ostream_type &os, DelimT &&delimiter) noexcept(std::is_nothrow_move_constructible_v< DelimT >)
ostream_joiner & operator*() noexcept
ostream_joiner & operator++(int) noexcept
ostream_joiner(ostream_type &os, const DelimT &delimiter) noexcept(std::is_nothrow_copy_constructible_v< DelimT >)
std::output_iterator_tag iterator_category
ostream_joiner & operator++() noexcept
ostream_joiner & operator=(const T &value)
Common includes and macros used throughout the library.
Definition: data.cpp:25
ostream_joiner< std::decay_t< DelimT >, CharT, Traits > make_ostream_joiner(std::basic_ostream< CharT, Traits > &os, DelimT &&delimiter)