basic_socket_iostream.hpp 4.7 KB

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