io_control.hpp 2.6 KB

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