mirror of
https://github.com/ThePhD/sol2.git
synced 2024-03-22 13:10:44 +08:00
LuaBridge and luwra interop examples as well, to give people a good idea
This commit is contained in:
parent
469be6670f
commit
0c20d066b9
122
examples/interop/LuaBridge.cpp
Normal file
122
examples/interop/LuaBridge.cpp
Normal file
@ -0,0 +1,122 @@
|
||||
#define SOL_CHECK_ARGUMENTS 1
|
||||
#define SOL_ENABLE_INTEROP 1 // MUST be defined to use interop features
|
||||
#include <sol.hpp>
|
||||
|
||||
#include <LuaBridge/LuaBridge.h>
|
||||
|
||||
#include <iostream>
|
||||
#include <cassert>
|
||||
|
||||
// LuaBridge,
|
||||
// no longer maintained by VinnieFalco:
|
||||
// https://github.com/vinniefalco/LuaBridge
|
||||
|
||||
struct A {
|
||||
|
||||
A(int v)
|
||||
: v_(v) {
|
||||
}
|
||||
|
||||
void print() {
|
||||
std::cout << "called A::print" << std::endl;
|
||||
}
|
||||
|
||||
int value() const {
|
||||
return v_;
|
||||
}
|
||||
|
||||
private:
|
||||
int v_ = 50;
|
||||
};
|
||||
|
||||
namespace sol {
|
||||
namespace stack {
|
||||
template <typename T>
|
||||
struct userdata_checker<extensible<T>> {
|
||||
template <typename Handler>
|
||||
static bool check(lua_State* L, int relindex, type index_type, Handler&& handler, record& tracking) {
|
||||
// just marking unused parameters for no compiler warnings
|
||||
(void)index_type;
|
||||
(void)handler;
|
||||
tracking.use(1);
|
||||
int index = lua_absindex(L, relindex);
|
||||
T* corrected = luabridge::Userdata::get<T>(L, index, true);
|
||||
return corrected != nullptr;
|
||||
}
|
||||
};
|
||||
|
||||
template <typename T>
|
||||
struct userdata_getter<extensible<T>> {
|
||||
static std::pair<bool, T*> get(lua_State* L, int relindex, void* unadjusted_pointer, record& tracking) {
|
||||
(void)unadjusted_pointer;
|
||||
int index = lua_absindex(L, relindex);
|
||||
if (!userdata_checker<extensible<T>>::check(L, index, type::userdata, no_panic, tracking)) {
|
||||
return { false, nullptr };
|
||||
}
|
||||
tracking.use(1);
|
||||
T* corrected = luabridge::Userdata::get<T>(L, index, true);
|
||||
return { true, corrected };
|
||||
}
|
||||
};
|
||||
}
|
||||
} // namespace sol::stack
|
||||
|
||||
void register_sol_stuff(lua_State* L) {
|
||||
// grab raw state and put into state_view
|
||||
// state_view is cheap to construct
|
||||
sol::state_view lua(L);
|
||||
// bind and set up your things: everything is entirely self-contained
|
||||
lua["f"] = sol::overload(
|
||||
[](A& from_luabridge) {
|
||||
std::cout << "calling 1-argument version with luabridge-created A {" << from_luabridge.value() << "}" << std::endl;
|
||||
},
|
||||
[](A& from_luabridge, int second_arg) {
|
||||
std::cout << "calling 2-argument version with luabridge-created A {" << from_luabridge.value() << "} and integer argument of " << second_arg << std::endl;
|
||||
});
|
||||
}
|
||||
|
||||
void check_with_sol(lua_State* L) {
|
||||
sol::state_view lua(L);
|
||||
A& obj = lua["obj"];
|
||||
(void)obj;
|
||||
assert(obj.value() == 24);
|
||||
}
|
||||
|
||||
int main(int, char* []) {
|
||||
|
||||
std::cout << "=== interop example (LuaBridge) ===" << std::endl;
|
||||
std::cout << "code modified from LuaBridge's examples: https://github.com/vinniefalco/LuaBridge" << std::endl;
|
||||
|
||||
struct closer {
|
||||
void operator()(lua_State* L) {
|
||||
lua_close(L);
|
||||
}
|
||||
};
|
||||
|
||||
std::unique_ptr<lua_State, closer> state(luaL_newstate());
|
||||
lua_State* L = state.get();
|
||||
|
||||
luabridge::getGlobalNamespace(L)
|
||||
.beginNamespace("test")
|
||||
.beginClass<A>("A")
|
||||
.addConstructor<void (*)(int)>()
|
||||
.addFunction("print", &A::print)
|
||||
.addFunction("value", &A::value)
|
||||
.endClass()
|
||||
.endNamespace();
|
||||
|
||||
register_sol_stuff(L);
|
||||
|
||||
|
||||
if (luaL_dostring(L, R"(
|
||||
obj = test.A(24)
|
||||
f(obj) -- call 1 argument version
|
||||
f(obj, 5) -- call 2 argument version
|
||||
)")) {
|
||||
lua_error(L);
|
||||
}
|
||||
|
||||
check_with_sol(L);
|
||||
|
||||
return 0;
|
||||
}
|
@ -2,7 +2,7 @@
|
||||
#define SOL_ENABLE_INTEROP 1 // MUST be defined to use interop features
|
||||
#include <sol.hpp>
|
||||
|
||||
#include "kaguya.hpp"
|
||||
#include <kaguya/kaguya.hpp>
|
||||
|
||||
#include <iostream>
|
||||
#include <cassert>
|
||||
@ -44,12 +44,13 @@ namespace stack {
|
||||
template <typename T>
|
||||
struct userdata_checker<extensible<T>> {
|
||||
template <typename Handler>
|
||||
static bool check(lua_State* L, int index, type index_type, Handler&& handler, record& tracking) {
|
||||
static bool check(lua_State* L, int relindex, type index_type, Handler&& handler, record& tracking) {
|
||||
// just marking unused parameters for no compiler warnings
|
||||
(void)index_type;
|
||||
(void)handler;
|
||||
// using 1 element
|
||||
tracking.use(1);
|
||||
int index = lua_absindex(L, relindex);
|
||||
// use kaguya's own detail wrapper check to see if it's correct
|
||||
bool is_correct_type = kaguya::detail::object_wrapper_type_check(L, index);
|
||||
return is_correct_type;
|
||||
@ -58,7 +59,7 @@ namespace stack {
|
||||
|
||||
template <typename T>
|
||||
struct userdata_getter<extensible<T>> {
|
||||
static std::pair<bool, T*> get(lua_State* L, int index, void* unadjusted_pointer, record& tracking) {
|
||||
static std::pair<bool, T*> get(lua_State* L, int relindex, void* unadjusted_pointer, record& tracking) {
|
||||
// you may not need to specialize this method every time:
|
||||
// some libraries are compatible with sol2's layout
|
||||
|
||||
@ -66,6 +67,7 @@ namespace stack {
|
||||
// it stores the data directly in the pointer, not a pointer inside of the `void*`
|
||||
// therefore, leave the raw userdata pointer as-is,
|
||||
// if it's of the right type
|
||||
int index = lua_absindex(L, relindex);
|
||||
if (!kaguya::detail::object_wrapper_type_check(L, index)) {
|
||||
return { false, nullptr };
|
||||
}
|
||||
|
117
examples/interop/luwra.cpp
Normal file
117
examples/interop/luwra.cpp
Normal file
@ -0,0 +1,117 @@
|
||||
#define SOL_CHECK_ARGUMENTS 1
|
||||
#define SOL_ENABLE_INTEROP 1 // MUST be defined to use interop features
|
||||
#include <sol.hpp>
|
||||
|
||||
#include <luwra.hpp>
|
||||
|
||||
#include <iostream>
|
||||
#include <cassert>
|
||||
|
||||
// luwra,
|
||||
// another C++ wrapper library:
|
||||
// https://github.com/vapourismo/luwra
|
||||
|
||||
struct ABC {
|
||||
ABC()
|
||||
: v_(0) {
|
||||
}
|
||||
ABC(int value)
|
||||
: v_(value) {
|
||||
}
|
||||
int value() const {
|
||||
return v_;
|
||||
}
|
||||
void setValue(int v) {
|
||||
v_ = v;
|
||||
}
|
||||
void overload1() {
|
||||
std::cout << "call overload1" << std::endl;
|
||||
}
|
||||
void overload2(int) {
|
||||
std::cout << "call overload2" << std::endl;
|
||||
}
|
||||
|
||||
private:
|
||||
int v_;
|
||||
};
|
||||
|
||||
namespace sol {
|
||||
namespace stack {
|
||||
template <typename T>
|
||||
struct userdata_checker<extensible<T>> {
|
||||
template <typename Handler>
|
||||
static bool check(lua_State* L, int relindex, type index_type, Handler&& handler, record& tracking) {
|
||||
// just marking unused parameters for no compiler warnings
|
||||
(void)index_type;
|
||||
(void)handler;
|
||||
// using 1 element
|
||||
tracking.use(1);
|
||||
int index = lua_absindex(L, relindex);
|
||||
if (lua_getmetatable(L, index) == 1) {
|
||||
luaL_getmetatable(L, luwra::internal::UserTypeReg<T>::name.c_str());
|
||||
bool is_correct_type = lua_rawequal(L, -2, -1) == 1;
|
||||
lua_pop(L, 2);
|
||||
return is_correct_type;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
};
|
||||
|
||||
template <typename T>
|
||||
struct userdata_getter<extensible<T>> {
|
||||
static std::pair<bool, T*> get(lua_State* L, int relindex, void* unadjusted_pointer, record& tracking) {
|
||||
// you may not need to specialize this method every time:
|
||||
// some libraries are compatible with sol2's layout
|
||||
int index = lua_absindex(L, relindex);
|
||||
if (!userdata_checker<extensible<T>>::check(L, index, type::userdata, no_panic, tracking)) {
|
||||
return { false, nullptr };
|
||||
}
|
||||
tracking.use(1);
|
||||
return { true, static_cast<T*>(unadjusted_pointer) };
|
||||
}
|
||||
};
|
||||
}
|
||||
} // namespace sol::stack
|
||||
|
||||
void register_sol_stuff(lua_State* L) {
|
||||
// grab raw state and put into state_view
|
||||
// state_view is cheap to construct
|
||||
sol::state_view lua(L);
|
||||
// bind and set up your things: everything is entirely self-contained
|
||||
lua["f"] = sol::overload(
|
||||
[](ABC& from_luwra) {
|
||||
std::cout << "calling 1-argument version with luwra-created ABC {" << from_luwra.value() << "}" << std::endl;
|
||||
},
|
||||
[](ABC& from_luwra, int second_arg) {
|
||||
std::cout << "calling 2-argument version with luwra-created ABC {" << from_luwra.value() << "} and integer argument of " << second_arg << std::endl;
|
||||
});
|
||||
}
|
||||
|
||||
void check_with_sol(lua_State* L) {
|
||||
sol::state_view lua(L);
|
||||
ABC& obj = lua["obj"];
|
||||
(void)obj;
|
||||
assert(obj.value() == 24);
|
||||
}
|
||||
|
||||
int main(int, char* []) {
|
||||
|
||||
std::cout << "=== interop example (luwra) ===" << std::endl;
|
||||
std::cout << "code modified from luwra's documentation examples: https://github.com/vapourismo/luwra" << std::endl;
|
||||
|
||||
luwra::StateWrapper state;
|
||||
|
||||
state.registerUserType<ABC(int)>("ABC", { LUWRA_MEMBER(ABC, value), LUWRA_MEMBER(ABC, setValue) }, {});
|
||||
|
||||
register_sol_stuff(state);
|
||||
|
||||
state.runString(R"(
|
||||
obj = ABC(24)
|
||||
f(obj) -- call 1 argument version
|
||||
f(obj, 5) -- call 2 argument version
|
||||
)");
|
||||
|
||||
check_with_sol(state);
|
||||
|
||||
return 0;
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user