Fix userdata -> usertype in examples.

This commit is contained in:
Rapptz 2015-07-21 19:26:35 -04:00
parent cd46aac23c
commit a5e24b4fa2
3 changed files with 111 additions and 106 deletions

View File

@ -30,7 +30,7 @@ struct vars {
int main() { int main() {
sol::state lua; sol::state lua;
lua.new_userdata<vars>("vars", "boop", &vars::boop); lua.new_usertype<vars>("vars", "boop", &vars::boop);
lua.script("beep = vars.new()\n" lua.script("beep = vars.new()\n"
"beep.boop = 1"); "beep.boop = 1");
assert(lua.get<vars>("beep").boop == 1); assert(lua.get<vars>("beep").boop == 1);

View File

@ -50,7 +50,7 @@ args = parser.parse_args()
# general variables # general variables
include = [ '.', os.path.join('Catch', 'include')] include = [ '.', os.path.join('Catch', 'include')]
depends = [] depends = []
cxxflags = [ '-Wall', '-Wextra', '-pedantic', '-pedantic-errors', '-std=c++11' ] cxxflags = [ '-Wall', '-Wextra', '-pedantic', '-pedantic-errors', '-std=c++11', '-Wno-unused-variable' ]
ldflags = [] ldflags = []
script_dir = os.path.dirname(os.path.realpath(sys.argv[0])) script_dir = os.path.dirname(os.path.realpath(sys.argv[0]))
sol_dir = os.path.join(script_dir, 'sol') sol_dir = os.path.join(script_dir, 'sol')

View File

@ -1,104 +1,109 @@
#include <sol.hpp> #include <sol.hpp>
#include <iostream> #include <iostream>
#include <cassert> #include <cassert>
#include <cmath> #include <cmath>
struct foo { struct foo {
private: private:
std::string name; std::string name;
public: public:
foo(std::string name): name(std::string(name)) {} foo(std::string name): name(std::string(name)) {}
void print() { void print() {
std::cout << name << '\n'; std::cout << name << '\n';
} }
int test(int x) { int test(int x) {
return name.length() + x; return name.length() + x;
} }
}; };
struct vector { struct vector {
private: private:
float x = 0; float x = 0;
float y = 0; float y = 0;
public: public:
vector() = default; vector() = default;
vector(float x): x(x) {} vector(float x): x(x) {}
vector(float x, float y): x(x), y(y) {} vector(float x, float y): x(x), y(y) {}
bool is_unit() const { bool is_unit() const {
return (x * x + y * y) == 1.f; return (x * x + y * y) == 1.f;
} }
}; };
struct variables { struct variables {
bool low_gravity = false; bool low_gravity = false;
int boost_level = 0; int boost_level = 0;
}; };
int main() { int main() {
sol::state lua; sol::state lua;
lua.open_libraries(sol::lib::base, sol::lib::math); lua.open_libraries(sol::lib::base, sol::lib::math);
// the simplest way to create a class is through // the simplest way to create a class is through
// sol::state::new_userdata // sol::state::new_usertype
// the first template is the class type // the first template is the class type
// the rest are the constructor parameters // the rest are the constructor parameters
// using new_userdata you can only have one constructor // using new_usertype you can only have one constructor
// you must make sure that the name of the function // you must make sure that the name of the function
// goes before the member function pointer // goes before the member function pointer
lua.new_userdata<foo, std::string>("foo", "print", &foo::print, "test", &foo::test); lua.new_usertype<foo, std::string>("foo", "print", &foo::print, "test", &foo::test);
// making the class from lua is simple // making the class from lua is simple
// same with calling member functions // same with calling member functions
lua.script("x = foo.new('test')\n" lua.script("x = foo.new('test')\n"
"x:print()\n" "x:print()\n"
"y = x:test(10)"); "y = x:test(10)");
auto y = lua.get<int>("y"); auto y = lua.get<int>("y");
assert(y == 14); assert(y == 14);
// if you want a class to have more than one constructor // if you want a class to have more than one constructor
// the way to do so is through set_userdata and creating // the way to do so is through set_usertype and creating
// a userdata yourself with constructor types // a usertype yourself with constructor types
{ {
// Notice the brace: this means we're in a new scope // Notice the brace: this means we're in a new scope
// first, define the different types of constructors // first, define the different types of constructors
sol::constructors<sol::types<>, sol::types<float>, sol::types<float, float>> ctor; sol::constructors<sol::types<>, sol::types<float>, sol::types<float, float>> ctor;
// the only template parameter is the class type // the only template parameter is the class type
// the first argument of construction is the name // the first argument of construction is the name
// second is the constructor types // second is the constructor types
// then the rest are function name and member function pointer pairs // then the rest are function name and member function pointer pairs
sol::userdata<vector> udata("vector", ctor, "is_unit", &vector::is_unit); sol::usertype<vector> udata(ctor, "is_unit", &vector::is_unit);
// then you must register it // then you must register it
lua.set_userdata(udata); // by default, the registered name is done through demangling of T in sol::usertype<T>
// You can throw away the userdata after you set it: you do NOT // using the demangling API provided by the compiler
// have to keep it around // if you don't want to use the unmangled name, you can provide your own name like so:
// cleanup happens automagically // lua.set_usertype("vector", udata);
} lua.set_usertype(udata);
// calling it is the same as new_userdata
// You can throw away the usertype after you set it: you do NOT
lua.script("v = vector.new()\n" // have to keep it around
"v = vector.new(12)\n" // cleanup happens automagically
"v = vector.new(10, 10)\n" }
"assert(not v:is_unit())\n"); // calling it is the same as new_usertype
// You can even have C++-like member-variable-access lua.script("v = vector.new()\n"
// just pass is public member variables in the same style as functions "v = vector.new(12)\n"
lua.new_userdata<variables>("variables", "low_gravity", &variables::low_gravity, "boost_level", &variables::boost_level); "v = vector.new(10, 10)\n"
"assert(not v:is_unit())\n");
// making the class from lua is simple
// same with calling member functions/variables // You can even have C++-like member-variable-access
lua.script("local vars = variables.new()\n" // just pass is public member variables in the same style as functions
"assert(not vars.low_gravity)\n" lua.new_usertype<variables>("variables", "low_gravity", &variables::low_gravity, "boost_level", &variables::boost_level);
"vars.low_gravity = true\n"
"local x = vars.low_gravity\n" // making the class from lua is simple
"assert(x)"); // same with calling member functions/variables
} lua.script("local vars = variables.new()\n"
"assert(not vars.low_gravity)\n"
"vars.low_gravity = true\n"
"local x = vars.low_gravity\n"
"assert(x)");
}