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); }
int main(int argc, char *argv[]) { cout << "main: This routine tests const conditions." << endl; Foo F; RCP<Bar> Bptr = rcp(new Bar); Bptr->setx(10.0); F.setBar(Bptr); cout << "Correct output is x = 10." << endl; cout << "x = " << Bptr->getx() << endl; F.test1(Bptr); cout << "Correct output is x = 15." << endl; cout << "x = " << Bptr->getx() << endl; // This demonstrates that Bptr was overwritten in this member fcn call with a // different RCP object, which is only possible because the argument // list definition was nonconst. F.test3(Bptr); cout << "Correct output is x = 25." << endl; cout << "x = " << Bptr->getx() << endl; Bptr = F.test5(); cout << "Correct output is x = 15." << endl; cout << "x = " << Bptr->getx() << endl; Bptr->setx(35.0); // Bptr = F.test6(); // not allowed because Bptr is nonconst and output of test6 is const RCP<const Bar> BptrConst = rcp(new Bar(40.0)); F.setConstBar(BptrConst); cout << "Correct output is x = 40." << endl; cout << "x = " << F.test6()->getx() << endl; // valid because we put const after Bar::getx Bptr = F.test7(); cout << "Correct output is x = 35." << endl; cout << "x = " << Bptr->getx() << endl; Bptr = F.test8(); cout << "Correct output is x = 45." << endl; cout << "x = " << Bptr->getx() << endl; //F.test8() = rcp(new Bar(50.0)); // not allowed because output is const. F.test9() = rcp(new Bar(60.0)); // allowed because output is nonconst, but // this is crazy. Especially since the object that is modified is a copied // object, so we don't have access to it after this call. F.test10() = rcp(new Bar(65.0)); // allowed because output is nonconst, and //this does something strange, as it modifies the internal data to the class // through a nonconst reference passed out. Bptr = F.test7(); // grab it back out with const output cout << "Correct output is x = 65." << endl; cout << "x = " << Bptr->getx() << endl; // F.test7() = rcp(new Bar(70.0)); // not allowed because output is const, this // is the expected behavior. You shouldn't be able to do assignments like this. BptrConst = F.test12(); cout << "Correct output is x = 40." << endl; cout << "x = " << BptrConst->getx() << endl; //Bptr = F.test12(); // not allowed because Bptr is nonconst BptrConst = F.test13(); // this is okay. cout << "Correct output is x = 40." << endl; cout << "x = " << BptrConst->getx() << endl; Bptr = F.test14(); // this is okay. cout << "Correct output is x = 65." << endl; cout << "x = " << Bptr->getx() << endl; Bptr = F.test15(); // this is okay. cout << "Correct output is x = 65." << endl; cout << "x = " << Bptr->getx() << endl; return(0); }
void reffun(Foo& ref) { ref.setBar("BarBar"); }