///\file /****************************************************************************** The MIT License(MIT) Embedded Template Library. https://github.com/ETLCPP/etl https://www.etlcpp.com Copyright(c) 2014 John Wellbelove 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 ETL_FUNCTIONAL_INCLUDED #define ETL_FUNCTIONAL_INCLUDED #include "platform.h" #include "utility.h" ///\defgroup functional functional ///\ingroup utilities ///\defgroup reference_wrapper reference_wrapper ///\ingroup functional namespace etl { //*************************************************************************** /// A definition of reference_wrapper for those that don't have C++11 support. ///\ingroup reference //*************************************************************************** template class reference_wrapper { public: typedef T type; ETL_CONSTEXPR20 explicit reference_wrapper(T& t_) ETL_NOEXCEPT : t(&t_) { } ETL_CONSTEXPR20 reference_wrapper(const reference_wrapper& rhs) ETL_NOEXCEPT : t(rhs.t) { } ETL_CONSTEXPR20 reference_wrapper& operator=(const reference_wrapper& rhs) ETL_NOEXCEPT { t = rhs.t; return *this; } ETL_CONSTEXPR20 T& get() const ETL_NOEXCEPT { return *t; } ETL_CONSTEXPR20 operator T&() const ETL_NOEXCEPT { return *t; } #if ETL_USING_CPP11 // implementation without etl::invoke, which would add a circular dependency template ETL_CONSTEXPR20 auto operator()(TArgs&&... args) const noexcept(noexcept(etl::declval()(etl::declval()...))) -> decltype(etl::declval()(etl::declval()...)) { return get()(etl::forward(args)...); } #endif private: T* t; }; //*************************************************************************** template reference_wrapper ref(T& t) { return reference_wrapper(t); } //*************************************************************************** template reference_wrapper ref(reference_wrapper t) { return reference_wrapper(t.get()); } //*************************************************************************** template reference_wrapper cref(const T& t) { return reference_wrapper(t); } //*************************************************************************** template reference_wrapper cref(reference_wrapper t) { return reference_wrapper(t.get()); } //*************************************************************************** /// unwrap_reference. //*************************************************************************** template struct unwrap_reference { typedef T type; }; template struct unwrap_reference > { typedef T& type; }; #if ETL_USING_CPP11 template using unwrap_reference_t = typename unwrap_reference::type; #endif //*************************************************************************** /// unwrap_ref_decay. //*************************************************************************** template struct unwrap_ref_decay : etl::unwrap_reference::type> { }; #if ETL_USING_CPP11 template using unwrap_ref_decay_t = typename unwrap_ref_decay::type; #endif //*************************************************************************** // is_reference_wrapper // Detect etl::reference_wrapper //*************************************************************************** template struct is_reference_wrapper : etl::false_type { }; template struct is_reference_wrapper > : etl::true_type { }; #if ETL_USING_CPP17 template inline constexpr bool is_reference_wrapper_v = is_reference_wrapper::value; #endif //*************************************************************************** /// unary_function //*************************************************************************** template struct unary_function { typedef TArgumentType argument_type; typedef TResultType result_type; }; //*************************************************************************** /// binary_function //*************************************************************************** template struct binary_function { typedef TFirstArgumentType first_argument_type; typedef TSecondArgumentType second_argument_type; typedef TResultType result_type; }; //*************************************************************************** template struct less : public etl::binary_function { typedef T value_type; ETL_CONSTEXPR bool operator()(const T& lhs, const T& rhs) const { return (lhs < rhs); } }; #if ETL_USING_CPP11 template <> struct less : public etl::binary_function { typedef int is_transparent; template constexpr auto operator()(T1&& lhs, T2&& rhs) const -> decltype(static_cast(lhs) < static_cast(rhs)) { return static_cast(lhs) < static_cast(rhs); } }; #endif //*************************************************************************** template struct less_equal : public etl::binary_function { typedef T value_type; ETL_CONSTEXPR bool operator()(const T& lhs, const T& rhs) const { return !(rhs < lhs); } }; #if ETL_USING_CPP11 template <> struct less_equal : public etl::binary_function { typedef int is_transparent; template constexpr auto operator()(T1&& lhs, T2&& rhs) const -> decltype(!(static_cast(rhs) < static_cast(lhs))) { return !(static_cast(rhs) < static_cast(lhs)); } }; #endif //*************************************************************************** template struct greater : public etl::binary_function { typedef T value_type; ETL_CONSTEXPR bool operator()(const T& lhs, const T& rhs) const { return (rhs < lhs); } }; #if ETL_USING_CPP11 template <> struct greater : public etl::binary_function { typedef int is_transparent; template constexpr auto operator()(T1&& lhs, T2&& rhs) const -> decltype(static_cast(rhs) < static_cast(lhs)) { return static_cast(rhs) < static_cast(lhs); } }; #endif //*************************************************************************** template struct greater_equal : public etl::binary_function { typedef T value_type; ETL_CONSTEXPR bool operator()(const T& lhs, const T& rhs) const { return !(lhs < rhs); } }; #if ETL_USING_CPP11 template <> struct greater_equal : public etl::binary_function { typedef int is_transparent; template constexpr auto operator()(T1&& lhs, T2&& rhs) const -> decltype(!(static_cast(lhs) < static_cast(rhs))) { return !(static_cast(lhs) < static_cast(rhs)); } }; #endif //*************************************************************************** template struct equal_to : public etl::binary_function { typedef T value_type; ETL_CONSTEXPR bool operator()(const T& lhs, const T& rhs) const { return lhs == rhs; } }; #if ETL_USING_CPP11 template <> struct equal_to : public etl::binary_function { typedef void value_type; typedef int is_transparent; template constexpr auto operator()(T1&& lhs, T2&& rhs) const -> decltype(static_cast(lhs) == static_cast(rhs)) { return static_cast(lhs) == static_cast(rhs); } }; #endif //*************************************************************************** template struct not_equal_to : public etl::binary_function { typedef T value_type; ETL_CONSTEXPR bool operator()(const T& lhs, const T& rhs) const { return !(lhs == rhs); } }; #if ETL_USING_CPP11 template <> struct not_equal_to : public etl::binary_function { typedef int is_transparent; template constexpr auto operator()(T1&& lhs, T2&& rhs) const -> decltype(!(static_cast(lhs) == static_cast(rhs))) { return !(static_cast(lhs) == static_cast(rhs)); } }; #endif //*************************************************************************** template class binder1st : public etl::unary_function { protected: TFunction operation; typename TFunction::first_argument_type value; public: binder1st(const TFunction& f, const typename TFunction::first_argument_type& v) : operation(f) , value(v) { } typename TFunction::result_type operator()(typename TFunction::second_argument_type& x) const { return operation(value, x); } typename TFunction::result_type operator()(const typename TFunction::second_argument_type& x) const { return operation(value, x); } }; template binder1st bind1st(const F& f, const T& x) { return binder1st(f, x); } //*************************************************************************** template class binder2nd : public etl::unary_function { protected: TFunction operation; typename TFunction::second_argument_type value; public: binder2nd(const TFunction& f, const typename TFunction::second_argument_type& v) : operation(f) , value(v) { } typename TFunction::result_type operator()(typename TFunction::first_argument_type& x) const { return operation(x, value); } typename TFunction::result_type operator()(const typename TFunction::first_argument_type& x) const { return operation(x, value); } }; template binder2nd bind2nd(const F& f, const T& x) { return binder2nd(f, x); } //*************************************************************************** template struct plus { typedef T first_argument_type; typedef T second_argument_type; typedef T result_type; ETL_CONSTEXPR T operator()(const T& lhs, const T& rhs) const { return lhs + rhs; } }; //*************************************************************************** template struct minus { typedef T first_argument_type; typedef T second_argument_type; typedef T result_type; ETL_CONSTEXPR T operator()(const T& lhs, const T& rhs) const { return lhs - rhs; } }; //*************************************************************************** template struct negate { typedef T argument_type; typedef T result_type; ETL_CONSTEXPR T operator()(const T& lhs) const { return -lhs; } }; //*************************************************************************** template struct multiplies { typedef T first_argument_type; typedef T second_argument_type; typedef T result_type; ETL_CONSTEXPR T operator()(const T& lhs, const T& rhs) const { return lhs * rhs; } }; //*************************************************************************** template struct divides { typedef T first_argument_type; typedef T second_argument_type; typedef T result_type; ETL_CONSTEXPR T operator()(const T& lhs, const T& rhs) const { return lhs / rhs; } }; //*************************************************************************** template struct modulus { typedef T first_argument_type; typedef T second_argument_type; typedef T result_type; ETL_CONSTEXPR T operator()(const T& lhs, const T& rhs) const { return lhs % rhs; } }; //*************************************************************************** template struct logical_and { typedef T first_argument_type; typedef T second_argument_type; typedef T result_type; ETL_CONSTEXPR T operator()(const T& lhs, const T& rhs) const { return lhs && rhs; } }; //*************************************************************************** template struct logical_or { typedef T first_argument_type; typedef T second_argument_type; typedef T result_type; ETL_CONSTEXPR T operator()(const T& lhs, const T& rhs) const { return lhs || rhs; } }; //*************************************************************************** template struct logical_not { typedef T first_argument_type; typedef T second_argument_type; typedef T result_type; ETL_CONSTEXPR T operator()(const T& lhs) const { return !lhs; } }; //*************************************************************************** template struct bit_and { typedef T first_argument_type; typedef T second_argument_type; typedef T result_type; ETL_CONSTEXPR T operator()(const T& lhs, const T& rhs) const { return lhs & rhs; } }; //*************************************************************************** template struct bit_or { typedef T first_argument_type; typedef T second_argument_type; typedef T result_type; ETL_CONSTEXPR T operator()(const T& lhs, const T& rhs) const { return lhs | rhs; } }; //*************************************************************************** template struct bit_xor { typedef T first_argument_type; typedef T second_argument_type; typedef T result_type; ETL_CONSTEXPR T operator()(const T& lhs, const T& rhs) const { return lhs ^ rhs; } }; //*************************************************************************** template struct bit_not { typedef T first_argument_type; typedef T second_argument_type; typedef T result_type; ETL_CONSTEXPR T operator()(const T& lhs) const { return ~lhs; } }; #if ETL_USING_CPP11 namespace private_functional { //*************************************************************************** template class mem_fn_impl { public: typedef TReturnType (TClassType::*MemberFunctionType)(TArgs...); ETL_CONSTEXPR mem_fn_impl(MemberFunctionType member_function_) : member_function(member_function_) { } ETL_CONSTEXPR TReturnType operator()(TClassType& instance, TArgs... args) const { return (instance.*member_function)(etl::forward(args)...); } private: MemberFunctionType member_function; }; //*************************************************************************** template class const_mem_fn_impl { public: typedef TReturnType (TClassType::*MemberFunctionType)(TArgs...) const; ETL_CONSTEXPR const_mem_fn_impl(MemberFunctionType member_function_) : member_function(member_function_) { } ETL_CONSTEXPR TReturnType operator()(const TClassType& instance, TArgs... args) const { return (instance.*member_function)(etl::forward(args)...); } private: MemberFunctionType member_function; }; } // namespace private_functional //*************************************************************************** template ETL_CONSTEXPR private_functional::mem_fn_impl mem_fn(TReturnType (TClassType::*member_function)(TArgs...)) { return private_functional::mem_fn_impl(member_function); } //*************************************************************************** template ETL_CONSTEXPR private_functional::const_mem_fn_impl mem_fn(TReturnType (TClassType::*member_function)(TArgs...) const) { return private_functional::const_mem_fn_impl(member_function); } #endif #if ETL_USING_CPP14 struct identity { template constexpr T&& operator()(T&& t) const noexcept { return etl::forward(t); } }; #endif #if ETL_USING_CPP17 namespace ranges { struct equal_to { template constexpr auto operator()(T&& t, U&& u) const -> decltype(static_cast(t) == static_cast(u)) { return static_cast(t) == static_cast(u); } }; struct less { template constexpr auto operator()(T&& t, U&& u) const -> decltype(static_cast(t) < static_cast(u)) { return static_cast(t) < static_cast(u); } }; } // namespace ranges #endif } // namespace etl #endif