libstdc++
net.h
Go to the documentation of this file.
00001 // Networking implementation details -*- C++ -*-
00002 
00003 // Copyright (C) 2015-2019 Free Software Foundation, Inc.
00004 //
00005 // This file is part of the GNU ISO C++ Library.  This library is free
00006 // software; you can redistribute it and/or modify it under the
00007 // terms of the GNU General Public License as published by the
00008 // Free Software Foundation; either version 3, or (at your option)
00009 // any later version.
00010 
00011 // This library is distributed in the hope that it will be useful,
00012 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00013 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014 // GNU General Public License for more details.
00015 
00016 // Under Section 7 of GPL version 3, you are granted additional
00017 // permissions described in the GCC Runtime Library Exception, version
00018 // 3.1, as published by the Free Software Foundation.
00019 
00020 // You should have received a copy of the GNU General Public License and
00021 // a copy of the GCC Runtime Library Exception along with this program;
00022 // see the files COPYING3 and COPYING.RUNTIME respectively.  If not, see
00023 // <http://www.gnu.org/licenses/>.
00024 
00025 /** @file experimental/bits/net.h
00026  *  This is an internal header file, included by other library headers.
00027  *  Do not attempt to use it directly. @headername{experimental/networking}
00028  */
00029 
00030 #ifndef _GLIBCXX_EXPERIMENTAL_NET_H
00031 #define _GLIBCXX_EXPERIMENTAL_NET_H 1
00032 
00033 #pragma GCC system_header
00034 
00035 #if __cplusplus >= 201402L
00036 
00037 #include <type_traits>
00038 #include <system_error>
00039 #include <experimental/netfwd>
00040 
00041 namespace std _GLIBCXX_VISIBILITY(default)
00042 {
00043 _GLIBCXX_BEGIN_NAMESPACE_VERSION
00044 namespace experimental
00045 {
00046 namespace net
00047 {
00048 inline namespace v1
00049 {
00050 
00051   /**
00052    * @ingroup networking
00053    * @{
00054    */
00055 
00056   template<typename _CompletionToken, typename _Signature, typename>
00057     class async_result;
00058 
00059   // A type denoted by DEDUCED in the TS.
00060   template<typename _CompletionToken, typename _Signature>
00061     using __deduced_t = typename
00062       async_result<decay_t<_CompletionToken>, _Signature, void>::return_type;
00063 
00064   // Trait to check for construction from const/non-const lvalue/rvalue.
00065   template<typename _Tp>
00066     using __is_value_constructible = typename __and_<
00067       is_copy_constructible<_Tp>, is_move_constructible<_Tp>,
00068       is_constructible<_Tp, _Tp&>, is_constructible<_Tp, const _Tp&&>
00069       >::type;
00070 
00071     struct __throw_on_error
00072     {
00073       explicit
00074       __throw_on_error(const char* __msg) : _M_msg(__msg) { }
00075 
00076       ~__throw_on_error() noexcept(false)
00077       {
00078         if (_M_ec)
00079           _GLIBCXX_THROW_OR_ABORT(system_error(_M_ec, _M_msg));
00080       }
00081 
00082       __throw_on_error(const __throw_on_error&) = delete;
00083       __throw_on_error& operator=(const __throw_on_error&) = delete;
00084 
00085       operator error_code&() noexcept { return _M_ec; }
00086 
00087       const char* _M_msg;
00088       error_code _M_ec;
00089     };
00090 
00091   // Base class for types meeting IntegerSocketOption requirements.
00092   template<typename _Tp>
00093     struct __sockopt_base
00094     {
00095       __sockopt_base() = default;
00096 
00097       explicit __sockopt_base(int __val) : _M_value(__val) { }
00098 
00099       int value() const noexcept { return _M_value; }
00100 
00101       template<typename _Protocol>
00102         void*
00103         data(const _Protocol&) noexcept
00104         { return std::addressof(_M_value); }
00105 
00106       template<typename _Protocol>
00107         const void*
00108         data(const _Protocol&) const noexcept
00109         { return std::addressof(_M_value); }
00110 
00111       template<typename _Protocol>
00112         size_t
00113         size(const _Protocol&) const noexcept
00114         { return sizeof(_M_value); }
00115 
00116       template<typename _Protocol>
00117         void
00118         resize(const _Protocol&, size_t __s)
00119         {
00120           if (__s != sizeof(_M_value))
00121             __throw_length_error("invalid value for socket option resize");
00122         }
00123 
00124     protected:
00125       _Tp _M_value { };
00126     };
00127 
00128   // Base class for types meeting BooleanSocketOption requirements.
00129   template<>
00130     struct __sockopt_base<bool> : __sockopt_base<int>
00131     {
00132       __sockopt_base() = default;
00133 
00134       explicit __sockopt_base(bool __val) : __sockopt_base<int>(__val) { }
00135 
00136       bool value() const noexcept { return __sockopt_base<int>::_M_value; }
00137       explicit operator bool() const noexcept { return value(); }
00138       bool operator!() const noexcept { return !value(); }
00139     };
00140 
00141   template<typename _Derived, typename _Tp = int>
00142     struct __sockopt_crtp : __sockopt_base<_Tp>
00143     {
00144       using __sockopt_base<_Tp>::__sockopt_base;
00145 
00146       _Derived&
00147       operator=(_Tp __value)
00148       {
00149         __sockopt_base<_Tp>::_M_value = __value;
00150         return static_cast<_Derived&>(*this);
00151       }
00152 
00153       template<typename _Protocol>
00154         int
00155         level(const _Protocol&) const noexcept
00156         { return _Derived::_S_level; }
00157 
00158       template<typename _Protocol>
00159         int
00160         name(const _Protocol&) const noexcept
00161         { return _Derived::_S_name; }
00162     };
00163 
00164   /// @}
00165 
00166 } // namespace v1
00167 } // namespace net
00168 } // namespace experimental
00169 _GLIBCXX_END_NAMESPACE_VERSION
00170 } // namespace std
00171 
00172 #endif // C++14
00173 
00174 #endif // _GLIBCXX_EXPERIMENTAL_NET_H