is_write_buffered.cpp 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. //
  2. // is_write_buffered.cpp
  3. // ~~~~~~~~~~~~~~~~~~~~~
  4. //
  5. // Copyright (c) 2003-2019 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. // Disable autolinking for unit tests.
  11. #if !defined(BOOST_ALL_NO_LIB)
  12. #define BOOST_ALL_NO_LIB 1
  13. #endif // !defined(BOOST_ALL_NO_LIB)
  14. // Test that header file is self-contained.
  15. #include <boost/asio/is_write_buffered.hpp>
  16. #include <boost/asio/buffered_read_stream.hpp>
  17. #include <boost/asio/buffered_write_stream.hpp>
  18. #include <boost/asio/io_context.hpp>
  19. #include <boost/asio/ip/tcp.hpp>
  20. #include "unit_test.hpp"
  21. using namespace std; // For memcmp, memcpy and memset.
  22. class test_stream
  23. {
  24. public:
  25. typedef boost::asio::io_context io_context_type;
  26. typedef test_stream lowest_layer_type;
  27. typedef io_context_type::executor_type executor_type;
  28. test_stream(boost::asio::io_context& io_context)
  29. : io_context_(io_context)
  30. {
  31. }
  32. io_context_type& io_context()
  33. {
  34. return io_context_;
  35. }
  36. lowest_layer_type& lowest_layer()
  37. {
  38. return *this;
  39. }
  40. template <typename Const_Buffers>
  41. size_t write(const Const_Buffers&)
  42. {
  43. return 0;
  44. }
  45. template <typename Const_Buffers>
  46. size_t write(const Const_Buffers&, boost::system::error_code& ec)
  47. {
  48. ec = boost::system::error_code();
  49. return 0;
  50. }
  51. template <typename Const_Buffers, typename Handler>
  52. void async_write(const Const_Buffers&, Handler handler)
  53. {
  54. boost::system::error_code error;
  55. boost::asio::post(io_context_,
  56. boost::asio::detail::bind_handler(handler, error, 0));
  57. }
  58. template <typename Mutable_Buffers>
  59. size_t read(const Mutable_Buffers&)
  60. {
  61. return 0;
  62. }
  63. template <typename Mutable_Buffers>
  64. size_t read(const Mutable_Buffers&, boost::system::error_code& ec)
  65. {
  66. ec = boost::system::error_code();
  67. return 0;
  68. }
  69. template <typename Mutable_Buffers, typename Handler>
  70. void async_read(const Mutable_Buffers&, Handler handler)
  71. {
  72. boost::system::error_code error;
  73. boost::asio::post(io_context_,
  74. boost::asio::detail::bind_handler(handler, error, 0));
  75. }
  76. private:
  77. io_context_type& io_context_;
  78. };
  79. void is_write_buffered_test()
  80. {
  81. BOOST_ASIO_CHECK(!boost::asio::is_write_buffered<
  82. boost::asio::ip::tcp::socket>::value);
  83. BOOST_ASIO_CHECK(!boost::asio::is_write_buffered<
  84. boost::asio::buffered_read_stream<
  85. boost::asio::ip::tcp::socket> >::value);
  86. BOOST_ASIO_CHECK(!!boost::asio::is_write_buffered<
  87. boost::asio::buffered_write_stream<
  88. boost::asio::ip::tcp::socket> >::value);
  89. BOOST_ASIO_CHECK(!!boost::asio::is_write_buffered<
  90. boost::asio::buffered_stream<boost::asio::ip::tcp::socket> >::value);
  91. BOOST_ASIO_CHECK(!boost::asio::is_write_buffered<test_stream>::value);
  92. BOOST_ASIO_CHECK(!boost::asio::is_write_buffered<
  93. boost::asio::buffered_read_stream<test_stream> >::value);
  94. BOOST_ASIO_CHECK(!!boost::asio::is_write_buffered<
  95. boost::asio::buffered_write_stream<test_stream> >::value);
  96. BOOST_ASIO_CHECK(!!boost::asio::is_write_buffered<
  97. boost::asio::buffered_stream<test_stream> >::value);
  98. }
  99. BOOST_ASIO_TEST_SUITE
  100. (
  101. "is_write_buffered",
  102. BOOST_ASIO_TEST_CASE(is_write_buffered_test)
  103. )