From aad0dabebe2b6cfdf3935be46148651592ad64b4 Mon Sep 17 00:00:00 2001 From: John Wellbelove Date: Tue, 22 Mar 2022 15:16:51 +0000 Subject: [PATCH] Re-initroduced etl::functor Added constexpr to etl::member_function_wrapper and etl:;functor_wrapper --- include/etl/utility.h | 45 ++++++++++++++++++++++++++++++++++++++----- test/test_utility.cpp | 37 +++++++++++++++++++++-------------- 2 files changed, 63 insertions(+), 19 deletions(-) diff --git a/include/etl/utility.h b/include/etl/utility.h index e8262975..7d985f82 100644 --- a/include/etl/utility.h +++ b/include/etl/utility.h @@ -444,7 +444,39 @@ namespace etl typename etl::add_rvalue_reference::type declval() ETL_NOEXCEPT; #endif +#if ETL_USING_CPP11 + //************************************************************************* + /// A function wrapper for free/global functions. + //************************************************************************* + template + class functor + { + public: + //********************************* + /// Constructor. + //********************************* + constexpr functor(TReturn(*ptr_)(TParams...)) + : ptr(ptr_) + { + } + + //********************************* + /// Const function operator. + //********************************* + constexpr TReturn operator()(TParams... args) const + { + return ptr(etl::forward(args)...); + } + + private: + + /// The pointer to the function. + TReturn(*ptr)(TParams...); + }; +#endif + +#if ETL_USING_CPP11 //***************************************************************************** // A wrapper for a member function // Creates a static member function that calls the specified member function. @@ -458,12 +490,14 @@ namespace etl public: template - static TReturn function(TParams... params) + static constexpr TReturn function(TParams... params) { - return (Instance.*Method)(std::forward(params)...); + return (Instance.*Method)(etl::forward(params)...); } }; +#endif +#if ETL_USING_CPP11 //***************************************************************************** // A wrapper for a functor // Creates a static member function that calls the specified functor. @@ -476,12 +510,13 @@ namespace etl { public: - template - static TReturn function(TParams... params) + template + static constexpr TReturn function(TParams... params) { - return Instance(std::forward(params)...); + return Instance(etl::forward(params)...); } }; +#endif } #endif diff --git a/test/test_utility.cpp b/test/test_utility.cpp index 0d294564..a2cad4be 100644 --- a/test/test_utility.cpp +++ b/test/test_utility.cpp @@ -36,6 +36,7 @@ namespace { bool nonConstCalled; bool constCalled; + int value; void TestText(std::string&) { @@ -47,6 +48,11 @@ namespace constCalled = true; } + int TestGlobal(int i) + { + return 2 * i; + } + using ItemM1 = TestDataM; using ItemM2 = TestDataM; @@ -56,17 +62,15 @@ namespace { public: - void MemberFunction(int i) + int MemberFunction(int i) { - value = i; + return 2 * i; } - void operator()(int i) + int operator()(int i) { - value = i; + return 2 * i; } - - int value; }; static TestClass test; @@ -325,24 +329,29 @@ namespace CHECK(constCalled); } + //************************************************************************* + TEST(test_functor) + { + constexpr auto ptr = TestGlobal; + + constexpr etl::functor fw1(ptr); + CHECK_EQUAL(2, fw1(1)); + } + //************************************************************************* TEST(test_member_function_wrapper) { - void(*pf)(int) = &etl::member_function_wrapper::function; + constexpr int(*pf)(int) = &etl::member_function_wrapper::function; - test.value = 0; - pf(1); - CHECK_EQUAL(1, test.value); + CHECK_EQUAL(2, pf(1)); } //************************************************************************* TEST(test_functor_wrapper) { - void(*pf)(int) = &etl::functor_wrapper::function; + constexpr int(*pf)(int) = &etl::functor_wrapper::function; - test.value = 0; - pf(1); - CHECK_EQUAL(1, test.value); + CHECK_EQUAL(2, pf(1)); } }; }