From af9b3deade30543bb7e3b046a7936546605ab2b4 Mon Sep 17 00:00:00 2001 From: John Wellbelove Date: Sun, 11 Aug 2024 12:13:46 +0100 Subject: [PATCH] Added the option of more logical ordering for template parameters for compile time member functions --- include/etl/private/delegate_cpp03.h | 104 +++++++++++++-- include/etl/private/delegate_cpp11.h | 56 +++++++- test/test_delegate.cpp | 184 +++++++++++++++++++++++++++ test/test_delegate_cpp03.cpp | 102 +++++++++++++++ 4 files changed, 427 insertions(+), 19 deletions(-) diff --git a/include/etl/private/delegate_cpp03.h b/include/etl/private/delegate_cpp03.h index 78a8440fd..74da0e497 100644 --- a/include/etl/private/delegate_cpp03.h +++ b/include/etl/private/delegate_cpp03.h @@ -273,7 +273,17 @@ namespace etl template static delegate create() { - return delegate(method_instance_stub); + return delegate(method_instance_stub); + } + + //************************************************************************* + /// Create from instance method (Compile time). + /// New API + //************************************************************************* + template + static delegate create() + { + return delegate(method_instance_stub); } //************************************************************************* @@ -282,7 +292,17 @@ namespace etl template static delegate create() { - return delegate(const_method_instance_stub); + return delegate(const_method_instance_stub); + } + + //************************************************************************* + /// Create from const instance method (Compile time). + /// New API + //************************************************************************* + template + static delegate create() + { + return delegate(const_method_instance_stub); } #if !(defined(ETL_COMPILER_GCC) && (__GNUC__ <= 8)) @@ -350,7 +370,17 @@ namespace etl template void set() { - assign(ETL_NULLPTR, method_instance_stub); + assign(ETL_NULLPTR, method_instance_stub); + } + + //************************************************************************* + /// Set from instance method (Compile time). + /// New API + //************************************************************************* + template + void set() + { + assign(ETL_NULLPTR, method_instance_stub); } //************************************************************************* @@ -359,7 +389,17 @@ namespace etl template void set() { - assign(ETL_NULLPTR, const_method_instance_stub); + assign(ETL_NULLPTR, const_method_instance_stub); + } + + //************************************************************************* + /// Set from const instance method (Compile time). + /// New API + //************************************************************************* + template + void set() + { + assign(ETL_NULLPTR, const_method_instance_stub); } //************************************************************************* @@ -571,7 +611,7 @@ namespace etl //************************************************************************* /// Stub call for a member function. Compile time instance. //************************************************************************* - template + template static TReturn method_instance_stub(void*, TParam param) { return (Instance.*Method)(param); @@ -580,7 +620,7 @@ namespace etl //************************************************************************* /// Stub call for a const member function. Compile time instance. //************************************************************************* - template + template static TReturn const_method_instance_stub(void*, TParam param) { return (Instance.*Method)(param); @@ -734,7 +774,17 @@ namespace etl template static delegate create() { - return delegate(method_instance_stub); + return delegate(method_instance_stub); + } + + //************************************************************************* + /// Create from instance method (Compile time). + /// New API + //************************************************************************* + template + static delegate create() + { + return delegate(method_instance_stub); } //************************************************************************* @@ -743,7 +793,17 @@ namespace etl template static delegate create() { - return delegate(const_method_instance_stub); + return delegate(const_method_instance_stub); + } + + //************************************************************************* + /// Create from const instance method (Compile time). + /// New API + //************************************************************************* + template + static delegate create() + { + return delegate(const_method_instance_stub); } #if !(defined(ETL_COMPILER_GCC) && (__GNUC__ <= 8)) @@ -811,7 +871,17 @@ namespace etl template void set() { - assign(ETL_NULLPTR, method_instance_stub); + assign(ETL_NULLPTR, method_instance_stub); + } + + //************************************************************************* + /// Set from instance method (Compile time). + /// New API + //************************************************************************* + template + void set() + { + assign(ETL_NULLPTR, method_instance_stub); } //************************************************************************* @@ -820,7 +890,17 @@ namespace etl template void set() { - assign(ETL_NULLPTR, const_method_instance_stub); + assign(ETL_NULLPTR, const_method_instance_stub); + } + + //************************************************************************* + /// Set from const instance method (Compile time). + /// New API + //************************************************************************* + template + void set() + { + assign(ETL_NULLPTR, const_method_instance_stub); } //************************************************************************* @@ -1032,7 +1112,7 @@ namespace etl //************************************************************************* /// Stub call for a member function. Compile time instance. //************************************************************************* - template + template static TReturn method_instance_stub(void*) { return (Instance.*Method)(); @@ -1041,7 +1121,7 @@ namespace etl //************************************************************************* /// Stub call for a const member function. Compile time instance. //************************************************************************* - template + template static TReturn const_method_instance_stub(void*) { return (Instance.*Method)(); diff --git a/include/etl/private/delegate_cpp11.h b/include/etl/private/delegate_cpp11.h index f904926ae..8f438602c 100644 --- a/include/etl/private/delegate_cpp11.h +++ b/include/etl/private/delegate_cpp11.h @@ -197,7 +197,18 @@ namespace etl ETL_NODISCARD static ETL_CONSTEXPR14 delegate create() { - return delegate(method_instance_stub); + return delegate(method_instance_stub); + } + + //************************************************************************* + /// Create from instance method (Compile time). + /// New API + //************************************************************************* + template + ETL_NODISCARD + static ETL_CONSTEXPR14 delegate create() + { + return delegate(method_instance_stub); } //************************************************************************* @@ -207,7 +218,18 @@ namespace etl ETL_NODISCARD static ETL_CONSTEXPR14 delegate create() { - return delegate(const_method_instance_stub); + return delegate(const_method_instance_stub); + } + + //************************************************************************* + /// Create from const instance method (Compile time). + /// New API + //************************************************************************* + template + ETL_NODISCARD + static ETL_CONSTEXPR14 delegate create() + { + return delegate(const_method_instance_stub); } #if !(defined(ETL_COMPILER_GCC) && (__GNUC__ <= 8)) @@ -217,7 +239,7 @@ namespace etl //************************************************************************* template ETL_NODISCARD - static ETL_CONSTEXPR14 delegate create() + static ETL_CONSTEXPR14 delegate create() { return delegate(operator_instance_stub); } @@ -274,7 +296,17 @@ namespace etl template ETL_CONSTEXPR14 void set() { - assign(ETL_NULLPTR, method_instance_stub); + assign(ETL_NULLPTR, method_instance_stub); + } + + //************************************************************************* + /// Set from instance method (Compile time). + /// New API + //************************************************************************* + template + ETL_CONSTEXPR14 void set() + { + assign(ETL_NULLPTR, method_instance_stub); } //************************************************************************* @@ -283,7 +315,17 @@ namespace etl template ETL_CONSTEXPR14 void set() { - assign(ETL_NULLPTR, const_method_instance_stub); + assign(ETL_NULLPTR, const_method_instance_stub); + } + + //************************************************************************* + /// Set from const instance method (Compile time). + /// New API + //************************************************************************* + template + ETL_CONSTEXPR14 void set() + { + assign(ETL_NULLPTR, const_method_instance_stub); } //************************************************************************* @@ -525,7 +567,7 @@ namespace etl //************************************************************************* /// Stub call for a member function. Compile time instance. //************************************************************************* - template + template static ETL_CONSTEXPR14 TReturn method_instance_stub(void*, TParams... params) { return (Instance.*Method)(etl::forward(params)...); @@ -534,7 +576,7 @@ namespace etl //************************************************************************* /// Stub call for a const member function. Compile time instance. //************************************************************************* - template + template static ETL_CONSTEXPR14 TReturn const_method_instance_stub(void*, TParams... params) { return (Instance.*Method)(etl::forward(params)...); diff --git a/test/test_delegate.cpp b/test/test_delegate.cpp index e3fe0b31f..0f9d29a8e 100644 --- a/test/test_delegate.cpp +++ b/test/test_delegate.cpp @@ -789,6 +789,16 @@ namespace CHECK(function_called == FunctionCalled::Member_Void_Called); } + //************************************************************************* + TEST_FIXTURE(SetupFixture, test_member_void_compile_time_new_api) + { + auto d = etl::delegate::create(); + + d(); + + CHECK(function_called == FunctionCalled::Member_Void_Called); + } + //************************************************************************* #if ETL_USING_CPP14 TEST_FIXTURE(SetupFixture, test_member_void_compile_time_constexpr) @@ -801,6 +811,18 @@ namespace } #endif + //************************************************************************* +#if ETL_USING_CPP14 + TEST_FIXTURE(SetupFixture, test_member_void_compile_time_constexpr_new_api) + { + constexpr auto d = etl::delegate::create(); + + d(); + + CHECK(function_called == FunctionCalled::Member_Void_Called); + } +#endif + //************************************************************************* TEST_FIXTURE(SetupFixture, test_member_void_const_compile_time) { @@ -811,6 +833,16 @@ namespace CHECK(function_called == FunctionCalled::Member_Void_Const_Called); } + //************************************************************************* + TEST_FIXTURE(SetupFixture, test_member_void_const_compile_time_new_api) + { + auto d = etl::delegate::create(); + + d(); + + CHECK(function_called == FunctionCalled::Member_Void_Const_Called); + } + //************************************************************************* #if ETL_USING_CPP14 TEST_FIXTURE(SetupFixture, test_member_void_const_compile_time_constexpr) @@ -823,6 +855,18 @@ namespace } #endif + //************************************************************************* +#if ETL_USING_CPP14 + TEST_FIXTURE(SetupFixture, test_member_void_const_compile_time_constexpr_new_api) + { + constexpr auto d = etl::delegate::create(); + + d(); + + CHECK(function_called == FunctionCalled::Member_Void_Const_Called); + } +#endif + //************************************************************************* TEST_FIXTURE(SetupFixture, test_member_int_compile_time) { @@ -834,6 +878,17 @@ namespace CHECK(parameter_correct); } + //************************************************************************* + TEST_FIXTURE(SetupFixture, test_member_int_compile_time_new_api) + { + auto d = etl::delegate::create(); + + d(VALUE1, VALUE2); + + CHECK(function_called == FunctionCalled::Member_Int_Called); + CHECK(parameter_correct); + } + //************************************************************************* #if ETL_USING_CPP14 TEST_FIXTURE(SetupFixture, test_member_int_compile_time_constexpr) @@ -847,6 +902,19 @@ namespace } #endif + //************************************************************************* +#if ETL_USING_CPP14 + TEST_FIXTURE(SetupFixture, test_member_int_compile_time_constexpr_new_api) + { + constexpr auto d = etl::delegate::create(); + + d(VALUE1, VALUE2); + + CHECK(function_called == FunctionCalled::Member_Int_Called); + CHECK(parameter_correct); + } +#endif + //************************************************************************* TEST_FIXTURE(SetupFixture, test_member_int_const_compile_time) { @@ -858,6 +926,17 @@ namespace CHECK(parameter_correct); } + //************************************************************************* + TEST_FIXTURE(SetupFixture, test_member_int_const_compile_time_new_api) + { + auto d = etl::delegate::create(); + + d(VALUE1, VALUE2); + + CHECK(function_called == FunctionCalled::Member_Int_Const_Called); + CHECK(parameter_correct); + } + //************************************************************************* #if ETL_USING_CPP14 TEST_FIXTURE(SetupFixture, test_member_int_const_compile_time_constexpr) @@ -871,6 +950,19 @@ namespace } #endif + //************************************************************************* +#if ETL_USING_CPP14 + TEST_FIXTURE(SetupFixture, test_member_int_const_compile_time_constexpr_new_api) + { + constexpr auto d = etl::delegate::create(); + + d(VALUE1, VALUE2); + + CHECK(function_called == FunctionCalled::Member_Int_Const_Called); + CHECK(parameter_correct); + } +#endif + //************************************************************************* TEST_FIXTURE(SetupFixture, test_member_reference_compile_time) { @@ -885,6 +977,20 @@ namespace CHECK(parameter_correct); } + //************************************************************************* + TEST_FIXTURE(SetupFixture, test_member_reference_compile_time_new_api) + { + auto d = etl::delegate::create(); + + Data data; + data.d = VALUE1; + + d(data, VALUE2); + + CHECK(function_called == FunctionCalled::Member_Reference_Called); + CHECK(parameter_correct); + } + //************************************************************************* #if ETL_USING_CPP14 TEST_FIXTURE(SetupFixture, test_member_reference_compile_time_constexpr) @@ -901,6 +1007,22 @@ namespace } #endif + //************************************************************************* +#if ETL_USING_CPP14 + TEST_FIXTURE(SetupFixture, test_member_reference_compile_time_constexpr_new_api) + { + constexpr auto d = etl::delegate::create(); + + Data data; + data.d = VALUE1; + + d(data, VALUE2); + + CHECK(function_called == FunctionCalled::Member_Reference_Called); + CHECK(parameter_correct); + } +#endif + //************************************************************************* TEST_FIXTURE(SetupFixture, test_member_reference_const_compile_time) { @@ -915,6 +1037,20 @@ namespace CHECK(parameter_correct); } + //************************************************************************* + TEST_FIXTURE(SetupFixture, test_member_reference_const_compile_time_new_api) + { + auto d = etl::delegate::create(); + + Data data; + data.d = VALUE1; + + d(data, VALUE2); + + CHECK(function_called == FunctionCalled::Member_Reference_Const_Called); + CHECK(parameter_correct); + } + //************************************************************************* #if ETL_USING_CPP14 TEST_FIXTURE(SetupFixture, test_member_reference_const_compile_time_constexpr) @@ -931,6 +1067,22 @@ namespace } #endif + //************************************************************************* +#if ETL_USING_CPP14 + TEST_FIXTURE(SetupFixture, test_member_reference_const_compile_time_constexpr_new_api) + { + constexpr auto d = etl::delegate::create(); + + Data data; + data.d = VALUE1; + + d(data, VALUE2); + + CHECK(function_called == FunctionCalled::Member_Reference_Const_Called); + CHECK(parameter_correct); + } +#endif + //************************************************************************* TEST_FIXTURE(SetupFixture, test_set_free_int) { @@ -1007,6 +1159,22 @@ namespace CHECK(parameter_correct); } + //************************************************************************* + TEST_FIXTURE(SetupFixture, test_set_member_reference_compile_time_new_api) + { + etl::delegate d; + + d.set(); + + Data data; + data.d = VALUE1; + + d(data, VALUE2); + + CHECK(function_called == FunctionCalled::Member_Reference_Called); + CHECK(parameter_correct); + } + //************************************************************************* TEST_FIXTURE(SetupFixture, test_set_member_reference_const_compile_time) { @@ -1022,6 +1190,22 @@ namespace CHECK(function_called == FunctionCalled::Member_Reference_Const_Called); CHECK(parameter_correct); } + + //************************************************************************* + TEST_FIXTURE(SetupFixture, test_set_member_reference_const_compile_time_new_api) + { + etl::delegate d; + + d.set(); + + Data data; + data.d = VALUE1; + + d(data, VALUE2); + + CHECK(function_called == FunctionCalled::Member_Reference_Const_Called); + CHECK(parameter_correct); + } #endif //************************************************************************* diff --git a/test/test_delegate_cpp03.cpp b/test/test_delegate_cpp03.cpp index 10be5d4d1..3a220de34 100644 --- a/test/test_delegate_cpp03.cpp +++ b/test/test_delegate_cpp03.cpp @@ -471,6 +471,16 @@ namespace CHECK(function_called == FunctionCalled::Member_Void_Called); } + //************************************************************************* + TEST_FIXTURE(SetupFixture, test_member_void_compile_time_new_api) + { + auto d = etl_cpp03::delegate::create(); + + d(); + + CHECK(function_called == FunctionCalled::Member_Void_Called); + } + //************************************************************************* TEST_FIXTURE(SetupFixture, test_member_void_const_compile_time) { @@ -481,6 +491,16 @@ namespace CHECK(function_called == FunctionCalled::Member_Void_Const_Called); } + //************************************************************************* + TEST_FIXTURE(SetupFixture, test_member_void_const_compile_time_new_api) + { + auto d = etl_cpp03::delegate::create(); + + d(); + + CHECK(function_called == FunctionCalled::Member_Void_Const_Called); + } + //************************************************************************* TEST_FIXTURE(SetupFixture, test_member_int_compile_time) { @@ -492,6 +512,17 @@ namespace CHECK(parameter_correct); } + //************************************************************************* + TEST_FIXTURE(SetupFixture, test_member_int_compile_time_new_api) + { + auto d = etl_cpp03::delegate::create(); + + d(VALUE1); + + CHECK(function_called == FunctionCalled::Member_Int_Called); + CHECK(parameter_correct); + } + //************************************************************************* TEST_FIXTURE(SetupFixture, test_member_int_const_compile_time) { @@ -503,6 +534,17 @@ namespace CHECK(parameter_correct); } + //************************************************************************* + TEST_FIXTURE(SetupFixture, test_member_int_const_compile_time_new_api) + { + auto d = etl_cpp03::delegate::create(); + + d(VALUE1); + + CHECK(function_called == FunctionCalled::Member_Int_Const_Called); + CHECK(parameter_correct); + } + //************************************************************************* TEST_FIXTURE(SetupFixture, test_member_reference_compile_time) { @@ -517,6 +559,20 @@ namespace CHECK(parameter_correct); } + //************************************************************************* + TEST_FIXTURE(SetupFixture, test_member_reference_compile_time_new_api) + { + auto d = etl_cpp03::delegate::create(); + + Data data; + data.d = VALUE1; + + d(data); + + CHECK(function_called == FunctionCalled::Member_Reference_Called); + CHECK(parameter_correct); + } + //************************************************************************* TEST_FIXTURE(SetupFixture, test_member_reference_const_compile_time) { @@ -531,6 +587,20 @@ namespace CHECK(parameter_correct); } + //************************************************************************* + TEST_FIXTURE(SetupFixture, test_member_reference_const_compile_time_new_api) + { + auto d = etl_cpp03::delegate::create(); + + Data data; + data.d = VALUE1; + + d(data); + + CHECK(function_called == FunctionCalled::Member_Reference_Const_Called); + CHECK(parameter_correct); + } + //************************************************************************* TEST_FIXTURE(SetupFixture, test_set_free_int) { @@ -607,6 +677,22 @@ namespace CHECK(parameter_correct); } + //************************************************************************* + TEST_FIXTURE(SetupFixture, test_set_member_reference_compile_time_new_api) + { + etl_cpp03::delegate d; + + d.set(); + + Data data; + data.d = VALUE1; + + d(data); + + CHECK(function_called == FunctionCalled::Member_Reference_Called); + CHECK(parameter_correct); + } + //************************************************************************* TEST_FIXTURE(SetupFixture, test_set_member_reference_const_compile_time) { @@ -622,6 +708,22 @@ namespace CHECK(function_called == FunctionCalled::Member_Reference_Const_Called); CHECK(parameter_correct); } + + //************************************************************************* + TEST_FIXTURE(SetupFixture, test_set_member_reference_const_compile_time_new_api) + { + etl_cpp03::delegate d; + + d.set(); + + Data data; + data.d = VALUE1; + + d(data); + + CHECK(function_called == FunctionCalled::Member_Reference_Const_Called); + CHECK(parameter_correct); + } #endif //*************************************************************************