mirror of
https://github.com/ThePhD/sol2.git
synced 2024-03-22 13:10:44 +08:00
Another example, constness derp fix
This commit is contained in:
parent
d51db7d23d
commit
0ba4650c64
41
examples/usertype_var.cpp
Normal file
41
examples/usertype_var.cpp
Normal file
|
@ -0,0 +1,41 @@
|
||||||
|
#define SOL_CHECK_ARGUMENTS
|
||||||
|
#include <sol.hpp>
|
||||||
|
#include <iostream>
|
||||||
|
|
||||||
|
struct test {
|
||||||
|
static int number;
|
||||||
|
};
|
||||||
|
int test::number = 25;
|
||||||
|
|
||||||
|
|
||||||
|
int main() {
|
||||||
|
sol::state lua;
|
||||||
|
lua.open_libraries();
|
||||||
|
lua.new_usertype<test>("test",
|
||||||
|
"direct", sol::var(2),
|
||||||
|
"number", sol::var(test::number),
|
||||||
|
"ref_number", sol::var(std::ref(test::number))
|
||||||
|
);
|
||||||
|
|
||||||
|
int direct_value = lua["test"]["direct"];
|
||||||
|
assert(direct_value == 2);
|
||||||
|
// direct_value == 2
|
||||||
|
|
||||||
|
int number = lua["test"]["number"];
|
||||||
|
assert(number == 25);
|
||||||
|
int ref_number = lua["test"]["ref_number"];
|
||||||
|
assert(ref_number == 25);
|
||||||
|
|
||||||
|
test::number = 542;
|
||||||
|
|
||||||
|
assert(lua["test"]["number"] == 25);
|
||||||
|
// number is its own memory: was passed by value
|
||||||
|
// So does not change
|
||||||
|
|
||||||
|
assert(lua["test"]["ref_number"] == 25);
|
||||||
|
// ref_number is just test::number
|
||||||
|
// passed through std::ref
|
||||||
|
// so, it holds a reference
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
|
@ -20,8 +20,8 @@
|
||||||
// CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
// CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||||
|
|
||||||
// This file was generated with a script.
|
// This file was generated with a script.
|
||||||
// Generated 2016-10-05 04:59:38.196043 UTC
|
// Generated 2016-10-06 00:39:21.588762 UTC
|
||||||
// This header was generated with sol v2.14.9 (revision 009d6b0)
|
// This header was generated with sol v2.14.9 (revision d51db7d)
|
||||||
// https://github.com/ThePhD/sol2
|
// https://github.com/ThePhD/sol2
|
||||||
|
|
||||||
#ifndef SOL_SINGLE_INCLUDE_HPP
|
#ifndef SOL_SINGLE_INCLUDE_HPP
|
||||||
|
@ -9423,7 +9423,7 @@ namespace sol {
|
||||||
}
|
}
|
||||||
|
|
||||||
template<std::size_t... I, typename T>
|
template<std::size_t... I, typename T>
|
||||||
void tuple_set(std::index_sequence<I...>, T&& value) const {
|
void tuple_set(std::index_sequence<I...>, T&& value) {
|
||||||
tbl.traverse_set(std::get<I>(key)..., std::forward<T>(value));
|
tbl.traverse_set(std::get<I>(key)..., std::forward<T>(value));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -40,7 +40,7 @@ namespace sol {
|
||||||
}
|
}
|
||||||
|
|
||||||
template<std::size_t... I, typename T>
|
template<std::size_t... I, typename T>
|
||||||
void tuple_set(std::index_sequence<I...>, T&& value) const {
|
void tuple_set(std::index_sequence<I...>, T&& value) {
|
||||||
tbl.traverse_set(std::get<I>(key)..., std::forward<T>(value));
|
tbl.traverse_set(std::get<I>(key)..., std::forward<T>(value));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
15
tests.cpp
15
tests.cpp
|
@ -663,3 +663,18 @@ TEST_CASE("proxy/proper-pushing", "allow proxies to reference other proxies and
|
||||||
bool b = lua["b"];
|
bool b = lua["b"];
|
||||||
REQUIRE(b);
|
REQUIRE(b);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
TEST_CASE("compilation/const-regression", "make sure constness in tables is respected all the way down") {
|
||||||
|
struct State {
|
||||||
|
public:
|
||||||
|
State() {
|
||||||
|
this->state_.registry()["state"] = this;
|
||||||
|
}
|
||||||
|
|
||||||
|
sol::state state_;
|
||||||
|
};
|
||||||
|
|
||||||
|
State state;
|
||||||
|
State* s = state.state_.registry()["state"];
|
||||||
|
REQUIRE(s == &state);
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in New Issue
Block a user