Skip to content

Commit

Permalink
Change class registration syntax to avoid pairs.
Browse files Browse the repository at this point in the history
  • Loading branch information
jeremyong committed Jan 22, 2014
1 parent 44b9360 commit 4ba6093
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 15 deletions.
7 changes: 4 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -126,10 +126,11 @@ sel::State state;
Foo foo(2);

// Binds the C++ instance foo to a table also called foo in Lua along
// with two methods bound to fields of that table
// with two methods bound to fields of that table.
// The user is not required to bind all methods
state.Register("foo", foo,
std::make_pair("double_add", &Foo::DoubleAdd),
std::make_pair("set_x", &Foo::SetX));
"double_add", &Foo::DoubleAdd,
"set_x", &Foo::SetX);

state.CallField("foo", "set_x", 4);
assert(foo.x == 4);
Expand Down
10 changes: 5 additions & 5 deletions include/Class.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
#include "State.h"
#include <string>
#include "util.h"
#include <utility>
#include <vector>

namespace sel {
Expand Down Expand Up @@ -38,14 +37,15 @@ class Class : public BaseClass {

template <typename F, typename... Fs>
void _register_funs(lua_State *state, T *t,
std::pair<const char *, F> fun,
std::pair<const char *, Fs>... funs) {
_register_fun(state, t, fun.first, fun.second);
const char *name,
F fun,
Fs... funs) {
_register_fun(state, t, name, fun);
_register_funs(state, t, funs...);
}
public:
Class(lua_State *&state, T *t, const std::string &name,
std::pair<const char *, Funs>... funs)
Funs... funs)
: _name(state, name) {
lua_createtable(state, 0, sizeof...(Funs));
_register_funs(state, t, funs...);
Expand Down
2 changes: 1 addition & 1 deletion include/State.h
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ class State {

template <typename T, typename... Funs>
void Register(const std::string &name, T &t,
std::pair<const char *, Funs>... funs) {
Funs... funs) {
Unregister(name);
auto tmp = std::unique_ptr<BaseClass>(
new Class<T, Funs...>{_l, &t, name, funs...});
Expand Down
12 changes: 6 additions & 6 deletions test/class_tests.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ bool test_register_class() {
sel::State state;
state.Register("foo_instance",
foo_instance,
std::make_pair("double_add", &Foo::DoubleAdd));
"double_add", &Foo::DoubleAdd);
const int answer = state.CallField<int>("foo_instance", "double_add", 3);
return (answer == 8);
}
Expand All @@ -28,7 +28,7 @@ bool test_mutate_instance() {
sel::State state;
state.Register("foo_instance",
foo_instance,
std::make_pair("set_x", &Foo::SetX));
"set_x", &Foo::SetX);
state.CallField("foo_instance", "set_x", 4);
return (foo_instance.x == 4);
}
Expand All @@ -38,8 +38,8 @@ bool test_multiple_methods() {
sel::State state;
state.Register("foo_instance",
foo_instance,
std::make_pair("double_add", &Foo::DoubleAdd),
std::make_pair("set_x", &Foo::SetX));
"double_add", &Foo::DoubleAdd,
"set_x", &Foo::SetX);
state.CallField("foo_instance", "set_x", 4);
const int answer = state.CallField<int>("foo_instance", "double_add", 3);
return (answer == 14);
Expand All @@ -50,8 +50,8 @@ bool test_unregister_instance() {
sel::State state;
state.Register("foo_instance",
foo_instance,
std::make_pair("double_add", &Foo::DoubleAdd),
std::make_pair("set_x", &Foo::SetX));
"double_add", &Foo::DoubleAdd,
"set_x", &Foo::SetX);
bool exists = !state.CheckNil("foo_instance");
state.Unregister("foo_instance");
return exists && state.CheckNil("foo_instance");
Expand Down

0 comments on commit 4ba6093

Please sign in to comment.