blob: e158117244b6a307d6fc2723c8ce51d0335974f6 [file] [log] [blame]
Jeff Thompson86b6d642013-10-17 15:01:56 -07001// (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 NDNBOOST_IOSTREAMS_STREAM_HPP_INCLUDED
9#define NDNBOOST_IOSTREAMS_STREAM_HPP_INCLUDED
10
11#if defined(_MSC_VER) && (_MSC_VER >= 1020)
12# pragma once
13#endif
14
15#include <ndnboost/iostreams/constants.hpp>
16#include <ndnboost/iostreams/detail/char_traits.hpp>
17#include <ndnboost/iostreams/detail/config/overload_resolution.hpp>
18#include <ndnboost/iostreams/detail/forward.hpp>
19#include <ndnboost/iostreams/detail/iostream.hpp> // standard streams.
20#include <ndnboost/iostreams/detail/select.hpp>
21#include <ndnboost/iostreams/stream_buffer.hpp>
22#include <ndnboost/mpl/and.hpp>
23#include <ndnboost/type_traits/is_convertible.hpp>
24#include <ndnboost/utility/base_from_member.hpp>
25
26namespace ndnboost { namespace iostreams { namespace detail {
27
28template<typename Device, typename Tr>
29struct 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 NDNBOOST_IOSTREAMS_BASIC_IOSTREAM(char_type, traits_type),
40 is_convertible<mode, input>,
41 NDNBOOST_IOSTREAMS_BASIC_ISTREAM(char_type, traits_type),
42 else_,
43 NDNBOOST_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// NDNBOOST_IOSTREAMS_DEFINE_FORWARDING_FUNCTIONS to generate constructors
61// without base member initializer lists.
62template< typename Device,
63 typename Tr =
64 NDNBOOST_IOSTREAMS_CHAR_TRAITS(
65 NDNBOOST_DEDUCED_TYPENAME char_type_of<Device>::type
66 ),
67 typename Alloc =
68 std::allocator<
69 NDNBOOST_DEDUCED_TYPENAME char_type_of<Device>::type
70 >,
71 typename Base = // VC6 Workaround.
72 NDNBOOST_DEDUCED_TYPENAME
73 detail::stream_traits<Device, Tr>::stream_type >
74class stream_base
75 : protected base_from_member< stream_buffer<Device, Tr, Alloc> >,
76 public Base
77{
78private:
79 typedef base_from_member< stream_buffer<Device, Tr, Alloc> > pbase_type;
80 typedef typename stream_traits<Device, Tr>::stream_type stream_type;
81protected:
82 using pbase_type::member; // Avoid warning about 'this' in initializer list.
83public:
84 stream_base() : pbase_type(), stream_type(&member) { }
85};
86
87} } } // End namespaces detail, iostreams, boost.
88
89#ifdef NDNBOOST_IOSTREAMS_BROKEN_OVERLOAD_RESOLUTION
90# include <ndnboost/iostreams/detail/broken_overload_resolution/stream.hpp>
91#else
92
93namespace ndnboost { 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//
103template< typename Device,
104 typename Tr =
105 NDNBOOST_IOSTREAMS_CHAR_TRAITS(
106 NDNBOOST_DEDUCED_TYPENAME char_type_of<Device>::type
107 ),
108 typename Alloc =
109 std::allocator<
110 NDNBOOST_DEDUCED_TYPENAME char_type_of<Device>::type
111 > >
112struct stream : detail::stream_base<Device, Tr, Alloc> {
113public:
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 NDNBOOST_IOSTREAMS_STREAMBUF_TYPEDEFS(Tr)
121private:
122 typedef typename
123 detail::stream_traits<
124 Device, Tr
125 >::stream_type stream_type;
126public:
127 stream() { }
128 NDNBOOST_IOSTREAMS_FORWARD( stream, open_impl, Device,
129 NDNBOOST_IOSTREAMS_PUSH_PARAMS,
130 NDNBOOST_IOSTREAMS_PUSH_ARGS )
131 bool is_open() const { return this->member.is_open(); }
132 void close() { this->member.close(); }
133 bool auto_close() const { return this->member.auto_close(); }
134 void set_auto_close(bool close) { this->member.set_auto_close(close); }
135 bool strict_sync() { return this->member.strict_sync(); }
136 Device& operator*() { return *this->member; }
137 Device* operator->() { return &*this->member; }
138 Device* component() { return this->member.component(); }
139private:
140 void open_impl(const Device& dev NDNBOOST_IOSTREAMS_PUSH_PARAMS()) // For forwarding.
141 {
142 this->clear();
143 this->member.open(dev NDNBOOST_IOSTREAMS_PUSH_ARGS());
144 }
145};
146
147} } // End namespaces iostreams, boost.
148
149#endif // #ifdef NDNBOOST_IOSTREAMS_BROKEN_OVERLOAD_RESOLUTION
150
151#endif // #ifndef NDNBOOST_IOSTREAMS_stream_HPP_INCLUDED