mirror of
https://github.com/ThePhD/sol2.git
synced 2024-03-22 13:10:44 +08:00
6c40c559e3
change how type T is gleaned from destructors and constructors in case of new syntax add a hell of a lot more examples, update and clean documentation
48 lines
972 B
C++
48 lines
972 B
C++
#define SOL_CHECK_ARGUMENTS 1
|
|
#include <sol.hpp>
|
|
|
|
#include <iostream>
|
|
#include "../../assert.hpp"
|
|
|
|
int main() {
|
|
std::cout << "=== namespacing ===" << std::endl;
|
|
|
|
struct my_class {
|
|
int b = 24;
|
|
|
|
int f() const {
|
|
return 24;
|
|
}
|
|
|
|
void g() {
|
|
++b;
|
|
}
|
|
};
|
|
|
|
sol::state lua;
|
|
lua.open_libraries();
|
|
|
|
// "bark" namespacing in Lua
|
|
// namespacing is just putting things in a table
|
|
sol::table bark = lua.create_named_table("bark");
|
|
bark.new_usertype<my_class>("my_class",
|
|
"f", &my_class::f,
|
|
"g", &my_class::g); // the usual
|
|
|
|
// can add functions, as well (just like the global table)
|
|
bark.set_function("print_my_class", [](my_class& self) { std::cout << "my_class { b: " << self.b << " }" << std::endl; });
|
|
|
|
// this works
|
|
lua.script("obj = bark.my_class.new()");
|
|
lua.script("obj:g()");
|
|
|
|
// calling this function also works
|
|
lua.script("bark.print_my_class(obj)");
|
|
my_class& obj = lua["obj"];
|
|
c_assert(obj.b == 25);
|
|
|
|
std::cout << std::endl;
|
|
|
|
return 0;
|
|
}
|