Skip to content

Commit

Permalink
etl/delegate: fix accident creation of a delegate to an rvalue delega…
Browse files Browse the repository at this point in the history
…te when copying/assigning from delegate with mismatching signature
  • Loading branch information
VladimirP1 committed Oct 10, 2024
1 parent 081e920 commit f747fb2
Showing 1 changed file with 19 additions and 10 deletions.
29 changes: 19 additions & 10 deletions include/etl/private/delegate_cpp11.h
Original file line number Diff line number Diff line change
Expand Up @@ -83,16 +83,25 @@ namespace etl
}
};

//*****************************************************************
/// The tag to identify an etl::delegate.
///\ingroup delegate
//*****************************************************************
struct delegate_tag
{
};

//*************************************************************************
/// Declaration.
//*************************************************************************
template <typename T> class delegate;
template <typename T>
class delegate;

//*************************************************************************
/// Specialisation.
//*************************************************************************
template <typename TReturn, typename... TParams>
class delegate<TReturn(TParams...)> final
class delegate<TReturn(TParams...)> final : public delegate_tag
{
public:

Expand All @@ -111,7 +120,7 @@ namespace etl
//*************************************************************************
// Construct from lambda or functor.
//*************************************************************************
template <typename TLambda, typename = etl::enable_if_t<etl::is_class<TLambda>::value && !etl::is_same<etl::delegate<TReturn(TParams...)>, TLambda>::value, void>>
template <typename TLambda, typename = etl::enable_if_t<etl::is_class<TLambda>::value && !etl::is_base_of<delegate_tag, TLambda>::value, void>>
ETL_CONSTEXPR14 delegate(TLambda& instance)
{
assign((void*)(&instance), lambda_stub<TLambda>);
Expand All @@ -120,7 +129,7 @@ namespace etl
//*************************************************************************
// Construct from const lambda or functor.
//*************************************************************************
template <typename TLambda, typename = etl::enable_if_t<etl::is_class<TLambda>::value && !etl::is_same<etl::delegate<TReturn(TParams...)>, TLambda>::value, void>>
template <typename TLambda, typename = etl::enable_if_t<etl::is_class<TLambda>::value && !etl::is_base_of<delegate_tag, TLambda>::value, void>>
ETL_CONSTEXPR14 delegate(const TLambda& instance)
{
assign((void*)(&instance), const_lambda_stub<TLambda>);
Expand All @@ -139,7 +148,7 @@ namespace etl
//*************************************************************************
/// Create from Lambda or Functor.
//*************************************************************************
template <typename TLambda, typename = etl::enable_if_t<etl::is_class<TLambda>::value && !etl::is_same<etl::delegate<TReturn(TParams...)>, TLambda>::value, void>>
template <typename TLambda, typename = etl::enable_if_t<etl::is_class<TLambda>::value && !etl::is_base_of<delegate_tag, TLambda>::value, void>>
ETL_NODISCARD
static ETL_CONSTEXPR14 delegate create(TLambda& instance)
{
Expand All @@ -149,7 +158,7 @@ namespace etl
//*************************************************************************
/// Create from const Lambda or Functor.
//*************************************************************************
template <typename TLambda, typename = etl::enable_if_t<etl::is_class<TLambda>::value && !etl::is_same<etl::delegate<TReturn(TParams...)>, TLambda>::value, void>>
template <typename TLambda, typename = etl::enable_if_t<etl::is_class<TLambda>::value && !etl::is_base_of<delegate_tag, TLambda>::value, void>>
ETL_NODISCARD
static ETL_CONSTEXPR14 delegate create(const TLambda& instance)
{
Expand Down Expand Up @@ -257,7 +266,7 @@ namespace etl
//*************************************************************************
/// Set from Lambda or Functor.
//*************************************************************************
template <typename TLambda, typename = etl::enable_if_t<etl::is_class<TLambda>::value && !etl::is_same<etl::delegate<TReturn(TParams...)>, TLambda>::value, void>>
template <typename TLambda, typename = etl::enable_if_t<etl::is_class<TLambda>::value && !etl::is_base_of<delegate_tag, TLambda>::value, void>>
ETL_CONSTEXPR14 void set(TLambda& instance)
{
assign((void*)(&instance), lambda_stub<TLambda>);
Expand All @@ -266,7 +275,7 @@ namespace etl
//*************************************************************************
/// Set from const Lambda or Functor.
//*************************************************************************
template <typename TLambda, typename = etl::enable_if_t<etl::is_class<TLambda>::value && !etl::is_same<etl::delegate<TReturn(TParams...)>, TLambda>::value, void>>
template <typename TLambda, typename = etl::enable_if_t<etl::is_class<TLambda>::value && !etl::is_base_of<delegate_tag, TLambda>::value, void>>
ETL_CONSTEXPR14 void set(const TLambda& instance)
{
assign((void*)(&instance), const_lambda_stub<TLambda>);
Expand Down Expand Up @@ -427,7 +436,7 @@ namespace etl
//*************************************************************************
/// Create from Lambda or Functor.
//*************************************************************************
template <typename TLambda, typename = etl::enable_if_t<etl::is_class<TLambda>::value && !etl::is_same<etl::delegate<TReturn(TParams...)>, TLambda>::value, void>>
template <typename TLambda, typename = etl::enable_if_t<etl::is_class<TLambda>::value && !etl::is_base_of<delegate_tag, TLambda>::value, void>>
ETL_CONSTEXPR14 delegate& operator =(TLambda& instance)
{
assign((void*)(&instance), lambda_stub<TLambda>);
Expand All @@ -437,7 +446,7 @@ namespace etl
//*************************************************************************
/// Create from const Lambda or Functor.
//*************************************************************************
template <typename TLambda, typename = etl::enable_if_t<etl::is_class<TLambda>::value && !etl::is_same<etl::delegate<TReturn(TParams...)>, TLambda>::value, void>>
template <typename TLambda, typename = etl::enable_if_t<etl::is_class<TLambda>::value && !etl::is_base_of<delegate_tag, TLambda>::value, void>>
ETL_CONSTEXPR14 delegate& operator =(const TLambda& instance)
{
assign((void*)(&instance), const_lambda_stub<TLambda>);
Expand Down

0 comments on commit f747fb2

Please sign in to comment.