1// Networking implementation details -*- C++ -*-
2
3// Copyright (C) 2015-2020 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/net}
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
41namespace std _GLIBCXX_VISIBILITY(default)
42{
43_GLIBCXX_BEGIN_NAMESPACE_VERSION
44namespace experimental
45{
46namespace net
47{
48inline namespace v1
49{
50
51  /** @addtogroup networking-ts
52   *  @{
53   */
54
55  template<typename _CompletionToken, typename _Signature, typename>
56    class async_result;
57
58  /// @cond undocumented
59
60  // A type denoted by DEDUCED in the TS.
61  template<typename _CompletionToken, typename _Signature>
62    using __deduced_t = typename
63      async_result<decay_t<_CompletionToken>, _Signature, void>::return_type;
64
65  // Trait to check for construction from const/non-const lvalue/rvalue.
66  template<typename _Tp>
67    using __is_value_constructible = typename __and_<
68      is_copy_constructible<_Tp>, is_move_constructible<_Tp>,
69      is_constructible<_Tp, _Tp&>, is_constructible<_Tp, const _Tp&&>
70      >::type;
71
72  struct __throw_on_error
73  {
74    explicit
75    __throw_on_error(const char* __msg) : _M_msg(__msg) { }
76
77    ~__throw_on_error() noexcept(false)
78    {
79      if (_M_ec)
80	_GLIBCXX_THROW_OR_ABORT(system_error(_M_ec, _M_msg));
81    }
82
83    __throw_on_error(const __throw_on_error&) = delete;
84    __throw_on_error& operator=(const __throw_on_error&) = delete;
85
86    operator error_code&() noexcept { return _M_ec; }
87
88    const char* _M_msg;
89    error_code _M_ec;
90  };
91
92  /// @endcond
93
94  // Base class for types meeting IntegerSocketOption requirements.
95  template<typename _Tp>
96    struct __sockopt_base
97    {
98      __sockopt_base() = default;
99
100      explicit __sockopt_base(int __val) : _M_value(__val) { }
101
102      int value() const noexcept { return _M_value; }
103
104      template<typename _Protocol>
105	void*
106	data(const _Protocol&) noexcept
107	{ return std::addressof(_M_value); }
108
109      template<typename _Protocol>
110	const void*
111	data(const _Protocol&) const noexcept
112	{ return std::addressof(_M_value); }
113
114      template<typename _Protocol>
115	size_t
116	size(const _Protocol&) const noexcept
117	{ return sizeof(_M_value); }
118
119      template<typename _Protocol>
120	void
121	resize(const _Protocol&, size_t __s)
122	{
123	  if (__s != sizeof(_M_value))
124	    __throw_length_error("invalid value for socket option resize");
125	}
126
127    protected:
128      _Tp _M_value { };
129    };
130
131  // Base class for types meeting BooleanSocketOption requirements.
132  template<>
133    struct __sockopt_base<bool> : __sockopt_base<int>
134    {
135      __sockopt_base() = default;
136
137      explicit __sockopt_base(bool __val) : __sockopt_base<int>(__val) { }
138
139      bool value() const noexcept { return __sockopt_base<int>::_M_value; }
140      explicit operator bool() const noexcept { return value(); }
141      bool operator!() const noexcept { return !value(); }
142    };
143
144  template<typename _Derived, typename _Tp = int>
145    struct __sockopt_crtp : __sockopt_base<_Tp>
146    {
147      using __sockopt_base<_Tp>::__sockopt_base;
148
149      _Derived&
150      operator=(_Tp __value)
151      {
152	__sockopt_base<_Tp>::_M_value = __value;
153	return static_cast<_Derived&>(*this);
154      }
155
156      template<typename _Protocol>
157	int
158	level(const _Protocol&) const noexcept
159	{ return _Derived::_S_level; }
160
161      template<typename _Protocol>
162	int
163	name(const _Protocol&) const noexcept
164	{ return _Derived::_S_name; }
165    };
166
167  /// @}
168
169} // namespace v1
170} // namespace net
171} // namespace experimental
172_GLIBCXX_END_NAMESPACE_VERSION
173} // namespace std
174
175#endif // C++14
176
177#endif // _GLIBCXX_EXPERIMENTAL_NET_H
178