mirror of
https://github.com/ThePhD/sol2.git
synced 2024-03-22 13:10:44 +08:00
update all the examples
This commit is contained in:
parent
31b4dd820c
commit
21bc3ef789
|
@ -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, ... )``
|
||||||
|
|
|
@ -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,
|
||||||
|
|
|
@ -71,7 +71,11 @@ int main() {
|
||||||
{
|
{
|
||||||
// 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,
|
||||||
|
// which allows you to use a shorter class name/void
|
||||||
|
// if necessary
|
||||||
|
sol::constructors<vector(), vector(float), void(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
|
||||||
|
|
|
@ -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,
|
||||||
|
|
|
@ -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
|
||||||
|
|
Loading…
Reference in New Issue
Block a user