1 // Networking implementation details -*- C++ -*-
2
3 // Copyright (C) 2015-2019 Free Software Foundation, Inc.
4 //
5 // This file is part of the GNU ISO C++ Library. This library is free
6 // software; you can redistribute it and/or modify it under the
7 // terms of the GNU General Public License as published by the
8 // Free Software Foundation; either version 3, or (at your option)
9 // any later version.
10
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
15
16 // Under Section 7 of GPL version 3, you are granted additional
17 // permissions described in the GCC Runtime Library Exception, version
18 // 3.1, as published by the Free Software Foundation.
19
20 // You should have received a copy of the GNU General Public License and
21 // a copy of the GCC Runtime Library Exception along with this program;
22 // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
23 // <http://www.gnu.org/licenses/>.
24
25 /** @file experimental/bits/net.h
26 * This is an internal header file, included by other library headers.
27 * Do not attempt to use it directly. @headername{experimental/networking}
28 */
29
30 #ifndef _GLIBCXX_EXPERIMENTAL_NET_H
31 #define _GLIBCXX_EXPERIMENTAL_NET_H 1
32
33 #pragma GCC system_header
34
35 #if __cplusplus >= 201402L
36
37 #include <type_traits>
38 #include <system_error>
39 #include <experimental/netfwd>
40
_GLIBCXX_VISIBILITY(default)41 namespace std _GLIBCXX_VISIBILITY(default)
42 {
43 _GLIBCXX_BEGIN_NAMESPACE_VERSION
44 namespace experimental
45 {
46 namespace net
47 {
48 inline namespace v1
49 {
50
51 /**
52 * @ingroup networking
53 * @{
54 */
55
56 template<typename _CompletionToken, typename _Signature, typename>
57 class async_result;
58
59 // A type denoted by DEDUCED in the TS.
60 template<typename _CompletionToken, typename _Signature>
61 using __deduced_t = typename
62 async_result<decay_t<_CompletionToken>, _Signature, void>::return_type;
63
64 // Trait to check for construction from const/non-const lvalue/rvalue.
65 template<typename _Tp>
66 using __is_value_constructible = typename __and_<
67 is_copy_constructible<_Tp>, is_move_constructible<_Tp>,
68 is_constructible<_Tp, _Tp&>, is_constructible<_Tp, const _Tp&&>
69 >::type;
70
71 struct __throw_on_error
72 {
73 explicit
74 __throw_on_error(const char* __msg) : _M_msg(__msg) { }
75
76 ~__throw_on_error() noexcept(false)
77 {
78 if (_M_ec)
79 _GLIBCXX_THROW_OR_ABORT(system_error(_M_ec, _M_msg));
80 }
81
82 __throw_on_error(const __throw_on_error&) = delete;
83 __throw_on_error& operator=(const __throw_on_error&) = delete;
84
85 operator error_code&() noexcept { return _M_ec; }
86
87 const char* _M_msg;
88 error_code _M_ec;
89 };
90
91 // Base class for types meeting IntegerSocketOption requirements.
92 template<typename _Tp>
93 struct __sockopt_base
94 {
95 __sockopt_base() = default;
96
97 explicit __sockopt_base(int __val) : _M_value(__val) { }
98
99 int value() const noexcept { return _M_value; }
100
101 template<typename _Protocol>
102 void*
103 data(const _Protocol&) noexcept
104 { return std::addressof(_M_value); }
105
106 template<typename _Protocol>
107 const void*
108 data(const _Protocol&) const noexcept
109 { return std::addressof(_M_value); }
110
111 template<typename _Protocol>
112 size_t
113 size(const _Protocol&) const noexcept
114 { return sizeof(_M_value); }
115
116 template<typename _Protocol>
117 void
118 resize(const _Protocol&, size_t __s)
119 {
120 if (__s != sizeof(_M_value))
121 __throw_length_error("invalid value for socket option resize");
122 }
123
124 protected:
125 _Tp _M_value { };
126 };
127
128 // Base class for types meeting BooleanSocketOption requirements.
129 template<>
130 struct __sockopt_base<bool> : __sockopt_base<int>
131 {
132 __sockopt_base() = default;
133
134 explicit __sockopt_base(bool __val) : __sockopt_base<int>(__val) { }
135
136 bool value() const noexcept { return __sockopt_base<int>::_M_value; }
137 explicit operator bool() const noexcept { return value(); }
138 bool operator!() const noexcept { return !value(); }
139 };
140
141 template<typename _Derived, typename _Tp = int>
142 struct __sockopt_crtp : __sockopt_base<_Tp>
143 {
144 using __sockopt_base<_Tp>::__sockopt_base;
145
146 _Derived&
147 operator=(_Tp __value)
148 {
149 __sockopt_base<_Tp>::_M_value = __value;
150 return static_cast<_Derived&>(*this);
151 }
152
153 template<typename _Protocol>
154 int
155 level(const _Protocol&) const noexcept
156 { return _Derived::_S_level; }
157
158 template<typename _Protocol>
159 int
160 name(const _Protocol&) const noexcept
161 { return _Derived::_S_name; }
162 };
163
164 /// @}
165
166 } // namespace v1
167 } // namespace net
168 } // namespace experimental
169 _GLIBCXX_END_NAMESPACE_VERSION
170 } // namespace std
171
172 #endif // C++14
173
174 #endif // _GLIBCXX_EXPERIMENTAL_NET_H
175