-- $LastChangedDate: 2011-04-23 21:07:07 -0400 (Sat, 23 Apr 2011) $
-- Macros written in Lua to expand C++ code for bindings between C++/Lua.
-- Avoid lua_/LUA_ which is reserved by Lua.

bindTable = {

class = "Replay",
typesig = "TYPESIG_REPLAY",
luaCanInstantiate = true,

headers =
[[
#include "base/module.hh"
#include "base/stream.hh"
using namespace base;
#include "object/module.hh"
#include "object/aircraft.hh"
using namespace object;
#include "control/module.hh"
#include "control/replay.hh"
using namespace control;
]],

func_New = {
    comments = "Replay:New( aircraft, milliseconds, commandMode )",
    code = [[
        const Command::ExecuteMode commandExecuteMode = Command::ExecuteMode( POP_INT( L ) );
        const Milliseconds milliseconds( POP_INT( L ) );
        shptr<Aircraft> aircraft = POP_USERDATA_AS_SHPTR<Aircraft>( L );
        CHECK_TYPESIG(aircraft,TYPESIG_AIRCRAFT);
        shptr<Replay> replay = new Replay( aircraft, milliseconds, commandExecuteMode );
        return NEW_USERDATA_SHPTR<Replay>( L, replay, BIND_METATABLE_NAME );
    ]]
},

func_Destroy = {
    comments = "Lua's garbage collector indirectly invokes this C++ destructor.",
    code = [[
        return DESTROY_USERDATA_SHPTR<Replay>( L, CheckTypesigReplay );
    ]]
},

func_Record = {
    comments = "bool Replay:Record()",
    code = [[
        shptr<Replay> replay = POP_USERDATA_AS_SHPTR<Replay>( L );
        CHECK_TYPESIG(replay,TYPESIG_REPLAY);
        lua_pushboolean( L, replay->Record() );
        return 1;  // stack items to return
    ]]
},

func_Play = {
    comments = "bool Replay:Play()",
    code = [[
        shptr<Replay> replay = POP_USERDATA_AS_SHPTR<Replay>( L );
        CHECK_TYPESIG(replay,TYPESIG_REPLAY);
        lua_pushboolean( L, replay->Play() );
        return 1;  // stack items to return
    ]]
},

func_Stop = {
    comments = "Replay:Stop()\n * Stop playing or recording (whichever is active).",
    code = [[
        shptr<Replay> replay = POP_USERDATA_AS_SHPTR<Replay>( L );
        CHECK_TYPESIG(replay,TYPESIG_REPLAY);
        replay->Stop();
    ]]
},

func_IfPlaying = {
    comments = "bool Replay:IfPlaying()",
    code = [[
        shptr<Replay> replay = POP_USERDATA_AS_SHPTR<Replay>( L );
        CHECK_TYPESIG(replay,TYPESIG_REPLAY);
        lua_pushboolean( L, replay->IfPlaying() );
        return 1;
    ]]
},

func_Save = {
    comments = "bool Replay:Save( filename )",
    code = [[
        const string filename = POP_STRING( L );
        shptr<Replay> replay = POP_USERDATA_AS_SHPTR<Replay>( L );
        CHECK_TYPESIG(replay,TYPESIG_REPLAY);
        try
        {
            const bool saved = replay->Save( filename );
            lua_pushboolean( L, saved );
            return 1;
        }
        catch( const Exception& e )
        {
            CERROR << e.what() << std::endl;
        }

        // Failed inside try or threw exception.
        lua_pushboolean( L, false );
        return 1;
    ]]
},

func_Load = {
    comments = "bool Replay:Load( filename )",
    code = [[
        const string filename = POP_STRING( L );
        shptr<Replay> replay = POP_USERDATA_AS_SHPTR<Replay>( L );
        CHECK_TYPESIG(replay,TYPESIG_REPLAY);
        // Load() will throw exception if file doesn't exist.
        try
        {
            const bool loaded = replay->Load( filename );
            lua_pushboolean( L, loaded );
            return 1;
        }
        catch( const Exception& e )
        {
            CERROR << e.what() << std::endl;
        }

        // Failed inside try or threw exception.
        lua_pushboolean( L, false );
        return 1;
    ]]
},

}
