update all the examples

This commit is contained in:
ThePhD 2017-03-16 08:51:19 -04:00
parent 31b4dd820c
commit 21bc3ef789
5 changed files with 120 additions and 112 deletions

View File

@ -150,7 +150,11 @@ If you don't specify any constructor options at all and the type is `default_con
.. _constructor: .. _constructor:
* ``"{name}", constructors<T(), T(arg-1-0), T(arg-2-0, arg-2-1), ...>``
- Specify the constructors to be bound under ``name``: list constructors by specifying their function signature with ``class_type(arg0, arg1, ... argN)``
- If you pass the ``constructors<...>`` argument first when constructing the usertype, then it will automatically be given a ``"{name}"`` of ``"new"``
* ``"{name}", constructors<Type-List-0, Type-List-1, ...>`` * ``"{name}", constructors<Type-List-0, Type-List-1, ...>``
- This syntax is longer and provided for backwards-compatibility: the above argument syntax is shorter and cleaner
- ``Type-List-N`` must be a ``sol::types<Args...>``, where ``Args...`` is a list of types that a constructor takes. Supports overloading by default - ``Type-List-N`` must be a ``sol::types<Args...>``, where ``Args...`` is a list of types that a constructor takes. Supports overloading by default
- If you pass the ``constructors<...>`` argument first when constructing the usertype, then it will automatically be given a ``"{name}"`` of ``"new"`` - If you pass the ``constructors<...>`` argument first when constructing the usertype, then it will automatically be given a ``"{name}"`` of ``"new"``
* ``"{name}", sol::initializers( func1, func2, ... )`` * ``"{name}", sol::initializers( func1, func2, ... )``

View File

@ -110,7 +110,7 @@ To do this, you bind things using the ``new_usertype`` and ``set_usertype`` meth
lua.new_usertype<player>( "player", lua.new_usertype<player>( "player",
// 3 constructors // 3 constructors
sol::constructors<sol::types<>, sol::types<int>, sol::types<int, int>>(), sol::constructors<player(), player(int), player(int, int)>(),
// typical member function that returns a variable // typical member function that returns a variable
"shoot", &player::shoot, "shoot", &player::shoot,

View File

@ -1,109 +1,113 @@
#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() {
std::cout << "=== usertype example ===" << std::endl; std::cout << "=== usertype example ===" << std::endl;
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_userdata
// 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_userdata 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_usertype<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");
std::cout << y << std::endl; // show 14 std::cout << y << std::endl; // show 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_userdata and creating
// a userdata yourself with constructor types // a userdata 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; // notice here that the return type
// on the function-type doesn't exactly matter,
// the only template parameter is the class type // which allows you to use a shorter class name/void
// the first argument of construction is the name // if necessary
// second is the constructor types sol::constructors<vector(), vector(float), void(float, float)> ctor;
// then the rest are function name and member function pointer pairs
sol::usertype<vector> utype(ctor, "is_unit", &vector::is_unit); // the only template parameter is the class type
// the first argument of construction is the name
// then you must register it // second is the constructor types
lua.set_usertype("vector", utype); // then the rest are function name and member function pointer pairs
// You can throw away the usertype after you set it: you do NOT sol::usertype<vector> utype(ctor, "is_unit", &vector::is_unit);
// have to keep it around
// cleanup happens automagically // then you must register it
} lua.set_usertype("vector", utype);
// calling it is the same as new_userdata // You can throw away the usertype after you set it: you do NOT
// have to keep it around
lua.script("v = vector.new()\n" // cleanup happens automagically
"v = vector.new(12)\n" }
"v = vector.new(10, 10)\n" // calling it is the same as new_userdata
"assert(not v:is_unit())\n");
lua.script("v = vector.new()\n"
// You can even have C++-like member-variable-access "v = vector.new(12)\n"
// just pass is public member variables in the same style as functions "v = vector.new(10, 10)\n"
lua.new_usertype<variables>("variables", "low_gravity", &variables::low_gravity, "boost_level", &variables::boost_level); "assert(not v:is_unit())\n");
// making the class from lua is simple // You can even have C++-like member-variable-access
// same with calling member functions/variables // just pass is public member variables in the same style as functions
lua.script("local vars = variables.new()\n" lua.new_usertype<variables>("variables", "low_gravity", &variables::low_gravity, "boost_level", &variables::boost_level);
"assert(not vars.low_gravity)\n"
"vars.low_gravity = true\n" // making the class from lua is simple
"local x = vars.low_gravity\n" // same with calling member functions/variables
"assert(x)"); lua.script("local vars = variables.new()\n"
"assert(not vars.low_gravity)\n"
std::cout << std::endl; "vars.low_gravity = true\n"
"local x = vars.low_gravity\n"
} "assert(x)");
std::cout << std::endl;
}

View File

@ -63,7 +63,7 @@ int main() {
lua.new_usertype<player>("player", lua.new_usertype<player>("player",
// 3 constructors // 3 constructors
sol::constructors<sol::types<>, sol::types<int>, sol::types<int, int>>(), sol::constructors<player(), player(int), player(int, int)>(),
// typical member function that returns a variable // typical member function that returns a variable
"shoot", &player::shoot, "shoot", &player::shoot,

View File

@ -29,7 +29,7 @@ int main() {
lua.open_libraries(); lua.open_libraries();
lua.new_usertype<vec>("vec", lua.new_usertype<vec>("vec",
sol::constructors<sol::types<>, sol::types<double, double>>(), sol::constructors<vec(), vec(double, double)>(),
"dot", &dot, "dot", &dot,
"norm", [](const vec& self) { double len = std::sqrt(dot(self, self)); return vec(self.x / len, self.y / len); }, "norm", [](const vec& self) { double len = std::sqrt(dot(self, self)); return vec(self.x / len, self.y / len); },
// we use `sol::resolve` because other operator+ can exist // we use `sol::resolve` because other operator+ can exist