/* mbed Microcontroller Library * Copyright (c) 2019 ARM Limited * SPDX-License-Identifier: Apache-2.0 * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under tUNChe License. */ #ifndef MSTD_FUNCTIONAL_ #define MSTD_FUNCTIONAL_ /* * * - includes toolchain's * - For ARM C 5, standard C++11/14 features: * - std::mem_fn, * - std::reference_wrapper, std::ref, std::cref * - transparent std::plus<> etc * - std::bit_and, std::bit_or, std::bit_xor, std::bit_not * - For all toolchains, C++17/20 backports: * - mstd::not_fn (C++17) * - mstd::invoke (C++17) * - mstd::unwrap_reference, mstd::unwrap_ref_decay (C++20) */ #include #include // addressof #include // forward #include #ifdef __CC_ARM namespace std { // [func.memfn] namespace impl { template class mem_fn_t { R T::* pm; public: mem_fn_t(R T::* pm) : pm(pm) { } template invoke_result_t operator()(Args&&... args) const { return std::invoke(pm, std::forward(args)...); } }; } template impl::mem_fn_t mem_fn(R T::* pm) { return impl::mem_fn_t(pm); } } // namespace std #endif // __CC_ARM namespace mstd { // [func.invoke] #if __cpp_lib_invoke >= 201411 using std::invoke; #else template invoke_result_t invoke(F&& f, Args&&... args) { return impl::INVOKE(std::forward(f), std::forward(args)...); } #endif // __cpp_lib_invoke } // namespace mstd #ifdef __CC_ARM namespace std { using mstd::invoke; // [refwrap] template class reference_wrapper { T *ptr; public: using type = T; // [refwrap.const] // LWG 2993 version of constructor does not seem to work in ARM C 5, so stick with // this original version. reference_wrapper(T &x) noexcept : ptr(addressof(x)) { } reference_wrapper(T &&x) = delete; reference_wrapper(const reference_wrapper &) noexcept = default; // [refwrap.assign] reference_wrapper &operator=(const reference_wrapper &) noexcept = default; // [refwrap.access] operator T &() const noexcept { return *ptr; } T &get() const noexcept { return *ptr; } // [refwrap.invoke] template invoke_result_t operator()(ArgTypes&&... args) const { return std::invoke(get(), forward(args)...); } }; // [refwrap.helpers] template reference_wrapper ref(T &t) noexcept { return reference_wrapper(t); } template reference_wrapper ref(reference_wrapper &t) noexcept { return ref(t.get()); } template void ref(const T &&) = delete; template reference_wrapper cref(const T &t) noexcept { return reference_wrapper(t); } template reference_wrapper cref(reference_wrapper &t) noexcept { return cref(t.get()); } template void cref(const T &&) = delete; // ARMC5 has basic plus etc - we can add plus specialisations; // and the language rules allow us to add the default void arguments missing // from its header. template struct plus; template struct minus; template struct multiplies; template struct divides; template struct modulus; template struct negate; template struct equal_to; template struct not_equal_to; template struct greater; template struct less; template struct greater_equal; template struct less_equal; template struct logical_and; template struct logical_or; template struct logical_not; // [arithmetic.operations.plus] template <> struct plus { using is_transparent = true_type; template constexpr auto operator()(T&& t, U&& u) const -> decltype(std::forward(t) + std::forward(u)) { return std::forward(t) + std::forward(u); } }; // [arithmetic.operations.minus] template <> struct minus { using is_transparent = true_type; template constexpr auto operator()(T&& t, U&& u) const -> decltype(std::forward(t) - std::forward(u)) { return std::forward(t) - std::forward(u); } }; // [arithmetic.operations.multiplies] template <> struct multiplies { using is_transparent = true_type; template constexpr auto operator()(T&& t, U&& u) const -> decltype(std::forward(t) * std::forward(u)) { return std::forward(t) * std::forward(u); } }; // [arithmetic.operations.divides] template <> struct divides { using is_transparent = true_type; template constexpr auto operator()(T&& t, U&& u) const -> decltype(std::forward(t) / std::forward(u)) { return std::forward(t) / std::forward(u); } }; // [arithmetic.operations.modulus] template <> struct modulus { using is_transparent = true_type; template constexpr auto operator()(T&& t, U&& u) const -> decltype(std::forward(t) % std::forward(u)) { return std::forward(t) % std::forward(u); } }; // [arithmetic.operations.negate] template <> struct negate { using is_transparent = true_type; template constexpr auto operator()(T&& t) const -> decltype(-std::forward(t)) { return -std::forward(t); } }; // [comparisons.equal_to] template <> struct equal_to { using is_transparent = true_type; template constexpr auto operator()(T&& t, U&& u) const -> decltype(std::forward(t) == std::forward(u)) { return std::forward(t) == std::forward(u); } }; // [comparisons.not_equal_to] template <> struct not_equal_to { using is_transparent = true_type; template constexpr auto operator()(T&& t, U&& u) const -> decltype(std::forward(t) != std::forward(u)) { return std::forward(t) != std::forward(u); } }; // [comparisons.greater] template <> struct greater { using is_transparent = true_type; template constexpr auto operator()(T&& t, U&& u) const -> decltype(std::forward(t) > std::forward(u)) { return std::forward(t) > std::forward(u); } }; // [comparisons.less] template <> struct less { using is_transparent = true_type; template constexpr auto operator()(T&& t, U&& u) const -> decltype(std::forward(t) < std::forward(u)) { return std::forward(t) < std::forward(u); } }; // [comparisons.greater_equal] template <> struct greater_equal { using is_transparent = true_type; template constexpr auto operator()(T&& t, U&& u) const -> decltype(std::forward(t) >= std::forward(u)) { return std::forward(t) >= std::forward(u); } }; // [comparisons.less_equal] template <> struct less_equal { using is_transparent = true_type; template constexpr auto operator()(T&& t, U&& u) const -> decltype(std::forward(t) <= std::forward(u)) { return std::forward(t) <= std::forward(u); } }; // [logical.operations.and] template <> struct logical_and { using is_transparent = true_type; template constexpr auto operator()(T&& t, U&& u) const -> decltype(std::forward(t) && std::forward(u)) { return std::forward(t) && std::forward(u); } }; // [logical.operations.or] template <> struct logical_or { using is_transparent = true_type; template constexpr auto operator()(T&& t, U&& u) const -> decltype(std::forward(t) || std::forward(u)) { return std::forward(t) || std::forward(u); } }; // [logical.operations.not] template <> struct logical_not { using is_transparent = true_type; template constexpr auto operator()(T&& t) const -> decltype(!std::forward(t)) { return !std::forward(t); } }; // [bitwise.operations.and] template struct bit_and { constexpr T operator()(const T &x, const T &y) const { return x & y; } }; template <> struct bit_and { using is_transparent = true_type; template constexpr auto operator()(T&& t,U&& u) const -> decltype(std::forward(t) & std::forward(u)) { return std::forward(t) & std::forward(u); } }; // [bitwise.operations.or] template struct bit_or { constexpr T operator()(const T &x, const T &y) const { return x & y; } }; template <> struct bit_or { using is_transparent = true_type; template constexpr auto operator()(T&& t,U&& u) const -> decltype(std::forward(t) | std::forward(u)) { return std::forward(t) | std::forward(u); } }; // [bitwise.operations.xor] template struct bit_xor { constexpr T operator()(const T &x, const T &y) const { return x ^ y; } }; template <> struct bit_xor { using is_transparent = true_type; template constexpr auto operator()(T&& t,U&& u) const -> decltype(std::forward(t) ^ std::forward(u)) { return std::forward(t) ^ std::forward(u); } }; // [bitwise.operations.not] template struct bit_not { constexpr T operator()(const T &arg) const { return ~arg; } }; template <> struct bit_not { using is_transparent = true_type; template constexpr auto operator()(T&& arg) const -> decltype(~std::forward(arg)) { return ~std::forward(arg); } }; } // namespace std #endif // __CC_ARM namespace mstd { using std::reference_wrapper; using std::ref; using std::cref; using std::plus; using std::minus; using std::multiplies; using std::divides; using std::modulus; using std::negate; using std::equal_to; using std::not_equal_to; using std::greater; using std::less; using std::greater_equal; using std::less_equal; using std::logical_and; using std::logical_or; using std::logical_not; using std::bit_and; using std::bit_or; using std::bit_xor; using std::bit_not; #if __cpp_lib_not_fn >= 201603 using std::not_fn; #else namespace impl { // [func.not_fn] template class not_fn_t { std::decay_t fn; public: explicit not_fn_t(F&& f) : fn(std::forward(f)) { } not_fn_t(const not_fn_t &other) = default; not_fn_t(not_fn_t &&other) = default; template auto operator()(Args&&... args) & -> decltype(!std::declval &, Args...>>()) { return !mstd::invoke(fn, std::forward(args)...); } template auto operator()(Args&&... args) const & -> decltype(!std::declval const &, Args...>>()) { return !mstd::invoke(fn, std::forward(args)...); } template auto operator()(Args&&... args) && -> decltype(!std::declval, Args...>>()) { return !mstd::invoke(std::move(fn), std::forward(args)...); } template auto operator()(Args&&... args) const && -> decltype(!std::declval const, Args...>>()) { return !mstd::invoke(std::move(fn), std::forward(args)...); } }; } template impl::not_fn_t not_fn(F&& f) { return impl::not_fn_t(std::forward(f)); } #endif /* C++20 unwrap_reference */ template struct unwrap_reference : type_identity { }; template struct unwrap_reference> : type_identity { }; template using unwrap_reference_t = typename unwrap_reference::type; /* C++20 unwrap_ref_decay */ template struct unwrap_ref_decay : unwrap_reference> { }; template using unwrap_ref_decay_t = typename unwrap_ref_decay::type; } #endif // MSTD_FUNCTIONAL_