Chris@16: // (C) Copyright 2008 CodeRage, LLC (turkanis at coderage dot com) Chris@16: // (C) Copyright 2005-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: // Contains implementations of get, read, put, write and seek which Chris@16: // check a device's mode at runtime instead of compile time. Chris@16: Chris@16: #ifndef BOOST_IOSTREAMS_DETAIL_CHECKED_OPERATIONS_HPP_INCLUDED Chris@16: #define BOOST_IOSTREAMS_DETAIL_CHECKED_OPERATIONS_HPP_INCLUDED Chris@16: Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include 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: namespace boost { namespace iostreams { Chris@16: Chris@16: namespace detail { Chris@16: Chris@16: template Chris@16: struct read_write_if_impl; Chris@16: Chris@16: template Chris@16: struct seek_if_impl; Chris@16: Chris@16: } // End namespace detail. Chris@16: Chris@16: template Chris@16: typename int_type_of::type get_if(T& t) Chris@16: { Chris@16: typedef typename detail::dispatch::type tag; Chris@16: return detail::read_write_if_impl::get(t); Chris@16: } Chris@16: Chris@16: template Chris@16: inline std::streamsize Chris@16: read_if(T& t, typename char_type_of::type* s, std::streamsize n) Chris@16: { Chris@16: typedef typename detail::dispatch::type tag; Chris@16: return detail::read_write_if_impl::read(t, s, n); Chris@16: } Chris@16: Chris@16: template Chris@16: bool put_if(T& t, typename char_type_of::type c) Chris@16: { Chris@16: typedef typename detail::dispatch::type tag; Chris@16: return detail::read_write_if_impl::put(t, c); Chris@16: } Chris@16: Chris@16: template Chris@16: inline std::streamsize write_if Chris@16: (T& t, const typename char_type_of::type* s, std::streamsize n) Chris@16: { Chris@16: typedef typename detail::dispatch::type tag; Chris@16: return detail::read_write_if_impl::write(t, s, n); Chris@16: } Chris@16: Chris@16: template Chris@16: inline std::streampos Chris@16: seek_if( T& t, stream_offset off, BOOST_IOS::seekdir way, Chris@16: BOOST_IOS::openmode which = BOOST_IOS::in | BOOST_IOS::out ) Chris@16: { Chris@16: using namespace detail; Chris@16: typedef typename dispatch::type tag; Chris@16: return seek_if_impl::seek(t, off, way, which); Chris@16: } Chris@16: Chris@16: namespace detail { Chris@16: Chris@16: //------------------Specializations of read_write_if_impl---------------------// Chris@16: Chris@16: template<> Chris@16: struct read_write_if_impl { Chris@16: template Chris@16: static typename int_type_of::type get(T& t) Chris@16: { return iostreams::get(t); } Chris@16: Chris@16: template Chris@16: static std::streamsize Chris@16: read(T& t, typename char_type_of::type* s, std::streamsize n) Chris@16: { return iostreams::read(t, s, n); } Chris@16: Chris@16: template Chris@16: static bool put(T&, typename char_type_of::type) Chris@16: { boost::throw_exception(cant_write()); Chris@16: BOOST_IOSTREAMS_UNREACHABLE_RETURN(false) } Chris@16: Chris@16: template Chris@16: static std::streamsize Chris@16: write(T&, const typename char_type_of::type*, 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 read_write_if_impl { Chris@16: template Chris@16: static typename int_type_of::type get(T&) 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: read(T&, 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 bool put(T& t, typename char_type_of::type c) Chris@16: { return iostreams::put(t, c); } Chris@16: Chris@16: template Chris@16: static std::streamsize Chris@16: write( T& t, const typename char_type_of::type* s, Chris@16: std::streamsize n ) Chris@16: { return iostreams::write(t, s, n); } Chris@16: }; Chris@16: Chris@16: //------------------Specializations of seek_if_impl---------------------------// Chris@16: Chris@16: template<> Chris@16: struct seek_if_impl { Chris@16: template Chris@16: static std::streampos Chris@16: seek( T& t, stream_offset off, BOOST_IOS::seekdir way, Chris@16: BOOST_IOS::openmode which ) Chris@16: { return iostreams::seek(t, off, way, which); } Chris@16: }; Chris@16: Chris@16: template<> Chris@16: struct seek_if_impl { Chris@16: template Chris@16: static std::streampos Chris@16: seek(T&, stream_offset, BOOST_IOS::seekdir, BOOST_IOS::openmode) Chris@16: { boost::throw_exception(cant_seek()); Chris@16: BOOST_IOSTREAMS_UNREACHABLE_RETURN(std::streampos()) } Chris@16: }; Chris@16: Chris@16: } // End namespace detail. Chris@16: Chris@16: } } // End namespaces iostreams, boost. Chris@16: Chris@16: #include // MSVC. Chris@16: Chris@16: #endif // #ifndef BOOST_IOSTREAMS_DETAIL_CHECKED_OPERATIONS_HPP_INCLUDED