functional 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  1. #ifndef __GBLIBCPP_FUNCTIONAL__
  2. #define __GBLIBCPP_FUNCTIONAL__
  3. #include <bits/fwd_functional>
  4. #include <cstddef>
  5. #include <memory>
  6. #include <new>
  7. #include <utility>
  8. #include <type_traits>
  9. namespace std {
  10. template <typename Func, typename... Args>
  11. constexpr auto invoke(Func&& func, Args&&... args)
  12. noexcept(std::is_nothrow_invocable_v<Func, Args...>)
  13. -> std::enable_if_t<std::is_invocable_v<Func, Args...>,
  14. std::invoke_result_t<Func, Args...>>
  15. {
  16. return __helpers::INVOKE(std::forward<Func>(func), std::forward<Args>(args)...);
  17. }
  18. template <typename R, typename Func, typename... Args>
  19. constexpr auto invoke_r(Func&& func, Args&&... args)
  20. noexcept(std::is_nothrow_invocable_r_v<R, Func, Args...>)
  21. -> std::enable_if_t<std::is_invocable_r_v<R, Func, Args...>, R>
  22. {
  23. return __helpers::INVOKE_R<R>(std::forward<Func>(func), std::forward<Args>(args)...);
  24. }
  25. namespace __helpers {
  26. template <typename T> constexpr void __reference_wrapper_check(T&) noexcept {}
  27. template <typename T> void __reference_wrapper_check(T&&) = delete;
  28. } // namespace __helpers
  29. template <typename T>
  30. class reference_wrapper {
  31. private:
  32. T* _ptr;
  33. template <typename Ref, typename =
  34. decltype(__helpers::__reference_wrapper_check<T>(std::declval<Ref>()))>
  35. struct __check_reference_valid
  36. : public bool_constant<!std::is_same_v<reference_wrapper, std::decay_t<Ref>>> {};
  37. public:
  38. using type = T;
  39. constexpr T& get(void) const noexcept { return *_ptr; }
  40. constexpr operator T&(void) const noexcept { return *_ptr; }
  41. template <typename U, std::enable_if_t<__check_reference_valid<U>::value, bool> = true>
  42. constexpr reference_wrapper(U&& ref)
  43. noexcept(noexcept(__helpers::__reference_wrapper_check<T>(std::declval<U>())))
  44. : _ptr(std::addressof(ref)) {}
  45. constexpr reference_wrapper(const reference_wrapper& val) noexcept
  46. {
  47. _ptr = val._ptr;
  48. }
  49. constexpr reference_wrapper& operator=(const reference_wrapper& val) noexcept = default;
  50. // TODO: std::invoke_result_t
  51. template <typename... Args>
  52. constexpr invoke_result_t<type&, Args...>
  53. operator()(Args&&... args) const noexcept(is_nothrow_invocable_v<type&, Args...>)
  54. {
  55. return std::invoke(get(), std::forward<Args>(args)...);
  56. }
  57. };
  58. template <typename T>
  59. reference_wrapper(T&) -> reference_wrapper<T>;
  60. namespace __inner {
  61. template <typename Ret, typename... Args>
  62. class _function_base {
  63. public:
  64. virtual constexpr ~_function_base() = default;
  65. virtual constexpr Ret operator()(Args... args) const = 0;
  66. virtual constexpr void copy_to(_function_base* func) = 0;
  67. virtual constexpr void move_to(_function_base* func) = 0;
  68. virtual explicit constexpr operator bool(void) const = 0;
  69. };
  70. template <typename FuncLike, typename Ret, typename... Args>
  71. class _function : public _function_base<Ret, Args...> {
  72. private:
  73. using __enable = std::enable_if_t<
  74. std::is_same_v<Ret,
  75. decltype(std::declval<std::decay_t<FuncLike>>()(std::declval<Args>()...))>
  76. >;
  77. FuncLike func;
  78. public:
  79. constexpr _function(const FuncLike& _func) : func(_func) {}
  80. constexpr _function(FuncLike&& _func) : func(std::move(_func)) {}
  81. constexpr Ret operator()(Args... args) const override
  82. {
  83. return std::invoke(func, std::forward<Args>(args)...);
  84. }
  85. constexpr void copy_to(_function_base<Ret, Args...>* dst) override
  86. {
  87. new (reinterpret_cast<_function*>(dst)) _function(*this);
  88. }
  89. constexpr void move_to(_function_base<Ret, Args...>* dst) override
  90. {
  91. new (reinterpret_cast<_function*>(dst)) _function(std::move(*this));
  92. }
  93. constexpr explicit operator bool(void) const override
  94. {
  95. return true;
  96. }
  97. };
  98. template <typename Ret, typename... Args>
  99. class _function<nullptr_t, Ret, Args...> : public _function_base<Ret, Args...> {
  100. public:
  101. [[noreturn]] Ret operator()(Args...) const override
  102. {
  103. // TODO: exception
  104. *((int*)(100^100)) = 1; // triggers access to null pointer
  105. for (;;);
  106. }
  107. constexpr void copy_to(_function_base<Ret, Args...>* dst) override
  108. {
  109. new (reinterpret_cast<_function*>(dst)) _function(*this);
  110. }
  111. constexpr void move_to(_function_base<Ret, Args...>* dst) override
  112. {
  113. new (reinterpret_cast<_function*>(dst)) _function(std::move(*this));
  114. }
  115. constexpr explicit operator bool(void) const override
  116. {
  117. return false;
  118. }
  119. };
  120. } // namespace __inner
  121. template <typename Ret, typename... Args>
  122. class function<Ret(Args...)> {
  123. public:
  124. using result_type = Ret;
  125. private:
  126. char _data[sizeof(void*) * 2];
  127. using fb_t = __inner::_function_base<Ret, Args...>;
  128. constexpr fb_t* _f(void) const
  129. {
  130. return (fb_t*)_data;
  131. }
  132. constexpr void _clear(void)
  133. {
  134. _f()->~_function_base();
  135. }
  136. public:
  137. constexpr function() noexcept
  138. {
  139. new (_f()) __inner::_function<nullptr_t, Ret, Args...>();
  140. }
  141. constexpr function(nullptr_t) noexcept : function() {}
  142. constexpr function(const function& func)
  143. {
  144. func._f()->copy_to(_f());
  145. }
  146. constexpr function(function&& func) noexcept
  147. {
  148. func._f()->move_to(_f());
  149. }
  150. template <typename FuncLike, std::enable_if_t
  151. <
  152. std::is_same_v<
  153. Ret,
  154. decltype(std::declval<std::decay_t<FuncLike>>()(std::declval<Args>()...))
  155. >
  156. && (sizeof(std::decay_t<FuncLike>) <= sizeof(_data))
  157. && !std::is_same_v<std::decay_t<FuncLike>, function>
  158. , bool> = true
  159. >
  160. constexpr function(FuncLike&& func)
  161. {
  162. new (_f()) __inner::_function<std::decay_t<FuncLike>, Ret, Args...>(std::forward<FuncLike>(func));
  163. }
  164. constexpr ~function()
  165. {
  166. _clear();
  167. }
  168. constexpr Ret operator()(Args... args) const
  169. {
  170. return (*_f())(std::forward<Args>(args)...);
  171. }
  172. explicit operator bool(void) const noexcept
  173. {
  174. return !!*_f();
  175. }
  176. function& operator=(function&& rhs)
  177. {
  178. _clear();
  179. rhs._f()->move_to(_f());
  180. return *this;
  181. }
  182. void swap(function& other) noexcept
  183. {
  184. function tmp(std::move(other));
  185. other = std::move(*this);
  186. *this = std::move(tmp);
  187. }
  188. function& operator=(const function& rhs)
  189. {
  190. _clear();
  191. rhs._f()->copy_to(_f());
  192. return *this;
  193. }
  194. function& operator=(nullptr_t) noexcept
  195. {
  196. _clear();
  197. new (_f()) __inner::_function<nullptr_t, Ret, Args...>();
  198. return *this;
  199. }
  200. template <typename FuncLike, std::enable_if_t
  201. <
  202. std::is_same_v<
  203. Ret,
  204. decltype(std::declval<std::decay_t<FuncLike>>()(std::declval<Args>()...))
  205. >
  206. && (sizeof(std::decay_t<FuncLike>) <= sizeof(_data))
  207. && !std::is_same_v<std::decay_t<FuncLike>, function>
  208. , bool> = true
  209. >
  210. function& operator=(FuncLike&& func)
  211. {
  212. function{std::forward<FuncLike>(func)}.swap(*this);
  213. return *this;
  214. }
  215. template <typename FuncLike>
  216. function& operator=(std::reference_wrapper<FuncLike> func) noexcept
  217. {
  218. function{func}.swap(*this);
  219. return *this;
  220. }
  221. };
  222. template <typename Ret, typename... Args>
  223. bool operator==(const std::function<Ret(Args...)>& func, std::nullptr_t) noexcept
  224. {
  225. return !func;
  226. }
  227. template <typename Ret, typename... Args>
  228. void swap(std::function<Ret(Args...)>& lhs, std::function<Ret(Args...)>& rhs) noexcept
  229. {
  230. return lhs.swap(rhs);
  231. }
  232. template <typename T>
  233. constexpr std::reference_wrapper<T> ref(T& t) noexcept
  234. {
  235. return std::reference_wrapper<T>((t));
  236. }
  237. template <typename T>
  238. constexpr std::reference_wrapper<T>
  239. ref(std::reference_wrapper<T> t) noexcept
  240. {
  241. return t;
  242. }
  243. template <typename T>
  244. constexpr std::reference_wrapper<const T> cref(const T& t) noexcept
  245. {
  246. return std::reference_wrapper<const T>((t));
  247. }
  248. template <typename T>
  249. constexpr std::reference_wrapper<const T>
  250. cref(std::reference_wrapper<T> t) noexcept
  251. {
  252. return t;
  253. }
  254. // Comparators
  255. template <typename T = void>
  256. struct less {
  257. constexpr bool operator()(const T& lhs, const T& rhs) const
  258. { return lhs < rhs; }
  259. };
  260. template <>
  261. struct less<void> {
  262. template <typename T, typename U>
  263. constexpr auto operator()(T&& lhs, U&& rhs) const
  264. -> decltype(std::forward<T>(lhs) < std::forward<U>(rhs))
  265. { return std::forward<T>(lhs) < std::forward<U>(rhs); }
  266. };
  267. } // namespace std
  268. #endif