blob: 1ba13808a8c2c3d6776e88f6ba37386f50883c58 [file] [log] [blame]
/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
/*
* Copyright (c) 2013-2018 Regents of the University of California.
*
* This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
*
* ndn-cxx library is free software: you can redistribute it and/or modify it under the
* terms of the GNU Lesser General Public License as published by the Free Software
* Foundation, either version 3 of the License, or (at your option) any later version.
*
* ndn-cxx library is distributed in the hope that it will be useful, but WITHOUT ANY
* WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
* PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
*
* You should have received copies of the GNU General Public License and GNU Lesser
* General Public License along with ndn-cxx, e.g., in COPYING.md file. If not, see
* <http://www.gnu.org/licenses/>.
*
* See AUTHORS.md for complete list of ndn-cxx authors and contributors.
*/
/** \file
* \brief Backport of ostream_joiner from the Library Fundamentals v2 TS
* \sa http://en.cppreference.com/w/cpp/experimental/ostream_joiner
*/
#ifndef NDN_UTIL_BACKPORTS_OSTREAM_JOINER_HPP
#define NDN_UTIL_BACKPORTS_OSTREAM_JOINER_HPP
#include "backports.hpp"
#if NDN_CXX_HAS_INCLUDE(<experimental/iterator>)
# include <experimental/iterator>
# if __cpp_lib_experimental_ostream_joiner >= 201411
# define NDN_CXX_HAVE_EXPERIMENTAL_OSTREAM_JOINER
# endif
#endif
#ifdef NDN_CXX_HAVE_EXPERIMENTAL_OSTREAM_JOINER
namespace ndn {
using std::experimental::ostream_joiner;
using std::experimental::make_ostream_joiner;
} // namespace ndn
#else
#include <iterator>
namespace ndn {
template<typename DelimT,
typename CharT = char,
typename Traits = std::char_traits<CharT>>
class ostream_joiner
{
public:
typedef CharT char_type;
typedef Traits traits_type;
typedef std::basic_ostream<CharT, Traits> ostream_type;
typedef std::output_iterator_tag iterator_category;
typedef void value_type;
typedef void difference_type;
typedef void pointer;
typedef void reference;
ostream_joiner(ostream_type& os, const DelimT& delimiter)
noexcept(std::is_nothrow_copy_constructible<DelimT>::value)
: m_os(std::addressof(os)), m_delim(delimiter)
{
}
ostream_joiner(ostream_type& os, DelimT&& delimiter)
noexcept(std::is_nothrow_move_constructible<DelimT>::value)
: m_os(std::addressof(os)), m_delim(std::move(delimiter))
{
}
template<typename T>
ostream_joiner&
operator=(const T& value)
{
if (!m_isFirst) {
*m_os << m_delim;
}
m_isFirst = false;
*m_os << value;
return *this;
}
ostream_joiner&
operator*() noexcept
{
return *this;
}
ostream_joiner&
operator++() noexcept
{
return *this;
}
ostream_joiner&
operator++(int) noexcept
{
return *this;
}
private:
ostream_type* m_os;
DelimT m_delim;
bool m_isFirst = true;
};
template<typename CharT, typename Traits, typename DelimT>
inline ostream_joiner<typename std::decay<DelimT>::type, CharT, Traits>
make_ostream_joiner(std::basic_ostream<CharT, Traits>& os, DelimT&& delimiter)
{
return {os, std::forward<DelimT>(delimiter)};
}
} // namespace ndn
#endif // NDN_CXX_HAVE_EXPERIMENTAL_OSTREAM_JOINER
#endif // NDN_UTIL_BACKPORTS_OSTREAM_JOINER_HPP