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
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///
36 class GNet::Descriptor
37{
38 public:
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
71 private:
72 SOCKET m_fd ; // NOLINT
73 HANDLE m_handle{0} ;
74} ;
75
76 inline
77 GNet::Descriptor::Descriptor( SOCKET fd , HANDLE h ) noexcept :
78 m_fd(fd) ,
79 m_handle(h)
80{
81}
82
83 inline
84SOCKET GNet::Descriptor::fd() const noexcept
85{
86 return m_fd ;
87}
88
89 inline
90 bool GNet::Descriptor::operator==( const Descriptor & other ) const noexcept
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 noexcept
97{
98 return !(*this == other) ;
99}
100
101 inline
102 bool 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
107 inline
108 GNet::Descriptor GNet::Descriptor::invalid() noexcept
109{
110 return {} ;
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
GNet::Descriptor
A class that encapsulates a network socket file descriptor and an associated windows event handle.
Definition: gdescriptor.h:37
GNet::Descriptor::streamOut
void streamOut(std::ostream &) const
Used by op<<().
Definition: gdescriptor_unix.cpp:39
GNet::Descriptor::operator<
bool operator<(const Descriptor &other) const noexcept
Comparison operator.
Definition: gdescriptor.h:102
GNet::Descriptor::fd
SOCKET fd() const noexcept
Returns the socket part.
Definition: gdescriptor.h:84
GNet::Descriptor::Descriptor
Descriptor() noexcept
Default constructor.
Definition: gdescriptor_unix.cpp:24
GNet::Descriptor::operator==
bool operator==(const Descriptor &other) const noexcept
Comparison operator.
Definition: gdescriptor.h:90
GNet::Descriptor::invalid
static Descriptor invalid() noexcept
Returns a descriptor with an invalid socket part and a zero handle.
Definition: gdescriptor.h:108
GNet::Descriptor::valid
bool valid() const noexcept
Returns true if the socket part is valid, ignoring the handle.
Definition: gdescriptor_unix.cpp:29
GNet::Descriptor::h
HANDLE h() const noexcept
Returns the handle part.
Definition: gdescriptor_unix.cpp:34
GNet::Descriptor::operator!=
bool operator!=(const Descriptor &other) const noexcept
Comparison operator.
Definition: gdescriptor.h:96
GNet
Network classes.
Definition: gdef.h:1115