Chris@16: // (C) Copyright 2008 CodeRage, LLC (turkanis at coderage dot com) Chris@16: // (C) Copyright 2003-2007 Jonathan Turkanis Chris@16: // Distributed under the Boost Software License, Version 1.0. (See accompanying Chris@16: // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt.) Chris@16: Chris@16: // See http://www.boost.org/libs/iostreams for documentation. Chris@16: Chris@16: #ifndef BOOST_IOSTREAMS_DETAIL_CONCEPT_ADAPTER_HPP_INCLUDED Chris@16: #define BOOST_IOSTREAMS_DETAIL_CONCEPT_ADAPTER_HPP_INCLUDED Chris@16: Chris@16: #include // SFINAE. Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include // pubsync. Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: Chris@16: // Must come last. Chris@16: #include // MSVC. Chris@16: Chris@16: Chris@16: namespace boost { namespace iostreams { namespace detail { Chris@16: Chris@16: template struct device_wrapper_impl; Chris@16: template struct flt_wrapper_impl; Chris@16: Chris@16: template Chris@16: class concept_adapter { Chris@16: private: Chris@16: typedef typename detail::value_type::type value_type; Chris@16: typedef typename dispatch::type input_tag; Chris@16: typedef typename dispatch::type output_tag; Chris@16: typedef typename Chris@16: mpl::if_< Chris@16: is_device, Chris@16: device_wrapper_impl, Chris@16: flt_wrapper_impl Chris@16: >::type input_impl; Chris@16: typedef typename Chris@16: mpl::if_< Chris@16: is_device, Chris@16: device_wrapper_impl, Chris@16: flt_wrapper_impl Chris@16: >::type output_impl; Chris@16: typedef typename Chris@16: mpl::if_< Chris@16: is_device, Chris@16: device_wrapper_impl, Chris@16: flt_wrapper_impl Chris@16: >::type any_impl; Chris@16: public: Chris@16: typedef typename char_type_of::type char_type; Chris@16: typedef typename category_of::type category; Chris@16: Chris@16: explicit concept_adapter(const reference_wrapper& ref) : t_(ref.get()) Chris@16: { BOOST_STATIC_ASSERT(is_std_io::value); } Chris@16: explicit concept_adapter(const T& t) : t_(t) Chris@16: { BOOST_STATIC_ASSERT(!is_std_io::value); } Chris@16: Chris@16: T& operator*() { return t_; } Chris@16: T* operator->() { return &t_; } Chris@16: Chris@16: std::streamsize read(char_type* s, std::streamsize n) Chris@16: { return this->read(s, n, (basic_null_source*) 0); } Chris@16: Chris@16: template Chris@16: std::streamsize read(char_type* s, std::streamsize n, Source* src) Chris@16: { return input_impl::read(t_, src, s, n); } Chris@16: Chris@16: std::streamsize write(const char_type* s, std::streamsize n) Chris@16: { return this->write(s, n, (basic_null_sink*) 0); } Chris@16: Chris@16: template Chris@16: std::streamsize write(const char_type* s, std::streamsize n, Sink* snk) Chris@16: { return output_impl::write(t_, snk, s, n); } Chris@16: Chris@16: std::streampos seek( stream_offset off, BOOST_IOS::seekdir way, Chris@16: BOOST_IOS::openmode which ) Chris@16: { Chris@16: return this->seek( off, way, which, Chris@16: (basic_null_device*) 0); Chris@16: } Chris@16: Chris@16: template Chris@16: std::streampos seek( stream_offset off, BOOST_IOS::seekdir way, Chris@16: BOOST_IOS::openmode which, Device* dev ) Chris@16: { return any_impl::seek(t_, dev, off, way, which); } Chris@16: Chris@16: void close(BOOST_IOS::openmode which) Chris@16: { this->close(which, (basic_null_device*) 0); } Chris@16: Chris@16: template Chris@16: void close(BOOST_IOS::openmode which, Device* dev) Chris@16: { any_impl::close(t_, dev, which); } Chris@16: Chris@16: template Chris@16: bool flush( Device* dev ) Chris@16: { Chris@16: bool result = any_impl::flush(t_, dev); Chris@16: if (dev && dev->BOOST_IOSTREAMS_PUBSYNC() == -1) Chris@16: result = false; Chris@16: return result; Chris@16: } Chris@16: Chris@16: template // Avoid dependency on Chris@16: void imbue(const Locale& loc) { iostreams::imbue(t_, loc); } Chris@16: Chris@16: std::streamsize optimal_buffer_size() const Chris@16: { return iostreams::optimal_buffer_size(t_); } Chris@16: public: Chris@16: concept_adapter& operator=(const concept_adapter&); Chris@16: value_type t_; Chris@16: }; Chris@16: Chris@16: //------------------Specializations of device_wrapper_impl--------------------// Chris@16: Chris@16: template<> Chris@16: struct device_wrapper_impl { Chris@16: template Chris@16: static std::streampos Chris@16: seek( Device& dev, Dummy*, stream_offset off, Chris@16: BOOST_IOS::seekdir way, BOOST_IOS::openmode which ) Chris@16: { Chris@16: typedef typename category_of::type category; Chris@16: return seek(dev, off, way, which, category()); Chris@16: } Chris@16: Chris@16: template Chris@16: static std::streampos Chris@16: seek( Device&, stream_offset, BOOST_IOS::seekdir, Chris@16: BOOST_IOS::openmode, any_tag ) Chris@16: { Chris@16: boost::throw_exception(cant_seek()); Chris@16: BOOST_IOSTREAMS_UNREACHABLE_RETURN(0) Chris@16: } Chris@16: Chris@16: template Chris@16: static std::streampos Chris@16: seek( Device& dev, stream_offset off, Chris@16: BOOST_IOS::seekdir way, BOOST_IOS::openmode which, Chris@16: random_access ) Chris@16: { Chris@16: return iostreams::seek(dev, off, way, which); Chris@16: } Chris@16: Chris@16: template Chris@16: static void close(Device& dev, Dummy*, BOOST_IOS::openmode which) Chris@16: { iostreams::close(dev, which); } Chris@16: Chris@16: template Chris@16: static bool flush(Device& dev, Dummy*) Chris@16: { return iostreams::flush(dev); } Chris@16: }; Chris@16: Chris@16: Chris@16: template<> Chris@16: struct device_wrapper_impl : device_wrapper_impl { Chris@16: template Chris@16: static std::streamsize Chris@16: read( Device& dev, Dummy*, typename char_type_of::type* s, Chris@16: std::streamsize n ) Chris@16: { return iostreams::read(dev, s, n); } Chris@16: Chris@16: template Chris@16: static std::streamsize Chris@16: write( Device&, Dummy*, const typename char_type_of::type*, Chris@16: std::streamsize ) Chris@16: { boost::throw_exception(cant_write()); Chris@16: BOOST_IOSTREAMS_UNREACHABLE_RETURN(0) } Chris@16: }; Chris@16: Chris@16: template<> Chris@16: struct device_wrapper_impl { Chris@16: template Chris@16: static std::streamsize Chris@16: read(Device&, Dummy*, typename char_type_of::type*, std::streamsize) Chris@16: { boost::throw_exception(cant_read()); Chris@16: BOOST_IOSTREAMS_UNREACHABLE_RETURN(0) } Chris@16: Chris@16: template Chris@16: static std::streamsize Chris@16: write( Device& dev, Dummy*, const typename char_type_of::type* s, Chris@16: std::streamsize n ) Chris@16: { return iostreams::write(dev, s, n); } Chris@16: }; Chris@16: Chris@16: //------------------Specializations of flt_wrapper_impl--------------------// Chris@16: Chris@16: template<> Chris@16: struct flt_wrapper_impl { Chris@16: template Chris@16: static std::streampos Chris@16: seek( Filter& f, Device* dev, stream_offset off, Chris@16: BOOST_IOS::seekdir way, BOOST_IOS::openmode which ) Chris@16: { Chris@16: typedef typename category_of::type category; Chris@16: return seek(f, dev, off, way, which, category()); Chris@16: } Chris@16: Chris@16: template Chris@16: static std::streampos Chris@16: seek( Filter&, Device*, stream_offset, Chris@16: BOOST_IOS::seekdir, BOOST_IOS::openmode, any_tag ) Chris@16: { boost::throw_exception(cant_seek()); Chris@16: BOOST_IOSTREAMS_UNREACHABLE_RETURN(0) } Chris@16: Chris@16: template Chris@16: static std::streampos Chris@16: seek( Filter& f, Device* dev, stream_offset off, Chris@16: BOOST_IOS::seekdir way, BOOST_IOS::openmode which, Chris@16: random_access tag ) Chris@16: { Chris@16: typedef typename category_of::type category; Chris@16: return seek(f, dev, off, way, which, tag, category()); Chris@16: } Chris@16: Chris@16: template Chris@16: static std::streampos Chris@16: seek( Filter& f, Device* dev, stream_offset off, Chris@16: BOOST_IOS::seekdir way, BOOST_IOS::openmode, Chris@16: random_access, any_tag ) Chris@16: { return f.seek(*dev, off, way); } Chris@16: Chris@16: template Chris@16: static std::streampos Chris@16: seek( Filter& f, Device* dev, stream_offset off, Chris@16: BOOST_IOS::seekdir way, BOOST_IOS::openmode which, Chris@16: random_access, two_sequence ) Chris@16: { return f.seek(*dev, off, way, which); } Chris@16: Chris@16: template Chris@16: static void close(Filter& f, Device* dev, BOOST_IOS::openmode which) Chris@16: { iostreams::close(f, *dev, which); } Chris@16: Chris@16: template Chris@16: static bool flush(Filter& f, Device* dev) Chris@16: { return iostreams::flush(f, *dev); } Chris@16: }; Chris@16: Chris@16: template<> Chris@16: struct flt_wrapper_impl { Chris@16: template Chris@16: static std::streamsize Chris@16: read( Filter& f, Source* src, typename char_type_of::type* s, Chris@16: std::streamsize n ) Chris@16: { return iostreams::read(f, *src, s, n); } Chris@16: Chris@16: template Chris@16: static std::streamsize Chris@16: write( Filter&, Sink*, const typename char_type_of::type*, Chris@16: std::streamsize ) Chris@16: { boost::throw_exception(cant_write()); Chris@16: BOOST_IOSTREAMS_UNREACHABLE_RETURN(0) } Chris@16: }; Chris@16: Chris@16: template<> Chris@16: struct flt_wrapper_impl { Chris@16: template Chris@16: static std::streamsize Chris@16: read(Filter&, Source*, typename char_type_of::type*,std::streamsize) Chris@16: { boost::throw_exception(cant_read()); Chris@16: BOOST_IOSTREAMS_UNREACHABLE_RETURN(0) } Chris@16: Chris@16: template Chris@16: static std::streamsize Chris@16: write( Filter& f, Sink* snk, const typename char_type_of::type* s, Chris@16: std::streamsize n ) Chris@16: { return iostreams::write(f, *snk, s, n); } Chris@16: }; Chris@16: Chris@16: //----------------------------------------------------------------------------// Chris@16: Chris@16: } } } // End namespaces detail, iostreams, boost. Chris@16: Chris@16: #include // MSVC. Chris@16: Chris@16: #endif // #ifndef BOOST_IOSTREAMS_DETAIL_CONCEPT_ADAPTER_HPP_INCLUDED