mirror of
https://github.com/ThePhD/sol2.git
synced 2024-03-22 13:10:44 +08:00
New example.
This commit is contained in:
parent
11d6e8c8d4
commit
5d7d1329fa
33
examples/source/table_as_container.cpp
Normal file
33
examples/source/table_as_container.cpp
Normal file
|
@ -0,0 +1,33 @@
|
||||||
|
#define SOL_ALL_SAFETIES_ON 1
|
||||||
|
#include <sol/sol.hpp>
|
||||||
|
|
||||||
|
#include "assert.hpp"
|
||||||
|
|
||||||
|
struct Vector {
|
||||||
|
int x;
|
||||||
|
int y;
|
||||||
|
|
||||||
|
Vector() = default;
|
||||||
|
|
||||||
|
Vector(int _x, int _y) : x { _x }, y { _y } {
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
int main() {
|
||||||
|
sol::state lua;
|
||||||
|
lua.open_libraries(sol::lib::base);
|
||||||
|
|
||||||
|
lua.new_usertype<Vector>(
|
||||||
|
"Vector", sol::constructors<Vector(), Vector(int, int)>(), "x", sol::property(&Vector::x, &Vector::x), "y", sol::property(&Vector::y, &Vector::y));
|
||||||
|
|
||||||
|
lua.script("vectors = { Vector.new(3, 6), Vector.new(6, 3) }");
|
||||||
|
auto vectors = lua["vectors"].get<std::vector<Vector>>();
|
||||||
|
|
||||||
|
c_assert(vectors[0].x == 3);
|
||||||
|
c_assert(vectors[0].y == 6);
|
||||||
|
|
||||||
|
c_assert(vectors[1].x == 6);
|
||||||
|
c_assert(vectors[1].y == 3);
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
|
@ -3,9 +3,7 @@
|
||||||
|
|
||||||
#include "assert.hpp"
|
#include "assert.hpp"
|
||||||
|
|
||||||
#include <iostream>
|
int main() {
|
||||||
|
|
||||||
int main () {
|
|
||||||
|
|
||||||
const auto& code = R"(
|
const auto& code = R"(
|
||||||
bark = {
|
bark = {
|
||||||
|
@ -40,7 +38,7 @@ int main () {
|
||||||
lua.script("assert(bark.woof[2] == 20)");
|
lua.script("assert(bark.woof[2] == 20)");
|
||||||
|
|
||||||
lua["a_new_value"] = 24;
|
lua["a_new_value"] = 24;
|
||||||
lua["chase_tail"] = [](int chasing) {
|
lua["chase_tail"] = [](int chasing) {
|
||||||
int r = 2;
|
int r = 2;
|
||||||
for (int i = 0; i < chasing; ++i) {
|
for (int i = 0; i < chasing; ++i) {
|
||||||
r *= r;
|
r *= r;
|
||||||
|
|
Loading…
Reference in New Issue
Block a user