E-MailRelay
gdescriptor.h
Go to the documentation of this file.
1//
2// Copyright (C) 2001-2021 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
27namespace 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{
38public:
39 Descriptor() noexcept ;
40 ///< Default constructor.
41
42 explicit Descriptor( SOCKET , HANDLE = HNULL ) noexcept ;
43 ///< Constructor.
44
45 bool valid() const noexcept ;
46 ///< Returns true if the socket part is valid, ignoring
47 ///< the handle.
48
49 static Descriptor invalid() noexcept ;
50 ///< Returns a descriptor with an invalid socket part and
51 ///< a zero handle.
52
53 SOCKET fd() const noexcept ;
54 ///< Returns the socket part.
55
56 HANDLE h() const noexcept ;
57 ///< Returns the handle part.
58
59 bool operator==( const Descriptor & other ) const noexcept ;
60 ///< Comparison operator.
61
62 bool operator!=( const Descriptor & other ) const noexcept ;
63 ///< Comparison operator.
64
65 bool operator<( const Descriptor & other ) const noexcept ;
66 ///< Comparison operator.
67
68 void streamOut( std::ostream & ) const ;
69 ///< Used by op<<().
70
71private:
72 SOCKET m_fd ; // NOLINT
73 HANDLE m_handle{0} ;
74} ;
75
76inline
77GNet::Descriptor::Descriptor( SOCKET fd , HANDLE h ) noexcept :
78 m_fd(fd) ,
79 m_handle(h)
80{
81}
82
83inline
84SOCKET GNet::Descriptor::fd() const noexcept
85{
86 return m_fd ;
87}
88
89inline
90bool GNet::Descriptor::operator==( const Descriptor & other ) const noexcept
91{
92 return m_fd == other.m_fd && m_handle == other.m_handle ;
93}
94
95inline
96bool GNet::Descriptor::operator!=( const Descriptor & other ) const noexcept
97{
98 return !(*this == other) ;
99}
100
101inline
102bool GNet::Descriptor::operator<( const Descriptor & other ) const noexcept
103{
104 return m_fd == other.m_fd ? ( m_handle < other.m_handle ) : ( m_fd < other.m_fd ) ;
105}
106
107inline
109{
110 return {} ;
111}
112
113namespace 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
A class that encapsulates a network socket file descriptor and an associated windows event handle.
Definition: gdescriptor.h:37
void streamOut(std::ostream &) const
Used by op<<().
bool operator<(const Descriptor &other) const noexcept
Comparison operator.
Definition: gdescriptor.h:102
SOCKET fd() const noexcept
Returns the socket part.
Definition: gdescriptor.h:84
Descriptor() noexcept
Default constructor.
bool operator==(const Descriptor &other) const noexcept
Comparison operator.
Definition: gdescriptor.h:90
static Descriptor invalid() noexcept
Returns a descriptor with an invalid socket part and a zero handle.
Definition: gdescriptor.h:108
bool valid() const noexcept
Returns true if the socket part is valid, ignoring the handle.
HANDLE h() const noexcept
Returns the handle part.
bool operator!=(const Descriptor &other) const noexcept
Comparison operator.
Definition: gdescriptor.h:96
Network classes.
Definition: gdef.h:1115