net.h 4.7 KB

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