Add simple alternative to std::function without smart pointers or move semantics

This commit is contained in:
Nanosonde 2020-07-14 13:25:16 +02:00
parent 6a3f5273ca
commit e970ef4dcd
2 changed files with 83 additions and 0 deletions

View File

@ -107,6 +107,7 @@ add_executable(knx-linux
../../src/knx/secure_application_layer.h
../../src/knx/security_interface_object.cpp
../../src/knx/security_interface_object.h
../../src/knx/simple_functional.h
../../src/knx/simple_map.h
../../src/knx/table_object.cpp
../../src/knx/save_restore.h

View File

@ -0,0 +1,82 @@
#pragma once
#include <memory>
//
// Basis is https://shaharmike.com/cpp/naive-std-function/
//
// Do not forget to call cleanup() once you do not need the function object (functor) anymore to free the memory.
// No move semantics and/or smart pointers are used
template <typename T>
class function;
template <typename ReturnValue, typename... Args>
class function<ReturnValue(Args...)>
{
public:
function()
{
//printf("function()\n");
callable_ = nullptr;
}
~function()
{
//printf("~function()\n");
}
template<typename T>
function(T t)
{
//printf("function(T)\n");
callable_ = new CallableT<T>(t);
}
template <typename T>
function& operator=(T t)
{
//printf("operator=(T)\n");
callable_ = new CallableT<T>(t);
return *this;
}
ReturnValue operator()(Args... args) const
{
//assert(callable_);
return callable_->Invoke(args...);
}
void cleanup()
{
delete callable_;
}
private:
class ICallable
{
public:
virtual ~ICallable() = default;
virtual ReturnValue Invoke(Args...) = 0;
};
template <typename T>
class CallableT : public ICallable
{
public:
CallableT(const T& t)
: t_(t) {
}
~CallableT() override = default;
ReturnValue Invoke(Args... args) override
{
return t_(args...);
}
private:
T t_;
};
ICallable* callable_;
};