Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
alexbevi
GitHub Repository: alexbevi/BizHawk
Path: blob/master/libsnes/bsnes/nall/function.hpp
2 views
1
#ifndef NALL_FUNCTION_HPP
2
#define NALL_FUNCTION_HPP
3
4
namespace nall {
5
template<typename T> class function;
6
7
template<typename R, typename... P> class function<R (P...)> {
8
struct container {
9
virtual R operator()(P... p) const = 0;
10
virtual container* copy() const = 0;
11
virtual ~container() {}
12
} *callback;
13
14
struct global : container {
15
R (*function)(P...);
16
R operator()(P... p) const { return function(std::forward<P>(p)...); }
17
container* copy() const { return new global(function); }
18
global(R (*function)(P...)) : function(function) {}
19
};
20
21
template<typename C> struct member : container {
22
R (C::*function)(P...);
23
C *object;
24
R operator()(P... p) const { return (object->*function)(std::forward<P>(p)...); }
25
container* copy() const { return new member(function, object); }
26
member(R (C::*function)(P...), C *object) : function(function), object(object) {}
27
};
28
29
template<typename L> struct lambda : container {
30
mutable L object;
31
R operator()(P... p) const { return object(std::forward<P>(p)...); }
32
container* copy() const { return new lambda(object); }
33
lambda(const L& object) : object(object) {}
34
};
35
36
public:
37
operator bool() const { return callback; }
38
R operator()(P... p) const { return (*callback)(std::forward<P>(p)...); }
39
void reset() { if(callback) { delete callback; callback = nullptr; } }
40
41
function& operator=(const function &source) {
42
if(this != &source) {
43
if(callback) { delete callback; callback = nullptr; }
44
if(source.callback) callback = source.callback->copy();
45
}
46
return *this;
47
}
48
49
function(const function &source) : callback(nullptr) { operator=(source); }
50
function() : callback(nullptr) {}
51
function(void *function) : callback(nullptr) { if(function) callback = new global((R (*)(P...))function); }
52
function(R (*function)(P...)) { callback = new global(function); }
53
template<typename C> function(R (C::*function)(P...), C *object) { callback = new member<C>(function, object); }
54
template<typename C> function(R (C::*function)(P...) const, C *object) { callback = new member<C>((R (C::*)(P...))function, object); }
55
template<typename L> function(const L& object) { callback = new lambda<L>(object); }
56
~function() { if(callback) delete callback; }
57
};
58
}
59
60
#endif
61
62