Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

3 роки тому
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. // Networking implementation details -*- C++ -*-
  2. // Copyright (C) 2015-2020 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/net}
  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. /** @addtogroup networking-ts
  41. * @{
  42. */
  43. template<typename _CompletionToken, typename _Signature, typename>
  44. class async_result;
  45. /// @cond undocumented
  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. /// @endcond
  72. // Base class for types meeting IntegerSocketOption requirements.
  73. template<typename _Tp>
  74. struct __sockopt_base
  75. {
  76. __sockopt_base() = default;
  77. explicit __sockopt_base(int __val) : _M_value(__val) { }
  78. int value() const noexcept { return _M_value; }
  79. template<typename _Protocol>
  80. void*
  81. data(const _Protocol&) noexcept
  82. { return std::addressof(_M_value); }
  83. template<typename _Protocol>
  84. const void*
  85. data(const _Protocol&) const noexcept
  86. { return std::addressof(_M_value); }
  87. template<typename _Protocol>
  88. size_t
  89. size(const _Protocol&) const noexcept
  90. { return sizeof(_M_value); }
  91. template<typename _Protocol>
  92. void
  93. resize(const _Protocol&, size_t __s)
  94. {
  95. if (__s != sizeof(_M_value))
  96. __throw_length_error("invalid value for socket option resize");
  97. }
  98. protected:
  99. _Tp _M_value { };
  100. };
  101. // Base class for types meeting BooleanSocketOption requirements.
  102. template<>
  103. struct __sockopt_base<bool> : __sockopt_base<int>
  104. {
  105. __sockopt_base() = default;
  106. explicit __sockopt_base(bool __val) : __sockopt_base<int>(__val) { }
  107. bool value() const noexcept { return __sockopt_base<int>::_M_value; }
  108. explicit operator bool() const noexcept { return value(); }
  109. bool operator!() const noexcept { return !value(); }
  110. };
  111. template<typename _Derived, typename _Tp = int>
  112. struct __sockopt_crtp : __sockopt_base<_Tp>
  113. {
  114. using __sockopt_base<_Tp>::__sockopt_base;
  115. _Derived&
  116. operator=(_Tp __value)
  117. {
  118. __sockopt_base<_Tp>::_M_value = __value;
  119. return static_cast<_Derived&>(*this);
  120. }
  121. template<typename _Protocol>
  122. int
  123. level(const _Protocol&) const noexcept
  124. { return _Derived::_S_level; }
  125. template<typename _Protocol>
  126. int
  127. name(const _Protocol&) const noexcept
  128. { return _Derived::_S_name; }
  129. };
  130. /// @}
  131. } // namespace v1
  132. } // namespace net
  133. } // namespace experimental
  134. _GLIBCXX_END_NAMESPACE_VERSION
  135. } // namespace std
  136. #endif // C++14
  137. #endif // _GLIBCXX_EXPERIMENTAL_NET_H