basic_socket_iostream.hpp 4.9 KB

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