posix_fd_set_adapter.hpp 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. //
  2. // detail/posix_fd_set_adapter.hpp
  3. // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  4. //
  5. // Copyright (c) 2003-2013 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_POSIX_FD_SET_ADAPTER_HPP
  11. #define BOOST_ASIO_DETAIL_POSIX_FD_SET_ADAPTER_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. #if !defined(BOOST_ASIO_WINDOWS) \
  17. && !defined(__CYGWIN__) \
  18. && !defined(BOOST_ASIO_WINDOWS_RUNTIME)
  19. #include <cstring>
  20. #include <boost/asio/detail/noncopyable.hpp>
  21. #include <boost/asio/detail/socket_types.hpp>
  22. #include <boost/asio/detail/push_options.hpp>
  23. namespace boost {
  24. namespace asio {
  25. namespace detail {
  26. // Adapts the FD_SET type to meet the Descriptor_Set concept's requirements.
  27. class posix_fd_set_adapter : noncopyable
  28. {
  29. public:
  30. posix_fd_set_adapter()
  31. : max_descriptor_(invalid_socket)
  32. {
  33. using namespace std; // Needed for memset on Solaris.
  34. FD_ZERO(&fd_set_);
  35. }
  36. void reset()
  37. {
  38. using namespace std; // Needed for memset on Solaris.
  39. FD_ZERO(&fd_set_);
  40. }
  41. bool set(socket_type descriptor)
  42. {
  43. if (descriptor < (socket_type)FD_SETSIZE)
  44. {
  45. if (max_descriptor_ == invalid_socket || descriptor > max_descriptor_)
  46. max_descriptor_ = descriptor;
  47. FD_SET(descriptor, &fd_set_);
  48. return true;
  49. }
  50. return false;
  51. }
  52. bool is_set(socket_type descriptor) const
  53. {
  54. return FD_ISSET(descriptor, &fd_set_) != 0;
  55. }
  56. operator fd_set*()
  57. {
  58. return &fd_set_;
  59. }
  60. socket_type max_descriptor() const
  61. {
  62. return max_descriptor_;
  63. }
  64. private:
  65. mutable fd_set fd_set_;
  66. socket_type max_descriptor_;
  67. };
  68. } // namespace detail
  69. } // namespace asio
  70. } // namespace boost
  71. #include <boost/asio/detail/pop_options.hpp>
  72. #endif // !defined(BOOST_ASIO_WINDOWS)
  73. // && !defined(__CYGWIN__)
  74. // && !defined(BOOST_ASIO_WINDOWS_RUNTIME)
  75. #endif // BOOST_ASIO_DETAIL_POSIX_FD_SET_ADAPTER_HPP