Back to home page

EIC code displayed by LXR

 
 

    


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

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_STREAMBUF_HPP_INCLUDED
0009 #define BOOST_IOSTREAMS_DETAIL_DIRECT_STREAMBUF_HPP_INCLUDED
0010 
0011 #if defined(_MSC_VER)
0012 # pragma once
0013 #endif              
0014 
0015 #include <boost/assert.hpp>
0016 #include <cstddef>
0017 #include <utility>                                 // pair.
0018 #include <boost/config.hpp>                        // BOOST_DEDUCED_TYPENAME, 
0019 #include <boost/core/typeinfo.hpp>
0020 #include <boost/iostreams/detail/char_traits.hpp>  // member template friends.
0021 #include <boost/iostreams/detail/config/wide_streams.hpp>
0022 #include <boost/iostreams/detail/error.hpp>
0023 #include <boost/iostreams/detail/execute.hpp>
0024 #include <boost/iostreams/detail/functional.hpp>
0025 #include <boost/iostreams/detail/ios.hpp>
0026 #include <boost/iostreams/detail/optional.hpp>
0027 #include <boost/iostreams/detail/streambuf.hpp>
0028 #include <boost/iostreams/detail/streambuf/linked_streambuf.hpp>
0029 #include <boost/iostreams/operations.hpp>
0030 #include <boost/iostreams/positioning.hpp>
0031 #include <boost/iostreams/traits.hpp>
0032 #include <boost/throw_exception.hpp>
0033 
0034 // Must come last.
0035 #include <boost/iostreams/detail/config/disable_warnings.hpp> // MSVC.
0036 
0037 namespace boost { namespace iostreams { 
0038     
0039 namespace detail {
0040 
0041 template< typename T,
0042           typename Tr = 
0043               BOOST_IOSTREAMS_CHAR_TRAITS(
0044                  BOOST_DEDUCED_TYPENAME char_type_of<T>::type 
0045               ) >
0046 class direct_streambuf 
0047     : public linked_streambuf<BOOST_DEDUCED_TYPENAME char_type_of<T>::type, Tr>
0048 {
0049 public:
0050     typedef typename char_type_of<T>::type                char_type;
0051     BOOST_IOSTREAMS_STREAMBUF_TYPEDEFS(Tr)
0052 private:
0053     typedef linked_streambuf<char_type, traits_type>      base_type;
0054     typedef typename category_of<T>::type                 category;
0055     typedef BOOST_IOSTREAMS_BASIC_STREAMBUF(
0056                 char_type, traits_type
0057             )                                             streambuf_type;
0058 public: // stream needs access.
0059     void open(const T& t, std::streamsize buffer_size, 
0060               std::streamsize pback_size);
0061     bool is_open() const;
0062     void close();
0063     bool auto_close() const { return auto_close_; }
0064     void set_auto_close(bool close) { auto_close_ = close; }
0065     bool strict_sync() { return true; }
0066 
0067     // Declared in linked_streambuf.
0068     T* component() { return storage_.get(); }
0069 protected:
0070     BOOST_IOSTREAMS_USING_PROTECTED_STREAMBUF_MEMBERS(base_type)
0071     direct_streambuf();
0072 
0073     //--------------Virtual functions-----------------------------------------//
0074 
0075     // Declared in linked_streambuf.
0076     void close_impl(BOOST_IOS::openmode m);
0077     const boost::core::typeinfo& component_type() const { return BOOST_CORE_TYPEID(T); }
0078     void* component_impl() { return component(); } 
0079 #ifdef BOOST_IOSTREAMS_NO_STREAM_TEMPLATES
0080     public:
0081 #endif
0082 
0083     // Declared in basic_streambuf.
0084     int_type underflow();
0085     int_type pbackfail(int_type c);
0086     int_type overflow(int_type c);
0087     pos_type seekoff( off_type off, BOOST_IOS::seekdir way,
0088                       BOOST_IOS::openmode which );
0089     pos_type seekpos(pos_type sp, BOOST_IOS::openmode which);
0090 private:
0091     pos_type seek_impl( stream_offset off, BOOST_IOS::seekdir way,
0092                         BOOST_IOS::openmode which );
0093     void init_input(any_tag) { }
0094     void init_input(input);
0095     void init_output(any_tag) { }
0096     void init_output(output);
0097     void init_get_area();
0098     void init_put_area();
0099     bool one_head() const;
0100     bool two_head() const;
0101     optional<T>  storage_;
0102     char_type   *ibeg_, *iend_, *obeg_, *oend_;
0103     bool         auto_close_;
0104 };
0105                     
0106 //------------------Implementation of direct_streambuf------------------------//
0107 
0108 template<typename T, typename Tr>
0109 direct_streambuf<T, Tr>::direct_streambuf() 
0110     : ibeg_(0), iend_(0), obeg_(0), oend_(0), auto_close_(true) 
0111 { this->set_true_eof(true); }
0112 
0113 template<typename T, typename Tr>
0114 void direct_streambuf<T, Tr>::open
0115     (const T& t, std::streamsize, std::streamsize)
0116 {
0117     storage_.reset(t);
0118     init_input(category());
0119     init_output(category());
0120     setg(0, 0, 0);
0121     setp(0, 0);
0122     this->set_needs_close();
0123 }
0124 
0125 template<typename T, typename Tr>
0126 bool direct_streambuf<T, Tr>::is_open() const 
0127 { return ibeg_ != 0 || obeg_ != 0; }
0128 
0129 template<typename T, typename Tr>
0130 void direct_streambuf<T, Tr>::close() 
0131 { 
0132     base_type* self = this;
0133     detail::execute_all( detail::call_member_close(*self, BOOST_IOS::in),
0134                          detail::call_member_close(*self, BOOST_IOS::out),
0135                          detail::call_reset(storage_) );
0136 }
0137 
0138 template<typename T, typename Tr>
0139 typename direct_streambuf<T, Tr>::int_type 
0140 direct_streambuf<T, Tr>::underflow()
0141 {
0142     if (!ibeg_) 
0143         boost::throw_exception(cant_read());
0144     if (!gptr()) 
0145         init_get_area();
0146     return gptr() != iend_ ? 
0147         traits_type::to_int_type(*gptr()) : 
0148         traits_type::eof();
0149 }
0150 
0151 template<typename T, typename Tr>
0152 typename direct_streambuf<T, Tr>::int_type 
0153 direct_streambuf<T, Tr>::pbackfail(int_type c)
0154 {
0155     using namespace std;
0156     if (!ibeg_) 
0157         boost::throw_exception(cant_read());
0158     if (gptr() != 0 && gptr() != ibeg_) {
0159         gbump(-1);
0160         if (!traits_type::eq_int_type(c, traits_type::eof()))
0161             *gptr() = traits_type::to_char_type(c);
0162         return traits_type::not_eof(c);
0163     }
0164     boost::throw_exception(bad_putback());
0165 }
0166 
0167 template<typename T, typename Tr>
0168 typename direct_streambuf<T, Tr>::int_type 
0169 direct_streambuf<T, Tr>::overflow(int_type c)
0170 {
0171     using namespace std;
0172     if (!obeg_)
0173         boost::throw_exception(BOOST_IOSTREAMS_FAILURE("no write access"));
0174     if (!pptr()) init_put_area();
0175     if (!traits_type::eq_int_type(c, traits_type::eof())) {
0176         if (pptr() == oend_)
0177             boost::throw_exception(
0178                 BOOST_IOSTREAMS_FAILURE("write area exhausted")
0179             );
0180         *pptr() = traits_type::to_char_type(c);
0181         pbump(1);
0182         return c;
0183     }
0184     return traits_type::not_eof(c);
0185 }
0186 
0187 template<typename T, typename Tr>
0188 inline typename direct_streambuf<T, Tr>::pos_type
0189 direct_streambuf<T, Tr>::seekoff
0190     (off_type off, BOOST_IOS::seekdir way, BOOST_IOS::openmode which)
0191 { return seek_impl(off, way, which); }
0192 
0193 template<typename T, typename Tr>
0194 inline typename direct_streambuf<T, Tr>::pos_type
0195 direct_streambuf<T, Tr>::seekpos
0196     (pos_type sp, BOOST_IOS::openmode which)
0197 { 
0198     return seek_impl(position_to_offset(sp), BOOST_IOS::beg, which);
0199 }
0200 
0201 template<typename T, typename Tr>
0202 void direct_streambuf<T, Tr>::close_impl(BOOST_IOS::openmode which)
0203 {
0204     if (which == BOOST_IOS::in && ibeg_ != 0) {
0205         setg(0, 0, 0);
0206         ibeg_ = iend_ = 0;
0207     }
0208     if (which == BOOST_IOS::out && obeg_ != 0) {
0209         sync();
0210         setp(0, 0);
0211         obeg_ = oend_ = 0;
0212     }
0213     boost::iostreams::close(*storage_, which);
0214 }
0215 
0216 template<typename T, typename Tr>
0217 typename direct_streambuf<T, Tr>::pos_type direct_streambuf<T, Tr>::seek_impl
0218     (stream_offset off, BOOST_IOS::seekdir way, BOOST_IOS::openmode which)
0219 {
0220     using namespace std;
0221     BOOST_IOS::openmode both = BOOST_IOS::in | BOOST_IOS::out;
0222     if (two_head() && (which & both) == both)
0223         boost::throw_exception(bad_seek());
0224     stream_offset result = -1;
0225     bool one = one_head();
0226     if (one && (pptr() != 0 || gptr()== 0))
0227         init_get_area(); // Switch to input mode, for code reuse.
0228     if (one || ((which & BOOST_IOS::in) != 0 && ibeg_ != 0)) {
0229         if (!gptr()) setg(ibeg_, ibeg_, iend_);
0230         ptrdiff_t next = 0;
0231         switch (way) {
0232         case BOOST_IOS::beg: next = off; break;
0233         case BOOST_IOS::cur: next = (gptr() - ibeg_) + off; break;
0234         case BOOST_IOS::end: next = (iend_ - ibeg_) + off; break;
0235         default: BOOST_ASSERT(0);
0236         }
0237         if (next < 0 || next > (iend_ - ibeg_))
0238             boost::throw_exception(bad_seek());
0239         setg(ibeg_, ibeg_ + next, iend_);
0240         result = next;
0241     }
0242     if (!one && (which & BOOST_IOS::out) != 0 && obeg_ != 0) {
0243         if (!pptr()) setp(obeg_, oend_);
0244         ptrdiff_t next = 0;
0245         switch (way) {
0246         case BOOST_IOS::beg: next = off; break;
0247         case BOOST_IOS::cur: next = (pptr() - obeg_) + off; break;
0248         case BOOST_IOS::end: next = (oend_ - obeg_) + off; break;
0249         default: BOOST_ASSERT(0);
0250         }
0251         if (next < 0 || next > (oend_ - obeg_))
0252             boost::throw_exception(bad_seek());
0253         pbump(static_cast<int>(next - (pptr() - obeg_)));
0254         result = next;
0255     }
0256     return offset_to_position(result);
0257 }
0258 
0259 template<typename T, typename Tr>
0260 void direct_streambuf<T, Tr>::init_input(input)
0261 {
0262     std::pair<char_type*, char_type*> p = input_sequence(*storage_);
0263     ibeg_ = p.first;
0264     iend_ = p.second;
0265 }
0266 
0267 template<typename T, typename Tr>
0268 void direct_streambuf<T, Tr>::init_output(output)
0269 {
0270     std::pair<char_type*, char_type*> p = output_sequence(*storage_);
0271     obeg_ = p.first;
0272     oend_ = p.second;
0273 }
0274 
0275 template<typename T, typename Tr>
0276 void direct_streambuf<T, Tr>::init_get_area()
0277 {
0278     setg(ibeg_, ibeg_, iend_);
0279     if (one_head() && pptr()) {
0280         gbump(static_cast<int>(pptr() - obeg_));
0281         setp(0, 0);
0282     }
0283 }
0284 
0285 template<typename T, typename Tr>
0286 void direct_streambuf<T, Tr>::init_put_area()
0287 {
0288     setp(obeg_, oend_);
0289     if (one_head() && gptr()) {
0290         pbump(static_cast<int>(gptr() - ibeg_));
0291         setg(0, 0, 0);
0292     }
0293 }
0294 
0295 template<typename T, typename Tr>
0296 inline bool direct_streambuf<T, Tr>::one_head() const
0297 { return ibeg_ && obeg_ && ibeg_ == obeg_; }
0298 
0299 template<typename T, typename Tr>
0300 inline bool direct_streambuf<T, Tr>::two_head() const
0301 { return ibeg_ && obeg_ && ibeg_ != obeg_; }
0302 
0303 //----------------------------------------------------------------------------//
0304 
0305 } // End namespace detail.
0306 
0307 } } // End namespaces iostreams, boost.
0308 
0309 #include <boost/iostreams/detail/config/enable_warnings.hpp> // MSVC
0310 
0311 #endif // #ifndef BOOST_IOSTREAMS_DETAIL_DIRECT_STREAMBUF_HPP_INCLUDED