//////////////////////////////////////////////////////////////////////////////// /// \file invoke.hpp /// /// \brief This internal header provides the definition of the INVOKE overload //////////////////////////////////////////////////////////////////////////////// /* The MIT License (MIT) Copyright (c) 2020 Matthew Rodusek All rights reserved. Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ #ifndef BPSTD_DETAIL_INVOKE_HPP #define BPSTD_DETAIL_INVOKE_HPP #if defined(_MSC_VER) && (_MSC_VER >= 1200) # pragma once #endif // defined(_MSC_VER) && (_MSC_VER >= 1200) #include "config.hpp" // BPSTD_INLINE_VISIBILITY #include "move.hpp" // forward #include // std::true_type, std::false_type, etc #include // std::reference_wrapper #include BPSTD_COMPILER_DIAGNOSTIC_PREAMBLE namespace bpstd { namespace detail { template struct is_reference_wrapper : std::false_type {}; template struct is_reference_wrapper> : std::true_type {}; template inline BPSTD_INLINE_VISIBILITY constexpr auto INVOKE(T Base::*pmf, Derived&& ref, Args&&... args) noexcept(noexcept((::bpstd::forward(ref).*pmf)(::bpstd::forward(args)...))) -> typename std::enable_if::value && std::is_base_of::type>::value, decltype((::bpstd::forward(ref).*pmf)(::bpstd::forward(args)...))>::type { return (bpstd::forward(ref).*pmf)(bpstd::forward(args)...); } template inline BPSTD_INLINE_VISIBILITY constexpr auto INVOKE(T Base::*pmf, RefWrap&& ref, Args&&... args) noexcept(noexcept((ref.get().*pmf)(std::forward(args)...))) -> typename std::enable_if::value && is_reference_wrapper::type>::value, decltype((ref.get().*pmf)(::bpstd::forward(args)...))>::type { return (ref.get().*pmf)(bpstd::forward(args)...); } template inline BPSTD_INLINE_VISIBILITY constexpr auto INVOKE(T Base::*pmf, Pointer&& ptr, Args&&... args) noexcept(noexcept(((*std::forward(ptr)).*pmf)(std::forward(args)...))) -> typename std::enable_if::value && !is_reference_wrapper::type>::value && !std::is_base_of::type>::value, decltype(((*::bpstd::forward(ptr)).*pmf)(::bpstd::forward(args)...))>::type { return ((*bpstd::forward(ptr)).*pmf)(bpstd::forward(args)...); } template inline BPSTD_INLINE_VISIBILITY constexpr auto INVOKE(T Base::*pmd, Derived&& ref) noexcept(noexcept(std::forward(ref).*pmd)) -> typename std::enable_if::value && std::is_base_of::type>::value, decltype(::bpstd::forward(ref).*pmd)>::type { return bpstd::forward(ref).*pmd; } template inline BPSTD_INLINE_VISIBILITY constexpr auto INVOKE(T Base::*pmd, RefWrap&& ref) noexcept(noexcept(ref.get().*pmd)) -> typename std::enable_if::value && is_reference_wrapper::type>::value, decltype(ref.get().*pmd)>::type { return ref.get().*pmd; } template inline BPSTD_INLINE_VISIBILITY constexpr auto INVOKE(T Base::*pmd, Pointer&& ptr) noexcept(noexcept((*std::forward(ptr)).*pmd)) -> typename std::enable_if::value && !is_reference_wrapper::type>::value && !std::is_base_of::type>::value, decltype((*::bpstd::forward(ptr)).*pmd)>::type { return (*bpstd::forward(ptr)).*pmd; } template inline BPSTD_INLINE_VISIBILITY constexpr auto INVOKE(F&& f, Args&&... args) noexcept(noexcept(std::forward(f)(std::forward(args)...))) -> typename std::enable_if::type>::value, decltype(::bpstd::forward(f)(::bpstd::forward(args)...))>::type { return bpstd::forward(f)(bpstd::forward(args)...); } //========================================================================== // is_nothrow_invocable //========================================================================== template struct is_nothrow_invocable { template static auto test( Fn2&&, Args2&&... ) -> decltype(INVOKE(std::declval(), std::declval()...), std::integral_constant(), std::declval()...))>{}); static auto test(...) -> std::false_type; using type = decltype(test(std::declval(), std::declval()...)); static constexpr bool value = type::value; }; //========================================================================== // is_invocable //========================================================================== template struct is_invocable { template static auto test( Fn2&&, Args2&&... ) -> decltype(INVOKE(std::declval(), std::declval()...), std::true_type{}); static auto test(...) -> std::false_type; using type = decltype(test(std::declval(), std::declval()...)); static constexpr bool value = type::value; }; // Used to SFINAE away non-invocable types template struct invoke_result_impl{}; template struct invoke_result_impl{ using type = decltype(INVOKE(std::declval(), std::declval()...)); }; template struct invoke_result : invoke_result_impl::value, Fn, Args...>{}; } // namespace detail } // namespace bpstd BPSTD_COMPILER_DIAGNOSTIC_POSTAMBLE #endif /* BPSTD_DETAIL_INVOKE_HPP */