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: // To do: add support for random-access. Chris@16: Chris@16: #ifndef BOOST_IOSTREAMS_COMBINE_HPP_INCLUDED Chris@16: #define BOOST_IOSTREAMS_COMBINE_HPP_INCLUDED Chris@16: Chris@16: #if defined(_MSC_VER) && (_MSC_VER >= 1020) Chris@16: # pragma once Chris@16: #endif Chris@16: Chris@16: #include // NO_STD_LOCALE, DEDUCED_TYPENAME. Chris@16: #ifndef BOOST_NO_STD_LOCALE Chris@16: # include Chris@16: #endif 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 Chris@16: Chris@16: namespace boost { namespace iostreams { Chris@16: Chris@16: namespace detail { Chris@16: Chris@16: // Chris@16: // Template name: combined_device. Chris@16: // Description: Model of Device defined in terms of a Source/Sink pair. Chris@16: // Template parameters: Chris@16: // Source - A model of Source, with the same char_type and traits_type Chris@16: // as Sink. Chris@16: // Sink - A model of Sink, with the same char_type and traits_type Chris@16: // as Source. Chris@16: // Chris@16: template Chris@16: class combined_device { Chris@16: private: Chris@16: typedef typename category_of::type in_category; Chris@16: typedef typename category_of::type out_category; Chris@16: typedef typename char_type_of::type sink_char_type; Chris@16: public: Chris@16: typedef typename char_type_of::type char_type; Chris@16: struct category Chris@16: : bidirectional, Chris@16: device_tag, Chris@16: closable_tag, Chris@16: localizable_tag Chris@16: { }; Chris@16: BOOST_STATIC_ASSERT(is_device::value); Chris@16: BOOST_STATIC_ASSERT(is_device::value); Chris@16: BOOST_STATIC_ASSERT((is_convertible::value)); Chris@16: BOOST_STATIC_ASSERT((is_convertible::value)); Chris@16: BOOST_STATIC_ASSERT((is_same::value)); Chris@16: combined_device(const Source& src, const Sink& snk); Chris@16: std::streamsize read(char_type* s, std::streamsize n); Chris@16: std::streamsize write(const char_type* s, std::streamsize n); Chris@16: void close(BOOST_IOS::openmode); Chris@16: #ifndef BOOST_NO_STD_LOCALE Chris@16: void imbue(const std::locale& loc); Chris@16: #endif Chris@16: private: Chris@16: Source src_; Chris@16: Sink sink_; Chris@16: }; Chris@16: Chris@16: // Chris@16: // Template name: combined_filter. Chris@16: // Description: Model of Device defined in terms of a Source/Sink pair. Chris@16: // Template parameters: Chris@16: // InputFilter - A model of InputFilter, with the same char_type as Chris@16: // OutputFilter. Chris@16: // OutputFilter - A model of OutputFilter, with the same char_type as Chris@16: // InputFilter. Chris@16: // Chris@16: template Chris@16: class combined_filter { Chris@16: private: Chris@16: typedef typename category_of::type in_category; Chris@16: typedef typename category_of::type out_category; Chris@16: typedef typename char_type_of::type output_char_type; Chris@16: public: Chris@16: typedef typename char_type_of::type char_type; Chris@16: struct category Chris@16: : multichar_bidirectional_filter_tag, Chris@16: closable_tag, Chris@16: localizable_tag Chris@16: { }; Chris@16: BOOST_STATIC_ASSERT(is_filter::value); Chris@16: BOOST_STATIC_ASSERT(is_filter::value); Chris@16: BOOST_STATIC_ASSERT((is_convertible::value)); Chris@16: BOOST_STATIC_ASSERT((is_convertible::value)); Chris@16: BOOST_STATIC_ASSERT((is_same::value)); Chris@16: combined_filter(const InputFilter& in, const OutputFilter& out); Chris@16: Chris@16: template Chris@16: std::streamsize read(Source& src, char_type* s, std::streamsize n) Chris@16: { return boost::iostreams::read(in_, src, s, n); } Chris@16: Chris@16: template Chris@16: std::streamsize write(Sink& snk, const char_type* s, std::streamsize n) Chris@16: { return boost::iostreams::write(out_, snk, s, n); } Chris@16: Chris@16: template Chris@16: void close(Sink& snk, BOOST_IOS::openmode which) Chris@16: { Chris@16: if (which == BOOST_IOS::in) { Chris@16: if (is_convertible::value) { Chris@16: iostreams::close(in_, snk, BOOST_IOS::in); Chris@16: } else { Chris@16: detail::close_all(in_, snk); Chris@16: } Chris@16: } Chris@16: if (which == BOOST_IOS::out) { Chris@16: if (is_convertible::value) { Chris@16: iostreams::close(out_, snk, BOOST_IOS::out); Chris@16: } else { Chris@16: detail::close_all(out_, snk); Chris@16: } Chris@16: } Chris@16: } Chris@16: #ifndef BOOST_NO_STD_LOCALE Chris@16: void imbue(const std::locale& loc); Chris@16: #endif Chris@16: private: Chris@16: InputFilter in_; Chris@16: OutputFilter out_; Chris@16: }; Chris@16: Chris@16: template Chris@16: struct combination_traits Chris@16: : mpl::if_< Chris@16: is_device, Chris@16: combined_device< Chris@16: typename wrapped_type::type, Chris@16: typename wrapped_type::type Chris@16: >, Chris@16: combined_filter< Chris@16: typename wrapped_type::type, Chris@16: typename wrapped_type::type Chris@16: > Chris@16: > Chris@16: { }; Chris@16: Chris@16: } // End namespace detail. Chris@16: Chris@16: template Chris@16: struct combination : detail::combination_traits::type { Chris@16: typedef typename detail::combination_traits::type base_type; Chris@16: typedef typename detail::wrapped_type::type in_type; Chris@16: typedef typename detail::wrapped_type::type out_type; Chris@16: combination(const in_type& in, const out_type& out) Chris@16: : base_type(in, out) { } Chris@16: }; Chris@16: Chris@16: namespace detail { Chris@16: Chris@16: // Workaround for VC6 ETI bug. Chris@16: template Chris@16: struct combine_traits { Chris@16: typedef combination< Chris@16: BOOST_DEDUCED_TYPENAME detail::unwrapped_type::type, Chris@16: BOOST_DEDUCED_TYPENAME detail::unwrapped_type::type Chris@16: > type; Chris@16: }; Chris@16: Chris@16: } // End namespace detail. Chris@16: Chris@16: // Chris@16: // Template name: combine. Chris@16: // Description: Takes a Source/Sink pair or InputFilter/OutputFilter pair and Chris@16: // returns a Source or Filter which performs input using the first member Chris@16: // of the pair and output using the second member of the pair. Chris@16: // Template parameters: Chris@16: // In - A model of Source or InputFilter, with the same char_type as Out. Chris@16: // Out - A model of Sink or OutputFilter, with the same char_type as In. Chris@16: // Chris@16: template Chris@16: typename detail::combine_traits::type Chris@16: combine(const In& in, const Out& out) Chris@16: { Chris@16: typedef typename detail::combine_traits::type return_type; Chris@16: return return_type(in, out); Chris@16: } Chris@16: Chris@16: //----------------------------------------------------------------------------// Chris@16: Chris@16: namespace detail { Chris@16: Chris@16: //--------------Implementation of combined_device-----------------------------// Chris@16: Chris@16: template Chris@16: inline combined_device::combined_device Chris@16: (const Source& src, const Sink& snk) Chris@16: : src_(src), sink_(snk) { } Chris@16: Chris@16: template Chris@16: inline std::streamsize Chris@16: combined_device::read(char_type* s, std::streamsize n) Chris@16: { return iostreams::read(src_, s, n); } Chris@16: Chris@16: template Chris@16: inline std::streamsize Chris@16: combined_device::write(const char_type* s, std::streamsize n) Chris@16: { return iostreams::write(sink_, s, n); } Chris@16: Chris@16: template Chris@16: inline void Chris@16: combined_device::close(BOOST_IOS::openmode which) Chris@16: { Chris@16: if (which == BOOST_IOS::in) Chris@16: detail::close_all(src_); Chris@16: if (which == BOOST_IOS::out) Chris@16: detail::close_all(sink_); Chris@16: } Chris@16: Chris@16: #ifndef BOOST_NO_STD_LOCALE Chris@16: template Chris@16: void combined_device::imbue(const std::locale& loc) Chris@16: { Chris@16: iostreams::imbue(src_, loc); Chris@16: iostreams::imbue(sink_, loc); Chris@16: } Chris@16: #endif Chris@16: Chris@16: //--------------Implementation of filter_pair---------------------------------// Chris@16: Chris@16: template Chris@16: inline combined_filter::combined_filter Chris@16: (const InputFilter& in, const OutputFilter& out) : in_(in), out_(out) Chris@16: { } Chris@16: Chris@16: #ifndef BOOST_NO_STD_LOCALE Chris@16: template Chris@16: void combined_filter::imbue Chris@16: (const std::locale& loc) Chris@16: { Chris@16: iostreams::imbue(in_, loc); Chris@16: iostreams::imbue(out_, loc); Chris@16: } Chris@16: #endif Chris@16: Chris@16: Chris@16: } // End namespace detail. Chris@16: Chris@16: } } // End namespaces iostreams, boost. Chris@16: Chris@16: #include Chris@16: Chris@16: #endif // #ifndef BOOST_IOSTREAMS_COMBINE_HPP_INCLUDED