io_control.hpp 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. //
  2. // io_control.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_DETAIL_IO_CONTROL_HPP
  11. #define BOOST_ASIO_DETAIL_IO_CONTROL_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 <cstddef>
  18. #include <boost/config.hpp>
  19. #include <boost/asio/detail/pop_options.hpp>
  20. #include <boost/asio/detail/socket_types.hpp>
  21. namespace boost {
  22. namespace asio {
  23. namespace detail {
  24. namespace io_control {
  25. // IO control command for non-blocking I/O.
  26. class non_blocking_io
  27. {
  28. public:
  29. // Default constructor.
  30. non_blocking_io()
  31. : value_(0)
  32. {
  33. }
  34. // Construct with a specific command value.
  35. non_blocking_io(bool value)
  36. : value_(value ? 1 : 0)
  37. {
  38. }
  39. // Get the name of the IO control command.
  40. int name() const
  41. {
  42. return FIONBIO;
  43. }
  44. // Set the value of the I/O control command.
  45. void set(bool value)
  46. {
  47. value_ = value ? 1 : 0;
  48. }
  49. // Get the current value of the I/O control command.
  50. bool get() const
  51. {
  52. return value_ != 0;
  53. }
  54. // Get the address of the command data.
  55. detail::ioctl_arg_type* data()
  56. {
  57. return &value_;
  58. }
  59. // Get the address of the command data.
  60. const detail::ioctl_arg_type* data() const
  61. {
  62. return &value_;
  63. }
  64. private:
  65. detail::ioctl_arg_type value_;
  66. };
  67. // I/O control command for getting number of bytes available.
  68. class bytes_readable
  69. {
  70. public:
  71. // Default constructor.
  72. bytes_readable()
  73. : value_(0)
  74. {
  75. }
  76. // Construct with a specific command value.
  77. bytes_readable(std::size_t value)
  78. : value_(static_cast<detail::ioctl_arg_type>(value))
  79. {
  80. }
  81. // Get the name of the IO control command.
  82. int name() const
  83. {
  84. return FIONREAD;
  85. }
  86. // Set the value of the I/O control command.
  87. void set(std::size_t value)
  88. {
  89. value_ = static_cast<detail::ioctl_arg_type>(value);
  90. }
  91. // Get the current value of the I/O control command.
  92. std::size_t get() const
  93. {
  94. return static_cast<std::size_t>(value_);
  95. }
  96. // Get the address of the command data.
  97. detail::ioctl_arg_type* data()
  98. {
  99. return &value_;
  100. }
  101. // Get the address of the command data.
  102. const detail::ioctl_arg_type* data() const
  103. {
  104. return &value_;
  105. }
  106. private:
  107. detail::ioctl_arg_type value_;
  108. };
  109. } // namespace io_control
  110. } // namespace detail
  111. } // namespace asio
  112. } // namespace boost
  113. #include <boost/asio/detail/pop_options.hpp>
  114. #endif // BOOST_ASIO_DETAIL_IO_CONTROL_HPP