PageRenderTime 59ms CodeModel.GetById 2ms app.highlight 50ms RepoModel.GetById 1ms app.codeStats 0ms

/Src/Dependencies/Boost/boost/iostreams/detail/restrict_impl.hpp

http://hadesmem.googlecode.com/
C++ Header | 482 lines | 374 code | 59 blank | 49 comment | 62 complexity | c0700f8788dae6aa5ee91a35cc4bf86c MD5 | raw file
  1/*
  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:        boost/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 BOOST_IOSTREAMS_RESTRICT undefined, defines the 
 14 * class template boost::iostreams::restriction. If included with the macro
 15 * BOOST_IOSTREAMS_RESTRICT defined as an identifier, defines the overloaded
 16 * function template boost::iostreams::BOOST_IOSTREAMS_RESTRICT, and object 
 17 * generator for boost::iostreams::restriction.
 18 *
 19 * This design allows <boost/iostreams/restrict.hpp> and 
 20 * <boost/iostreams/slice.hpp> to share an implementation.
 21 */
 22
 23#if !defined(BOOST_IOSTREAMS_RESTRICT_IMPL_HPP_INCLUDED) && \
 24    !defined(BOOST_IOSTREAMS_RESTRICT)
 25# define BOOST_IOSTREAMS_RESTRICT_IMPL_HPP_INCLUDED
 26                    
 27//------------------Implementation of restriction-----------------------------//
 28
 29# include <algorithm>          // min.
 30# include <utility>            // pair.
 31# include <boost/cstdint.hpp>  // intmax_t.
 32# include <boost/config.hpp>   // DEDUCED_TYPENAME.
 33# include <boost/iostreams/categories.hpp>
 34# include <boost/iostreams/char_traits.hpp>
 35# include <boost/iostreams/detail/adapter/device_adapter.hpp>
 36# include <boost/iostreams/detail/adapter/filter_adapter.hpp>
 37# include <boost/iostreams/detail/call_traits.hpp>
 38# include <boost/iostreams/detail/enable_if_stream.hpp>
 39# include <boost/iostreams/detail/error.hpp>
 40# include <boost/iostreams/detail/ios.hpp>     // failure.
 41# include <boost/iostreams/detail/select.hpp>
 42# include <boost/iostreams/operations.hpp>
 43# include <boost/iostreams/skip.hpp>
 44# include <boost/iostreams/traits.hpp>         // mode_of, is_direct.
 45# include <boost/mpl/bool.hpp>
 46# include <boost/static_assert.hpp>
 47# include <boost/throw_exception.hpp>
 48# include <boost/type_traits/is_convertible.hpp>
 49
 50# include <boost/iostreams/detail/config/disable_warnings.hpp>
 51
 52namespace boost { 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 paramters:
 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    BOOST_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, BOOST_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 paramters:
 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    BOOST_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 paramters:
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    BOOST_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, BOOST_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, BOOST_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            boost::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, BOOST_IOS::seekdir way)
177    {
178        stream_offset next;
179        if (way == BOOST_IOS::beg) {
180            next = beg_ + off;
181        } else if (way == BOOST_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, BOOST_IOS::end);
188            if (pos_ < beg_)
189                boost::throw_exception(bad_seek());
190            return offset_to_position(pos_ - beg_);
191        }
192        if (next < beg_ || (end_ != -1 && next >= end_))
193            boost::throw_exception(bad_seek());
194        pos_ = this->component().seek(dev, next, BOOST_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, BOOST_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, BOOST_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 : (BOOST_IOS::in | BOOST_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        boost::throw_exception(BOOST_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        boost::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, BOOST_IOS::seekdir way)
292{
293    stream_offset next;
294    if (way == BOOST_IOS::beg) {
295        next = beg_ + off;
296    } else if (way == BOOST_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, BOOST_IOS::end);
303        if (pos_ < beg_)
304            boost::throw_exception(bad_seek());
305        return offset_to_position(pos_ - beg_);
306    }
307    if (next < beg_ || (end_ != -1 && next > end_))
308        boost::throw_exception(bad_seek());
309    pos_ = iostreams::seek(this->component(), next - pos_, BOOST_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        boost::throw_exception(BOOST_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    BOOST_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    BOOST_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        boost::throw_exception(BOOST_IOSTREAMS_FAILURE("bad offset"));
369}
370
371} // End namespace detail.
372
373} } // End namespaces iostreams, boost.
374
375#elif defined(BOOST_IOSTREAMS_RESTRICT)
376
377namespace boost { 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 BOOST_IOSTREAMS_BROKEN_OVERLOAD_RESOLUTION //------------------------//
385#  ifndef BOOST_IOSTREAMS_NO_STREAM_TEMPLATES //------------------------------//
386
387template<typename T>
388restriction<T> 
389BOOST_IOSTREAMS_RESTRICT( const T& t, stream_offset off, stream_offset len = -1
390                          BOOST_IOSTREAMS_DISABLE_IF_STREAM(T) )
391{ return restriction<T>(t, off, len); }
392
393template<typename Ch, typename Tr>
394restriction< std::basic_streambuf<Ch, Tr> >
395BOOST_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> >
401BOOST_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> >
407BOOST_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> >
413BOOST_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 BOOST_IOSTREAMS_NO_STREAM_TEMPLATES //--------------------//
418
419template<typename T>
420restriction<T> 
421BOOST_IOSTREAMS_RESTRICT( const T& t, stream_offset off, stream_offset len = -1
422                          BOOST_IOSTREAMS_DISABLE_IF_STREAM(T) )
423{ return restriction<T>(t, off, len); }
424
425restriction<std::streambuf> 
426BOOST_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> 
431BOOST_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> 
436BOOST_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> 
441BOOST_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 BOOST_IOSTREAMS_NO_STREAM_TEMPLATES //-------------------//
446# else // #ifndef BOOST_IOSTREAMS_BROKEN_OVERLOAD_RESOLUTION //---------------//
447
448template<typename T>
449restriction<T> 
450BOOST_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> 
458BOOST_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> 
464BOOST_IOSTREAMS_RESTRICT( const T& t, stream_offset off, stream_offset len = -1
465                          BOOST_IOSTREAMS_DISABLE_IF_STREAM(T) )
466{ return BOOST_IOSTREAMS_RESTRICT(t, off, len, is_std_io<T>()); }
467
468# if !BOOST_WORKAROUND(__BORLANDC__, < 0x600) && \
469     !BOOST_WORKAROUND(BOOST_MSVC, <= 1300) && \
470     !defined(__GNUC__) // ---------------------------------------------------//
471
472template<typename T>
473restriction<T>
474BOOST_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 BOOST_IOSTREAMS_BROKEN_OVERLOAD_RESOLUTION //--------------//
479
480} } // End namespaces iostreams, boost.
481
482#endif // #if !defined(BOOST_IOSTREAMS_RESTRICT_IMPL_HPP_INCLUDED) ...