Bytemaster's Boost Libraries
|
00001 // 00002 // basic_socket_iostream.hpp 00003 // ~~~~~~~~~~~~~~~~~~~~~~~~~ 00004 // 00005 // Copyright (c) 2003-2010 Christopher M. Kohlhoff (chris at kohlhoff dot com) 00006 // 00007 // Distributed under the Boost Software License, Version 1.0. (See accompanying 00008 // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) 00009 // 00010 00011 #ifndef BOOST_CMT_ASIO_BASIC_SOCKET_IOSTREAM_HPP 00012 #define BOOST_CMT_ASIO_BASIC_SOCKET_IOSTREAM_HPP 00013 00014 #if defined(_MSC_VER) && (_MSC_VER >= 1200) 00015 # pragma once 00016 #endif // defined(_MSC_VER) && (_MSC_VER >= 1200) 00017 00018 #include <boost/asio/detail/config.hpp> 00019 00020 #if !defined(BOOST_NO_IOSTREAM) 00021 00022 #include <boost/preprocessor/arithmetic/inc.hpp> 00023 #include <boost/preprocessor/repetition/enum_binary_params.hpp> 00024 #include <boost/preprocessor/repetition/enum_params.hpp> 00025 #include <boost/preprocessor/repetition/repeat_from_to.hpp> 00026 #include <boost/utility/base_from_member.hpp> 00027 #include <boost/cmt/asio/basic_socket_streambuf.hpp> 00028 #include <boost/asio/stream_socket_service.hpp> 00029 00030 #if !defined(BOOST_ASIO_SOCKET_IOSTREAM_MAX_ARITY) 00031 #define BOOST_ASIO_SOCKET_IOSTREAM_MAX_ARITY 5 00032 #endif // !defined(BOOST_ASIO_SOCKET_IOSTREAM_MAX_ARITY) 00033 00034 // A macro that should expand to: 00035 // template <typename T1, ..., typename Tn> 00036 // explicit basic_socket_iostream(T1 x1, ..., Tn xn) 00037 // : basic_iostream<char>(&this->boost::base_from_member< 00038 // basic_socket_streambuf<Protocol, StreamSocketService> >::member) 00039 // { 00040 // if (rdbuf()->connect(x1, ..., xn) == 0) 00041 // this->setstate(std::ios_base::failbit); 00042 // } 00043 // This macro should only persist within this file. 00044 00045 #define BOOST_ASIO_PRIVATE_CTR_DEF(z, n, data) \ 00046 template <BOOST_PP_ENUM_PARAMS(n, typename T)> \ 00047 explicit basic_socket_iostream(BOOST_PP_ENUM_BINARY_PARAMS(n, T, x)) \ 00048 : std::basic_iostream<char>(&this->boost::base_from_member< \ 00049 boost::cmt::asio::basic_socket_streambuf<Protocol, StreamSocketService> >::member) \ 00050 { \ 00051 tie(this); \ 00052 if (rdbuf()->connect(BOOST_PP_ENUM_PARAMS(n, x)) == 0) \ 00053 this->setstate(std::ios_base::failbit); \ 00054 } \ 00055 00056 00057 // A macro that should expand to: 00058 // template <typename T1, ..., typename Tn> 00059 // void connect(T1 x1, ..., Tn xn) 00060 // { 00061 // if (rdbuf()->connect(x1, ..., xn) == 0) 00062 // this->setstate(std::ios_base::failbit); 00063 // } 00064 // This macro should only persist within this file. 00065 00066 #define BOOST_ASIO_PRIVATE_CONNECT_DEF(z, n, data) \ 00067 template <BOOST_PP_ENUM_PARAMS(n, typename T)> \ 00068 void connect(BOOST_PP_ENUM_BINARY_PARAMS(n, T, x)) \ 00069 { \ 00070 if (rdbuf()->connect(BOOST_PP_ENUM_PARAMS(n, x)) == 0) \ 00071 this->setstate(std::ios_base::failbit); \ 00072 } \ 00073 00074 00075 #include <boost/asio/detail/push_options.hpp> 00076 00077 namespace boost { 00078 namespace cmt { 00079 namespace asio { 00080 00082 template <typename Protocol, 00083 typename StreamSocketService = stream_socket_service<Protocol> > 00084 class basic_socket_iostream 00085 : public boost::base_from_member< 00086 boost::cmt::asio::basic_socket_streambuf<Protocol, StreamSocketService> >, 00087 public std::basic_iostream<char> 00088 { 00089 public: 00090 typedef boost::shared_ptr<basic_socket_iostream> ptr; 00091 00093 basic_socket_iostream() 00094 : std::basic_iostream<char>(&this->boost::base_from_member< 00095 basic_socket_streambuf<Protocol, StreamSocketService> >::member) 00096 { 00097 tie(this); 00098 } 00099 00100 #if defined(GENERATING_DOCUMENTATION) 00101 00102 00107 template <typename T1, ..., typename TN> 00108 explicit basic_socket_iostream(T1 t1, ..., TN tn); 00109 #else 00110 BOOST_PP_REPEAT_FROM_TO( 00111 1, BOOST_PP_INC(BOOST_ASIO_SOCKET_IOSTREAM_MAX_ARITY), 00112 BOOST_ASIO_PRIVATE_CTR_DEF, _ ) 00113 #endif 00114 00115 #if defined(GENERATING_DOCUMENTATION) 00116 00117 00122 template <typename T1, ..., typename TN> 00123 void connect(T1 t1, ..., TN tn); 00124 #else 00125 BOOST_PP_REPEAT_FROM_TO( 00126 1, BOOST_PP_INC(BOOST_ASIO_SOCKET_IOSTREAM_MAX_ARITY), 00127 BOOST_ASIO_PRIVATE_CONNECT_DEF, _ ) 00128 #endif 00129 00131 void close() 00132 { 00133 if (rdbuf()->close() == 0) 00134 this->setstate(std::ios_base::failbit); 00135 } 00136 00138 basic_socket_streambuf<Protocol, StreamSocketService>* rdbuf() const 00139 { 00140 return const_cast<basic_socket_streambuf<Protocol, StreamSocketService>*>( 00141 &this->boost::base_from_member< 00142 basic_socket_streambuf<Protocol, StreamSocketService> >::member); 00143 } 00144 }; 00145 00146 } // namespace asio 00147 } // namespace cmt 00148 } // namespace boost 00149 00150 #include <boost/asio/detail/pop_options.hpp> 00151 00152 #undef BOOST_ASIO_PRIVATE_CTR_DEF 00153 #undef BOOST_ASIO_PRIVATE_CONNECT_DEF 00154 00155 #endif // defined(BOOST_NO_IOSTREAM) 00156 00157 #endif // BOOST_CMT_ASIO_BASIC_SOCKET_IOSTREAM_HPP