stream.hpp 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  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_STREAM_HPP_INCLUDED
  7. #define BOOST_IOSTREAMS_STREAM_HPP_INCLUDED
  8. #if defined(_MSC_VER)
  9. # pragma once
  10. #endif
  11. #include <boost/iostreams/constants.hpp>
  12. #include <boost/iostreams/detail/char_traits.hpp>
  13. #include <boost/iostreams/detail/config/overload_resolution.hpp>
  14. #include <boost/iostreams/detail/forward.hpp>
  15. #include <boost/iostreams/detail/iostream.hpp> // standard streams.
  16. #include <boost/iostreams/detail/select.hpp>
  17. #include <boost/iostreams/stream_buffer.hpp>
  18. #include <boost/mpl/and.hpp>
  19. #include <boost/type_traits/is_convertible.hpp>
  20. #include <boost/utility/base_from_member.hpp>
  21. namespace boost { namespace iostreams { namespace detail {
  22. template<typename Device, typename Tr>
  23. struct stream_traits {
  24. typedef typename char_type_of<Device>::type char_type;
  25. typedef Tr traits_type;
  26. typedef typename category_of<Device>::type mode;
  27. typedef typename
  28. iostreams::select< // Disambiguation required for Tru64.
  29. mpl::and_<
  30. is_convertible<mode, input>,
  31. is_convertible<mode, output>
  32. >,
  33. BOOST_IOSTREAMS_BASIC_IOSTREAM(char_type, traits_type),
  34. is_convertible<mode, input>,
  35. BOOST_IOSTREAMS_BASIC_ISTREAM(char_type, traits_type),
  36. else_,
  37. BOOST_IOSTREAMS_BASIC_OSTREAM(char_type, traits_type)
  38. >::type stream_type;
  39. typedef typename
  40. iostreams::select< // Disambiguation required for Tru64.
  41. mpl::and_<
  42. is_convertible<mode, input>,
  43. is_convertible<mode, output>
  44. >,
  45. iostream_tag,
  46. is_convertible<mode, input>,
  47. istream_tag,
  48. else_,
  49. ostream_tag
  50. >::type stream_tag;
  51. };
  52. #if defined(BOOST_MSVC) && (BOOST_MSVC == 1700)
  53. # pragma warning(push)
  54. // https://connect.microsoft.com/VisualStudio/feedback/details/733720/
  55. # pragma warning(disable: 4250)
  56. #endif
  57. // By encapsulating initialization in a base, we can define the macro
  58. // BOOST_IOSTREAMS_DEFINE_FORWARDING_FUNCTIONS to generate constructors
  59. // without base member initializer lists.
  60. template< typename Device,
  61. typename Tr =
  62. BOOST_IOSTREAMS_CHAR_TRAITS(
  63. BOOST_DEDUCED_TYPENAME char_type_of<Device>::type
  64. ),
  65. typename Alloc =
  66. std::allocator<
  67. BOOST_DEDUCED_TYPENAME char_type_of<Device>::type
  68. >,
  69. typename Base = // VC6 Workaround.
  70. BOOST_DEDUCED_TYPENAME
  71. detail::stream_traits<Device, Tr>::stream_type >
  72. class stream_base
  73. : protected base_from_member< stream_buffer<Device, Tr, Alloc> >,
  74. public Base
  75. {
  76. private:
  77. typedef base_from_member< stream_buffer<Device, Tr, Alloc> > pbase_type;
  78. typedef typename stream_traits<Device, Tr>::stream_type stream_type;
  79. protected:
  80. using pbase_type::member; // Avoid warning about 'this' in initializer list.
  81. public:
  82. stream_base() : pbase_type(), stream_type(&member) { }
  83. };
  84. #if defined(BOOST_MSVC) && (BOOST_MSVC == 1700)
  85. # pragma warning(pop)
  86. #endif
  87. } } } // End namespaces detail, iostreams, boost.
  88. #ifdef BOOST_IOSTREAMS_BROKEN_OVERLOAD_RESOLUTION
  89. # include <boost/iostreams/detail/broken_overload_resolution/stream.hpp>
  90. #else
  91. namespace boost { namespace iostreams {
  92. #if defined(BOOST_MSVC) && (BOOST_MSVC == 1700)
  93. # pragma warning(push)
  94. // https://connect.microsoft.com/VisualStudio/feedback/details/733720/
  95. # pragma warning(disable: 4250)
  96. #endif
  97. //
  98. // Template name: stream.
  99. // Description: A iostream which reads from and writes to an instance of a
  100. // designated device type.
  101. // Template parameters:
  102. // Device - A device type.
  103. // Alloc - The allocator type.
  104. //
  105. template< typename Device,
  106. typename Tr =
  107. BOOST_IOSTREAMS_CHAR_TRAITS(
  108. BOOST_DEDUCED_TYPENAME char_type_of<Device>::type
  109. ),
  110. typename Alloc =
  111. std::allocator<
  112. BOOST_DEDUCED_TYPENAME char_type_of<Device>::type
  113. > >
  114. struct stream : detail::stream_base<Device, Tr, Alloc> {
  115. public:
  116. typedef typename char_type_of<Device>::type char_type;
  117. struct category
  118. : mode_of<Device>::type,
  119. closable_tag,
  120. detail::stream_traits<Device, Tr>::stream_tag
  121. { };
  122. BOOST_IOSTREAMS_STREAMBUF_TYPEDEFS(Tr)
  123. private:
  124. typedef typename
  125. detail::stream_traits<
  126. Device, Tr
  127. >::stream_type stream_type;
  128. public:
  129. stream() { }
  130. BOOST_IOSTREAMS_FORWARD( stream, open_impl, Device,
  131. BOOST_IOSTREAMS_PUSH_PARAMS,
  132. BOOST_IOSTREAMS_PUSH_ARGS )
  133. bool is_open() const { return this->member.is_open(); }
  134. void close() { this->member.close(); }
  135. bool auto_close() const { return this->member.auto_close(); }
  136. void set_auto_close(bool close) { this->member.set_auto_close(close); }
  137. bool strict_sync() { return this->member.strict_sync(); }
  138. Device& operator*() { return *this->member; }
  139. Device* operator->() { return &*this->member; }
  140. Device* component() { return this->member.component(); }
  141. private:
  142. void open_impl(const Device& dev BOOST_IOSTREAMS_PUSH_PARAMS()) // For forwarding.
  143. {
  144. this->clear();
  145. this->member.open(dev BOOST_IOSTREAMS_PUSH_ARGS());
  146. }
  147. };
  148. #if defined(BOOST_MSVC) && (BOOST_MSVC == 1700)
  149. # pragma warning(pop)
  150. #endif
  151. } } // End namespaces iostreams, boost.
  152. #endif // #ifdef BOOST_IOSTREAMS_BROKEN_OVERLOAD_RESOLUTION
  153. #endif // #ifndef BOOST_IOSTREAMS_stream_HPP_INCLUDED