basic_socket_iostream.hpp 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. //
  2. // basic_socket_iostream.hpp
  3. // ~~~~~~~~~~~~~~~~~~~~~~~~~
  4. //
  5. // Copyright (c) 2003-2008 Christopher M. Kohlhoff (chris at kohlhoff dot com)
  6. //
  7. // Distributed under the Boost Software License, Version 1.0. (See accompanying
  8. // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  9. //
  10. #ifndef BOOST_ASIO_BASIC_SOCKET_IOSTREAM_HPP
  11. #define BOOST_ASIO_BASIC_SOCKET_IOSTREAM_HPP
  12. #if defined(_MSC_VER) && (_MSC_VER >= 1200)
  13. # pragma once
  14. #endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
  15. #include <boost/asio/detail/push_options.hpp>
  16. #include <boost/asio/detail/push_options.hpp>
  17. #include <boost/preprocessor/arithmetic/inc.hpp>
  18. #include <boost/preprocessor/repetition/enum_binary_params.hpp>
  19. #include <boost/preprocessor/repetition/enum_params.hpp>
  20. #include <boost/preprocessor/repetition/repeat_from_to.hpp>
  21. #include <boost/utility/base_from_member.hpp>
  22. #include <boost/asio/detail/pop_options.hpp>
  23. #include <boost/asio/basic_socket_streambuf.hpp>
  24. #include <boost/asio/stream_socket_service.hpp>
  25. #if !defined(BOOST_ASIO_SOCKET_IOSTREAM_MAX_ARITY)
  26. #define BOOST_ASIO_SOCKET_IOSTREAM_MAX_ARITY 5
  27. #endif // !defined(BOOST_ASIO_SOCKET_IOSTREAM_MAX_ARITY)
  28. // A macro that should expand to:
  29. // template <typename T1, ..., typename Tn>
  30. // explicit basic_socket_iostream(T1 x1, ..., Tn xn)
  31. // : basic_iostream<char>(&this->boost::base_from_member<
  32. // basic_socket_streambuf<Protocol, StreamSocketService> >::member)
  33. // {
  34. // if (rdbuf()->connect(x1, ..., xn) == 0)
  35. // this->setstate(std::ios_base::failbit);
  36. // }
  37. // This macro should only persist within this file.
  38. #define BOOST_ASIO_PRIVATE_CTR_DEF(z, n, data) \
  39. template <BOOST_PP_ENUM_PARAMS(n, typename T)> \
  40. explicit basic_socket_iostream(BOOST_PP_ENUM_BINARY_PARAMS(n, T, x)) \
  41. : std::basic_iostream<char>(&this->boost::base_from_member< \
  42. basic_socket_streambuf<Protocol, StreamSocketService> >::member) \
  43. { \
  44. tie(this); \
  45. if (rdbuf()->connect(BOOST_PP_ENUM_PARAMS(n, x)) == 0) \
  46. this->setstate(std::ios_base::failbit); \
  47. } \
  48. /**/
  49. // A macro that should expand to:
  50. // template <typename T1, ..., typename Tn>
  51. // void connect(T1 x1, ..., Tn xn)
  52. // {
  53. // if (rdbuf()->connect(x1, ..., xn) == 0)
  54. // this->setstate(std::ios_base::failbit);
  55. // }
  56. // This macro should only persist within this file.
  57. #define BOOST_ASIO_PRIVATE_CONNECT_DEF(z, n, data) \
  58. template <BOOST_PP_ENUM_PARAMS(n, typename T)> \
  59. void connect(BOOST_PP_ENUM_BINARY_PARAMS(n, T, x)) \
  60. { \
  61. if (rdbuf()->connect(BOOST_PP_ENUM_PARAMS(n, x)) == 0) \
  62. this->setstate(std::ios_base::failbit); \
  63. } \
  64. /**/
  65. namespace boost {
  66. namespace asio {
  67. /// Iostream interface for a socket.
  68. template <typename Protocol,
  69. typename StreamSocketService = stream_socket_service<Protocol> >
  70. class basic_socket_iostream
  71. : public boost::base_from_member<
  72. basic_socket_streambuf<Protocol, StreamSocketService> >,
  73. public std::basic_iostream<char>
  74. {
  75. public:
  76. /// Construct a basic_socket_iostream without establishing a connection.
  77. basic_socket_iostream()
  78. : std::basic_iostream<char>(&this->boost::base_from_member<
  79. basic_socket_streambuf<Protocol, StreamSocketService> >::member)
  80. {
  81. tie(this);
  82. }
  83. #if defined(GENERATING_DOCUMENTATION)
  84. /// Establish a connection to an endpoint corresponding to a resolver query.
  85. /**
  86. * This constructor automatically establishes a connection based on the
  87. * supplied resolver query parameters. The arguments are used to construct
  88. * a resolver query object.
  89. */
  90. template <typename T1, ..., typename TN>
  91. explicit basic_socket_iostream(T1 t1, ..., TN tn);
  92. #else
  93. BOOST_PP_REPEAT_FROM_TO(
  94. 1, BOOST_PP_INC(BOOST_ASIO_SOCKET_IOSTREAM_MAX_ARITY),
  95. BOOST_ASIO_PRIVATE_CTR_DEF, _ )
  96. #endif
  97. #if defined(GENERATING_DOCUMENTATION)
  98. /// Establish a connection to an endpoint corresponding to a resolver query.
  99. /**
  100. * This function automatically establishes a connection based on the supplied
  101. * resolver query parameters. The arguments are used to construct a resolver
  102. * query object.
  103. */
  104. template <typename T1, ..., typename TN>
  105. void connect(T1 t1, ..., TN tn);
  106. #else
  107. BOOST_PP_REPEAT_FROM_TO(
  108. 1, BOOST_PP_INC(BOOST_ASIO_SOCKET_IOSTREAM_MAX_ARITY),
  109. BOOST_ASIO_PRIVATE_CONNECT_DEF, _ )
  110. #endif
  111. /// Close the connection.
  112. void close()
  113. {
  114. if (rdbuf()->close() == 0)
  115. this->setstate(std::ios_base::failbit);
  116. }
  117. /// Return a pointer to the underlying streambuf.
  118. basic_socket_streambuf<Protocol, StreamSocketService>* rdbuf() const
  119. {
  120. return const_cast<basic_socket_streambuf<Protocol, StreamSocketService>*>(
  121. &this->boost::base_from_member<
  122. basic_socket_streambuf<Protocol, StreamSocketService> >::member);
  123. }
  124. };
  125. } // namespace asio
  126. } // namespace boost
  127. #undef BOOST_ASIO_PRIVATE_CTR_DEF
  128. #undef BOOST_ASIO_PRIVATE_CONNECT_DEF
  129. #include <boost/asio/detail/pop_options.hpp>
  130. #endif // BOOST_ASIO_BASIC_SOCKET_IOSTREAM_HPP