io_control.hpp 2.7 KB

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