#pragma once #include namespace nall { template struct function; template struct function R> { //value = true if auto L::operator()(P...) -> R exists template struct is_compatible { template static auto exists(T*) -> const typename is_same().operator()(declval

()...))>::type; template static auto exists(...) -> const false_type; static constexpr bool value = decltype(exists(0))::value; }; function() {} function(const function& source) { operator=(source); } function(void* function) { if(function) callback = new global((auto (*)(P...) -> R)function); } function(auto (*function)(P...) -> R) { callback = new global(function); } template function(auto (C::*function)(P...) -> R, C* object) { callback = new member(function, object); } template function(auto (C::*function)(P...) const -> R, C* object) { callback = new member((auto (C::*)(P...) -> R)function, object); } template::value>> function(const L& object) { callback = new lambda(object); } ~function() { if(callback) delete callback; } explicit operator bool() const { return callback; } auto operator()(P... p) const -> R { return (*callback)(forward

(p)...); } auto reset() -> void { if(callback) { delete callback; callback = nullptr; } } auto operator=(const function& source) -> function& { if(this != &source) { if(callback) { delete callback; callback = nullptr; } if(source.callback) callback = source.callback->copy(); } return *this; } private: struct container { virtual auto operator()(P... p) const -> R = 0; virtual auto copy() const -> container* = 0; virtual ~container() = default; }; container* callback = nullptr; struct global : container { auto (*function)(P...) -> R; auto operator()(P... p) const -> R { return function(forward

(p)...); } auto copy() const -> container* { return new global(function); } global(auto (*function)(P...) -> R) : function(function) {} }; template struct member : container { auto (C::*function)(P...) -> R; C* object; auto operator()(P... p) const -> R { return (object->*function)(forward

(p)...); } auto copy() const -> container* { return new member(function, object); } member(auto (C::*function)(P...) -> R, C* object) : function(function), object(object) {} }; template struct lambda : container { mutable L object; auto operator()(P... p) const -> R { return object(forward

(p)...); } auto copy() const -> container* { return new lambda(object); } lambda(const L& object) : object(object) {} }; }; }