Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 09:38:48

0001 // (C) Copyright 2008 CodeRage, LLC (turkanis at coderage dot com)
0002 // (C) Copyright 2003-2007 Jonathan Turkanis
0003 // Distributed under the Boost Software License, Version 1.0. (See accompanying
0004 // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt.)
0005 
0006 // See http://www.boost.org/libs/iostreams for documentation.
0007 
0008 #ifndef BOOST_IOSTREAMS_DETAIL_DIRECT_ADAPTER_HPP_INCLUDED
0009 #define BOOST_IOSTREAMS_DETAIL_DIRECT_ADAPTER_HPP_INCLUDED
0010 
0011 #if defined(_MSC_VER)
0012 # pragma once
0013 #endif              
0014 
0015 #include <boost/config.hpp>       // SFINAE, MSVC, put ptrdiff_t in std.
0016 #include <algorithm>              // copy, min.
0017 #include <cstddef>                // ptrdiff_t.
0018 #include <boost/detail/workaround.hpp>
0019 #include <boost/iostreams/categories.hpp>
0020 #include <boost/iostreams/detail/config/limits.hpp>        // forwarding.
0021 #include <boost/iostreams/detail/config/wide_streams.hpp>  // locale.
0022 #include <boost/iostreams/detail/double_object.hpp>
0023 #include <boost/iostreams/detail/error.hpp>
0024 #include <boost/iostreams/detail/ios.hpp>  // openmode, seekdir, int types.
0025 #include <boost/iostreams/traits.hpp>      // mode_of, is_direct.
0026 #include <boost/iostreams/operations.hpp>
0027 #include <boost/mpl/bool.hpp> 
0028 #include <boost/mpl/or.hpp> 
0029 #include <boost/preprocessor/iteration/local.hpp>
0030 #include <boost/preprocessor/repetition/enum_binary_params.hpp>
0031 #include <boost/preprocessor/repetition/enum_params.hpp>
0032 #include <boost/static_assert.hpp>
0033 #include <boost/throw_exception.hpp>
0034 #include <boost/type_traits/is_convertible.hpp>
0035 
0036 // Must come last.
0037 #include <boost/iostreams/detail/config/disable_warnings.hpp> // VC7.1
0038 
0039 namespace boost { namespace iostreams { namespace detail {
0040                     
0041 //------------------Definition of direct_adapter_base-------------------------//
0042 
0043 // Put all initialization in base class to faciliate forwarding.
0044 template<typename Direct>
0045 class direct_adapter_base {
0046 public:
0047     typedef typename char_type_of<Direct>::type  char_type;
0048     typedef typename mode_of<Direct>::type       mode_type;
0049     struct category 
0050         : mode_type,
0051           device_tag,
0052           closable_tag
0053           #ifndef BOOST_IOSTREAMS_NO_LOCALE
0054           , localizable_tag
0055           #endif
0056         { };
0057 protected:
0058     explicit direct_adapter_base(const Direct& d);
0059     typedef is_convertible<category, two_sequence> is_double;
0060     struct pointers {
0061         pointers() : beg(0), ptr(0), end(0) { }
0062         char_type *beg, *ptr, *end;
0063     };
0064     void init_input(mpl::true_);
0065     void init_input(mpl::false_) { }
0066     void init_output(mpl::true_);
0067     void init_output(mpl::false_) { }
0068     double_object<pointers, is_double>  ptrs_;
0069     Direct                              d_;
0070 };
0071 
0072 template<typename Direct>
0073 class direct_adapter : private direct_adapter_base<Direct> {
0074 private:
0075     typedef direct_adapter_base<Direct>      base_type;
0076     typedef typename base_type::pointers     pointers;
0077     typedef typename base_type::is_double    is_double;
0078     using base_type::ptrs_;
0079     using base_type::d_;
0080 public:
0081     typedef typename base_type::char_type    char_type;
0082     typedef typename base_type::category     category;
0083 
0084         // Constructors
0085 
0086 #if !BOOST_WORKAROUND(BOOST_MSVC, <= 1310)
0087     direct_adapter(const Direct& d) : base_type(d) { }   
0088     direct_adapter(const direct_adapter& d) : base_type(d) { }
0089 # define BOOST_PP_LOCAL_LIMITS (1, BOOST_IOSTREAMS_MAX_FORWARDING_ARITY)
0090 #else
0091     template<typename U>
0092     struct is_direct
0093         : mpl::or_< 
0094               is_same<U, direct_adapter<Direct> >, 
0095               is_same<U, Direct> 
0096           >
0097         { };
0098     template<typename U>
0099     direct_adapter(const U& u) 
0100         : base_type(forward(u, is_direct<U>()))
0101         { }
0102 # define BOOST_PP_LOCAL_LIMITS (2, BOOST_IOSTREAMS_MAX_FORWARDING_ARITY)
0103 #endif
0104 
0105 #define BOOST_PP_LOCAL_MACRO(n) \
0106     template<BOOST_PP_ENUM_PARAMS(n, typename P)> \
0107     direct_adapter(BOOST_PP_ENUM_BINARY_PARAMS(n, const P, &p)) \
0108         : base_type(Direct(BOOST_PP_ENUM_PARAMS(n, p))) \
0109         { } \
0110     /**/
0111 #include BOOST_PP_LOCAL_ITERATE()
0112 #undef BOOST_PP_LOCAL_MACRO
0113 
0114         // Device interface.
0115 
0116     std::streamsize read(char_type* s, std::streamsize n);
0117     std::streamsize write(const char_type* s, std::streamsize n);
0118     std::streampos seek( stream_offset, BOOST_IOS::seekdir,
0119                          BOOST_IOS::openmode = BOOST_IOS::in | BOOST_IOS::out );
0120     void close();
0121     void close(BOOST_IOS::openmode which);
0122 #ifndef BOOST_IOSTREAMS_NO_LOCALE
0123     void imbue(const std::locale&);
0124 #endif
0125 
0126         // Direct device access.
0127 
0128     Direct& operator*() { return d_; }
0129     Direct* operator->() { return &d_; }
0130 #if BOOST_WORKAROUND(BOOST_MSVC, <= 1310)
0131 private:
0132     template<typename U>
0133     static Direct forward(const U& u, mpl::true_) { return u; }
0134     template<typename U>
0135     static Direct forward(const U& u, mpl::false_) { return Direct(u); }
0136 #endif
0137 };
0138 
0139 //--------------Definition of wrap_direct and unwrap_direct-------------------//
0140 
0141 template<typename Device>
0142 struct wrap_direct_traits 
0143     : mpl::if_<
0144           is_direct<Device>,
0145           direct_adapter<Device>,
0146           Device
0147       >
0148     { };
0149 
0150 template<typename Device>
0151 typename wrap_direct_traits<Device>::type
0152 inline wrap_direct(Device dev) 
0153 { 
0154     typedef typename wrap_direct_traits<Device>::type type;
0155     return type(dev); 
0156 }
0157 
0158 template<typename Device>
0159 inline Device& unwrap_direct(Device& d) { return d; }  
0160 
0161 template<typename Device>
0162 inline Device& unwrap_direct(direct_adapter<Device>& d) { return *d; }  
0163 
0164 //--------------Implementation of direct_adapter_base-------------------------//
0165 
0166 template<typename Direct>
0167 direct_adapter_base<Direct>::direct_adapter_base(const Direct& d) : d_(d)
0168 {
0169     init_input(is_convertible<category, input>());
0170     init_output(is_convertible<category, output>());
0171 }
0172 
0173 template<typename Direct>
0174 void direct_adapter_base<Direct>::init_input(mpl::true_) 
0175 {
0176     std::pair<char_type*, char_type*> seq = iostreams::input_sequence(d_);
0177     ptrs_.first().beg = seq.first;
0178     ptrs_.first().ptr = seq.first;
0179     ptrs_.first().end = seq.second;
0180 }
0181 
0182 template<typename Direct>
0183 void direct_adapter_base<Direct>::init_output(mpl::true_) 
0184 {
0185     std::pair<char_type*, char_type*> seq = iostreams::output_sequence(d_);
0186     ptrs_.second().beg = seq.first;
0187     ptrs_.second().ptr = seq.first;
0188     ptrs_.second().end = seq.second;
0189 }
0190 
0191 //--------------Implementation of direct_adapter------------------------------//
0192 
0193 template<typename Direct>
0194 inline std::streamsize direct_adapter<Direct>::read
0195     (char_type* s, std::streamsize n)
0196 {
0197     using namespace std;
0198     pointers& get = ptrs_.first();
0199     std::streamsize avail = 
0200         static_cast<std::streamsize>(get.end - get.ptr);
0201     std::streamsize result = (std::min)(n, avail);
0202     std::copy(get.ptr, get.ptr + result, s);
0203     get.ptr += result;
0204     return result != 0 ? result : -1;
0205 }
0206 
0207 template<typename Direct>
0208 inline std::streamsize direct_adapter<Direct>::write
0209     (const char_type* s, std::streamsize n)
0210 {
0211     using namespace std;
0212     pointers& put = ptrs_.second();
0213     if (n > static_cast<std::streamsize>(put.end - put.ptr))
0214         boost::throw_exception(write_area_exhausted());
0215     std::copy(s, s + n, put.ptr);
0216     put.ptr += n;
0217     return n;
0218 }
0219 
0220 template<typename Direct>
0221 inline std::streampos direct_adapter<Direct>::seek
0222     ( stream_offset off, BOOST_IOS::seekdir way, 
0223       BOOST_IOS::openmode which )
0224 {
0225     using namespace std;
0226     pointers& get = ptrs_.first();
0227     pointers& put = ptrs_.second();
0228     if (way == BOOST_IOS::cur && get.ptr != put.ptr)
0229        boost::throw_exception(bad_seek());
0230     ptrdiff_t next = 0;
0231     if ((which & BOOST_IOS::in) || !is_double::value) {
0232         if (way == BOOST_IOS::beg)
0233             next = off; 
0234         else if (way == BOOST_IOS::cur)
0235             next = get.ptr - get.beg + off; 
0236         else
0237             next = get.end - get.beg + off; 
0238         if (next >= 0 && next <= get.end - get.beg)
0239             get.ptr = get.beg + next;
0240         else
0241             boost::throw_exception(bad_seek());
0242     }
0243     if ((which & BOOST_IOS::out) && is_double::value) {
0244         if (way == BOOST_IOS::beg)
0245             next = off; 
0246         else if (way == BOOST_IOS::cur)
0247             next = put.ptr - put.beg + off; 
0248         else
0249             next = put.end - put.beg + off; 
0250         if (next >= 0 && next <= put.end - put.beg)
0251             put.ptr = put.beg + next;
0252         else
0253             boost::throw_exception(bad_seek());
0254     }
0255     return offset_to_position(next);
0256 }
0257 
0258 template<typename Direct>
0259 void direct_adapter<Direct>::close() 
0260 { 
0261     BOOST_STATIC_ASSERT((!is_convertible<category, two_sequence>::value));
0262     detail::close_all(d_);
0263 }
0264 
0265 template<typename Direct>
0266 void direct_adapter<Direct>::close(BOOST_IOS::openmode which) 
0267 { 
0268     BOOST_STATIC_ASSERT((is_convertible<category, two_sequence>::value));
0269     boost::iostreams::close(d_, which);
0270 }
0271 
0272 #ifndef BOOST_IOSTREAMS_NO_LOCALE
0273     template<typename Direct>
0274     void direct_adapter<Direct>::imbue(const std::locale& loc) 
0275     { boost::iostreams::imbue(d_, loc); }
0276 #endif
0277 
0278 } } } // End namespaces detail, iostreams, boost.
0279 
0280 #include <boost/iostreams/detail/config/enable_warnings.hpp>
0281 
0282 #endif // #ifndef BOOST_IOSTREAMS_DETAIL_DIRECT_ADAPTER_HPP_INCLUDED