66 lines
1.6 KiB
C++
66 lines
1.6 KiB
C++
#include <memory>
|
|
|
|
|
|
|
|
template <typename Fun>
|
|
class Function;
|
|
|
|
|
|
template <typename Ret, typename... Args>
|
|
class Function<Ret(Args...)>
|
|
{
|
|
private:
|
|
class callable_base
|
|
{
|
|
public:
|
|
virtual Ret call(Args...) = 0;
|
|
virtual ~callable_base() {}
|
|
//virtual ~callable_base() = default; // melyik számít szebbnek? van bármi különbség?
|
|
};
|
|
|
|
template <typename Fun>
|
|
class callable : public callable_base
|
|
{
|
|
private:
|
|
Fun _fun;
|
|
public:
|
|
callable(Fun fun):_fun(fun) {}
|
|
|
|
virtual Ret call(Args... args) override
|
|
{ return _fun(std::forward<Args>(args)...); }
|
|
|
|
virtual ~callable() override {}
|
|
};
|
|
|
|
std::unique_ptr _fun;
|
|
/* kérdés, hogy ezt lehet-e szebben, vagy ez így a state-of-the-art megoldás?
|
|
* lent majd látod, de úgy van megoldva, hogy overloadolva van az operator=
|
|
* std::nullptr_t-re. próbáltam varázsolni ilyen std::is_null_pointer-rel,
|
|
* meg hasonlókkal a callable::operator()-ben, meg a Function::operator()-ben,
|
|
* de nem akarta az igazat. */
|
|
bool _is_null = false;
|
|
|
|
public:
|
|
Function() {}
|
|
|
|
template <typename Fun>
|
|
Function& operator=(Fun fun)
|
|
{
|
|
_fun = std::make_unique<callable<Fun>>(fun);
|
|
_is_null = false;
|
|
return *this;
|
|
}
|
|
|
|
Function& operator=(std::nullptr_t)
|
|
{ _is_null = true; }
|
|
|
|
Ret operator()(Args... args) const
|
|
{
|
|
if (_is_null)
|
|
throw std::bad_function_call("Error: call to nullptr Function!");
|
|
return _fun->call(std::forward<Args>(args)...);
|
|
}
|
|
|
|
operator bool() const
|
|
{ return static_cast<bool>(_fun); }
|
|
}; |