PageRenderTime 32ms CodeModel.GetById 13ms app.highlight 14ms RepoModel.GetById 1ms app.codeStats 0ms

/src/contrib/boost/asio/detail/win_fd_set_adapter.hpp

http://pythonocc.googlecode.com/
C++ Header | 90 lines | 60 code | 18 blank | 12 comment | 6 complexity | c1deed58308108c9c5b0819a678a744e MD5 | raw file
 1//
 2// win_fd_set_adapter.hpp
 3// ~~~~~~~~~~~~~~~~~~~~~~
 4//
 5// Copyright (c) 2003-2010 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
11#ifndef BOOST_ASIO_DETAIL_WIN_FD_SET_ADAPTER_HPP
12#define BOOST_ASIO_DETAIL_WIN_FD_SET_ADAPTER_HPP
13
14#if defined(_MSC_VER) && (_MSC_VER >= 1200)
15# pragma once
16#endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
17
18#include <boost/asio/detail/push_options.hpp>
19
20#include <boost/asio/detail/socket_types.hpp>
21
22#if defined(BOOST_WINDOWS) || defined(__CYGWIN__)
23
24namespace boost {
25namespace asio {
26namespace detail {
27
28// Adapts the FD_SET type to meet the Descriptor_Set concept's requirements.
29class win_fd_set_adapter
30{
31public:
32  enum { win_fd_set_size = 1024 };
33
34  win_fd_set_adapter()
35    : max_descriptor_(invalid_socket)
36  {
37    fd_set_.fd_count = 0;
38  }
39
40  bool set(socket_type descriptor)
41  {
42    for (u_int i = 0; i < fd_set_.fd_count; ++i)
43      if (fd_set_.fd_array[i] == descriptor)
44        return true;
45    if (fd_set_.fd_count < win_fd_set_size)
46    {
47      fd_set_.fd_array[fd_set_.fd_count++] = descriptor;
48      return true;
49    }
50    return false;
51  }
52
53  bool is_set(socket_type descriptor) const
54  {
55    return !!__WSAFDIsSet(descriptor,
56        const_cast<fd_set*>(reinterpret_cast<const fd_set*>(&fd_set_)));
57  }
58
59  operator fd_set*()
60  {
61    return reinterpret_cast<fd_set*>(&fd_set_);
62  }
63
64  socket_type max_descriptor() const
65  {
66    return max_descriptor_;
67  }
68
69private:
70  // This structure is defined to be compatible with the Windows API fd_set
71  // structure, but without being dependent on the value of FD_SETSIZE.
72  struct win_fd_set
73  {
74    u_int fd_count;
75    SOCKET fd_array[win_fd_set_size];
76  };
77
78  win_fd_set fd_set_;
79  socket_type max_descriptor_;
80};
81
82} // namespace detail
83} // namespace asio
84} // namespace boost
85
86#endif // defined(BOOST_WINDOWS) || defined(__CYGWIN__)
87
88#include <boost/asio/detail/pop_options.hpp>
89
90#endif // BOOST_ASIO_DETAIL_WIN_FD_SET_ADAPTER_HPP