diff --git a/include/boost/io/nullstream.hpp b/include/boost/io/nullstream.hpp new file mode 100644 index 0000000..c623297 --- /dev/null +++ b/include/boost/io/nullstream.hpp @@ -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 +#include +#include + +namespace boost { +namespace io { + +template > +class basic_nullbuf + : public std::basic_streambuf { +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 +struct nullbuf { + boost::io::basic_nullbuf buf; +}; + +} /* detail */ + +template > +class basic_onullstream + : detail::nullbuf + , public std::basic_ostream { +public: + basic_onullstream() + : std::basic_ostream(&(detail::nullbuf::buf)) { } +}; + +typedef basic_onullstream onullstream; +typedef basic_onullstream wonullstream; + +} /* io */ +} /* boost */ + +#endif diff --git a/test/Jamfile b/test/Jamfile index 3c5141b..ffc9160 100644 --- a/test/Jamfile +++ b/test/Jamfile @@ -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 ; diff --git a/test/nullstream_test.cpp b/test/nullstream_test.cpp new file mode 100644 index 0000000..8b49335 --- /dev/null +++ b/test/nullstream_test.cpp @@ -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 +#include + +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(); +}