/*****************************************************************************
 * $LastChangedDate: 2011-03-17 19:59:52 -0400 (Thu, 17 Mar 2011) $
 * @file
 * @author  Jim E. Brooks  http://www.palomino3d.org
 * @brief   Interface of C functions for Lua scripts.
 * @remarks @remarks module.txt documents how to export new C++ classes to Lua.
 *
 * ----------------------------
 * -- THIS IS AUTO-GENERATED --
 * ----------------------------
 *
 *//*
 * LEGAL:   COPYRIGHT (C) 2008 JIM E. BROOKS
 *          THIS SOURCE CODE IS RELEASED UNDER THE TERMS
 *          OF THE GNU GENERAL PUBLIC LICENSE VERSION 2 (GPL 2).
 *****************************************************************************/

#include "base/module.hh"
#include "base/bits.hh"
#include "base/misc.hh"
using namespace base;
#include "lua_bind/module.hh"
#include "lua_bind/bind.hh"

namespace lua {


namespace bind {

#define BIND_CLASS_NAME      "lib"
#define BIND_METATABLE_NAME  LUABIND_METATABLE_STRING(BIND_CLASS_NAME)

//==============================================================================
// C functions called by Lua that access C++ objects.
//==============================================================================

/*****************************************************************************
 * string lib:SignedHexNumber2String( signedHexNumber, {minWidth,noShowBase,padZeros,showPlus} )
 *****************************************************************************/
INTERN int 
lib_SignedHexNumber2String( lua_State* L ) 
{ 
   try { 

        eNumberFormat format = eNumberFormat(0);
        uint minWidth = 0;

        BEGIN_OPTIONAL_TABLE_MEMBER
            minWidth = GET_MEMBER_AS_NUMBER( L, "minWidth", OPTIONAL );
        END_OPTIONAL_TABLE_MEMBER

        BEGIN_OPTIONAL_TABLE_MEMBER
            if ( GET_MEMBER_AS_BOOL( L, "noShowBase", OPTIONAL ) )
                format = SetBit<eNumberFormat>( format, eNumberFormat_NO_SHOW_BASE );
        END_OPTIONAL_TABLE_MEMBER

        BEGIN_OPTIONAL_TABLE_MEMBER
            if ( GET_MEMBER_AS_BOOL( L, "padZeros", OPTIONAL ) )
                format = SetBit<eNumberFormat>( format, eNumberFormat_PAD_ZEROS );
        END_OPTIONAL_TABLE_MEMBER

        BEGIN_OPTIONAL_TABLE_MEMBER
            if ( GET_MEMBER_AS_BOOL( L, "showPlus", OPTIONAL ) )
                format = SetBit<eNumberFormat>( format, eNumberFormat_SHOW_PLUS );
        END_OPTIONAL_TABLE_MEMBER

        // Pop the passed table arg.
        lua_pop( L, 1 );

        // Pop hex number.
        const long signedHexValue = long(POP_FP(L));

        const string hexStr = SignedHexNumber2String( signedHexValue, minWidth, format );
        lua_pushstring( L, hexStr.c_str() );
        return 1;  // stack items to return
    
   } CATCH_LUA_EXCEPTION 
   return 0; 
} 

//==============================================================================
// Register binding with Lua. 
//==============================================================================

#if ! DOXYGEN 

INTERN const struct luaL_reg sMethodTable[] =
{
  // New(),__gc() omitted as Lua cannot instantiate this C++ class.
  { "SignedHexNumber2String", lib_SignedHexNumber2String },
  { NULL, NULL }
};

void
Registerlib( lua_State* L )
{
    REGISTER_BINDING( L,
                      BIND_METATABLE_NAME,
                      BIND_CLASS_NAME,
                      sMethodTable );
}

#endif // ! DOXYGEN 

} // namespace bind 

} // namespace lua 
