Mercurial > hg > vamp-build-and-test
comparison DEPENDENCIES/generic/include/boost/iostreams/detail/adapter/direct_adapter.hpp @ 16:2665513ce2d3
Add boost headers
author | Chris Cannam |
---|---|
date | Tue, 05 Aug 2014 11:11:38 +0100 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
15:663ca0da4350 | 16:2665513ce2d3 |
---|---|
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_DETAIL_DIRECT_ADAPTER_HPP_INCLUDED | |
9 #define BOOST_IOSTREAMS_DETAIL_DIRECT_ADAPTER_HPP_INCLUDED | |
10 | |
11 #if defined(_MSC_VER) && (_MSC_VER >= 1020) | |
12 # pragma once | |
13 #endif | |
14 | |
15 #include <boost/config.hpp> // SFINAE, MSVC, put ptrdiff_t in std. | |
16 #include <algorithm> // copy, min. | |
17 #include <cstddef> // ptrdiff_t. | |
18 #include <boost/detail/workaround.hpp> | |
19 #include <boost/iostreams/categories.hpp> | |
20 #include <boost/iostreams/detail/config/limits.hpp> // forwarding. | |
21 #include <boost/iostreams/detail/config/wide_streams.hpp> // locale. | |
22 #include <boost/iostreams/detail/double_object.hpp> | |
23 #include <boost/iostreams/detail/error.hpp> | |
24 #include <boost/iostreams/detail/ios.hpp> // openmode, seekdir, int types. | |
25 #include <boost/iostreams/traits.hpp> // mode_of, is_direct. | |
26 #include <boost/iostreams/operations.hpp> | |
27 #include <boost/mpl/bool.hpp> | |
28 #include <boost/mpl/or.hpp> | |
29 #include <boost/preprocessor/iteration/local.hpp> | |
30 #include <boost/preprocessor/repetition/enum_binary_params.hpp> | |
31 #include <boost/preprocessor/repetition/enum_params.hpp> | |
32 #include <boost/static_assert.hpp> | |
33 #include <boost/throw_exception.hpp> | |
34 #include <boost/type_traits/is_convertible.hpp> | |
35 | |
36 // Must come last. | |
37 #include <boost/iostreams/detail/config/disable_warnings.hpp> // VC7.1 | |
38 | |
39 namespace boost { namespace iostreams { namespace detail { | |
40 | |
41 //------------------Definition of direct_adapter_base-------------------------// | |
42 | |
43 // Put all initialization in base class to faciliate forwarding. | |
44 template<typename Direct> | |
45 class direct_adapter_base { | |
46 public: | |
47 typedef typename char_type_of<Direct>::type char_type; | |
48 typedef typename mode_of<Direct>::type mode_type; | |
49 struct category | |
50 : mode_type, | |
51 device_tag, | |
52 closable_tag | |
53 #ifndef BOOST_IOSTREAMS_NO_LOCALE | |
54 , localizable_tag | |
55 #endif | |
56 { }; | |
57 protected: | |
58 explicit direct_adapter_base(const Direct& d); | |
59 typedef is_convertible<category, two_sequence> is_double; | |
60 struct pointers { | |
61 char_type *beg, *ptr, *end; | |
62 }; | |
63 void init_input(mpl::true_); | |
64 void init_input(mpl::false_) { } | |
65 void init_output(mpl::true_); | |
66 void init_output(mpl::false_) { } | |
67 double_object<pointers, is_double> ptrs_; | |
68 Direct d_; | |
69 }; | |
70 | |
71 template<typename Direct> | |
72 class direct_adapter : private direct_adapter_base<Direct> { | |
73 private: | |
74 typedef direct_adapter_base<Direct> base_type; | |
75 typedef typename base_type::pointers pointers; | |
76 typedef typename base_type::is_double is_double; | |
77 using base_type::ptrs_; | |
78 using base_type::d_; | |
79 public: | |
80 typedef typename base_type::char_type char_type; | |
81 typedef typename base_type::category category; | |
82 | |
83 // Constructors | |
84 | |
85 #if !BOOST_WORKAROUND(BOOST_MSVC, <= 1310) | |
86 direct_adapter(const Direct& d) : base_type(d) { } | |
87 direct_adapter(const direct_adapter& d) : base_type(d) { } | |
88 # define BOOST_PP_LOCAL_LIMITS (1, BOOST_IOSTREAMS_MAX_FORWARDING_ARITY) | |
89 #else | |
90 template<typename U> | |
91 struct is_direct | |
92 : mpl::or_< | |
93 is_same<U, direct_adapter<Direct> >, | |
94 is_same<U, Direct> | |
95 > | |
96 { }; | |
97 template<typename U> | |
98 direct_adapter(const U& u) | |
99 : base_type(forward(u, is_direct<U>())) | |
100 { } | |
101 # define BOOST_PP_LOCAL_LIMITS (2, BOOST_IOSTREAMS_MAX_FORWARDING_ARITY) | |
102 #endif | |
103 | |
104 #define BOOST_PP_LOCAL_MACRO(n) \ | |
105 template<BOOST_PP_ENUM_PARAMS(n, typename P)> \ | |
106 direct_adapter(BOOST_PP_ENUM_BINARY_PARAMS(n, const P, &p)) \ | |
107 : base_type(Direct(BOOST_PP_ENUM_PARAMS(n, p))) \ | |
108 { } \ | |
109 /**/ | |
110 #include BOOST_PP_LOCAL_ITERATE() | |
111 #undef BOOST_PP_LOCAL_MACRO | |
112 | |
113 // Device interface. | |
114 | |
115 std::streamsize read(char_type* s, std::streamsize n); | |
116 std::streamsize write(const char_type* s, std::streamsize n); | |
117 std::streampos seek( stream_offset, BOOST_IOS::seekdir, | |
118 BOOST_IOS::openmode = BOOST_IOS::in | BOOST_IOS::out ); | |
119 void close(); | |
120 void close(BOOST_IOS::openmode which); | |
121 #ifndef BOOST_IOSTREAMS_NO_LOCALE | |
122 void imbue(const std::locale&); | |
123 #endif | |
124 | |
125 // Direct device access. | |
126 | |
127 Direct& operator*() { return d_; } | |
128 Direct* operator->() { return &d_; } | |
129 #if BOOST_WORKAROUND(BOOST_MSVC, <= 1310) | |
130 private: | |
131 template<typename U> | |
132 static Direct forward(const U& u, mpl::true_) { return u; } | |
133 template<typename U> | |
134 static Direct forward(const U& u, mpl::false_) { return Direct(u); } | |
135 #endif | |
136 }; | |
137 | |
138 //--------------Definition of wrap_direct and unwrap_direct-------------------// | |
139 | |
140 template<typename Device> | |
141 struct wrap_direct_traits | |
142 : mpl::if_< | |
143 is_direct<Device>, | |
144 direct_adapter<Device>, | |
145 Device | |
146 > | |
147 { }; | |
148 | |
149 template<typename Device> | |
150 typename wrap_direct_traits<Device>::type | |
151 inline wrap_direct(Device dev) | |
152 { | |
153 typedef typename wrap_direct_traits<Device>::type type; | |
154 return type(dev); | |
155 } | |
156 | |
157 template<typename Device> | |
158 inline Device& unwrap_direct(Device& d) { return d; } | |
159 | |
160 template<typename Device> | |
161 inline Device& unwrap_direct(direct_adapter<Device>& d) { return *d; } | |
162 | |
163 //--------------Implementation of direct_adapter_base-------------------------// | |
164 | |
165 template<typename Direct> | |
166 direct_adapter_base<Direct>::direct_adapter_base(const Direct& d) : d_(d) | |
167 { | |
168 init_input(is_convertible<category, input>()); | |
169 init_output(is_convertible<category, output>()); | |
170 } | |
171 | |
172 template<typename Direct> | |
173 void direct_adapter_base<Direct>::init_input(mpl::true_) | |
174 { | |
175 std::pair<char_type*, char_type*> seq = iostreams::input_sequence(d_); | |
176 ptrs_.first().beg = seq.first; | |
177 ptrs_.first().ptr = seq.first; | |
178 ptrs_.first().end = seq.second; | |
179 } | |
180 | |
181 template<typename Direct> | |
182 void direct_adapter_base<Direct>::init_output(mpl::true_) | |
183 { | |
184 std::pair<char_type*, char_type*> seq = iostreams::output_sequence(d_); | |
185 ptrs_.second().beg = seq.first; | |
186 ptrs_.second().ptr = seq.first; | |
187 ptrs_.second().end = seq.second; | |
188 } | |
189 | |
190 //--------------Implementation of direct_adapter------------------------------// | |
191 | |
192 template<typename Direct> | |
193 inline std::streamsize direct_adapter<Direct>::read | |
194 (char_type* s, std::streamsize n) | |
195 { | |
196 using namespace std; | |
197 pointers& get = ptrs_.first(); | |
198 std::streamsize avail = | |
199 static_cast<std::streamsize>(get.end - get.ptr); | |
200 std::streamsize result = (std::min)(n, avail); | |
201 std::copy(get.ptr, get.ptr + result, s); | |
202 get.ptr += result; | |
203 return result != 0 ? result : -1; | |
204 } | |
205 | |
206 template<typename Direct> | |
207 inline std::streamsize direct_adapter<Direct>::write | |
208 (const char_type* s, std::streamsize n) | |
209 { | |
210 using namespace std; | |
211 pointers& put = ptrs_.second(); | |
212 if (n > static_cast<std::streamsize>(put.end - put.ptr)) | |
213 boost::throw_exception(write_area_exhausted()); | |
214 std::copy(s, s + n, put.ptr); | |
215 put.ptr += n; | |
216 return n; | |
217 } | |
218 | |
219 template<typename Direct> | |
220 inline std::streampos direct_adapter<Direct>::seek | |
221 ( stream_offset off, BOOST_IOS::seekdir way, | |
222 BOOST_IOS::openmode which ) | |
223 { | |
224 using namespace std; | |
225 pointers& get = ptrs_.first(); | |
226 pointers& put = ptrs_.second(); | |
227 if (way == BOOST_IOS::cur && get.ptr != put.ptr) | |
228 boost::throw_exception(bad_seek()); | |
229 ptrdiff_t next = 0; | |
230 if ((which & BOOST_IOS::in) || !is_double::value) { | |
231 if (way == BOOST_IOS::beg) | |
232 next = off; | |
233 else if (way == BOOST_IOS::cur) | |
234 next = get.ptr - get.beg + off; | |
235 else | |
236 next = get.end - get.beg + off; | |
237 if (next >= 0 && next <= get.end - get.beg) | |
238 get.ptr = get.beg + next; | |
239 else | |
240 boost::throw_exception(bad_seek()); | |
241 } | |
242 if ((which & BOOST_IOS::out) && is_double::value) { | |
243 if (way == BOOST_IOS::beg) | |
244 next = off; | |
245 else if (way == BOOST_IOS::cur) | |
246 next = put.ptr - put.beg + off; | |
247 else | |
248 next = put.end - put.beg + off; | |
249 if (next >= 0 && next <= put.end - put.beg) | |
250 put.ptr = put.beg + next; | |
251 else | |
252 boost::throw_exception(bad_seek()); | |
253 } | |
254 return offset_to_position(next); | |
255 } | |
256 | |
257 template<typename Direct> | |
258 void direct_adapter<Direct>::close() | |
259 { | |
260 BOOST_STATIC_ASSERT((!is_convertible<category, two_sequence>::value)); | |
261 detail::close_all(d_); | |
262 } | |
263 | |
264 template<typename Direct> | |
265 void direct_adapter<Direct>::close(BOOST_IOS::openmode which) | |
266 { | |
267 BOOST_STATIC_ASSERT((is_convertible<category, two_sequence>::value)); | |
268 boost::iostreams::close(d_, which); | |
269 } | |
270 | |
271 #ifndef BOOST_IOSTREAMS_NO_LOCALE | |
272 template<typename Direct> | |
273 void direct_adapter<Direct>::imbue(const std::locale& loc) | |
274 { boost::iostreams::imbue(d_, loc); } | |
275 #endif | |
276 | |
277 } } } // End namespaces detail, iostreams, boost. | |
278 | |
279 #include <boost/iostreams/detail/config/enable_warnings.hpp> | |
280 | |
281 #endif // #ifndef BOOST_IOSTREAMS_DETAIL_DIRECT_ADAPTER_HPP_INCLUDED |