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) && (_MSC_VER >= 1020)
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 // By encapsulating initialization in a base, we can define the macro
60 // BOOST_IOSTREAMS_DEFINE_FORWARDING_FUNCTIONS to generate constructors
61 // without base member initializer lists.
62 template< typename Device,
63           typename Tr =
64               BOOST_IOSTREAMS_CHAR_TRAITS(
65                   BOOST_DEDUCED_TYPENAME char_type_of<Device>::type
66               ),
67           typename Alloc =
68               std::allocator<
69                   BOOST_DEDUCED_TYPENAME char_type_of<Device>::type
70               >,
71           typename Base = // VC6 Workaround.
72               BOOST_DEDUCED_TYPENAME
73               detail::stream_traits<Device, Tr>::stream_type >
74 class stream_base
75     : protected base_from_member< stream_buffer<Device, Tr, Alloc> >,
76       public Base
77 {
78 private:
79     typedef base_from_member< stream_buffer<Device, Tr, Alloc> >  pbase_type;
80     typedef typename stream_traits<Device, Tr>::stream_type       stream_type;
81 protected:
82     using pbase_type::member; // Avoid warning about 'this' in initializer list.
83 public:
stream_base()84     stream_base() : pbase_type(), stream_type(&member) { }
85 };
86 
87 } } } // End namespaces detail, iostreams, boost.
88 
89 #ifdef BOOST_IOSTREAMS_BROKEN_OVERLOAD_RESOLUTION
90 # include <boost/iostreams/detail/broken_overload_resolution/stream.hpp>
91 #else
92 
93 namespace boost { namespace iostreams {
94 
95 //
96 // Template name: stream.
97 // Description: A iostream which reads from and writes to an instance of a
98 //      designated device type.
99 // Template parameters:
100 //      Device - A device type.
101 //      Alloc - The allocator type.
102 //
103 template< typename Device,
104           typename Tr =
105               BOOST_IOSTREAMS_CHAR_TRAITS(
106                   BOOST_DEDUCED_TYPENAME char_type_of<Device>::type
107               ),
108           typename Alloc =
109               std::allocator<
110                   BOOST_DEDUCED_TYPENAME char_type_of<Device>::type
111               > >
112 struct stream : detail::stream_base<Device, Tr, Alloc> {
113 public:
114     typedef typename char_type_of<Device>::type  char_type;
115     struct category
116         : mode_of<Device>::type,
117           closable_tag,
118           detail::stream_traits<Device, Tr>::stream_tag
119         { };
120     BOOST_IOSTREAMS_STREAMBUF_TYPEDEFS(Tr)
121 private:
122     typedef typename
123             detail::stream_traits<
124                 Device, Tr
125             >::stream_type                       stream_type;
126 public:
streamboost::iostreams::stream127     stream() { }
BOOST_IOSTREAMS_FORWARDboost::iostreams::stream128     BOOST_IOSTREAMS_FORWARD( stream, open_impl, Device,
129                              BOOST_IOSTREAMS_PUSH_PARAMS,
130                              BOOST_IOSTREAMS_PUSH_ARGS )
131     bool is_open() const { return this->member.is_open(); }
closeboost::iostreams::stream132     void close() { this->member.close(); }
auto_closeboost::iostreams::stream133     bool auto_close() const { return this->member.auto_close(); }
set_auto_closeboost::iostreams::stream134     void set_auto_close(bool close) { this->member.set_auto_close(close); }
strict_syncboost::iostreams::stream135     bool strict_sync() { return this->member.strict_sync(); }
operator *boost::iostreams::stream136     Device& operator*() { return *this->member; }
operator ->boost::iostreams::stream137     Device* operator->() { return &*this->member; }
componentboost::iostreams::stream138     Device* component() { return this->member.component(); }
139 private:
open_implboost::iostreams::stream140     void open_impl(const Device& dev BOOST_IOSTREAMS_PUSH_PARAMS()) // For forwarding.
141     {
142         this->clear();
143         this->member.open(dev BOOST_IOSTREAMS_PUSH_ARGS());
144     }
145 };
146 
147 } } // End namespaces iostreams, boost.
148 
149 #endif // #ifdef BOOST_IOSTREAMS_BROKEN_OVERLOAD_RESOLUTION
150 
151 #endif // #ifndef BOOST_IOSTREAMS_stream_HPP_INCLUDED
152