Add nullstream

This commit is contained in:
Glen Fernandes
2022-01-06 22:11:08 -05:00
parent 7392d7274c
commit 0a0241dc9d
3 changed files with 86 additions and 0 deletions

View File

@ -0,0 +1,57 @@
/*
Copyright 2021 Glen Joseph Fernandes
(glenjofe@gmail.com)
Distributed under the Boost Software License, Version 1.0.
(http://www.boost.org/LICENSE_1_0.txt)
*/
#ifndef BOOST_IO_NULLSTREAM_HPP
#define BOOST_IO_NULLSTREAM_HPP
#include <boost/config.hpp>
#include <ostream>
#include <streambuf>
namespace boost {
namespace io {
template<class CharT, class Traits = std::char_traits<CharT> >
class basic_nullbuf
: public std::basic_streambuf<CharT, Traits> {
protected:
typename Traits::int_type overflow(typename Traits::int_type c)
BOOST_OVERRIDE {
return Traits::not_eof(c);
}
std::streamsize xsputn(const CharT*, std::streamsize n) BOOST_OVERRIDE {
return n;
}
};
namespace detail {
template<class CharT, class Traits>
struct nullbuf {
boost::io::basic_nullbuf<CharT, Traits> buf;
};
} /* detail */
template<class CharT, class Traits = std::char_traits<CharT> >
class basic_onullstream
: detail::nullbuf<CharT, Traits>
, public std::basic_ostream<CharT, Traits> {
public:
basic_onullstream()
: std::basic_ostream<CharT, Traits>(&(detail::nullbuf<CharT,
Traits>::buf)) { }
};
typedef basic_onullstream<char> onullstream;
typedef basic_onullstream<wchar_t> wonullstream;
} /* io */
} /* boost */
#endif

View File

@ -17,3 +17,4 @@ run quoted_fill_test.cpp ;
run ostream_joiner_test.cpp ;
run make_ostream_joiner_test.cpp ;
run ostream_put_test.cpp ;
run nullstream_test.cpp ;

28
test/nullstream_test.cpp Normal file
View File

@ -0,0 +1,28 @@
/*
Copyright 2021 Glen Joseph Fernandes
(glenjofe@gmail.com)
Distributed under the Boost Software License, Version 1.0.
(http://www.boost.org/LICENSE_1_0.txt)
*/
#include <boost/io/nullstream.hpp>
#include <boost/core/lightweight_test.hpp>
int main()
{
{
boost::io::onullstream s;
s << "abc";
s << 100;
s << std::endl;
BOOST_TEST(s);
}
{
boost::io::wonullstream s;
s << L"abc";
s << 100;
s << std::endl;
BOOST_TEST(s);
}
return boost::report_errors();
}