本文整理汇总了C++中kaguya::State::newRef方法的典型用法代码示例。如果您正苦于以下问题:C++ State::newRef方法的具体用法?C++ State::newRef怎么用?C++ State::newRef使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类kaguya::State
的用法示例。
在下文中一共展示了State::newRef方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1:
KAGUYA_TEST_FUNCTION_DEF(compare_null_ptr)(kaguya::State& state)
{
{
kaguya::LuaRef nullref = state.newRef(nullptr);
TEST_CHECK(nullref.typeTest<std::nullptr_t>());
TEST_CHECK(nullref.weakTypeTest<std::nullptr_t>());
TEST_CHECK(nullref == nullptr);
void* ptr = nullref.get<std::nullptr_t>();
TEST_CHECK(!ptr);
}
{
kaguya::LuaRef ref = state.newRef(1);
TEST_CHECK(!(ref.typeTest<std::nullptr_t>()));
TEST_CHECK(!(ref.weakTypeTest<std::nullptr_t>()));
TEST_CHECK(ref != nullptr);
bool catch_except = false;
try
{
ref.get<std::nullptr_t>();
}
catch (const kaguya::LuaTypeMismatch&)
{
catch_except = true;
}
TEST_CHECK(catch_except);
}
}
示例2: capture
KAGUYA_TEST_FUNCTION_DEF(native_function_call_test)(kaguya::State& state)
{
using namespace kaguya::nativefunction;
Foo foo;
state.newRef(6).push();
state.newRef(9).push();
state.newRef(2).push();
call(state.state(), &free_standing_function);
lua_settop(state.state(), 0);
state.newRef(&foo).push();
state.newRef("Bar").push();
call(state.state(), &Foo::setBar);
#if KAGUYA_USE_CPP11
state.newRef(&foo).push();
state.newRef(9).push();
call(state.state(), [](Foo* foo, int b) {
foo->setBar("fromlambda");
});
TEST_EQUAL(foo.bar, "fromlambda");
std::string capture("capture");
call(state.state(), [=](Foo* foo, int b) {
foo->setBar(capture + "lambda");
});
TEST_EQUAL(foo.bar, "capturelambda");
#endif
lua_settop(state.state(), 0);
}
示例3:
KAGUYA_TEST_FUNCTION_DEF(function_call_error)(kaguya::State& state)
{
error_count = 0;
state.setErrorHandler(error_fun);
state["errofun"] = kaguya::function(error_fun);
state["errofun"](33);
TEST_CHECK(error_count == 1);
kaguya::LuaRef f;
f.resume<void>();
f.call<void>();
TEST_COMPARE_EQ(f.threadStatus(), LUA_ERRRUN);
TEST_COMPARE_EQ(state.newRef(1).threadStatus(), LUA_ERRRUN);
}
示例4: MoveOnlyClass
KAGUYA_TEST_FUNCTION_DEF(movable_class_test)(kaguya::State& state)
{
state["MoveOnlyClass"].setClass(kaguya::UserdataMetatable<MoveOnlyClass>()
.setConstructors<MoveOnlyClass(int)>()
.addProperty("member", &MoveOnlyClass::member)
);
state["moveonly"] = MoveOnlyClass(2);
const MoveOnlyClass* ref = state["moveonly"];
TEST_CHECK(ref);
TEST_CHECK(ref->member == 2);
state("func =function(arg) return assert(arg.member == 5) end");
state["func"](MoveOnlyClass(5));
state.newRef(MoveOnlyClass(5));
}