posix_fd_set_adapter.hpp 1.9 KB

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