File indexing completed on 2025-12-16 09:53:47
0001
0002
0003
0004
0005
0006
0007
0008 #ifndef BOOST_IO_OSTREAM_JOINER_HPP
0009 #define BOOST_IO_OSTREAM_JOINER_HPP
0010
0011 #include <boost/config.hpp>
0012 #include <ostream>
0013 #include <string>
0014 #if !defined(BOOST_NO_CXX11_RVALUE_REFERENCES)
0015 #if !defined(BOOST_NO_CXX11_HDR_TYPE_TRAITS)
0016 #include <type_traits>
0017 #endif
0018 #include <utility>
0019 #endif
0020
0021 namespace boost {
0022 namespace io {
0023 namespace detail {
0024
0025 #if !defined(BOOST_NO_CXX11_ADDRESSOF)
0026 template<class T>
0027 inline T*
0028 osj_address(T& o)
0029 {
0030 return std::addressof(o);
0031 }
0032 #else
0033 template<class T>
0034 inline T*
0035 osj_address(T& obj)
0036 {
0037 return &obj;
0038 }
0039 #endif
0040
0041 }
0042
0043 template<class Delim, class Char = char,
0044 class Traits = std::char_traits<Char> >
0045 class ostream_joiner {
0046 public:
0047 typedef Char char_type;
0048 typedef Traits traits_type;
0049 typedef std::basic_ostream<Char, Traits> ostream_type;
0050 typedef std::output_iterator_tag iterator_category;
0051 typedef void value_type;
0052 typedef void difference_type;
0053 typedef void pointer;
0054 typedef void reference;
0055
0056 ostream_joiner(ostream_type& output, const Delim& delim)
0057 : output_(detail::osj_address(output))
0058 , delim_(delim)
0059 , first_(true) { }
0060
0061 #if !defined(BOOST_NO_CXX11_RVALUE_REFERENCES)
0062 ostream_joiner(ostream_type& output, Delim&& delim)
0063 : output_(detail::osj_address(output))
0064 , delim_(std::move(delim))
0065 , first_(true) { }
0066 #endif
0067
0068 template<class T>
0069 ostream_joiner& operator=(const T& value) {
0070 if (!first_) {
0071 *output_ << delim_;
0072 }
0073 first_ = false;
0074 *output_ << value;
0075 return *this;
0076 }
0077
0078 ostream_joiner& operator*() BOOST_NOEXCEPT {
0079 return *this;
0080 }
0081
0082 ostream_joiner& operator++() BOOST_NOEXCEPT {
0083 return *this;
0084 }
0085
0086 ostream_joiner& operator++(int) BOOST_NOEXCEPT {
0087 return *this;
0088 }
0089
0090 private:
0091 ostream_type* output_;
0092 Delim delim_;
0093 bool first_;
0094 };
0095
0096 #if !defined(BOOST_NO_CXX11_RVALUE_REFERENCES) && \
0097 !defined(BOOST_NO_CXX11_HDR_TYPE_TRAITS)
0098 template<class Char, class Traits, class Delim>
0099 inline ostream_joiner<typename std::decay<Delim>::type, Char, Traits>
0100 make_ostream_joiner(std::basic_ostream<Char, Traits>& output, Delim&& delim)
0101 {
0102 return ostream_joiner<typename std::decay<Delim>::type, Char,
0103 Traits>(output, std::forward<Delim>(delim));
0104 }
0105 #else
0106 template<class Char, class Traits, class Delim>
0107 inline ostream_joiner<Delim, Char, Traits>
0108 make_ostream_joiner(std::basic_ostream<Char, Traits>& output,
0109 const Delim& delim)
0110 {
0111 return ostream_joiner<Delim, Char, Traits>(output, delim);
0112 }
0113 #endif
0114
0115 }
0116 }
0117
0118 #endif