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