mirror of
https://github.com/ETLCPP/etl.git
synced 2026-06-16 00:46:03 +08:00
Re-initroduced etl::functor
Added constexpr to etl::member_function_wrapper and etl:;functor_wrapper
This commit is contained in:
parent
6a00274b75
commit
aad0dabebe
@ -444,7 +444,39 @@ namespace etl
|
||||
typename etl::add_rvalue_reference<T>::type declval() ETL_NOEXCEPT;
|
||||
#endif
|
||||
|
||||
#if ETL_USING_CPP11
|
||||
//*************************************************************************
|
||||
/// A function wrapper for free/global functions.
|
||||
//*************************************************************************
|
||||
template <typename TReturn, typename... TParams>
|
||||
class functor
|
||||
{
|
||||
public:
|
||||
|
||||
//*********************************
|
||||
/// Constructor.
|
||||
//*********************************
|
||||
constexpr functor(TReturn(*ptr_)(TParams...))
|
||||
: ptr(ptr_)
|
||||
{
|
||||
}
|
||||
|
||||
//*********************************
|
||||
/// Const function operator.
|
||||
//*********************************
|
||||
constexpr TReturn operator()(TParams... args) const
|
||||
{
|
||||
return ptr(etl::forward<TParams>(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 <typename T, T& Instance, TReturn(T::* Method)(TParams...)>
|
||||
static TReturn function(TParams... params)
|
||||
static constexpr TReturn function(TParams... params)
|
||||
{
|
||||
return (Instance.*Method)(std::forward<TParams>(params)...);
|
||||
return (Instance.*Method)(etl::forward<TParams>(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 <typename TDelegate, TDelegate& Instance>
|
||||
static TReturn function(TParams... params)
|
||||
template <typename TFunctor, TFunctor& Instance>
|
||||
static constexpr TReturn function(TParams... params)
|
||||
{
|
||||
return Instance(std::forward<TParams>(params)...);
|
||||
return Instance(etl::forward<TParams>(params)...);
|
||||
}
|
||||
};
|
||||
#endif
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
@ -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<int>;
|
||||
using ItemM2 = TestDataM<double>;
|
||||
|
||||
@ -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<void(int)>::function<TestClass, test, &TestClass::MemberFunction>;
|
||||
constexpr int(*pf)(int) = &etl::member_function_wrapper<int(int)>::function<TestClass, test, &TestClass::MemberFunction>;
|
||||
|
||||
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<void(int)>::function<TestClass, test>;
|
||||
constexpr int(*pf)(int) = &etl::functor_wrapper<int(int)>::function<TestClass, test>;
|
||||
|
||||
test.value = 0;
|
||||
pf(1);
|
||||
CHECK_EQUAL(1, test.value);
|
||||
CHECK_EQUAL(2, pf(1));
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user