Utility: add the ability to generate static functions from IFunction
This commit is contained in:
parent
9924559fe0
commit
e554d30bf6
@ -15,10 +15,6 @@
|
|||||||
namespace Peripherals
|
namespace Peripherals
|
||||||
{
|
{
|
||||||
|
|
||||||
#define SKULLC_CONCAT_IMPL(x, y) x##y
|
|
||||||
#define SKULLC_CONCAT(x, y) SKULLC_CONCAT_IMPL(x, y)
|
|
||||||
#define SKULLC_TAG struct SKULLC_CONCAT(SkullCTag_, __COUNTER__)
|
|
||||||
|
|
||||||
template<typename T>
|
template<typename T>
|
||||||
constexpr const T& clamp(const T& v, const T& lo, const T& hi)
|
constexpr const T& clamp(const T& v, const T& lo, const T& hi)
|
||||||
{
|
{
|
||||||
|
|||||||
@ -5,6 +5,7 @@
|
|||||||
#include <catch2/catch.hpp>
|
#include <catch2/catch.hpp>
|
||||||
|
|
||||||
#include <utility_function.hpp>
|
#include <utility_function.hpp>
|
||||||
|
#include <utility_tag.hpp>
|
||||||
|
|
||||||
TEST_CASE("Function calls function appropriately.", "[utility],[function]")
|
TEST_CASE("Function calls function appropriately.", "[utility],[function]")
|
||||||
{
|
{
|
||||||
@ -38,6 +39,26 @@ TEST_CASE("Function passes arguments appropriately.", "[utility],[function]")
|
|||||||
REQUIRE(bool_to_set == true);
|
REQUIRE(bool_to_set == true);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
TEST_CASE("Function works with toStaticFunction.", "[utility],[function]")
|
||||||
|
{
|
||||||
|
static int int_to_set = 0;
|
||||||
|
static bool bool_to_set = false;
|
||||||
|
|
||||||
|
auto func_to_call = [](const int i, const bool b) {
|
||||||
|
int_to_set = i;
|
||||||
|
bool_to_set = b;
|
||||||
|
};
|
||||||
|
|
||||||
|
Utility::Function<void(int, bool)> function(func_to_call);
|
||||||
|
using signature = void (*)(int, bool);
|
||||||
|
|
||||||
|
signature func_pointer = function.toStaticFunction<SKULLC_TAG>();
|
||||||
|
func_pointer(10, true);
|
||||||
|
|
||||||
|
REQUIRE(int_to_set == 10);
|
||||||
|
REQUIRE(bool_to_set == true);
|
||||||
|
}
|
||||||
|
|
||||||
TEST_CASE("FunctionOwned calls function appropriately.", "[utility],[function]")
|
TEST_CASE("FunctionOwned calls function appropriately.", "[utility],[function]")
|
||||||
{
|
{
|
||||||
struct S
|
struct S
|
||||||
@ -82,3 +103,29 @@ TEST_CASE("FunctionOwned passes arguments appropriately.", "[utility],[function]
|
|||||||
REQUIRE(subject.int_to_set == 10);
|
REQUIRE(subject.int_to_set == 10);
|
||||||
REQUIRE(subject.bool_to_set == false);
|
REQUIRE(subject.bool_to_set == false);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
TEST_CASE("FunctionOwned works with toStaticFunction.", "[utility],[function]")
|
||||||
|
{
|
||||||
|
struct S
|
||||||
|
{
|
||||||
|
int int_to_set = 0;
|
||||||
|
bool bool_to_set = true;
|
||||||
|
|
||||||
|
void toCall(const int i, const bool b)
|
||||||
|
{
|
||||||
|
int_to_set = i;
|
||||||
|
bool_to_set = b;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
S subject;
|
||||||
|
|
||||||
|
Utility::FunctionOwned<S, void(int, bool)> function(subject, &S::toCall);
|
||||||
|
using signature = void (*)(int, bool);
|
||||||
|
|
||||||
|
signature func_pointer = function.toStaticFunction<SKULLC_TAG>();
|
||||||
|
func_pointer(10, true);
|
||||||
|
|
||||||
|
REQUIRE(subject.int_to_set == 10);
|
||||||
|
REQUIRE(subject.bool_to_set == true);
|
||||||
|
}
|
||||||
|
|||||||
@ -19,11 +19,19 @@ class IFunction<R(Args...)>
|
|||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
using result_type = R;
|
using result_type = R;
|
||||||
|
using static_signature = R (*)(Args...);
|
||||||
|
|
||||||
virtual ~IFunction()
|
virtual ~IFunction()
|
||||||
{}
|
{}
|
||||||
|
|
||||||
virtual result_type operator()(Args... args) = 0;
|
virtual result_type operator()(Args... args) = 0;
|
||||||
|
|
||||||
|
template<typename Tag>
|
||||||
|
static_signature toStaticFunction()
|
||||||
|
{
|
||||||
|
static decltype(this) h = this;
|
||||||
|
return +[](Args... args) { (*h)(args...); };
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
template<class>
|
template<class>
|
||||||
|
|||||||
12
Utility/Inc/utility_tag.hpp
Normal file
12
Utility/Inc/utility_tag.hpp
Normal file
@ -0,0 +1,12 @@
|
|||||||
|
//
|
||||||
|
// Created by erki on 26.06.22.
|
||||||
|
//
|
||||||
|
|
||||||
|
#ifndef SKULLC_UTILITY_TAG_HPP_
|
||||||
|
#define SKULLC_UTILITY_TAG_HPP_
|
||||||
|
|
||||||
|
#define SKULLC_CONCAT_IMPL(x, y) x##y
|
||||||
|
#define SKULLC_CONCAT(x, y) SKULLC_CONCAT_IMPL(x, y)
|
||||||
|
#define SKULLC_TAG struct SKULLC_CONCAT(SkullCTag_, __COUNTER__)
|
||||||
|
|
||||||
|
#endif// SKULLC_UTILITY_TAG_HPP_
|
||||||
Loading…
x
Reference in New Issue
Block a user