Utility: add Function and FunctionOwned classes
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
Erki 2021-07-03 12:09:25 +03:00
parent c0f1ca6a87
commit e5c5df5373
3 changed files with 157 additions and 1 deletions

View File

@ -18,7 +18,7 @@ add_executable(tests
rand.cpp
fixedpoint.cpp
pixelbuffer.cpp
pixelbuffer_effects.cpp)
pixelbuffer_effects.cpp function.cpp)
target_link_libraries(tests
PUBLIC

84
Tests/function.cpp Normal file
View File

@ -0,0 +1,84 @@
//
// Created by erki on 03.07.21.
//
#include <catch2/catch.hpp>
#include <utility_function.hpp>
TEST_CASE("Function calls function appropriately.", "[utility],[function]")
{
static bool func_called = false;
auto func_to_call = []() {
func_called = true;
};
Utility::Function<void()> function(func_to_call);
function();
REQUIRE(func_called == true);
}
TEST_CASE("Function passes arguments appropriately.", "[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);
function(10, true);
REQUIRE(int_to_set == 10);
REQUIRE(bool_to_set == true);
}
TEST_CASE("FunctionOwned calls function appropriately.", "[utility],[function]")
{
struct S
{
bool to_set = false;
void toCall()
{
to_set = true;
}
};
S subject;
Utility::FunctionOwned<S, void()> function(subject, &S::toCall);
function();
REQUIRE(subject.to_set == true);
}
TEST_CASE("FunctionOwned passes arguments appropriately.", "[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);
function(10, false);
REQUIRE(subject.int_to_set == 10);
REQUIRE(subject.bool_to_set == false);
}

View File

@ -0,0 +1,72 @@
/*
* utility_function.hpp
*
* Created on: Jun 20, 2021
* Author: erki
*/
#ifndef SKULLC_UTILITY_FUNCTION_HPP_
#define SKULLC_UTILITY_FUNCTION_HPP_
namespace Utility
{
template<class>
class Function;
template<typename R, typename... Args>
class Function<R(Args...)>
{
public:
using result_type = R;
using signature = R (*)(Args...);
Function() = delete;
explicit Function(signature callable)
: callable_(callable)
{
assert(callable_);
}
result_type operator()(Args&&... args)
{
return (*callable_)(std::forward<Args>(args)...);
}
private:
signature callable_ = nullptr;
};
template<class, class>
class FunctionOwned;
template<typename H, typename R, typename... Args>
class FunctionOwned<H, R(Args...)>
{
public:
using source = H;
using result_type = R;
using signature = R (source::*)(Args...);
FunctionOwned() = delete;
explicit FunctionOwned(source& src, signature callable)
: src_(&src), callable_(callable)
{
assert(callable_);
}
result_type operator()(Args&&... args)
{
return (src_->*callable_)(std::forward<Args>(args)...);
}
private:
source* src_ = nullptr;
signature callable_ = nullptr;
};
}// namespace Utility
#endif /* SKULLC_UTILITY_FUNCTION_HPP_ */