mode_adapter.hpp 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. // (C) Copyright 2008 CodeRage, LLC (turkanis at coderage dot com)
  2. // (C) Copyright 2003-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. #ifndef BOOST_IOSTREAMS_DETAIL_MODE_ADAPTER_HPP_INCLUDED
  7. #define BOOST_IOSTREAMS_DETAIL_MODE_ADAPTER_HPP_INCLUDED
  8. #if defined(_MSC_VER)
  9. # pragma once
  10. #endif
  11. // Contains the definition of the class template mode_adapter, which allows
  12. // a filter or device to function as if it has a different i/o mode than that
  13. // deduced by the metafunction mode_of.
  14. #include <boost/config.hpp> // BOOST_MSVC.
  15. #include <boost/detail/workaround.hpp>
  16. #include <boost/iostreams/categories.hpp>
  17. #include <boost/iostreams/detail/ios.hpp> // openmode, seekdir, int types.
  18. #include <boost/iostreams/traits.hpp>
  19. #include <boost/iostreams/operations.hpp>
  20. #include <boost/mpl/if.hpp>
  21. namespace boost { namespace iostreams { namespace detail {
  22. template<typename Mode, typename T>
  23. class mode_adapter {
  24. private:
  25. struct empty_base { };
  26. public:
  27. typedef typename wrapped_type<T>::type component_type;
  28. typedef typename char_type_of<T>::type char_type;
  29. struct category
  30. : Mode,
  31. device_tag,
  32. mpl::if_<is_filter<T>, filter_tag, device_tag>,
  33. mpl::if_<is_filter<T>, multichar_tag, empty_base>,
  34. closable_tag,
  35. localizable_tag
  36. { };
  37. explicit mode_adapter(const component_type& t) : t_(t) { }
  38. // Device member functions.
  39. std::streamsize read(char_type* s, std::streamsize n);
  40. std::streamsize write(const char_type* s, std::streamsize n);
  41. std::streampos seek( stream_offset off, BOOST_IOS::seekdir way,
  42. BOOST_IOS::openmode which =
  43. BOOST_IOS::in | BOOST_IOS::out );
  44. void close();
  45. void close(BOOST_IOS::openmode which);
  46. // Filter member functions.
  47. template<typename Source>
  48. std::streamsize read(Source& src, char_type* s, std::streamsize n)
  49. { return iostreams::read(t_, src, s, n); }
  50. template<typename Sink>
  51. std::streamsize write(Sink& snk, const char_type* s, std::streamsize n)
  52. { return iostreams::write(t_, snk, s, n); }
  53. template<typename Device>
  54. std::streampos seek(Device& dev, stream_offset off, BOOST_IOS::seekdir way)
  55. { return iostreams::seek(t_, dev, off, way); }
  56. template<typename Device>
  57. std::streampos seek( Device& dev, stream_offset off,
  58. BOOST_IOS::seekdir way, BOOST_IOS::openmode which )
  59. { return iostreams::seek(t_, dev, off, way, which); }
  60. template<typename Device>
  61. void close(Device& dev)
  62. { detail::close_all(t_, dev); }
  63. template<typename Device>
  64. void close(Device& dev, BOOST_IOS::openmode which)
  65. { iostreams::close(t_, dev, which); }
  66. template<typename Locale>
  67. void imbue(const Locale& loc)
  68. { iostreams::imbue(t_, loc); }
  69. private:
  70. component_type t_;
  71. };
  72. //------------------Implementation of mode_adapter----------------------------//
  73. template<typename Mode, typename T>
  74. std::streamsize mode_adapter<Mode, T>::read
  75. (char_type* s, std::streamsize n)
  76. { return boost::iostreams::read(t_, s, n); }
  77. template<typename Mode, typename T>
  78. std::streamsize mode_adapter<Mode, T>::write
  79. (const char_type* s, std::streamsize n)
  80. { return boost::iostreams::write(t_, s, n); }
  81. template<typename Mode, typename T>
  82. std::streampos mode_adapter<Mode, T>::seek
  83. (stream_offset off, BOOST_IOS::seekdir way, BOOST_IOS::openmode which)
  84. { return boost::iostreams::seek(t_, off, way, which); }
  85. template<typename Mode, typename T>
  86. void mode_adapter<Mode, T>::close()
  87. { detail::close_all(t_); }
  88. template<typename Mode, typename T>
  89. void mode_adapter<Mode, T>::close(BOOST_IOS::openmode which)
  90. { iostreams::close(t_, which); }
  91. } } } // End namespaces detail, iostreams, boost.
  92. #endif // #ifndef BOOST_IOSTREAMS_DETAIL_MODE_ADAPTER_HPP_INCLUDED //-----//