From cedb47bc75b02ef4c1a35211658b581e98c8be6b Mon Sep 17 00:00:00 2001 From: Dario Sassi Date: Tue, 14 Jul 2015 15:52:37 +0000 Subject: [PATCH] Include : - agguiornamenti vari. --- EGkLuaAux.h | 15 ++-- EGnLuaAux.h | 202 +++++++++++++++++++++++++++++++++++++++++++------- EGnLuaMgr.h | 98 +++++++++++++++++++++++- EInAPI.h | 15 +++- EXeExecutor.h | 13 +++- 5 files changed, 306 insertions(+), 37 deletions(-) diff --git a/EGkLuaAux.h b/EGkLuaAux.h index 1fafa2e..ef237c9 100644 --- a/EGkLuaAux.h +++ b/EGkLuaAux.h @@ -11,7 +11,8 @@ // //---------------------------------------------------------------------------- -//--------------------------- Include ---------------------------------------- +#pragma once + #include "/EgtDev/Include/EGkVector3d.h" #include "/EgtDev/Include/EGkPoint3d.h" #include "/EgtDev/Include/EGkFrame3d.h" @@ -247,9 +248,11 @@ LuaGetParam( lua_State* L, int nInd, SELVECTOR& vSel) return false ; } + +//---------------------------------------------------------------------------- //---------------------------------------------------------------------------- inline bool -LuaSetReturn( lua_State* L, const Vector3d& vtPar) +LuaSetParam( lua_State* L, const Vector3d& vtPar) { try { lua_createtable( L, 3, 0) ; @@ -266,7 +269,7 @@ LuaSetReturn( lua_State* L, const Vector3d& vtPar) //---------------------------------------------------------------------------- inline bool -LuaSetReturn( lua_State* L, const Point3d& ptPar) +LuaSetParam( lua_State* L, const Point3d& ptPar) { try { lua_createtable( L, 3, 0) ; @@ -283,7 +286,7 @@ LuaSetReturn( lua_State* L, const Point3d& ptPar) //------------------------------------------------------------------------------- inline bool -LuaSetReturn( lua_State* L, const Frame3d& frPar) +LuaSetParam( lua_State* L, const Frame3d& frPar) { try { // creo tavola per frame @@ -337,7 +340,7 @@ LuaSetReturn( lua_State* L, const Frame3d& frPar) //------------------------------------------------------------------------------- inline bool -LuaSetReturn( lua_State* L, const BBox3d& b3Par) +LuaSetParam( lua_State* L, const BBox3d& b3Par) { try { // creo tavola per bbox @@ -371,7 +374,7 @@ LuaSetReturn( lua_State* L, const BBox3d& b3Par) //------------------------------------------------------------------------------- inline bool -LuaSetReturn( lua_State* L, const Color& colPar) +LuaSetParam( lua_State* L, const Color& colPar) { try { lua_createtable( L, 4, 0) ; diff --git a/EGnLuaAux.h b/EGnLuaAux.h index 7f66832..04a604f 100644 --- a/EGnLuaAux.h +++ b/EGnLuaAux.h @@ -11,7 +11,9 @@ // //---------------------------------------------------------------------------- -//--------------------------- Include ---------------------------------------- +#pragma once + +#include "/EgtDev/Include/EGnStringUtils.h" #include "/EgtDev/Include/EgtNumCollection.h" #include "/EgtDev/Extern/Lua/Include/lua.hpp" @@ -21,6 +23,16 @@ #define LuaCheckTabFieldParam(L,I,F,P) { if ( ! LuaGetTabFieldParam(L,I,F,P)) \ return luaL_error( L, " Invalid Parameter # " #I " (field %s)", F) ;} +//---------------------------------------------------------------------------- +inline bool +LuaClearStack( lua_State* L) +{ + int n = lua_gettop( L) ; + if ( n > 0) + lua_pop( L, n) ; + return true ; +} + //---------------------------------------------------------------------------- inline bool LuaGetParam( lua_State* L, int nInd, bool& bPar) @@ -71,8 +83,10 @@ LuaGetParam( lua_State* L, int nInd, int (&nVal)[size]) return false ; for ( int i = 1 ; i <= size ; ++ i) { lua_rawgeti( L, nInd, i) ; - if ( ! lua_isnumber( L, -1)) + if ( ! lua_isnumber( L, -1)) { + lua_pop( L, 1) ; return false ; + } double dVal = lua_tonumber( L, -1) ; nVal[i-1] = int( dVal + (( dVal > 0) ? 0.5 : - 0.5)) ; lua_pop( L, 1) ; @@ -89,8 +103,10 @@ LuaGetParam( lua_State* L, int nInd, double (&dVal)[size]) return false ; for ( int i = 1 ; i <= size ; ++ i) { lua_rawgeti( L, nInd, i) ; - if ( ! lua_isnumber( L, -1)) + if ( ! lua_isnumber( L, -1)) { + lua_pop( L, 1) ; return false ; + } dVal[i-1] = lua_tonumber( L, -1) ; lua_pop( L, 1) ; } @@ -110,15 +126,19 @@ LuaGetParam( lua_State* L, int nInd, INTVECTOR& vPar) else if ( lua_istable( L, nInd)) { // lunghezza della tavola lua_len( L, nInd) ; - if ( ! lua_isnumber( L, -1)) + if ( ! lua_isnumber( L, -1)) { + lua_pop( L, 1) ; return false ; + } int nLen = int( lua_tointeger( L, -1)) ; lua_pop( L, 1) ; vPar.reserve( nLen) ; for ( int i = 1 ; i <= nLen ; ++ i) { lua_rawgeti( L, nInd, i) ; - if ( ! lua_isnumber( L, -1)) + if ( ! lua_isnumber( L, -1)) { + lua_pop( L, 1) ; return false ; + } int nVal = int( lua_tointeger( L, -1)) ; vPar.push_back( nVal) ; lua_pop( L, 1) ; @@ -142,15 +162,19 @@ LuaGetParam( lua_State* L, int nInd, DBLVECTOR& vPar) else if ( lua_istable( L, nInd)) { // lunghezza della tavola lua_len( L, nInd) ; - if ( ! lua_isnumber( L, -1)) + if ( ! lua_isnumber( L, -1)) { + lua_pop( L, 1) ; return false ; + } int nLen = int( lua_tointeger( L, -1)) ; lua_pop( L, 1) ; vPar.reserve( nLen) ; for ( int i = 1 ; i <= nLen ; ++ i) { lua_rawgeti( L, nInd, i) ; - if ( ! lua_isnumber( L, -1)) + if ( ! lua_isnumber( L, -1)) { + lua_pop( L, 1) ; return false ; + } double dVal = lua_tonumber( L, -1) ; vPar.push_back( dVal) ; lua_pop( L, 1) ; @@ -166,26 +190,19 @@ template bool LuaGetTabFieldParam( lua_State* L, int nInd, const char* szField, T& Val) { - lua_getfield( L, nInd, szField) ; - if ( ! LuaGetParam( L, -1, Val)) + if ( ! lua_istable( L, nInd)) return false ; + lua_getfield( L, nInd, szField) ; + bool bOk = LuaGetParam( L, -1, Val) ; lua_pop( L, 1) ; - return true ; + return bOk ; } -//---------------------------------------------------------------------------- -inline bool -LuaClearStack( lua_State* L) -{ - int n = lua_gettop( L) ; - if ( n > 0) - lua_pop( L, n) ; - return true ; -} +//---------------------------------------------------------------------------- //---------------------------------------------------------------------------- inline bool -LuaSetReturn( lua_State* L) +LuaSetParam( lua_State* L) { try { lua_pushnil( L) ; @@ -198,7 +215,7 @@ LuaSetReturn( lua_State* L) //---------------------------------------------------------------------------- inline bool -LuaSetReturn( lua_State* L, bool bPar) +LuaSetParam( lua_State* L, bool bPar) { try { lua_pushboolean( L, ( bPar ? 1 : 0)) ; @@ -211,7 +228,7 @@ LuaSetReturn( lua_State* L, bool bPar) //---------------------------------------------------------------------------- inline bool -LuaSetReturn( lua_State* L, int nPar) +LuaSetParam( lua_State* L, int nPar) { try { lua_pushinteger( L, nPar) ; @@ -224,7 +241,7 @@ LuaSetReturn( lua_State* L, int nPar) //---------------------------------------------------------------------------- inline bool -LuaSetReturn( lua_State* L, double dPar) +LuaSetParam( lua_State* L, double dPar) { try { lua_pushnumber( L, dPar) ; @@ -237,7 +254,7 @@ LuaSetReturn( lua_State* L, double dPar) //---------------------------------------------------------------------------- inline bool -LuaSetReturn( lua_State* L, const std::string& sPar) +LuaSetParam( lua_State* L, const std::string& sPar) { try { lua_pushstring( L, sPar.c_str()) ; @@ -250,7 +267,25 @@ LuaSetReturn( lua_State* L, const std::string& sPar) //---------------------------------------------------------------------------- inline bool -LuaSetReturn( lua_State* L, const INTVECTOR& vPar) +LuaSetParam( lua_State* L, const INTVECTOR& vPar) +{ + try { + int nSize = int( vPar.size()) ; + lua_createtable( L, nSize, 0) ; + for ( int i = 1 ; i <= nSize ; ++ i) { + lua_pushinteger( L, vPar[i-1]) ; + lua_rawseti( L, -2, i) ; + } + } + catch( ...) { + return false ; + } + return true ; +} + +//---------------------------------------------------------------------------- +inline bool +LuaSetParam( lua_State* L, const DBLVECTOR& vPar) { try { int nSize = int( vPar.size()) ; @@ -265,3 +300,120 @@ LuaSetReturn( lua_State* L, const INTVECTOR& vPar) } return true ; } + +//---------------------------------------------------------------------------- +inline bool +LuaSetTabFieldParam( lua_State* L, int nInd, const char* szField) +{ + if ( ! lua_istable( L, nInd)) + return false ; + if ( ! LuaSetParam( L)) + return false ; + int nPos = ( nInd > 0 ? nInd : nInd - 1) ; + lua_setfield( L, nPos, szField) ; + return true ; +} + +//---------------------------------------------------------------------------- +template +bool +LuaSetTabFieldParam( lua_State* L, int nInd, const char* szField, const T& Val) +{ + if ( ! lua_istable( L, nInd)) + return false ; + if ( ! LuaSetParam( L, Val)) + return false ; + int nPos = ( nInd > 0 ? nInd : nInd - 1) ; + lua_setfield( L, nPos, szField) ; + return true ; +} + + +//---------------------------------------------------------------------------- +//---------------------------------------------------------------------------- +template +bool +LuaGetGlobVar( lua_State* L, const std::string& sVar, T& Val) +{ + // se variabile standard + if ( sVar.find( '.') == std::string::npos) { + lua_getglobal( L, sVar.c_str()) ; + bool bOk = LuaGetParam( L, -1, Val) ; + lua_pop( L, 1) ; + return bOk ; + } + // altrimenti campo di tabella + else { + std::string sTab, sField ; + SplitFirst( sVar, ".", sTab, sField) ; + lua_getglobal( L, sTab.c_str()) ; + bool bOk = LuaGetTabFieldParam( L, -1, sField.c_str(), Val) ; + lua_pop( L, 1) ; + return bOk ; + } +} + + +//---------------------------------------------------------------------------- +//---------------------------------------------------------------------------- +template +bool +LuaSetGlobVar( lua_State* L, const std::string& sVar, const T& Val) +{ + // se variabile standard + if ( sVar.find( '.') == std::string::npos) { + if ( ! LuaSetParam( L, Val)) + return false ; + lua_setglobal( L, sVar.c_str()) ; + return true ; + } + // altrimenti campo di tabella + else { + std::string sTab, sField ; + SplitFirst( sVar, ".", sTab, sField) ; + lua_getglobal( L, sTab.c_str()) ; + bool bOk = LuaSetTabFieldParam( L, -1, sField.c_str(), Val) ; + lua_pop( L, 1) ; + return bOk ; + } +} + + +//---------------------------------------------------------------------------- +//---------------------------------------------------------------------------- +inline bool +LuaResetGlobVar( lua_State* L, const std::string& sVar) +{ + // se variabile standard + if ( sVar.find( '.') == std::string::npos) { + if ( ! LuaSetParam( L)) + return false ; + lua_setglobal( L, sVar.c_str()) ; + return true ; + } + // altrimenti campo di tabella + else { + std::string sTab, sField ; + SplitFirst( sVar, ".", sTab, sField) ; + lua_getglobal( L, sTab.c_str()) ; + bool bOk = LuaSetTabFieldParam( L, -1, sField.c_str()) ; + lua_pop( L, 1) ; + return bOk ; + } +} + + +//---------------------------------------------------------------------------- +//---------------------------------------------------------------------------- +inline bool +LuaCreateGlobTable( lua_State* L, const std::string& sVar) +{ + try { + lua_newtable( L) ; + lua_setglobal( L, sVar.c_str()) ; + } + catch(...) { + return false ; + } + return true ; +} diff --git a/EGnLuaMgr.h b/EGnLuaMgr.h index 3ec976a..de570de 100644 --- a/EGnLuaMgr.h +++ b/EGnLuaMgr.h @@ -13,6 +13,7 @@ #pragma once +#include "/EgtDev/Include/EGnLuaAux.h" #include //----------------------- Macro per import/export ----------------------------- @@ -32,7 +33,7 @@ class LuaMgr { public : EGN_EXPORT LuaMgr( void) - : m_pL( nullptr) {} + : m_pL( nullptr), m_nRets( 0) {} EGN_EXPORT ~LuaMgr( void) { Exit() ; } EGN_EXPORT bool Init( void) ; @@ -41,20 +42,111 @@ class LuaMgr EGN_EXPORT bool SetLuaLibsDir( const std::string& sDir) ; EGN_EXPORT bool Require( const std::string& sFile) ; EGN_EXPORT bool RegisterFunction( const std::string& sFunName, PFLUA pFun) ; - EGN_EXPORT bool EvalNumExpr( const std::string& sExpr, double& dVal) ; - EGN_EXPORT bool EvalStringExpr( const std::string& sExpr, std::string& sVal) ; + EGN_EXPORT bool EvalExpr( const std::string& sExpr, double& dVal) ; + EGN_EXPORT bool EvalExpr( const std::string& sExpr, std::string& sVal) ; EGN_EXPORT bool ExecLine( const std::string& sLine) ; EGN_EXPORT bool ExecFile( const std::string& sFile) ; + EGN_EXPORT lua_State* GetLuaState( void) + { return m_pL ; } EGN_EXPORT const std::string& GetLastError( void) { return m_sLastError ; } EGN_EXPORT const std::string& GetLuaLibsDir( void) { return m_sLuaLibsDir ; } EGN_EXPORT const std::string& GetLastRequire( void) { return m_sLastRequire ; } + template + bool CallFunction( const std::string& sFunName, int nRets, const Args&... params) ; + template + bool GetFunctionReturns( Args&... rets) ; + + private : + EGN_EXPORT void LogError( const std::string& sErr) ; private : lua_State* m_pL ; std::string m_sLastError ; std::string m_sLuaLibsDir ; std::string m_sLastRequire ; + int m_nRets ; } ; + +//---------------------------------------------------------------------------- +template +bool +LuaSetFunParams( lua_State* L, const T param, const Args&... params) +{ + return ( LuaSetParam( L, param) && LuaSetFunParams( L, params...)) ; +} + +//---------------------------------------------------------------------------- +inline bool +LuaSetFunParams( lua_State* L) +{ + return true ; +} + +//---------------------------------------------------------------------------- +template +bool +LuaMgr::CallFunction( const std::string& sFunName, int nRets, const Args&... params) +{ + // recupero la funzione + lua_getglobal( m_pL, sFunName.c_str()) ; + // numero di parametri + int nParNbr = sizeof...( Args) ; + // inserisco i parametri sullo stack + LuaSetFunParams( m_pL, params...) ; + // eseguo la chiamata + int nErr = lua_pcall( m_pL, nParNbr, nRets, 0) ; + // senza errori + if ( nErr == LUA_OK) { + m_nRets = nRets ; + m_sLastError.clear() ; + return true ; + } + // altrimenti, errore + else { + // non ci sono parametri di ritorno + m_nRets = 0 ; + // recupero il messaggio di errore + const char* szErr = lua_tostring( m_pL, -1) ; + m_sLastError = sFunName + " " + (( szErr != nullptr) ? szErr : "Error calling") ; + lua_pop( m_pL, 1) ; + // lo scrivo nel log + LogError( m_sLastError) ; + return false ; + } +} + +//---------------------------------------------------------------------------- +template +bool +LuaGetFunParams( lua_State* L, T& param, Args&... params) +{ + bool bOk = LuaGetFunParams( L, params...) && LuaGetParam( L, -1, param) ; + lua_pop( L, 1) ; + return bOk ; +} + +//---------------------------------------------------------------------------- +inline bool +LuaGetFunParams( lua_State* L) +{ + return true ; +} + +//---------------------------------------------------------------------------- +template +bool +LuaMgr::GetFunctionReturns( Args&... rets) +{ + // verifico che il numero di parametri lasciati dalla funzione chiamata sia adeguato + int nParNbr = sizeof...( Args) ; + if ( m_nRets < nParNbr) + return false ; + // recupero i parametri + bool bOk = LuaGetFunParams( m_pL, rets...) ; + // decremento il contatore + m_nRets -= nParNbr ; + return bOk ; +} diff --git a/EInAPI.h b/EInAPI.h index ad8d4d2..cd79e1c 100644 --- a/EInAPI.h +++ b/EInAPI.h @@ -49,8 +49,9 @@ EIN_EXPORT BOOL __stdcall EgtGetMemoryInfo( wchar_t*& wsMem) ; EIN_EXPORT BOOL __stdcall EgtFreeMemory( void* pMem) ; EIN_EXPORT BOOL __stdcall EgtOutLog( const wchar_t* wsMsg) ; -// GeomDB -EIN_EXPORT int __stdcall EgtInitGeomDB( void) ; +// Context (GeomDB) +EIN_EXPORT int __stdcall EgtInitContext( void) ; +EIN_EXPORT int __stdcall EgtDeleteContext( int nGseCtx) ; EIN_EXPORT BOOL __stdcall EgtSetCurrentContext( int nGseCtx) ; EIN_EXPORT BOOL __stdcall EgtResetCurrentContext( void) ; EIN_EXPORT int __stdcall EgtGetCurrentContext( void) ; @@ -85,6 +86,16 @@ EIN_EXPORT BOOL __stdcall EgtTscExecFile( const wchar_t* wsFilePath) ; EIN_EXPORT BOOL __stdcall EgtTscExecLine( const wchar_t* wsLine) ; // LUA Executor +EIN_EXPORT BOOL __stdcall EgtLuaSetGlobBoolVar( const wchar_t* wsVar, BOOL bVal) ; +EIN_EXPORT BOOL __stdcall EgtLuaSetGlobIntVar( const wchar_t* wsVar, int nVal) ; +EIN_EXPORT BOOL __stdcall EgtLuaSetGlobNumVar( const wchar_t* wsVar, double dVal) ; +EIN_EXPORT BOOL __stdcall EgtLuaSetGlobStringVar( const wchar_t* wsVar, const wchar_t* wsVal) ; +EIN_EXPORT BOOL __stdcall EgtLuaGetGlobBoolVar( const wchar_t* wsVar, BOOL* pbVal) ; +EIN_EXPORT BOOL __stdcall EgtLuaGetGlobIntVar( const wchar_t* wsVar, int* pnVal) ; +EIN_EXPORT BOOL __stdcall EgtLuaGetGlobNumVar( const wchar_t* wsVar, double* pdVal) ; +EIN_EXPORT BOOL __stdcall EgtLuaGetGlobStringVar( const wchar_t* wsVar, wchar_t*& wsVal) ; +EIN_EXPORT BOOL __stdcall EgtLuaResetGlobVar( const wchar_t* wsVar) ; +EIN_EXPORT BOOL __stdcall EgtLuaCreateGlobTable( const wchar_t* wsVar) ; EIN_EXPORT BOOL __stdcall EgtLuaEvalNumExpr( const wchar_t* wsExpr, double* pdVal) ; EIN_EXPORT BOOL __stdcall EgtLuaEvalStringExpr( const wchar_t* wsExpr, wchar_t*& wsVal) ; EIN_EXPORT BOOL __stdcall EgtLuaExecLine( const wchar_t* wsLine) ; diff --git a/EXeExecutor.h b/EXeExecutor.h index 782c4da..57f799e 100644 --- a/EXeExecutor.h +++ b/EXeExecutor.h @@ -50,7 +50,8 @@ EXE_EXPORT bool ExeGetMemoryInfo( std::string& sMem) ; EXE_EXPORT bool ExeOutLog( const std::string& sMsg) ; // GeomDB -EXE_EXPORT int ExeInitGeomDB( void) ; +EXE_EXPORT int ExeInitContext( void) ; +EXE_EXPORT int ExeDeleteContext( int nGseCtx) ; EXE_EXPORT bool ExeSetCurrentContext( int nGseCtx) ; EXE_EXPORT bool ExeResetCurrentContext( void) ; EXE_EXPORT int ExeGetCurrentContext( void) ; @@ -86,6 +87,16 @@ EXE_EXPORT bool ExeTscExecFile( const std::string& sFilePath) ; EXE_EXPORT bool ExeTscExecLine( const std::string& sLine) ; // LUA Executor +EXE_EXPORT bool ExeLuaSetGlobBoolVar( const std::string& sVar, bool bVal) ; +EXE_EXPORT bool ExeLuaSetGlobIntVar( const std::string& sVar, int nVal) ; +EXE_EXPORT bool ExeLuaSetGlobNumVar( const std::string& sVar, double dVal) ; +EXE_EXPORT bool ExeLuaSetGlobStringVar( const std::string& sVar, const std::string& sVal) ; +EXE_EXPORT bool ExeLuaGetGlobBoolVar( const std::string& sVar, bool* pbVal) ; +EXE_EXPORT bool ExeLuaGetGlobIntVar( const std::string& sVar, int* pnVal) ; +EXE_EXPORT bool ExeLuaGetGlobNumVar( const std::string& sVar, double* pdVal) ; +EXE_EXPORT bool ExeLuaGetGlobStringVar( const std::string& sVar, std::string& sVal) ; +EXE_EXPORT bool ExeLuaResetGlobVar( const std::string& sVar) ; +EXE_EXPORT bool ExeLuaCreateGlobTable( const std::string& sVar) ; EXE_EXPORT bool ExeLuaEvalNumExpr( const std::string& sExpr, double* pdVal) ; EXE_EXPORT bool ExeLuaEvalStringExpr( const std::string& sExpr, std::string& sVal) ; EXE_EXPORT bool ExeLuaExecLine( const std::string& sLine) ;