1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/os/ossrv/ossrv_pub/boost_apis/boost/iostreams/invert.hpp Fri Jun 15 03:10:57 2012 +0200
1.3 @@ -0,0 +1,162 @@
1.4 +// (C) Copyright Jonathan Turkanis 2003.
1.5 +// Distributed under the Boost Software License, Version 1.0. (See accompanying
1.6 +// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt.)
1.7 +
1.8 +// See http://www.boost.org/libs/iostreams for documentation.
1.9 +
1.10 +#ifndef BOOST_IOSTREAMS_INVERT_HPP_INCLUDED
1.11 +#define BOOST_IOSTREAMS_INVERT_HPP_INCLUDED
1.12 +
1.13 +#if defined(_MSC_VER) && (_MSC_VER >= 1020)
1.14 +# pragma once
1.15 +#endif
1.16 +
1.17 +#include <algorithm> // copy, min.
1.18 +#include <cassert>
1.19 +#include <boost/config.hpp> // BOOST_DEDUCED_TYPENAME.
1.20 +#include <boost/detail/workaround.hpp> // default_filter_buffer_size.
1.21 +#include <boost/iostreams/char_traits.hpp>
1.22 +#include <boost/iostreams/compose.hpp>
1.23 +#include <boost/iostreams/constants.hpp>
1.24 +#include <boost/iostreams/device/array.hpp>
1.25 +#include <boost/iostreams/detail/buffer.hpp>
1.26 +#include <boost/iostreams/detail/counted_array.hpp>
1.27 +#include <boost/mpl/if.hpp>
1.28 +#include <boost/ref.hpp>
1.29 +#include <boost/shared_ptr.hpp>
1.30 +#include <boost/type_traits/is_convertible.hpp>
1.31 +
1.32 +// Must come last.
1.33 +#include <boost/iostreams/detail/config/disable_warnings.hpp> // MSVC.
1.34 +
1.35 +namespace boost { namespace iostreams {
1.36 +
1.37 +//
1.38 +// Template name: inverse.
1.39 +// Template paramters:
1.40 +// Filter - A filter adapter which
1.41 +// Description: Returns an instance of an appropriate specialization of inverse.
1.42 +//
1.43 +template<typename Filter>
1.44 +class inverse {
1.45 +private:
1.46 + typedef typename category_of<Filter>::type base_category;
1.47 + typedef reference_wrapper<Filter> filter_ref;
1.48 +public:
1.49 + typedef typename char_type_of<Filter>::type char_type;
1.50 + typedef typename int_type_of<Filter>::type int_type;
1.51 + typedef char_traits<char_type> traits_type;
1.52 + typedef typename
1.53 + mpl::if_<
1.54 + is_convertible<
1.55 + base_category,
1.56 + input
1.57 + >,
1.58 + output,
1.59 + input
1.60 + >::type mode;
1.61 + struct category
1.62 + : mode,
1.63 + filter_tag,
1.64 + multichar_tag,
1.65 + closable_tag
1.66 + { };
1.67 + explicit inverse( const Filter& filter,
1.68 + std::streamsize buffer_size =
1.69 + default_filter_buffer_size)
1.70 + : pimpl_(new impl(filter, buffer_size))
1.71 + { }
1.72 +
1.73 + template<typename Source>
1.74 + std::streamsize read(Source& src, char* s, std::streamsize n)
1.75 + {
1.76 + typedef detail::counted_array_sink<char_type> array_sink;
1.77 + typedef composite<filter_ref, array_sink> filtered_array_sink;
1.78 +
1.79 + assert((flags() & f_write) == 0);
1.80 + if (flags() == 0) {
1.81 + flags() = f_read;
1.82 + buf().set(0, 0);
1.83 + }
1.84 +
1.85 + filtered_array_sink snk(filter(), array_sink(s, n));
1.86 + int_type status;
1.87 + for ( status = traits_type::good();
1.88 + snk.second().count() < n && status == traits_type::good(); )
1.89 + {
1.90 + status = buf().fill(src);
1.91 + buf().flush(snk);
1.92 + }
1.93 + return snk.second().count() == 0 &&
1.94 + status == traits_type::eof()
1.95 + ?
1.96 + -1
1.97 + :
1.98 + snk.second().count();
1.99 + }
1.100 +
1.101 + template<typename Sink>
1.102 + std::streamsize write(Sink& dest, const char* s, std::streamsize n)
1.103 + {
1.104 + typedef detail::counted_array_source<char_type> array_source;
1.105 + typedef composite<filter_ref, array_source> filtered_array_source;
1.106 +
1.107 + assert((flags() & f_read) == 0);
1.108 + if (flags() == 0) {
1.109 + flags() = f_write;
1.110 + buf().set(0, 0);
1.111 + }
1.112 +
1.113 + filtered_array_source src(filter(), array_source(s, n));
1.114 + for (bool good = true; src.second().count() < n && good; ) {
1.115 + buf().fill(src);
1.116 + good = buf().flush(dest);
1.117 + }
1.118 + return src.second().count();
1.119 + }
1.120 +
1.121 + template<typename Device>
1.122 + void close( Device& dev,
1.123 + BOOST_IOS::openmode which =
1.124 + BOOST_IOS::in | BOOST_IOS::out )
1.125 + {
1.126 + if ((which & BOOST_IOS::out) != 0 && (flags() & f_write) != 0)
1.127 + buf().flush(dev);
1.128 + flags() = 0;
1.129 + }
1.130 +private:
1.131 + filter_ref filter() { return boost::ref(pimpl_->filter_); }
1.132 + detail::buffer<char_type>& buf() { return pimpl_->buf_; }
1.133 + int& flags() { return pimpl_->flags_; }
1.134 +
1.135 + enum flags_ {
1.136 + f_read = 1, f_write = 2
1.137 + };
1.138 +
1.139 + struct impl {
1.140 + impl(const Filter& filter, std::streamsize n)
1.141 + : filter_(filter), buf_(n), flags_(0)
1.142 + { buf_.set(0, 0); }
1.143 + Filter filter_;
1.144 + detail::buffer<char_type> buf_;
1.145 + int flags_;
1.146 + };
1.147 + shared_ptr<impl> pimpl_;
1.148 +};
1.149 +
1.150 +//
1.151 +// Template name: invert.
1.152 +// Template paramters:
1.153 +// Filter - A model of InputFilter or OutputFilter.
1.154 +// Description: Returns an instance of an appropriate specialization of inverse.
1.155 +//
1.156 +template<typename Filter>
1.157 +inverse<Filter> invert(const Filter& f) { return inverse<Filter>(f); }
1.158 +
1.159 +//----------------------------------------------------------------------------//
1.160 +
1.161 +} } // End namespaces iostreams, boost.
1.162 +
1.163 +#include <boost/iostreams/detail/config/enable_warnings.hpp> // MSVC.
1.164 +
1.165 +#endif // #ifndef BOOST_IOSTREAMS_INVERT_HPP_INCLUDED