Lua_icxx  1.02 (Aug 2011)
tdd.cpp
00001 %%(cpp)
00002 
00003 void func(const lua::args& args) 
00004 {
00005     if (!args)
00006     {
00007         printf("There are no arguments\n");
00008         return; // automatically causes return 0 to Lua
00009     }
00010     if (args[2].exists()) 
00011     {
00012         printf("There are at least two arguments\n");
00013         if (args[2].nil()) 
00014             printf("But it is nil\n");
00015     }
00016     int ai = args[2];   // auto converts number/string to int
00017     float af = args[2]; // auto converts number/string to float
00018 
00019     lua::obj a("a"); // get lua global, "nil" if doesn't exist
00020     int aa = a; // throws if can't
00021     int bb = (a.nil() ? 0 : a); // won't throw, don't care if error
00022     std::string sa = a; // converts to string? or throws?
00023 
00024     // loop over all args
00025     for (lua::args_iter arg = args.begin(); arg != args.end(); ++arg)
00026         printf("Type of arg: %s\n", arg->type());
00027     
00028     // automatically pushes appropriate lua types onto stack
00029     // and handles return value count
00030     lua::return_("hello", af);
00031 
00032     // raw state/stack access possible too but there will surely
00033     // be *some* restrictions
00034     const lua::state& lua( args.lua() );
00035     assert( lua.get_stack_depth() == lua_gettop(lua::get_state()));
00036     
00037 }
00038 
00039 
00040 int main()
00041 {
00042     lua::state lua;
00043 
00044     lua::table foo( lua, "dontexist" );  // local ref to s/t that doesn't exist
00045     assert(foo.nil());
00046 
00047     lua::table(); // same as just typing the line '{}' in lua; does nothing so don't need lua
00048     lua.set( "tt", lua::table() ); // good idiom if don't need local
00049     lua.set( "tt", lua::nil() ); // destroy
00050     lua::table( lua, "tt", lua::table() ); // same as set()
00051 
00052     lua::table tt1 = lua["tt"];  // same as lua::table tt1(lua, "tt")
00053     assert(! tt1.nil());
00054     assert(tt1.empty());
00055     lua["tt"] = lua::table();
00056     lua["tt"].set(1, "a"); // tt[1]="a"
00057     lua["tt"][1] = "a"; // tt[1]="a"
00058     tt1.set(2, "b")  // support chaining
00059        .set(3, "c")
00060        .set("key", 5) // tt.key = 5
00061        .set(4, "d"); 
00062     tt1 << "a" << "b" << "c"; // injection tt[last+1]='a', etc
00063 
00064     tt1 = lua::nil(); // same
00065     lua::table tt2( lua, "tt", lua::table() ); // resets lua ns table to {}
00066     lua::table( "tt", lua::nil() ); // destroy table in lua
00067 
00068     // will be wrapped so lua::return_ works
00069     lua["tt.func"] = func;
00070     tt1.set("func", func);  // same effect
00071     lua::func ff = tt1["func"];
00072     assert( ff.is_func() );
00073     
00074     lua::obj i(lua, "tt.i", 1); 
00075     lua::obj f(lua, "f", 1.0);
00076     tt2.set("s1", "hello1"); // same as ns.s1 = 'hello1'
00077     lua::obj s2 = tt2("s2", "hello2");
00078     lua::obj s3( tt2, "s2", "hello2");
00079 
00080     lua.do_string("t.func(2)");
00081     lua.do_file("file.lua"); // may call ns.func()
00082 }
00083 %%