//////////////////////////////////////////////////////////////////////////////// /// \file functional.hpp /// /// \brief This header provides definitions from the C++ header //////////////////////////////////////////////////////////////////////////////// /* 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_FUNCTIONAL_HPP #define BPSTD_FUNCTIONAL_HPP #if defined(_MSC_VER) && (_MSC_VER >= 1200) # pragma once #endif // defined(_MSC_VER) && (_MSC_VER >= 1200) #include "detail/config.hpp" #include "type_traits.hpp" #include "utility.hpp" #include "detail/invoke.hpp" #include // to proxy API BPSTD_COMPILER_DIAGNOSTIC_PREAMBLE namespace bpstd { /// \brief Invoke the Callable object \p function with the parameters \p args. /// /// As by \c INVOKE(std::forward(f), std::forward(args)...) /// /// \param function Callable object to be invoked /// \param args arguments to pass to \p function template constexpr auto invoke(Func &&function, Args &&...args) noexcept(is_nothrow_invocable::value) -> invoke_result_t; namespace detail { template struct not_fn_t { Fn fn; template inline BPSTD_INLINE_VISIBILITY BPSTD_CPP14_CONSTEXPR auto operator()(Args &&...args) & noexcept(noexcept(!::bpstd::invoke(fn, ::bpstd::forward(args)...))) -> decltype(!::bpstd::invoke(fn, ::bpstd::forward(args)...)) { return !::bpstd::invoke(fn, bpstd::forward(args)...); } template inline BPSTD_INLINE_VISIBILITY BPSTD_CPP14_CONSTEXPR auto operator()(Args&&... args) && noexcept(noexcept(!::bpstd::invoke(std::move(fn), ::bpstd::forward(args)...))) -> decltype(!::bpstd::invoke(std::move(fn), ::bpstd::forward(args)...)) { return !::bpstd::invoke(std::move(fn), bpstd::forward(args)...); } template inline BPSTD_INLINE_VISIBILITY constexpr auto operator()(Args&&... args) const& noexcept(noexcept(!::bpstd::invoke(fn, ::bpstd::forward(args)...))) -> decltype(!::bpstd::invoke(fn, ::bpstd::forward(args)...)) { return !::bpstd::invoke(fn, bpstd::forward(args)...); } template inline BPSTD_INLINE_VISIBILITY constexpr auto operator()(Args&&... args) const&& noexcept(noexcept(!::bpstd::invoke(std::move(fn), ::bpstd::forward(args)...))) -> decltype(!::bpstd::invoke(std::move(fn), ::bpstd::forward(args)...)) { return !::bpstd::invoke(std::move(fn), bpstd::forward(args)...); } }; } // namespace detail /// \brief Creates a forwarding call wrapper that returns the negation of the /// callable object it holds. /// /// \param fn the object from which the Callable object held by the wrapper /// is constructed /// \return the negated object template constexpr auto not_fn(Fn &&fn) -> detail::not_fn_t>; //============================================================================ // struct : plus //============================================================================ template struct plus { inline BPSTD_INLINE_VISIBILITY constexpr auto operator()(const T &lhs, const T &rhs) const -> T { return lhs + rhs; } }; template <> struct plus { using is_transparent = true_type; template inline BPSTD_INLINE_VISIBILITY constexpr auto operator()(T&& lhs, U&& rhs) const -> decltype(::bpstd::forward(lhs) + ::bpstd::forward(rhs)) { return bpstd::forward(lhs) + bpstd::forward(rhs); } }; //============================================================================ // struct : minus //============================================================================ template struct minus { inline BPSTD_INLINE_VISIBILITY constexpr auto operator()(const T &lhs, const T &rhs) const -> T { return lhs - rhs; } }; template <> struct minus { using is_transparent = true_type; template inline BPSTD_INLINE_VISIBILITY constexpr auto operator()(T&& lhs, U&& rhs) const -> decltype(::bpstd::forward(lhs) - ::bpstd::forward(rhs)) { return bpstd::forward(lhs) - bpstd::forward(rhs); } }; //============================================================================ // struct : multiplies //============================================================================ template struct multiplies { inline BPSTD_INLINE_VISIBILITY constexpr auto operator()(const T &lhs, const T &rhs) const -> T { return lhs * rhs; } }; template <> struct multiplies { using is_transparent = true_type; template inline BPSTD_INLINE_VISIBILITY constexpr auto operator()(T&& lhs, U&& rhs) const -> decltype(::bpstd::forward(lhs) * ::bpstd::forward(rhs)) { return bpstd::forward(lhs) * bpstd::forward(rhs); } }; //============================================================================ // struct : divides //============================================================================ template struct divides { inline BPSTD_INLINE_VISIBILITY constexpr auto operator()(const T &lhs, const T &rhs) const -> T { return lhs / rhs; } }; template <> struct divides { using is_transparent = true_type; template inline BPSTD_INLINE_VISIBILITY constexpr auto operator()(T&& lhs, U&& rhs) const -> decltype(::bpstd::forward(lhs) / ::bpstd::forward(rhs)) { return bpstd::forward(lhs) / bpstd::forward(rhs); } }; //============================================================================ // struct : modulus //============================================================================ template struct modulus { inline BPSTD_INLINE_VISIBILITY constexpr auto operator()(const T &lhs, const T &rhs) const -> T { return lhs % rhs; } }; template <> struct modulus { using is_transparent = true_type; template inline BPSTD_INLINE_VISIBILITY constexpr auto operator()(T&& lhs, U&& rhs) const -> decltype(::bpstd::forward(lhs) % ::bpstd::forward(rhs)) { return bpstd::forward(lhs) % bpstd::forward(rhs); } }; //============================================================================ // struct : negate //============================================================================ template struct negate { inline BPSTD_INLINE_VISIBILITY constexpr auto operator()(const T &arg) const -> T { return -arg; } }; template <> struct negate { using is_transparent = true_type; template inline BPSTD_INLINE_VISIBILITY constexpr auto operator()(T&& arg) const -> decltype(-::bpstd::forward(arg)) { return -bpstd::forward(arg); } }; //============================================================================ // struct : equal_to //============================================================================ template struct equal_to { inline BPSTD_INLINE_VISIBILITY constexpr auto operator()(const T &lhs, const T &rhs) const -> bool { return lhs == rhs; } }; template <> struct equal_to { using is_transparent = true_type; template inline BPSTD_INLINE_VISIBILITY constexpr auto operator()(T&& lhs, U&& rhs) const -> decltype(::bpstd::forward(lhs) == ::bpstd::forward(rhs)) { return bpstd::forward(lhs) == bpstd::forward(rhs); } }; //============================================================================ // struct : not_equal_to //============================================================================ template struct not_equal_to { inline BPSTD_INLINE_VISIBILITY constexpr auto operator()(const T &lhs, const T &rhs) const -> bool { return lhs != rhs; } }; template <> struct not_equal_to { using is_transparent = true_type; template inline BPSTD_INLINE_VISIBILITY constexpr auto operator()(T&& lhs, U&& rhs) const -> decltype(::bpstd::forward(lhs) != ::bpstd::forward(rhs)) { return bpstd::forward(lhs) != bpstd::forward(rhs); } }; //============================================================================ // struct : greater //============================================================================ template struct greater { inline BPSTD_INLINE_VISIBILITY constexpr auto operator()(const T &lhs, const T &rhs) const -> bool { return lhs > rhs; } }; template <> struct greater { using is_transparent = true_type; template inline BPSTD_INLINE_VISIBILITY constexpr auto operator()(T&& lhs, U&& rhs) const -> decltype(::bpstd::forward(lhs) > ::bpstd::forward(rhs)) { return bpstd::forward(lhs) > bpstd::forward(rhs); } }; //============================================================================ // struct : greater_equal //============================================================================ template struct greater_equal { inline BPSTD_INLINE_VISIBILITY constexpr auto operator()(const T &lhs, const T &rhs) const -> bool { return lhs >= rhs; } }; template <> struct greater_equal { using is_transparent = true_type; template inline BPSTD_INLINE_VISIBILITY constexpr auto operator()(T&& lhs, U&& rhs) const -> decltype(::bpstd::forward(lhs) >= ::bpstd::forward(rhs)) { return bpstd::forward(lhs) >= bpstd::forward(rhs); } }; //============================================================================ // struct : less //============================================================================ template struct less { inline BPSTD_INLINE_VISIBILITY constexpr auto operator()(const T &lhs, const T &rhs) const -> bool { return lhs < rhs; } }; template <> struct less { using is_transparent = true_type; template inline BPSTD_INLINE_VISIBILITY constexpr auto operator()(T&& lhs, U&& rhs) const -> decltype(::bpstd::forward(lhs) < ::bpstd::forward(rhs)) { return bpstd::forward(lhs) < bpstd::forward(rhs); } }; //============================================================================ // struct : less_equal //============================================================================ template struct less_equal { inline BPSTD_INLINE_VISIBILITY constexpr auto operator()(const T &lhs, const T &rhs) const -> bool { return lhs <= rhs; } }; template <> struct less_equal { using is_transparent = true_type; template inline BPSTD_INLINE_VISIBILITY constexpr auto operator()(T&& lhs, U&& rhs) const -> decltype(::bpstd::forward(lhs) <= ::bpstd::forward(rhs)) { return bpstd::forward(lhs) <= bpstd::forward(rhs); } }; //============================================================================ // struct : logical_and //============================================================================ template struct logical_and { inline BPSTD_INLINE_VISIBILITY constexpr auto operator()(const T &lhs, const T &rhs) const -> bool { return lhs && rhs; } }; template <> struct logical_and { using is_transparent = true_type; template inline BPSTD_INLINE_VISIBILITY constexpr auto operator()(T&& lhs, U&& rhs) const -> decltype(::bpstd::forward(lhs) && ::bpstd::forward(rhs)) { return bpstd::forward(lhs) && bpstd::forward(rhs); } }; //============================================================================ // struct : logical_or //============================================================================ template struct logical_or { inline BPSTD_INLINE_VISIBILITY constexpr auto operator()(const T &lhs, const T &rhs) const -> bool { return lhs || rhs; } }; template <> struct logical_or { using is_transparent = true_type; template inline BPSTD_INLINE_VISIBILITY constexpr auto operator()(T&& lhs, U&& rhs) const -> decltype(::bpstd::forward(lhs) || ::bpstd::forward(rhs)) { return bpstd::forward(lhs) || bpstd::forward(rhs); } }; //============================================================================ // struct : logical_not //============================================================================ template struct logical_not { inline BPSTD_INLINE_VISIBILITY constexpr auto operator()(const T &arg) const -> bool { return !arg; } }; template <> struct logical_not { using is_transparent = true_type; template inline BPSTD_INLINE_VISIBILITY constexpr auto operator()(T&& arg) const -> decltype(!::bpstd::forward(arg)) { return !bpstd::forward(arg); } }; //============================================================================ // struct : bit_and //============================================================================ template struct bit_and { inline BPSTD_INLINE_VISIBILITY constexpr auto operator()(const T &lhs, const T &rhs) const -> T { return lhs & rhs; } }; template <> struct bit_and { using is_transparent = true_type; template inline BPSTD_INLINE_VISIBILITY constexpr auto operator()(T&& lhs, U&& rhs) const -> decltype(::bpstd::forward(lhs) & ::bpstd::forward(rhs)) { return bpstd::forward(lhs) & bpstd::forward(rhs); } }; //============================================================================ // struct : bit_or //============================================================================ template struct bit_or { inline BPSTD_INLINE_VISIBILITY constexpr auto operator()(const T &lhs, const T &rhs) const -> T { return lhs | rhs; } }; template <> struct bit_or { using is_transparent = true_type; template inline BPSTD_INLINE_VISIBILITY constexpr auto operator()(T&& lhs, U&& rhs) const -> decltype(::bpstd::forward(lhs) | ::bpstd::forward(rhs)) { return bpstd::forward(lhs) | bpstd::forward(rhs); } }; //============================================================================ // struct : bit_xor //============================================================================ template struct bit_xor { inline BPSTD_INLINE_VISIBILITY constexpr auto operator()(const T &lhs, const T &rhs) const -> T { return lhs ^ rhs; } }; template <> struct bit_xor { using is_transparent = true_type; template inline BPSTD_INLINE_VISIBILITY constexpr auto operator()(T&& lhs, U&& rhs) const -> decltype(::bpstd::forward(lhs) ^ ::bpstd::forward(rhs)) { return bpstd::forward(lhs) ^ bpstd::forward(rhs); } }; //============================================================================ // struct : bit_not //============================================================================ template struct bit_not { inline BPSTD_INLINE_VISIBILITY constexpr auto operator()(const T &arg) const -> T { return ~arg; } }; template <> struct bit_not { using is_transparent = true_type; template inline BPSTD_INLINE_VISIBILITY constexpr auto operator()(T&& arg) const -> decltype(~::bpstd::forward(arg)) { return ~bpstd::forward(arg); } }; } // namespace bpstd //============================================================================== // definition : invoke //============================================================================== template inline BPSTD_INLINE_VISIBILITY constexpr auto bpstd::invoke(Func &&function, Args &&...args) noexcept(is_nothrow_invocable::value) -> bpstd::invoke_result_t { return detail::INVOKE(bpstd::forward(function), bpstd::forward(args)...); } //============================================================================== // definition : not_fn //============================================================================== template inline BPSTD_INLINE_VISIBILITY constexpr auto bpstd::not_fn(Fn &&fn) -> bpstd::detail::not_fn_t> { static_assert( is_move_constructible::value, "Fn must be move constructible" ); static_assert( is_constructible,Fn>::value, "Fn must be constructible from an instance of fn" ); return { bpstd::forward(fn) }; } BPSTD_COMPILER_DIAGNOSTIC_POSTAMBLE #endif /* BPSTD_FUNCTIONAL_HPP */