E-MailRelay
gdescriptor.h
Go to the documentation of this file.
1 //
2 // Copyright (C) 2001-2018 Graeme Walker <graeme_walker@users.sourceforge.net>
3 //
4 // This program is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation, either version 3 of the License, or
7 // (at your option) any later version.
8 //
9 // This program is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
13 //
14 // You should have received a copy of the GNU General Public License
15 // along with this program. If not, see <http://www.gnu.org/licenses/>.
16 // ===
17 ///
18 /// \file gdescriptor.h
19 ///
20 
21 #ifndef G_NET_DESCRIPTOR__H
22 #define G_NET_DESCRIPTOR__H
23 
24 #include "gdef.h"
25 #include <iostream>
26 
27 namespace GNet
28 {
29  class Descriptor ;
30 }
31 
32 /// \class GNet::Descriptor
33 /// A class that encapsulates a network socket file descriptor and
34 /// an associated windows event handle.
35 ///
37 {
38 public:
39  Descriptor() ;
40  ///< Default constructor.
41 
42  explicit Descriptor( SOCKET , HANDLE = 0 ) ;
43  ///< Constructor.
44 
45  bool valid() const ;
46  ///< Returns true if the socket part is valid, ignoring
47  ///< the handle.
48 
49  static Descriptor invalid() ;
50  ///< Returns a descriptor with an invalid socket part and
51  ///< a zero handle.
52 
53  SOCKET fd() const ;
54  ///< Returns the socket part.
55 
56  HANDLE h() const ;
57  ///< Returns the handle part.
58 
59  bool operator==( const Descriptor & other ) const ;
60  ///< Comparison operator.
61 
62  bool operator!=( const Descriptor & other ) const ;
63  ///< Comparison operator.
64 
65  bool operator<( const Descriptor & other ) const ;
66  ///< Comparison operator.
67 
68  void streamOut( std::ostream & ) const ;
69  ///< Used by op<<().
70 
71 private:
72  SOCKET m_fd ;
73  HANDLE m_handle ;
74 } ;
75 
76 inline
77 GNet::Descriptor::Descriptor( SOCKET fd , HANDLE h ) :
78  m_fd(fd) ,
79  m_handle(h)
80 {
81 }
82 
83 inline
84 SOCKET GNet::Descriptor::fd() const
85 {
86  return m_fd ;
87 }
88 
89 inline
90 bool GNet::Descriptor::operator==( const Descriptor & other ) const
91 {
92  return m_fd == other.m_fd && m_handle == other.m_handle ;
93 }
94 
95 inline
96 bool GNet::Descriptor::operator!=( const Descriptor & other ) const
97 {
98  return !(*this == other) ;
99 }
100 
101 inline
102 bool GNet::Descriptor::operator<( const Descriptor & other ) const
103 {
104  return m_fd == other.m_fd ? ( m_handle < other.m_handle ) : ( m_fd < other.m_fd ) ;
105 }
106 
107 inline
109 {
110  return Descriptor() ;
111 }
112 
113 namespace GNet
114 {
115  inline
116  std::ostream & operator<<( std::ostream & stream , const Descriptor & d )
117  {
118  d.streamOut( stream ) ;
119  return stream ;
120  }
121 }
122 
123 #endif
HANDLE h() const
Returns the handle part.
static Descriptor invalid()
Returns a descriptor with an invalid socket part and a zero handle.
Definition: gdescriptor.h:108
Network classes.
Definition: gaddress.h:29
bool operator==(const Descriptor &other) const
Comparison operator.
Definition: gdescriptor.h:90
bool operator<(const Descriptor &other) const
Comparison operator.
Definition: gdescriptor.h:102
A class that encapsulates a network socket file descriptor and an associated windows event handle...
Definition: gdescriptor.h:36
SOCKET fd() const
Returns the socket part.
Definition: gdescriptor.h:84
Descriptor()
Default constructor.
bool valid() const
Returns true if the socket part is valid, ignoring the handle.
bool operator!=(const Descriptor &other) const
Comparison operator.
Definition: gdescriptor.h:96
void streamOut(std::ostream &) const
Used by op<<().