Use variadic templates when supported.

This commit is contained in:
Christopher Kohlhoff 2011-03-30 22:55:06 +11:00
parent 13d0261e10
commit b9d08a4b3d
3 changed files with 76 additions and 20 deletions

View File

@ -19,17 +19,20 @@
#if !defined(BOOST_NO_IOSTREAM)
#include <boost/preprocessor/arithmetic/inc.hpp>
#include <boost/preprocessor/repetition/enum_binary_params.hpp>
#include <boost/preprocessor/repetition/enum_params.hpp>
#include <boost/preprocessor/repetition/repeat_from_to.hpp>
#include <boost/utility/base_from_member.hpp>
#include "asio/basic_socket_streambuf.hpp"
#include "asio/stream_socket_service.hpp"
#if !defined(ASIO_SOCKET_IOSTREAM_MAX_ARITY)
#define ASIO_SOCKET_IOSTREAM_MAX_ARITY 5
#endif // !defined(ASIO_SOCKET_IOSTREAM_MAX_ARITY)
#if !defined(ASIO_HAS_VARIADIC_TEMPLATES)
# include <boost/preprocessor/arithmetic/inc.hpp>
# include <boost/preprocessor/repetition/enum_binary_params.hpp>
# include <boost/preprocessor/repetition/enum_params.hpp>
# include <boost/preprocessor/repetition/repeat_from_to.hpp>
# if !defined(ASIO_SOCKET_IOSTREAM_MAX_ARITY)
# define ASIO_SOCKET_IOSTREAM_MAX_ARITY 5
# endif // !defined(ASIO_SOCKET_IOSTREAM_MAX_ARITY)
// A macro that should expand to:
// template <typename T1, ..., typename Tn>
@ -43,7 +46,7 @@
// }
// This macro should only persist within this file.
#define ASIO_PRIVATE_CTR_DEF(z, n, data) \
# define ASIO_PRIVATE_CTR_DEF(z, n, data) \
template <BOOST_PP_ENUM_PARAMS(n, typename T)> \
explicit basic_socket_iostream(BOOST_PP_ENUM_BINARY_PARAMS(n, T, x)) \
: std::basic_iostream<char>(&this->boost::base_from_member< \
@ -65,7 +68,7 @@
// }
// This macro should only persist within this file.
#define ASIO_PRIVATE_CONNECT_DEF(z, n, data) \
# define ASIO_PRIVATE_CONNECT_DEF(z, n, data) \
template <BOOST_PP_ENUM_PARAMS(n, typename T)> \
void connect(BOOST_PP_ENUM_BINARY_PARAMS(n, T, x)) \
{ \
@ -74,6 +77,8 @@
} \
/**/
#endif // !defined(ASIO_HAS_VARIADIC_TEMPLATES)
#include "asio/detail/push_options.hpp"
namespace asio {
@ -118,6 +123,17 @@ public:
*/
template <typename T1, ..., typename TN>
explicit basic_socket_iostream(T1 t1, ..., TN tn);
#elif defined(ASIO_HAS_VARIADIC_TEMPLATES)
template <typename... T>
explicit basic_socket_iostream(T... x)
: std::basic_iostream<char>(&this->boost::base_from_member<
basic_socket_streambuf<Protocol, StreamSocketService,
Time, TimeTraits, TimerService> >::member)
{
tie(this);
if (rdbuf()->connect(x...) == 0)
this->setstate(std::ios_base::failbit);
}
#else
BOOST_PP_REPEAT_FROM_TO(
1, BOOST_PP_INC(ASIO_SOCKET_IOSTREAM_MAX_ARITY),
@ -133,6 +149,13 @@ public:
*/
template <typename T1, ..., typename TN>
void connect(T1 t1, ..., TN tn);
#elif defined(ASIO_HAS_VARIADIC_TEMPLATES)
template <typename... T>
void connect(T... x)
{
if (rdbuf()->connect(x...) == 0)
this->setstate(std::ios_base::failbit);
}
#else
BOOST_PP_REPEAT_FROM_TO(
1, BOOST_PP_INC(ASIO_SOCKET_IOSTREAM_MAX_ARITY),
@ -225,8 +248,10 @@ public:
#include "asio/detail/pop_options.hpp"
#undef ASIO_PRIVATE_CTR_DEF
#undef ASIO_PRIVATE_CONNECT_DEF
#if !defined(ASIO_HAS_VARIADIC_TEMPLATES)
# undef ASIO_PRIVATE_CTR_DEF
# undef ASIO_PRIVATE_CONNECT_DEF
#endif // !defined(ASIO_HAS_VARIADIC_TEMPLATES)
#endif // defined(BOOST_NO_IOSTREAM)

View File

@ -20,10 +20,6 @@
#if !defined(BOOST_NO_IOSTREAM)
#include <streambuf>
#include <boost/preprocessor/arithmetic/inc.hpp>
#include <boost/preprocessor/repetition/enum_binary_params.hpp>
#include <boost/preprocessor/repetition/enum_params.hpp>
#include <boost/preprocessor/repetition/repeat_from_to.hpp>
#include <boost/utility/base_from_member.hpp>
#include "asio/basic_socket.hpp"
#include "asio/deadline_timer_service.hpp"
@ -37,9 +33,16 @@
#include <boost/date_time/posix_time/posix_time_types.hpp>
#include "asio/detail/pop_options.hpp"
#if !defined(ASIO_SOCKET_STREAMBUF_MAX_ARITY)
#define ASIO_SOCKET_STREAMBUF_MAX_ARITY 5
#endif // !defined(ASIO_SOCKET_STREAMBUF_MAX_ARITY)
#if !defined(ASIO_HAS_VARIADIC_TEMPLATES)
# include <boost/preprocessor/arithmetic/inc.hpp>
# include <boost/preprocessor/repetition/enum_binary_params.hpp>
# include <boost/preprocessor/repetition/enum_params.hpp>
# include <boost/preprocessor/repetition/repeat_from_to.hpp>
# if !defined(ASIO_SOCKET_STREAMBUF_MAX_ARITY)
# define ASIO_SOCKET_STREAMBUF_MAX_ARITY 5
# endif // !defined(ASIO_SOCKET_STREAMBUF_MAX_ARITY)
// A macro that should expand to:
// template <typename T1, ..., typename Tn>
@ -57,7 +60,7 @@
// }
// This macro should only persist within this file.
#define ASIO_PRIVATE_CONNECT_DEF( z, n, data ) \
# define ASIO_PRIVATE_CONNECT_DEF( z, n, data ) \
template <BOOST_PP_ENUM_PARAMS(n, typename T)> \
basic_socket_streambuf<Protocol, StreamSocketService, \
Time, TimeTraits, TimerService>* connect( \
@ -73,6 +76,8 @@
} \
/**/
#endif // !defined(ASIO_HAS_VARIADIC_TEMPLATES)
#include "asio/detail/push_options.hpp"
namespace asio {
@ -164,6 +169,19 @@ public:
template <typename T1, ..., typename TN>
basic_socket_streambuf<Protocol, StreamSocketService>* connect(
T1 t1, ..., TN tn);
#elif defined(ASIO_HAS_VARIADIC_TEMPLATES)
template <typename... T>
basic_socket_streambuf<Protocol, StreamSocketService,
Time, TimeTraits, TimerService>* connect(T... x)
{
init_buffers();
this->basic_socket<Protocol, StreamSocketService>::close(ec_);
typedef typename Protocol::resolver resolver_type;
typedef typename resolver_type::query resolver_query;
resolver_query query(x...);
resolve_and_connect(query);
return !ec_ ? this : 0;
}
#else
BOOST_PP_REPEAT_FROM_TO(
1, BOOST_PP_INC(ASIO_SOCKET_STREAMBUF_MAX_ARITY),
@ -519,7 +537,9 @@ private:
#include "asio/detail/pop_options.hpp"
#undef ASIO_PRIVATE_CONNECT_DEF
#if !defined(ASIO_HAS_VARIADIC_TEMPLATES)
# undef ASIO_PRIVATE_CONNECT_DEF
#endif // !defined(ASIO_HAS_VARIADIC_TEMPLATES)
#endif // !defined(BOOST_NO_IOSTREAM)

View File

@ -69,6 +69,17 @@
# define ASIO_MOVE_CAST(type) static_cast<const type&>
#endif // !defined_ASIO_MOVE_CAST
// Support variadic templates on compilers known to allow it.
#if !defined(ASIO_DISABLE_VARIADIC_TEMPLATES)
# if defined(__GNUC__)
# if ((__GNUC__ == 4) && (__GNUC_MINOR__ >= 3)) || (__GNUC__ > 4)
# if defined(__GXX_EXPERIMENTAL_CXX0X__)
# define ASIO_HAS_VARIADIC_TEMPLATES
# endif // defined(__GXX_EXPERIMENTAL_CXX0X__)
# endif // ((__GNUC__ == 4) && (__GNUC_MINOR__ >= 3)) || (__GNUC__ > 4)
# endif // defined(__GNUC__)
#endif // !defined(ASIO_DISABLE_VARIADIC_TEMPLATES)
// Standard library support for system errors.
#if !defined(ASIO_DISABLE_STD_SYSTEM_ERROR)
# if defined(__GNUC__)