Include :

- agguiornamenti vari.
This commit is contained in:
Dario Sassi
2015-07-14 15:52:37 +00:00
parent cb0cf8592f
commit cedb47bc75
5 changed files with 306 additions and 37 deletions
+9 -6
View File
@@ -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) ;
+177 -25
View File
@@ -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 <class T>
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 <class T>
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 <class T>
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 <class T>
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 ;
}
+95 -3
View File
@@ -13,6 +13,7 @@
#pragma once
#include "/EgtDev/Include/EGnLuaAux.h"
#include <string>
//----------------------- 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<typename... Args>
bool CallFunction( const std::string& sFunName, int nRets, const Args&... params) ;
template<typename... Args>
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<typename T, typename... Args>
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<typename... Args>
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<typename T, typename... Args>
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<typename... Args>
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 ;
}
+13 -2
View File
@@ -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) ;
+12 -1
View File
@@ -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) ;