From 30ccdea62974fffa0e416c9926b295cf1b9ae829 Mon Sep 17 00:00:00 2001 From: John Wellbelove Date: Sun, 12 Oct 2025 11:05:30 +0100 Subject: [PATCH] Opimisation of delegate invocations --- .gitignore | 1 + include/etl/private/delegate_cpp11.h | 49 +++++++++++++++++++--------- 2 files changed, 34 insertions(+), 16 deletions(-) diff --git a/.gitignore b/.gitignore index 679552e6..068e7602 100644 --- a/.gitignore +++ b/.gitignore @@ -410,3 +410,4 @@ examples/UniquePtrWithPool/CMakeCache.txt examples/UniquePtrWithPool/UniquePtrWithPool test/vs2022/Debug Clang C++20 - Optimised -O2 include/etl/header_file_list.txt +temp diff --git a/include/etl/private/delegate_cpp11.h b/include/etl/private/delegate_cpp11.h index 79182656..4aa0244b 100644 --- a/include/etl/private/delegate_cpp11.h +++ b/include/etl/private/delegate_cpp11.h @@ -372,25 +372,33 @@ namespace etl //************************************************************************* /// Execute the delegate. //************************************************************************* - ETL_CONSTEXPR14 TReturn operator()(TParams... args) const + template + ETL_CONSTEXPR14 + return_type operator()(TArgs&&... args) const { + ETL_STATIC_ASSERT((sizeof...(TArgs) == sizeof...(TParams)), "Incorrect number of parameters passed to delegate"); + ETL_STATIC_ASSERT((etl::type_lists_are_convertible, argument_types>::value), "Incompatible parameter types passed to delegate"); + ETL_ASSERT(is_valid(), ETL_ERROR(delegate_uninitialised)); - return (*invocation.stub)(invocation.object, etl::forward(args)...); + return (*invocation.stub)(invocation.object, etl::forward(args)...); } //************************************************************************* /// Execute the delegate if valid. /// 'void' return delegate. //************************************************************************* - template + template ETL_CONSTEXPR14 typename etl::enable_if_t::value, bool> - call_if(TParams... args) const + call_if(TArgs&&... args) const { + ETL_STATIC_ASSERT((sizeof...(TArgs) == sizeof...(TParams)), "Incorrect number of parameters passed to delegate"); + ETL_STATIC_ASSERT((etl::type_lists_are_convertible, argument_types>::value), "Incompatible parameter types passed to delegate"); + if (is_valid()) { - (*invocation.stub)(invocation.object, etl::forward(args)...); + (*invocation.stub)(invocation.object, etl::forward(args)...); return true; } else @@ -403,16 +411,19 @@ namespace etl /// Execute the delegate if valid. /// Non 'void' return delegate. //************************************************************************* - template + template ETL_CONSTEXPR14 typename etl::enable_if_t::value, etl::optional> - call_if(TParams... args) const + call_if(TArgs&&... args) const { + ETL_STATIC_ASSERT((sizeof...(TArgs) == sizeof...(TParams)), "Incorrect number of parameters passed to delegate"); + ETL_STATIC_ASSERT((etl::type_lists_are_convertible, argument_types>::value), "Incompatible parameter types passed to delegate"); + etl::optional result; if (is_valid()) { - result = (*invocation.stub)(invocation.object, etl::forward(args)...); + result = (*invocation.stub)(invocation.object, etl::forward(args)...); } return result; @@ -422,16 +433,19 @@ namespace etl /// Execute the delegate if valid or call alternative. /// Run time alternative. //************************************************************************* - template - ETL_CONSTEXPR14 TReturn call_or(TAlternative alternative, TParams... args) const + template + ETL_CONSTEXPR14 TReturn call_or(TAlternative alternative, TArgs&&... args) const { + ETL_STATIC_ASSERT((sizeof...(TArgs) == sizeof...(TParams)), "Incorrect number of parameters passed to delegate"); + ETL_STATIC_ASSERT((etl::type_lists_are_convertible, argument_types>::value), "Incompatible parameter types passed to delegate"); + if (is_valid()) { - return (*invocation.stub)(invocation.object, etl::forward(args)...); + return (*invocation.stub)(invocation.object, etl::forward(args)...); } else { - return alternative(etl::forward(args)...); + return alternative(etl::forward(args)...); } } @@ -439,16 +453,19 @@ namespace etl /// Execute the delegate if valid or call alternative. /// Compile time alternative. //************************************************************************* - template - ETL_CONSTEXPR14 TReturn call_or(TParams... args) const + template + ETL_CONSTEXPR14 TReturn call_or(TArgs&&... args) const { + ETL_STATIC_ASSERT((sizeof...(TArgs) == sizeof...(TParams)), "Incorrect number of parameters passed to delegate"); + ETL_STATIC_ASSERT((etl::type_lists_are_convertible, argument_types>::value), "Incompatible parameter types passed to delegate"); + if (is_valid()) { - return (*invocation.stub)(invocation.object, etl::forward(args)...); + return (*invocation.stub)(invocation.object, etl::forward(args)...); } else { - return (Method)(etl::forward(args)...); + return (Method)(etl::forward(args)...); } }