| 
 | 1 | +/* mbed Microcontroller Library  | 
 | 2 | + * Copyright (c) 2019 ARM Limited  | 
 | 3 | + * SPDX-License-Identifier: Apache-2.0  | 
 | 4 | + *  | 
 | 5 | + * Licensed under the Apache License, Version 2.0 (the "License");  | 
 | 6 | + * you may not use this file except in compliance with the License.  | 
 | 7 | + * You may obtain a copy of the License at  | 
 | 8 | + *  | 
 | 9 | + *     http://www.apache.org/licenses/LICENSE-2.0  | 
 | 10 | + *  | 
 | 11 | + * Unless required by applicable law or agreed to in writing, software  | 
 | 12 | + * distributed under the License is distributed on an "AS IS" BASIS,  | 
 | 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  | 
 | 14 | + * See the License for the specific language governing permissions and  | 
 | 15 | + * limitations under tUNChe License.  | 
 | 16 | + */  | 
 | 17 | +#ifndef MSTD_FUNCTIONAL_  | 
 | 18 | +#define MSTD_FUNCTIONAL_  | 
 | 19 | + | 
 | 20 | +/* <mstd_functional>  | 
 | 21 | + *  | 
 | 22 | + * - includes toolchain's <functional>  | 
 | 23 | + * - For ARM C 5, standard C++11/14 features:  | 
 | 24 | + *   - std::mem_fn,  | 
 | 25 | + *   - std::reference_wrapper, std::ref, std::cref  | 
 | 26 | + *   - transparent std::plus<> etc  | 
 | 27 | + *   - std::bit_and, std::bit_or, std::bit_xor, std::bit_not  | 
 | 28 | + * - For all toolchains, C++17/20 backports:  | 
 | 29 | + *   - mbed::not_fn (C++17)  | 
 | 30 | + *   - mbed::invoke (C++17)  | 
 | 31 | + *   - mstd::unwrap_reference, mstd::unwrap_ref_decay (C++20)  | 
 | 32 | + */  | 
 | 33 | + | 
 | 34 | +#include <functional>  | 
 | 35 | + | 
 | 36 | +#include <mstd_memory> // addressof  | 
 | 37 | +#include <mstd_utility> // forward  | 
 | 38 | +#include <mstd_type_traits>  | 
 | 39 | + | 
 | 40 | +namespace mstd {  | 
 | 41 | + | 
 | 42 | +// [func.invoke]  | 
 | 43 | +#if __cpp_lib_invoke >= 201411  | 
 | 44 | +using std::invoke;  | 
 | 45 | +#else  | 
 | 46 | +template <typename F, typename... Args>  | 
 | 47 | +invoke_result_t<F, Args...> invoke(F&& f, Args&&... args)  | 
 | 48 | +{  | 
 | 49 | +    return impl::INVOKE(std::forward<F>(f), std::forward<Args>(args)...);  | 
 | 50 | +}  | 
 | 51 | +#endif // __cpp_lib_invoke  | 
 | 52 | + | 
 | 53 | +} // namespace mstd  | 
 | 54 | + | 
 | 55 | +namespace mstd {  | 
 | 56 | +using std::reference_wrapper;  | 
 | 57 | +using std::ref;  | 
 | 58 | +using std::cref;  | 
 | 59 | +using std::plus;  | 
 | 60 | +using std::minus;  | 
 | 61 | +using std::multiplies;  | 
 | 62 | +using std::divides;  | 
 | 63 | +using std::modulus;  | 
 | 64 | +using std::negate;  | 
 | 65 | +using std::equal_to;  | 
 | 66 | +using std::not_equal_to;  | 
 | 67 | +using std::greater;  | 
 | 68 | +using std::less;  | 
 | 69 | +using std::greater_equal;  | 
 | 70 | +using std::less_equal;  | 
 | 71 | +using std::logical_and;  | 
 | 72 | +using std::logical_or;  | 
 | 73 | +using std::logical_not;  | 
 | 74 | +using std::bit_and;  | 
 | 75 | +using std::bit_or;  | 
 | 76 | +using std::bit_xor;  | 
 | 77 | +using std::bit_not;  | 
 | 78 | + | 
 | 79 | +#if __cpp_lib_not_fn >= 201603  | 
 | 80 | +using std::not_fn;  | 
 | 81 | +#else  | 
 | 82 | +namespace impl {  | 
 | 83 | +// [func.not_fn]  | 
 | 84 | +template <typename F>  | 
 | 85 | +class not_fn_t {  | 
 | 86 | +    std::decay_t<F> fn;  | 
 | 87 | +public:  | 
 | 88 | +    explicit not_fn_t(F&& f) : fn(std::forward<F>(f)) { }  | 
 | 89 | +    not_fn_t(const not_fn_t &other) = default;  | 
 | 90 | +    not_fn_t(not_fn_t &&other) = default;  | 
 | 91 | + | 
 | 92 | +    template<typename... Args>  | 
 | 93 | +    auto operator()(Args&&... args) & -> decltype(!std::declval<invoke_result_t<std::decay_t<F> &, Args...>>())  | 
 | 94 | +    {  | 
 | 95 | +        return !mstd::invoke(fn, std::forward<Args>(args)...);  | 
 | 96 | +    }  | 
 | 97 | + | 
 | 98 | +    template<typename... Args>  | 
 | 99 | +    auto operator()(Args&&... args) const & -> decltype(!std::declval<invoke_result_t<std::decay_t<F> const &, Args...>>())  | 
 | 100 | +    {  | 
 | 101 | +        return !mstd::invoke(fn, std::forward<Args>(args)...);  | 
 | 102 | +    }  | 
 | 103 | + | 
 | 104 | +    template<typename... Args>  | 
 | 105 | +    auto operator()(Args&&... args) && -> decltype(!std::declval<invoke_result_t<std::decay_t<F>, Args...>>())  | 
 | 106 | +    {  | 
 | 107 | +        return !mstd::invoke(std::move(fn), std::forward<Args>(args)...);  | 
 | 108 | +    }  | 
 | 109 | + | 
 | 110 | +    template<typename... Args>  | 
 | 111 | +    auto operator()(Args&&... args) const && -> decltype(!std::declval<invoke_result_t<std::decay_t<F> const, Args...>>())  | 
 | 112 | +    {  | 
 | 113 | +        return !mstd::invoke(std::move(fn), std::forward<Args>(args)...);  | 
 | 114 | +    }  | 
 | 115 | +};  | 
 | 116 | +}  | 
 | 117 | + | 
 | 118 | +template <typename F>  | 
 | 119 | +impl::not_fn_t<F> not_fn_t(F&& f)  | 
 | 120 | +{  | 
 | 121 | +    return impl::not_fn_t<F>(std::forward<F>(f));  | 
 | 122 | +}  | 
 | 123 | +#endif  | 
 | 124 | + | 
 | 125 | +/* C++20 unwrap_reference */  | 
 | 126 | +template <typename T>  | 
 | 127 | +struct unwrap_reference : type_identity<T> { };  | 
 | 128 | +template <typename T>  | 
 | 129 | +struct unwrap_reference<std::reference_wrapper<T>> : type_identity<T &> { };  | 
 | 130 | +template <typename T>  | 
 | 131 | +using unwrap_reference_t = typename unwrap_reference<T>::type;  | 
 | 132 | + | 
 | 133 | +/* C++20 unwrap_ref_decay */  | 
 | 134 | +template <typename T>  | 
 | 135 | +struct unwrap_ref_decay : unwrap_reference<std::decay_t<T>> { };  | 
 | 136 | +template <typename T>  | 
 | 137 | +using unwrap_ref_decay_t = typename unwrap_ref_decay<T>::type;  | 
 | 138 | + | 
 | 139 | +}  | 
 | 140 | + | 
 | 141 | +#endif // MSTD_FUNCTIONAL_  | 
0 commit comments