Actually call on_reset callback
[lsnes.git] / include / library / lua-function.hpp
blobaa341361ca23c855331992fc04456bff9fbb088d
1 #ifndef _library__lua_function__hpp__included__
2 #define _library__lua_function__hpp__included__
4 #include <functional>
6 namespace lua
8 class parameters;
9 class state;
11 /**
12 * Group of functions.
14 class function_group
16 public:
17 /**
18 * Create a group.
20 function_group();
21 /**
22 * Destroy a group.
24 ~function_group();
25 /**
26 * Add a function to group.
28 void do_register(const std::string& name, function& fun);
29 /**
30 * Drop a function from group.
32 void do_unregister(const std::string& name, function& dummy);
33 /**
34 * Request callbacks on all currently registered functions.
36 void request_callback(std::function<void(std::string, function*)> cb);
37 /**
38 * Bind a callback.
40 * Callbacks for all registered functions are immediately called.
42 int add_callback(std::function<void(std::string, function*)> cb,
43 std::function<void(function_group*)> dcb);
44 /**
45 * Unbind a calback.
47 void drop_callback(int handle);
48 private:
49 char dummy;
52 /**
53 * Function implemented in C++ exported to Lua.
55 class function
57 public:
58 /**
59 * Register function.
61 function(function_group& group, const std::string& name);
62 /**
63 * Unregister function.
65 virtual ~function() throw();
67 /**
68 * Invoke function.
70 virtual int invoke(state& L) = 0;
71 protected:
72 std::string fname;
73 function_group& group;
76 /**
77 * Register multiple functions at once.
79 class functions
81 public:
82 /**
83 * Entry in list.
85 struct entry
87 const std::string& name;
88 std::function<int(state& L, parameters& P)> func;
90 /**
91 * Create new functions.
93 * Parameter grp: The group to put the functions to.
94 * Parameter basetable: The base table to interpret function names relative to.
95 * Parameter fnlist: The list of functions to register.
97 functions(function_group& grp, const std::string& basetable, std::initializer_list<entry> fnlist);
98 /**
99 * Dtor.
101 ~functions();
102 private:
103 class fn : public function
105 public:
106 fn(function_group& grp, const std::string& name, std::function<int(state& L, parameters& P)> _func);
107 ~fn() throw();
108 int invoke(state& L);
109 private:
110 std::function<int(state& L, parameters& P)> func;
112 functions(const functions&);
113 functions& operator=(const functions&);
114 std::set<fn*> funcs;
119 #endif