null.hpp 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. // (C) Copyright 2008 CodeRage, LLC (turkanis at coderage dot com)
  2. // (C) Copyright 2004-2007 Jonathan Turkanis
  3. // Distributed under the Boost Software License, Version 1.0. (See accompanying
  4. // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt.)
  5. // See http://www.boost.org/libs/iostreams for documentation.
  6. // Inspired by Daryle Walker's nullbuf from his More I/O submission.
  7. #ifndef BOOST_IOSTREAMS_NULL_HPP_INCLUDED
  8. #define BOOST_IOSTREAMS_NULL_HPP_INCLUDED
  9. #if defined(_MSC_VER)
  10. # pragma once
  11. #endif
  12. #include <boost/iostreams/categories.hpp>
  13. #include <boost/iostreams/detail/ios.hpp> // openmode, streamsize.
  14. #include <boost/iostreams/positioning.hpp>
  15. namespace boost { namespace iostreams {
  16. template<typename Ch, typename Mode>
  17. class basic_null_device {
  18. public:
  19. typedef Ch char_type;
  20. struct category
  21. : public Mode,
  22. public device_tag,
  23. public closable_tag
  24. { };
  25. std::streamsize read(Ch*, std::streamsize) { return -1; }
  26. std::streamsize write(const Ch*, std::streamsize n) { return n; }
  27. std::streampos seek( stream_offset, BOOST_IOS::seekdir,
  28. BOOST_IOS::openmode =
  29. BOOST_IOS::in | BOOST_IOS::out )
  30. { return -1; }
  31. void close() { }
  32. void close(BOOST_IOS::openmode) { }
  33. };
  34. template<typename Ch>
  35. struct basic_null_source : private basic_null_device<Ch, input> {
  36. typedef Ch char_type;
  37. typedef source_tag category;
  38. using basic_null_device<Ch, input>::read;
  39. using basic_null_device<Ch, input>::close;
  40. };
  41. typedef basic_null_source<char> null_source;
  42. typedef basic_null_source<wchar_t> wnull_source;
  43. template<typename Ch>
  44. struct basic_null_sink : private basic_null_device<Ch, output> {
  45. typedef Ch char_type;
  46. typedef sink_tag category;
  47. using basic_null_device<Ch, output>::write;
  48. using basic_null_device<Ch, output>::close;
  49. };
  50. typedef basic_null_sink<char> null_sink;
  51. typedef basic_null_sink<wchar_t> wnull_sink;
  52. } } // End namespaces iostreams, boost.
  53. #endif // #ifndef BOOST_IOSTREAMS_NULL_HPP_INCLUDED