blob: a33047a914cc2727aebb9c3ca43f0f8b13bb36f6 [file] [log] [blame]
Jeff Thompson86b6d642013-10-17 15:01:56 -07001/*
2 * Distributed under the Boost Software License, Version 1.0.(See accompanying
3 * file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt.)
4 *
5 * See http://www.boost.org/libs/iostreams for documentation.
6
7 * File: ndnboost/iostreams/detail/restrict_impl.hpp
8 * Date: Sun Jan 06 12:57:30 MST 2008
9 * Copyright: 2007-2008 CodeRage, LLC
10 * Author: Jonathan Turkanis
11 * Contact: turkanis at coderage dot com
12 *
13 * If included with the macro NDNBOOST_IOSTREAMS_RESTRICT undefined, defines the
14 * class template ndnboost::iostreams::restriction. If included with the macro
15 * NDNBOOST_IOSTREAMS_RESTRICT defined as an identifier, defines the overloaded
16 * function template ndnboost::iostreams::NDNBOOST_IOSTREAMS_RESTRICT, and object
17 * generator for ndnboost::iostreams::restriction.
18 *
19 * This design allows <ndnboost/iostreams/restrict.hpp> and
20 * <ndnboost/iostreams/slice.hpp> to share an implementation.
21 */
22
23#if !defined(NDNBOOST_IOSTREAMS_RESTRICT_IMPL_HPP_INCLUDED) && \
24 !defined(NDNBOOST_IOSTREAMS_RESTRICT)
25# define NDNBOOST_IOSTREAMS_RESTRICT_IMPL_HPP_INCLUDED
26
27//------------------Implementation of restriction-----------------------------//
28
29# include <algorithm> // min.
30# include <utility> // pair.
31# include <ndnboost/cstdint.hpp> // intmax_t.
32# include <ndnboost/config.hpp> // DEDUCED_TYPENAME.
33# include <ndnboost/iostreams/categories.hpp>
34# include <ndnboost/iostreams/char_traits.hpp>
35# include <ndnboost/iostreams/detail/adapter/device_adapter.hpp>
36# include <ndnboost/iostreams/detail/adapter/filter_adapter.hpp>
37# include <ndnboost/iostreams/detail/call_traits.hpp>
38# include <ndnboost/iostreams/detail/enable_if_stream.hpp>
39# include <ndnboost/iostreams/detail/error.hpp>
40# include <ndnboost/iostreams/detail/ios.hpp> // failure.
41# include <ndnboost/iostreams/detail/select.hpp>
42# include <ndnboost/iostreams/operations.hpp>
43# include <ndnboost/iostreams/skip.hpp>
44# include <ndnboost/iostreams/traits.hpp> // mode_of, is_direct.
45# include <ndnboost/mpl/bool.hpp>
46# include <ndnboost/static_assert.hpp>
47# include <ndnboost/throw_exception.hpp>
48# include <ndnboost/type_traits/is_convertible.hpp>
49
50# include <ndnboost/iostreams/detail/config/disable_warnings.hpp>
51
52namespace ndnboost { namespace iostreams {
53
54namespace detail {
55
56//
57// Template name: restricted_indirect_device.
58// Description: Provides an restricted view of an indirect Device.
59// Template parameters:
60// Device - An indirect model of Device that models either Source or
61// SeekableDevice.
62//
63template<typename Device>
64class restricted_indirect_device : public device_adapter<Device> {
65private:
66 typedef typename detail::param_type<Device>::type param_type;
67public:
68 typedef typename char_type_of<Device>::type char_type;
69 typedef typename mode_of<Device>::type mode;
70 NDNBOOST_STATIC_ASSERT(!(is_convertible<mode, detail::two_sequence>::value));
71 struct category
72 : mode,
73 device_tag,
74 closable_tag,
75 flushable_tag,
76 localizable_tag,
77 optimally_buffered_tag
78 { };
79 restricted_indirect_device( param_type dev, stream_offset off,
80 stream_offset len = -1 );
81 std::streamsize read(char_type* s, std::streamsize n);
82 std::streamsize write(const char_type* s, std::streamsize n);
83 std::streampos seek(stream_offset off, NDNBOOST_IOS::seekdir way);
84private:
85 stream_offset beg_, pos_, end_;
86};
87
88//
89// Template name: restricted_direct_device.
90// Description: Provides an restricted view of a Direct Device.
91// Template parameters:
92// Device - A model of Direct and Device.
93//
94template<typename Device>
95class restricted_direct_device : public device_adapter<Device> {
96public:
97 typedef typename char_type_of<Device>::type char_type;
98 typedef std::pair<char_type*, char_type*> pair_type;
99 typedef typename mode_of<Device>::type mode;
100 NDNBOOST_STATIC_ASSERT(!(is_convertible<mode, detail::two_sequence>::value));
101 struct category
102 : mode_of<Device>::type,
103 device_tag,
104 direct_tag,
105 closable_tag,
106 localizable_tag
107 { };
108 restricted_direct_device( const Device& dev, stream_offset off,
109 stream_offset len = -1 );
110 pair_type input_sequence();
111 pair_type output_sequence();
112private:
113 pair_type sequence(mpl::true_);
114 pair_type sequence(mpl::false_);
115 char_type *beg_, *end_;
116};
117
118//
119// Template name: restricted_filter.
120// Description: Provides an restricted view of a Filter.
121// Template parameters:
122// Filter - An indirect model of Filter.
123//
124template<typename Filter>
125class restricted_filter : public filter_adapter<Filter> {
126public:
127 typedef typename char_type_of<Filter>::type char_type;
128 typedef typename mode_of<Filter>::type mode;
129 NDNBOOST_STATIC_ASSERT(!(is_convertible<mode, detail::two_sequence>::value));
130 struct category
131 : mode,
132 filter_tag,
133 multichar_tag,
134 closable_tag,
135 localizable_tag,
136 optimally_buffered_tag
137 { };
138 restricted_filter( const Filter& flt, stream_offset off,
139 stream_offset len = -1 );
140
141 template<typename Source>
142 std::streamsize read(Source& src, char_type* s, std::streamsize n)
143 {
144 using namespace std;
145 if (!open_)
146 open(src, NDNBOOST_IOS::in);
147 std::streamsize amt =
148 end_ != -1 ?
149 (std::min) (n, static_cast<std::streamsize>(end_ - pos_)) :
150 n;
151 std::streamsize result =
152 iostreams::read(this->component(), src, s, amt);
153 if (result != -1)
154 pos_ += result;
155 return result;
156 }
157
158 template<typename Sink>
159 std::streamsize write(Sink& snk, const char_type* s, std::streamsize n)
160 {
161 if (!open_)
162 open(snk, NDNBOOST_IOS::out);
163 if (end_ != -1 && pos_ + n >= end_) {
164 if(pos_ < end_)
165 pos_ += iostreams::write(this->component(),
166 snk, s, end_ - pos_);
167 ndnboost::throw_exception(bad_write());
168 }
169 std::streamsize result =
170 iostreams::write(this->component(), snk, s, n);
171 pos_ += result;
172 return result;
173 }
174
175 template<typename Device>
176 std::streampos seek(Device& dev, stream_offset off, NDNBOOST_IOS::seekdir way)
177 {
178 stream_offset next;
179 if (way == NDNBOOST_IOS::beg) {
180 next = beg_ + off;
181 } else if (way == NDNBOOST_IOS::cur) {
182 next = pos_ + off;
183 } else if (end_ != -1) {
184 next = end_ + off;
185 } else {
186 // Restriction is half-open; seek relative to the actual end.
187 pos_ = this->component().seek(dev, off, NDNBOOST_IOS::end);
188 if (pos_ < beg_)
189 ndnboost::throw_exception(bad_seek());
190 return offset_to_position(pos_ - beg_);
191 }
192 if (next < beg_ || (end_ != -1 && next >= end_))
193 ndnboost::throw_exception(bad_seek());
194 pos_ = this->component().seek(dev, next, NDNBOOST_IOS::cur);
195 return offset_to_position(pos_ - beg_);
196 }
197
198 template<typename Device>
199 void close(Device& dev)
200 {
201 open_ = false;
202 detail::close_all(this->component(), dev);
203 }
204
205 template<typename Device>
206 void close(Device& dev, NDNBOOST_IOS::openmode which)
207 {
208 open_ = false;
209 iostreams::close(this->component(), dev, which);
210 }
211private:
212 template<typename Device>
213 void open(Device& dev, NDNBOOST_IOS::openmode which)
214 {
215 typedef typename is_convertible<mode, dual_use>::type is_dual_use;
216 open_ = true;
217 which = is_dual_use() ? which : (NDNBOOST_IOS::in | NDNBOOST_IOS::out);
218 iostreams::skip(this->component(), dev, beg_, which);
219 }
220
221 stream_offset beg_, pos_, end_;
222 bool open_;
223};
224
225template<typename T>
226struct restriction_traits
227 : iostreams::select< // Disambiguation for Tru64.
228 is_filter<T>, restricted_filter<T>,
229 is_direct<T>, restricted_direct_device<T>,
230 else_, restricted_indirect_device<T>
231 >
232 { };
233
234} // End namespace detail.
235
236template<typename T>
237struct restriction : public detail::restriction_traits<T>::type {
238 typedef typename detail::param_type<T>::type param_type;
239 typedef typename detail::restriction_traits<T>::type base_type;
240 restriction(param_type t, stream_offset off, stream_offset len = -1)
241 : base_type(t, off, len)
242 { }
243};
244
245namespace detail {
246
247//--------------Implementation of restricted_indirect_device------------------//
248
249template<typename Device>
250restricted_indirect_device<Device>::restricted_indirect_device
251 (param_type dev, stream_offset off, stream_offset len)
252 : device_adapter<Device>(dev), beg_(off), pos_(off),
253 end_(len != -1 ? off + len : -1)
254{
255 if (len < -1 || off < 0)
256 ndnboost::throw_exception(NDNBOOST_IOSTREAMS_FAILURE("bad offset"));
257 iostreams::skip(this->component(), off);
258}
259
260template<typename Device>
261inline std::streamsize restricted_indirect_device<Device>::read
262 (char_type* s, std::streamsize n)
263{
264 using namespace std;
265 std::streamsize amt =
266 end_ != -1 ?
267 (std::min) (n, static_cast<std::streamsize>(end_ - pos_)) :
268 n;
269 std::streamsize result = iostreams::read(this->component(), s, amt);
270 if (result != -1)
271 pos_ += result;
272 return result;
273}
274
275template<typename Device>
276inline std::streamsize restricted_indirect_device<Device>::write
277 (const char_type* s, std::streamsize n)
278{
279 if (end_ != -1 && pos_ + n >= end_) {
280 if(pos_ < end_)
281 pos_ += iostreams::write(this->component(), s, end_ - pos_);
282 ndnboost::throw_exception(bad_write());
283 }
284 std::streamsize result = iostreams::write(this->component(), s, n);
285 pos_ += result;
286 return result;
287}
288
289template<typename Device>
290std::streampos restricted_indirect_device<Device>::seek
291 (stream_offset off, NDNBOOST_IOS::seekdir way)
292{
293 stream_offset next;
294 if (way == NDNBOOST_IOS::beg) {
295 next = beg_ + off;
296 } else if (way == NDNBOOST_IOS::cur) {
297 next = pos_ + off;
298 } else if (end_ != -1) {
299 next = end_ + off;
300 } else {
301 // Restriction is half-open; seek relative to the actual end.
302 pos_ = iostreams::seek(this->component(), off, NDNBOOST_IOS::end);
303 if (pos_ < beg_)
304 ndnboost::throw_exception(bad_seek());
305 return offset_to_position(pos_ - beg_);
306 }
307 if (next < beg_ || (end_ != -1 && next > end_))
308 ndnboost::throw_exception(bad_seek());
309 pos_ = iostreams::seek(this->component(), next - pos_, NDNBOOST_IOS::cur);
310 return offset_to_position(pos_ - beg_);
311}
312
313//--------------Implementation of restricted_direct_device--------------------//
314
315template<typename Device>
316restricted_direct_device<Device>::restricted_direct_device
317 (const Device& dev, stream_offset off, stream_offset len)
318 : device_adapter<Device>(dev), beg_(0), end_(0)
319{
320 std::pair<char_type*, char_type*> seq =
321 sequence(is_convertible<category, input>());
322 if ( off < 0 || len < -1 ||
323 (len != -1 && off + len > seq.second - seq.first) )
324 {
325 ndnboost::throw_exception(NDNBOOST_IOSTREAMS_FAILURE("bad offset"));
326 }
327 beg_ = seq.first + off;
328 end_ = len != -1 ?
329 seq.first + off + len :
330 seq.second;
331}
332
333template<typename Device>
334typename restricted_direct_device<Device>::pair_type
335restricted_direct_device<Device>::input_sequence()
336{
337 NDNBOOST_STATIC_ASSERT((is_convertible<category, input>::value));
338 return std::make_pair(beg_, end_);
339}
340
341template<typename Device>
342typename restricted_direct_device<Device>::pair_type
343restricted_direct_device<Device>::output_sequence()
344{
345 NDNBOOST_STATIC_ASSERT((is_convertible<category, output>::value));
346 return std::make_pair(beg_, end_);
347}
348
349template<typename Device>
350typename restricted_direct_device<Device>::pair_type
351restricted_direct_device<Device>::sequence(mpl::true_)
352{ return iostreams::input_sequence(this->component()); }
353
354template<typename Device>
355typename restricted_direct_device<Device>::pair_type
356restricted_direct_device<Device>::sequence(mpl::false_)
357{ return iostreams::output_sequence(this->component()); }
358
359//--------------Implementation of restricted_filter---------------------------//
360
361template<typename Filter>
362restricted_filter<Filter>::restricted_filter
363 (const Filter& flt, stream_offset off, stream_offset len)
364 : filter_adapter<Filter>(flt), beg_(off),
365 pos_(off), end_(len != -1 ? off + len : -1), open_(false)
366{
367 if (len < -1 || off < 0)
368 ndnboost::throw_exception(NDNBOOST_IOSTREAMS_FAILURE("bad offset"));
369}
370
371} // End namespace detail.
372
373} } // End namespaces iostreams, boost.
374
375#elif defined(NDNBOOST_IOSTREAMS_RESTRICT)
376
377namespace ndnboost { namespace iostreams {
378
379//--------------Implementation of restrict/slice------------------------------//
380
381// Note: The following workarounds are patterned after resolve.hpp. It has not
382// yet been confirmed that they are necessary.
383
384# ifndef NDNBOOST_IOSTREAMS_BROKEN_OVERLOAD_RESOLUTION //------------------------//
385# ifndef NDNBOOST_IOSTREAMS_NO_STREAM_TEMPLATES //------------------------------//
386
387template<typename T>
388restriction<T>
389NDNBOOST_IOSTREAMS_RESTRICT( const T& t, stream_offset off, stream_offset len = -1
390 NDNBOOST_IOSTREAMS_DISABLE_IF_STREAM(T) )
391{ return restriction<T>(t, off, len); }
392
393template<typename Ch, typename Tr>
394restriction< std::basic_streambuf<Ch, Tr> >
395NDNBOOST_IOSTREAMS_RESTRICT( std::basic_streambuf<Ch, Tr>& sb, stream_offset off,
396 stream_offset len = -1 )
397{ return restriction< std::basic_streambuf<Ch, Tr> >(sb, off, len); }
398
399template<typename Ch, typename Tr>
400restriction< std::basic_istream<Ch, Tr> >
401NDNBOOST_IOSTREAMS_RESTRICT
402 (std::basic_istream<Ch, Tr>& is, stream_offset off, stream_offset len = -1)
403{ return restriction< std::basic_istream<Ch, Tr> >(is, off, len); }
404
405template<typename Ch, typename Tr>
406restriction< std::basic_ostream<Ch, Tr> >
407NDNBOOST_IOSTREAMS_RESTRICT
408 (std::basic_ostream<Ch, Tr>& os, stream_offset off, stream_offset len = -1)
409{ return restriction< std::basic_ostream<Ch, Tr> >(os, off, len); }
410
411template<typename Ch, typename Tr>
412restriction< std::basic_iostream<Ch, Tr> >
413NDNBOOST_IOSTREAMS_RESTRICT
414 (std::basic_iostream<Ch, Tr>& io, stream_offset off, stream_offset len = -1)
415{ return restriction< std::basic_iostream<Ch, Tr> >(io, off, len); }
416
417# else // # ifndef NDNBOOST_IOSTREAMS_NO_STREAM_TEMPLATES //--------------------//
418
419template<typename T>
420restriction<T>
421NDNBOOST_IOSTREAMS_RESTRICT( const T& t, stream_offset off, stream_offset len = -1
422 NDNBOOST_IOSTREAMS_DISABLE_IF_STREAM(T) )
423{ return restriction<T>(t, off, len); }
424
425restriction<std::streambuf>
426NDNBOOST_IOSTREAMS_RESTRICT
427 (std::streambuf& sb, stream_offset off, stream_offset len = -1)
428{ return restriction<std::streambuf>(sb, off, len); }
429
430restriction<std::istream>
431NDNBOOST_IOSTREAMS_RESTRICT
432 (std::istream<Ch, Tr>& is, stream_offset off, stream_offset len = -1)
433{ return restriction<std::istream>(is, off, len); }
434
435restriction<std::ostream>
436NDNBOOST_IOSTREAMS_RESTRICT
437 (std::ostream<Ch, Tr>& os, stream_offset off, stream_offset len = -1)
438{ return restriction<std::ostream>(os, off, len); }
439
440restriction<std::iostream>
441NDNBOOST_IOSTREAMS_RESTRICT
442 (std::iostream<Ch, Tr>& io, stream_offset off, stream_offset len = -1)
443{ return restriction<std::iostream>(io, off, len); }
444
445# endif // # ifndef NDNBOOST_IOSTREAMS_NO_STREAM_TEMPLATES //-------------------//
446# else // #ifndef NDNBOOST_IOSTREAMS_BROKEN_OVERLOAD_RESOLUTION //---------------//
447
448template<typename T>
449restriction<T>
450NDNBOOST_IOSTREAMS_RESTRICT
451 (const T& t, stream_offset off, stream_offset len, mpl::true_)
452{ // Bad overload resolution.
453 return restriction<T>(const_cast<T&>(t, off, len));
454}
455
456template<typename T>
457restriction<T>
458NDNBOOST_IOSTREAMS_RESTRICT
459 (const T& t, stream_offset off, stream_offset len, mpl::false_)
460{ return restriction<T>(t, off, len); }
461
462template<typename T>
463restriction<T>
464NDNBOOST_IOSTREAMS_RESTRICT( const T& t, stream_offset off, stream_offset len = -1
465 NDNBOOST_IOSTREAMS_DISABLE_IF_STREAM(T) )
466{ return NDNBOOST_IOSTREAMS_RESTRICT(t, off, len, is_std_io<T>()); }
467
468# if !NDNBOOST_WORKAROUND(__BORLANDC__, < 0x600) && \
469 !NDNBOOST_WORKAROUND(NDNBOOST_MSVC, <= 1300) && \
470 !defined(__GNUC__) // ---------------------------------------------------//
471
472template<typename T>
473restriction<T>
474NDNBOOST_IOSTREAMS_RESTRICT(T& t, stream_offset off, stream_offset len = -1)
475{ return restriction<T>(t, off, len); }
476
477# endif // Borland 5.x, VC6-7.0 or GCC 2.9x //-------------------------------//
478# endif // #ifndef NDNBOOST_IOSTREAMS_BROKEN_OVERLOAD_RESOLUTION //--------------//
479
480} } // End namespaces iostreams, boost.
481
482#endif // #if !defined(NDNBOOST_IOSTREAMS_RESTRICT_IMPL_HPP_INCLUDED) ...