wxIScan
wxluascript.h
Go to the documentation of this file.
00001 /***************************************************************
00002  * Name:      wxluascript.h
00003  * Purpose:   Defines a lua scripting object
00004  * Author:    Daniel Nell (daniel.nell@nellresearch.de)
00005  * Created:   2009-11-11
00006  * Copyright: Daniel Nell (www.nellresearch.de)
00007  * License:   wxWindows license
00008  **************************************************************/
00009 
00010 #ifndef WXLUASCRIPT_H
00011 #define WXLUASCRIPT_H
00012 
00013 // Additional wxWidgets header file(s).
00014 #if __WXLUASCRIPT_DYNAMIC__
00015 #   include <wx/dynlib.h>
00016 #endif // __WXLUASCRIPT_DYNAMIC__
00017 #include <wx/event.h>
00018 
00019 // Include Lua header file(s).
00020 #include "lua5.1/lua.hpp"
00021 
00022 
00023 /////////////////////////////////////////////////////////////////////////////
00024 //  Defines
00025 //
00026 #if __WXLUASCRIPT_DYNAMIC__
00027 #   define WXLUASCRIPT_LIBLUA    "lua5.1"
00028 #endif // __WXLUASCRIPT_DYNAMIC__
00029 
00030 
00031 /////////////////////////////////////////////////////////////////////////////
00032 //  Class wxLuaScript
00033 //
00034 /// \brief A class to handle Lua scripting.
00035 ///
00036 /// ...
00037 class wxLuaScript
00038 {
00039   public:
00040     /// \brief Standard constructor.
00041     ///
00042     /// \param strScript            The Lua source code of the script to execute.
00043     ///                             Default is the empty string.
00044     /// \param poParent             A pointer to the parent event handler (mostly
00045     ///                             a window), may be NULL
00046     /// \param strDomain            The name of the domain (the table name where
00047     ///                             to register the functions).
00048     ///                             Default is the empty string.
00049     /// \param pbParentState        A pointer to a boolean to get the error state,
00050     ///                             may be NULL
00051     wxLuaScript( const wxString& strScript= wxEmptyString,
00052                  wxEvtHandler *poParent= NULL,
00053                  const wxString& strDomain= wxEmptyString,
00054                  bool *pbParentState= NULL );
00055 
00056     /// \brief Virtual destructor.
00057     virtual ~wxLuaScript();
00058 
00059   public:
00060     /// \brief Run the Lua script in memory.
00061     virtual void Run()
00062     {
00063         // Run the script.
00064         lua_cpcall( L, wxLuaScript::Run, NULL );
00065     }
00066 
00067     /// \brief Register a new (LuaC-)function to Lua.
00068     ///
00069     /// \param strLuaCFunctionName  the function's name in Lua
00070     /// \param pLuaCFunction        a pointer to the LuaC-function
00071     virtual void RegisterFunction( const wxString& strLuaCFunctionName, lua_CFunction pLuaCFunction )
00072     {
00073         if( m_strDomain.IsEmpty() )
00074         {
00075             lua_register( L, strLuaCFunctionName.char_str(), pLuaCFunction );
00076         }
00077         else
00078         {
00079             RegisterFunction( L, m_strDomain.char_str(), strLuaCFunctionName.char_str(), pLuaCFunction );
00080         }
00081     }
00082 
00083     /// \brief Register a string constant to Lua.
00084     ///
00085     /// \param strLuaConstantName   the constant's name in Lua
00086     /// \param strValue             the constant's value as string
00087     virtual void RegisterConstant( const wxString& strLuaConstantName, const wxString& strValue )
00088     {
00089         lua_pushstring( L, strLuaConstantName.char_str() );
00090         lua_pushstring( L, strValue.char_str() );
00091         lua_settable( L, LUA_GLOBALSINDEX );
00092     }
00093 
00094     /// \brief Register an integer constant to Lua.
00095     ///
00096     /// \param strLuaConstantName   the constant's name in Lua
00097     /// \param nValue               the constant's value as integer
00098     virtual void RegisterConstant( const wxString& strLuaConstantName, const int nValue )
00099     {
00100         lua_pushstring( L, strLuaConstantName.char_str() );
00101         lua_pushinteger( L, nValue );
00102         lua_settable( L, LUA_GLOBALSINDEX );
00103     }
00104 
00105     /// \brief Register an integer variable to Lua.
00106     ///
00107     /// \param strLuaVarName        the variable's name in Lua
00108     /// \param pnValue              a pointer to the integer variable
00109     virtual void RegisterVariable( const wxString& strLuaVarName, const int *pnValue );
00110 
00111     /// \brief Register a boolean variable to Lua.
00112     ///
00113     /// \param strLuaVarName        the variable's name in Lua
00114     /// \param pnValue              a pointer to the boolean variable
00115     virtual void RegisterVariable( const wxString& strLuaVarName, const bool *pnValue );
00116 
00117     /// \brief Register a double variable to Lua.
00118     ///
00119     /// \param strLuaVarName        the variable's name in Lua
00120     /// \param pnValue              a pointer to the double variable
00121     virtual void RegisterVariable( const wxString& strLuaVarName, const double *pnValue );
00122 
00123     /// \brief Register a wxString variable to Lua.
00124     ///
00125     /// \param strLuaVarName        the variable's name in Lua
00126     /// \param pstrValue            a pointer to the string variable
00127     virtual void RegisterVariable( const wxString& strLuaVarName, const wxString *pstrValue );
00128 
00129     /// \brief Call a Lua function without return value (sometimes called a 'procedure') by given name.
00130     ///
00131     /// \param strLuaFunction       the Lua function name to call
00132     virtual void CallLuaFunction( const wxString& strLuaFunction )
00133     {
00134         lua_getfield( L, LUA_GLOBALSINDEX, strLuaFunction.char_str() );
00135         lua_call( L, 0, 0 );
00136     }
00137 
00138   protected:
00139     // Static helper function(s).
00140     /// \brief Register a new (LuaC-)function to Lua. Lower level function.
00141     ///
00142     /// \param L                    a pointer to the Lua context.
00143     /// \param pszTableName         the table name where the function is assigned
00144     ///                             to (a kind of domain)
00145     /// \param pszFuncName          the name of the C function
00146     /// \param pLuaCFunction        a pointer to the C function
00147     static void RegisterFunction( lua_State* L, const char *pszTableName,
00148                                   const char *pszFuncName, lua_CFunction pLuaCFunction);
00149 
00150     /// \brief Get the this-pointer from Lua.
00151     ///
00152     /// \param L                    a pointer to the Lua context.
00153     static wxLuaScript *GetThis( lua_State* L );
00154 
00155     /// \brief Run the Lua script in memory in protected mode.
00156     ///
00157     /// \param L                    a pointer to the Lua context.
00158     static int Run( lua_State* L );
00159 
00160     /// \brief Create a stack trace on the Lua stack.
00161     ///
00162     /// \param L                    a pointer to the Lua context.
00163     static int TraceBack( lua_State* L );
00164 
00165   protected:
00166     // Functions accessible by the Lua language.
00167     /// \brief Error handler for (maybe fatal) Lua errors.
00168     ///
00169     /// \param L                    a pointer to the Lua context.
00170     static int LuaAtPanic( lua_State* L );
00171 
00172     /// \brief Log a message on the standard wxWidgets log target.
00173     ///
00174     /// \param L                    a pointer to the Lua context.
00175     static int LogMessage( lua_State* L );
00176 
00177     /// \brief Send a menu event to the parent event handler.
00178     ///
00179     /// \param L                    a pointer to the Lua context.
00180     static int SendMenuEvent( lua_State* L );
00181 
00182     /// \brief Accessor function to a C-closure bound integer on the C/C++ side
00183     ///
00184     /// \param L                    a pointer to the Lua context.
00185     static int LuaIntVarAccessor( lua_State *L );
00186 
00187     /// \brief Accessor function to a C-closure bound integer on the C/C++ side
00188     ///
00189     /// \param L                    a pointer to the Lua context.
00190     static int LuaBoolVarAccessor( lua_State *L );
00191 
00192     /// \brief Accessor function to a C-closure bound integer on the C/C++ side
00193     ///
00194     /// \param L                    a pointer to the Lua context.
00195     static int LuaDoubleVarAccessor( lua_State *L );
00196 
00197     /// \brief Accessor function to a C-closure bound wxString on the C++ side
00198     ///
00199     /// \param L                    a pointer to the Lua context.
00200     static int LuaStringVarAccessor( lua_State *L );
00201 
00202 
00203   protected:
00204     wxEvtHandler *m_poParent;    ///< Pointer to the parent event handler (most often the parent window).
00205     bool *m_pbParentState;       ///< Pointer to the state of the parent object (true == OK, false == not OK).
00206     lua_State* L;                ///< Lua context.
00207     wxString m_strScript;        ///< Lua script as a string in memory.
00208     wxString m_strDomain;        ///< Lua "domain" name (the table where the functions are to register to).
00209 
00210 #if __WXLUASCRIPT_DYNAMIC__
00211   public:
00212     bool IsOk(){ return m_bOk; } ///< Returns the object state: true on OK and false otherwise.
00213 
00214   protected:
00215     static wxDynamicLibrary m_oWxLuaScriptDynamicLibrary; ///< ...
00216     bool m_bOk;                  ///< ...
00217 
00218     // Definition of variables for the corresponding Lua_C functions.
00219     typedef lua_State* t_luaL_newstate();
00220     typedef void t_luaL_openlibs( lua_State * );
00221     typedef lua_CFunction t_lua_atpanic( lua_State *, lua_CFunction );
00222     typedef int t_lua_error( lua_State * );
00223     typedef int t_lua_gettop( lua_State * );
00224     typedef int t_luaL_loadstring( lua_State *, const char * );
00225     typedef void t_lua_pushstring( lua_State *, const char * );
00226     typedef void t_lua_pushinteger( lua_State *, lua_Integer );
00227     typedef void t_lua_pushboolean( lua_State *, int );
00228     typedef lua_Integer t_lua_tointeger( lua_State *, int ) ;
00229     typedef const char *t_lua_tolstring (lua_State *, int, size_t * );
00230     typedef lua_Number t_lua_tonumber( lua_State *, int );
00231     typedef int t_lua_toboolean( lua_State *, int );
00232     typedef int t_lua_isnumber( lua_State *, int );
00233     typedef int t_lua_type( lua_State *, int );
00234     typedef int t_lua_isstring( lua_State *, int );
00235     typedef void t_lua_pushcclosure( lua_State *, lua_CFunction, int );
00236     typedef void t_lua_call( lua_State *, int, int );
00237     typedef int t_lua_pcall( lua_State *, int, int, int );
00238     typedef int t_lua_cpcall( lua_State *, lua_CFunction, void * );
00239     typedef void t_lua_settable( lua_State *, int );
00240     typedef void t_lua_setfield( lua_State *, int, const char * );
00241     typedef void t_lua_getfield( lua_State *, int, const char * );
00242     typedef void t_lua_settop( lua_State *, int );
00243     typedef void t_lua_insert( lua_State *, int );
00244     typedef void t_lua_pushvalue( lua_State *, int );
00245     typedef void t_lua_close( lua_State * );
00246     typedef void t_lua_createtable( lua_State *, int, int );
00247     typedef void t_lua_pushlightuserdata( lua_State *, void * );
00248     typedef const void *t_lua_topointer( lua_State *, int );
00249     typedef void t_lua_gettable( lua_State *, int );
00250     typedef void t_lua_pushnumber( lua_State *, lua_Number );
00251 
00252     static t_luaL_newstate *luaL_newstate;
00253     static t_luaL_openlibs *luaL_openlibs;
00254     static t_lua_atpanic *lua_atpanic;
00255     static t_lua_error *lua_error;
00256     static t_lua_gettop *lua_gettop;
00257     static t_luaL_loadstring *luaL_loadstring;
00258     static t_lua_pushstring* lua_pushstring;
00259     static t_lua_pushinteger* lua_pushinteger;
00260     static t_lua_pushboolean* lua_pushboolean;
00261     static t_lua_tointeger* lua_tointeger;
00262     static t_lua_tolstring* lua_tolstring;
00263     static t_lua_tonumber* lua_tonumber;
00264     static t_lua_toboolean* lua_toboolean;
00265     static t_lua_isnumber* lua_isnumber;
00266     static t_lua_type* lua_type;
00267     static t_lua_isstring* lua_isstring;
00268     static t_lua_pushcclosure* lua_pushcclosure;
00269     static t_lua_call* lua_call;
00270     static t_lua_pcall *lua_pcall;
00271     static t_lua_cpcall *lua_cpcall;
00272     static t_lua_settable *lua_settable;
00273     static t_lua_setfield *lua_setfield;
00274     static t_lua_getfield *lua_getfield;
00275     static t_lua_settop *lua_settop;
00276     static t_lua_insert *lua_insert;
00277     static t_lua_pushvalue *lua_pushvalue;
00278     static t_lua_close *lua_close;
00279     static t_lua_createtable *lua_createtable;
00280     static t_lua_pushlightuserdata *lua_pushlightuserdata;
00281     static t_lua_topointer *lua_topointer;
00282     static t_lua_gettable *lua_gettable;
00283     static t_lua_pushnumber* lua_pushnumber;
00284 #endif // __WXLUASCRIPT_DYNAMIC__
00285 };
00286 
00287 #endif // WXLUASCRIPT_H