Loading...
Searching...
No Matches
closure.h
Go to the documentation of this file.
/******************************************************************************
The MIT License(MIT)
Embedded Template Library.
https://github.com/ETLCPP/etl
https://www.etlcpp.com
Copyright(c) 2025 BMW AG
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_CLOSURE_INCLUDED
#define ETL_CLOSURE_INCLUDED
#include "platform.h"
#include "delegate.h"
#include "invoke.h"
#include "tuple.h"
#include "type_list.h"
#include "utility.h"
namespace etl
{
//*************************************************************************
//*************************************************************************
template <typename TSignature, typename TCallback = etl::delegate<TSignature> >
class closure;
#if ETL_USING_CPP11 && !defined(ETL_CLOSURE_FORCE_CPP03_IMPLEMENTATION)
//*************************************************************************
//*************************************************************************
template <typename TReturn, typename... TArgs, typename TCallback>
class closure<TReturn(TArgs...), TCallback>
{
public:
using delegate_type ETL_DEPRECATED_REASON("Use callback_type") =
TCallback;
using callback_type = TCallback;
using argument_types = etl::type_list<TArgs...>;
static_assert(etl::is_invocable_r<TReturn, TCallback, TArgs...>::value, "Callback is not invocable with the specified arguments");
static_assert(etl::is_copy_constructible<TCallback>::value, "Callback type must be copy constructible");
//*********************************************************************
//*********************************************************************
ETL_CONSTEXPR14 closure(const callback_type& f, const TArgs... args)
: m_f(f)
, m_args(args...)
{
}
//*********************************************************************
//*********************************************************************
ETL_CONSTEXPR14 TReturn operator()() const
{
return execute(etl::index_sequence_for<TArgs...>{});
}
//*********************************************************************
//*********************************************************************
template <size_t Index, typename UArg>
ETL_CONSTEXPR14 void bind(UArg arg)
{
static_assert(etl::is_convertible< UArg, etl::type_list_type_at_index_t<argument_types, Index>>::value, "Argument is not convertible");
static_assert(!etl::is_reference<UArg>::value, "Cannot bind reference arguments");
etl::get<Index>(m_args) = arg;
}
//*********************************************************************
template <typename... UArgs>
ETL_CONSTEXPR14 void bind(UArgs&&... args)
{
static_assert(sizeof...(UArgs) == sizeof...(TArgs), "Argument count mismatch");
bind_impl(etl::make_index_sequence<sizeof...(TArgs)>{}, etl::forward<UArgs>(args)...);
}
private:
//*********************************************************************
template <size_t... Indexes, typename... UArgs>
ETL_CONSTEXPR14 void bind_impl(etl::index_sequence<Indexes...>, UArgs&&... args)
{
// Expand the pack and call bind<Index>(arg) for each argument
int dummy[] = {0, (bind<Indexes>(etl::forward<UArgs>(args)), 0)...};
(void)dummy; // Suppress unused variable warning
}
//*********************************************************************
//*********************************************************************
template <size_t... Indexes>
ETL_CONSTEXPR14 TReturn execute(etl::index_sequence<Indexes...>) const
{
return m_f(etl::get<Indexes>(m_args)...);
}
callback_type m_f;
etl::tuple<TArgs...> m_args;
};
#else
//*************************************************************************
//*************************************************************************
template <typename TReturn, typename TArg0, typename TCallback>
class closure<TReturn(TArg0), TCallback>
{
public:
typedef TCallback callback_type;
//*********************************************************************
//*********************************************************************
closure(const callback_type& f, const TArg0 arg0)
: m_f(f)
, m_arg0(arg0)
{
}
//*********************************************************************
//*********************************************************************
TReturn operator()() const
{
return m_f(m_arg0);
}
private:
callback_type m_f;
TArg0 m_arg0;
};
//*************************************************************************
//*************************************************************************
template <typename TReturn, typename TArg0, typename TArg1, typename TCallback>
class closure<TReturn(TArg0, TArg1), TCallback>
{
public:
typedef TCallback callback_type;
//*********************************************************************
//*********************************************************************
closure(const callback_type& f, const TArg0 arg0, const TArg1 arg1)
: m_f(f)
, m_arg0(arg0)
, m_arg1(arg1)
{
}
//*********************************************************************
//*********************************************************************
TReturn operator()() const
{
return m_f(m_arg0, m_arg1);
}
private:
callback_type m_f;
TArg0 m_arg0;
TArg1 m_arg1;
};
//*************************************************************************
//*************************************************************************
template <typename TReturn, typename TArg0, typename TArg1, typename TArg2, typename TCallback>
class closure<TReturn(TArg0, TArg1, TArg2), TCallback>
{
public:
typedef TCallback callback_type;
//*********************************************************************
//*********************************************************************
closure(const callback_type& f, const TArg0 arg0, const TArg1 arg1, const TArg2 arg2)
: m_f(f)
, m_arg0(arg0)
, m_arg1(arg1)
, m_arg2(arg2)
{
}
//*********************************************************************
//*********************************************************************
TReturn operator()() const
{
return m_f(m_arg0, m_arg1, m_arg2);
}
private:
callback_type m_f;
TArg0 m_arg0;
TArg1 m_arg1;
TArg2 m_arg2;
};
//*************************************************************************
//*************************************************************************
template <typename TReturn, typename TArg0, typename TArg1, typename TArg2, typename TArg3, typename TCallback>
class closure<TReturn(TArg0, TArg1, TArg2, TArg3), TCallback>
{
public:
typedef TCallback callback_type;
//*********************************************************************
//*********************************************************************
closure(const callback_type& f, const TArg0 arg0, const TArg1 arg1, const TArg2 arg2, const TArg3 arg3)
: m_f(f)
, m_arg0(arg0)
, m_arg1(arg1)
, m_arg2(arg2)
, m_arg3(arg3)
{
}
//*********************************************************************
//*********************************************************************
TReturn operator()() const
{
return m_f(m_arg0, m_arg1, m_arg2, m_arg3);
}
private:
callback_type m_f;
TArg0 m_arg0;
TArg1 m_arg1;
TArg2 m_arg2;
TArg3 m_arg3;
};
//*************************************************************************
//*************************************************************************
template <typename TReturn, typename TArg0, typename TArg1, typename TArg2, typename TArg3, typename TArg4, typename TCallback>
class closure<TReturn(TArg0, TArg1, TArg2, TArg3, TArg4), TCallback>
{
public:
typedef TCallback callback_type;
//*********************************************************************
//*********************************************************************
closure(const callback_type& f, const TArg0 arg0, const TArg1 arg1, const TArg2 arg2, const TArg3 arg3, const TArg4 arg4)
: m_f(f)
, m_arg0(arg0)
, m_arg1(arg1)
, m_arg2(arg2)
, m_arg3(arg3)
, m_arg4(arg4)
{
}
//*********************************************************************
//*********************************************************************
TReturn operator()() const
{
return m_f(m_arg0, m_arg1, m_arg2, m_arg3, m_arg4);
}
private:
callback_type m_f;
TArg0 m_arg0;
TArg1 m_arg1;
TArg2 m_arg2;
TArg3 m_arg3;
TArg4 m_arg4;
};
#endif
} // namespace etl
#endif
etl::closure< TReturn(TArg0, TArg1, TArg2, TArg3, TArg4), TCallback >::closure
closure(const callback_type &f, const TArg0 arg0, const TArg1 arg1, const TArg2 arg2, const TArg3 arg3, const TArg4 arg4)
Definition closure.h:362
etl::closure< TReturn(TArg0, TArg1, TArg2, TArg3, TArg4), TCallback >::operator()
TReturn operator()() const
Definition closure.h:376
etl::closure< TReturn(TArg0, TArg1, TArg2, TArg3), TCallback >::operator()
TReturn operator()() const
Definition closure.h:322
etl::closure< TReturn(TArg0, TArg1, TArg2, TArg3), TCallback >::closure
closure(const callback_type &f, const TArg0 arg0, const TArg1 arg1, const TArg2 arg2, const TArg3 arg3)
Definition closure.h:309
etl::closure< TReturn(TArg0, TArg1, TArg2), TCallback >::operator()
TReturn operator()() const
Definition closure.h:272
etl::closure< TReturn(TArg0, TArg1, TArg2), TCallback >::closure
closure(const callback_type &f, const TArg0 arg0, const TArg1 arg1, const TArg2 arg2)
Definition closure.h:260
etl::closure< TReturn(TArg0, TArg1), TCallback >::operator()
TReturn operator()() const
Definition closure.h:226
etl::closure< TReturn(TArg0, TArg1), TCallback >::closure
closure(const callback_type &f, const TArg0 arg0, const TArg1 arg1)
Definition closure.h:215
etl::closure< TReturn(TArg0), TCallback >::closure
closure(const callback_type &f, const TArg0 arg0)
Definition closure.h:174
etl::closure< TReturn(TArg0), TCallback >::operator()
TReturn operator()() const
Definition closure.h:184
etl::closure< TReturn(TArg0), TCallback >::callback_type
TCallback callback_type
The callback type to be invoked.
Definition closure.h:167
etl::closure
Definition closure.h:49
etl::callback_type
delegate.h
invoke.h
etl
Definition absolute.h:40
etl::get
T & get(array< T, Size > &a)
Definition array.h:1158
etl::ETL_DEPRECATED_REASON
ETL_DEPRECATED_REASON("Misspelt class name") typedef scheduler_policy_sequential_single scheduler_policy_sequencial_single
Typedef for backwards compatibility with miss-spelt struct name.
platform.h
utility.h