move_only_function.h 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. // Implementation of std::move_only_function -*- C++ -*-
  2. // Copyright The GNU Toolchain Authors.
  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 include/bits/move_only_function.h
  21. * This is an internal header file, included by other library headers.
  22. * Do not attempt to use it directly. @headername{functional}
  23. */
  24. #ifndef _GLIBCXX_MOVE_ONLY_FUNCTION_H
  25. #define _GLIBCXX_MOVE_ONLY_FUNCTION_H 1
  26. #pragma GCC system_header
  27. #if __cplusplus > 202002L
  28. #include <bits/invoke.h>
  29. #include <bits/utility.h>
  30. namespace std _GLIBCXX_VISIBILITY(default)
  31. {
  32. _GLIBCXX_BEGIN_NAMESPACE_VERSION
  33. #define __cpp_lib_move_only_function 202110L
  34. template<typename... _Signature>
  35. class move_only_function; // not defined
  36. /// @cond undocumented
  37. class _Mofunc_base
  38. {
  39. protected:
  40. _Mofunc_base() noexcept
  41. : _M_manage(_S_empty)
  42. { }
  43. _Mofunc_base(_Mofunc_base&& __x) noexcept
  44. {
  45. _M_manage = std::__exchange(__x._M_manage, _S_empty);
  46. _M_manage(_M_storage, &__x._M_storage);
  47. }
  48. template<typename _Tp, typename... _Args>
  49. static constexpr bool
  50. _S_nothrow_init() noexcept
  51. {
  52. if constexpr (__stored_locally<_Tp>)
  53. return is_nothrow_constructible_v<_Tp, _Args...>;
  54. return false;
  55. }
  56. template<typename _Tp, typename... _Args>
  57. void
  58. _M_init(_Args&&... __args) noexcept(_S_nothrow_init<_Tp, _Args...>())
  59. {
  60. if constexpr (__stored_locally<_Tp>)
  61. ::new (_M_storage._M_addr()) _Tp(std::forward<_Args>(__args)...);
  62. else
  63. _M_storage._M_p = new _Tp(std::forward<_Args>(__args)...);
  64. _M_manage = &_S_manage<_Tp>;
  65. }
  66. _Mofunc_base&
  67. operator=(_Mofunc_base&& __x) noexcept
  68. {
  69. _M_manage(_M_storage, nullptr);
  70. _M_manage = std::__exchange(__x._M_manage, _S_empty);
  71. _M_manage(_M_storage, &__x._M_storage);
  72. return *this;
  73. }
  74. _Mofunc_base&
  75. operator=(nullptr_t) noexcept
  76. {
  77. _M_manage(_M_storage, nullptr);
  78. _M_manage = _S_empty;
  79. return *this;
  80. }
  81. ~_Mofunc_base() { _M_manage(_M_storage, nullptr); }
  82. void
  83. swap(_Mofunc_base& __x) noexcept
  84. {
  85. // Order of operations here is more efficient if __x is empty.
  86. _Storage __s;
  87. __x._M_manage(__s, &__x._M_storage);
  88. _M_manage(__x._M_storage, &_M_storage);
  89. __x._M_manage(_M_storage, &__s);
  90. std::swap(_M_manage, __x._M_manage);
  91. }
  92. template<typename _Tp, typename _Self>
  93. static _Tp*
  94. _S_access(_Self* __self) noexcept
  95. {
  96. if constexpr (__stored_locally<remove_const_t<_Tp>>)
  97. return static_cast<_Tp*>(__self->_M_storage._M_addr());
  98. else
  99. return static_cast<_Tp*>(__self->_M_storage._M_p);
  100. }
  101. private:
  102. struct _Storage
  103. {
  104. void* _M_addr() noexcept { return &_M_bytes[0]; }
  105. const void* _M_addr() const noexcept { return &_M_bytes[0]; }
  106. // We want to have enough space to store a simple delegate type.
  107. struct _Delegate { void (_Storage::*__pfm)(); _Storage* __obj; };
  108. union {
  109. void* _M_p;
  110. alignas(_Delegate) alignas(void(*)())
  111. unsigned char _M_bytes[sizeof(_Delegate)];
  112. };
  113. };
  114. template<typename _Tp>
  115. static constexpr bool __stored_locally
  116. = sizeof(_Tp) <= sizeof(_Storage) && alignof(_Tp) <= alignof(_Storage)
  117. && is_nothrow_move_constructible_v<_Tp>;
  118. // A function that either destroys the target object stored in __target,
  119. // or moves the target object from *__src to __target.
  120. using _Manager = void (*)(_Storage& __target, _Storage* __src) noexcept;
  121. // The no-op manager function for objects with no target.
  122. static void _S_empty(_Storage&, _Storage*) noexcept { }
  123. // The real manager function for a target object of type _Tp.
  124. template<typename _Tp>
  125. static void
  126. _S_manage(_Storage& __target, _Storage* __src) noexcept
  127. {
  128. if constexpr (__stored_locally<_Tp>)
  129. {
  130. if (__src)
  131. {
  132. _Tp* __rval = static_cast<_Tp*>(__src->_M_addr());
  133. ::new (__target._M_addr()) _Tp(std::move(*__rval));
  134. __rval->~_Tp();
  135. }
  136. else
  137. static_cast<_Tp*>(__target._M_addr())->~_Tp();
  138. }
  139. else
  140. {
  141. if (__src)
  142. __target._M_p = __src->_M_p;
  143. else
  144. delete static_cast<_Tp*>(__target._M_p);
  145. }
  146. }
  147. _Storage _M_storage;
  148. _Manager _M_manage;
  149. };
  150. template<typename _Tp>
  151. inline constexpr bool __is_move_only_function_v = false;
  152. template<typename _Tp>
  153. constexpr bool __is_move_only_function_v<move_only_function<_Tp>> = true;
  154. /// @endcond
  155. _GLIBCXX_END_NAMESPACE_VERSION
  156. } // namespace std
  157. #include "mofunc_impl.h"
  158. #define _GLIBCXX_MOF_CV const
  159. #include "mofunc_impl.h"
  160. #define _GLIBCXX_MOF_REF &
  161. #include "mofunc_impl.h"
  162. #define _GLIBCXX_MOF_REF &&
  163. #include "mofunc_impl.h"
  164. #define _GLIBCXX_MOF_CV const
  165. #define _GLIBCXX_MOF_REF &
  166. #include "mofunc_impl.h"
  167. #define _GLIBCXX_MOF_CV const
  168. #define _GLIBCXX_MOF_REF &&
  169. #include "mofunc_impl.h"
  170. #endif // C++23
  171. #endif // _GLIBCXX_MOVE_ONLY_FUNCTION_H