A bizarre C++ wart wants us to declare FromStack overloads
before the ThemeMetric templates that use them. That's broken and unreasonable, so change this around a bit and make FromStack (and Push) templates. Push() takes a bit of a trick. Some Push overloads push the actual value: scalars (int, float), RageColor (pushes a table). Others--most of them--push a reference to a C++ object. We want the scalars to have a reference parameter type, so we don't make extra copies of things like RageColor when we push them. We need to pass C++ objects by pointer (we need to push the actual object's pointer, not a pointer to a copy). Further, pushing a scalar is a const operation, but pushing a reference to an object is not. To do both with the same template, we handle objects with this slightly odd template: template<> void LuaHelpers::Push<T*>( lua_State *L, T *const &pObject ); The actual overload (T) is eg. "Actor*"; this fits within the general prototype, "Push(lua_State *L, const T &object)", giving us a const reference to a (non-const) pointer to Actor, and we're conceptually pushing the pointer. The net effect of this is that 1: what was before compile errors now becomes link time errors, but 2: these specializations don't have to be in the headers (except for new ones for Preference and BroadcastOnChange).
This commit is contained in:
@@ -21,6 +21,19 @@ static LuaFunctionList *g_LuaFunctions = NULL;
|
||||
#pragma warning (disable : 4611)
|
||||
#endif
|
||||
|
||||
namespace LuaHelpers
|
||||
{
|
||||
template<> void Push<bool>( lua_State *L, const bool &Object );
|
||||
template<> void Push<float>( lua_State *L, const float &Object );
|
||||
template<> void Push<int>( lua_State *L, const int &Object );
|
||||
template<> void Push<RString>( lua_State *L, const RString &Object );
|
||||
|
||||
template<> bool FromStack<bool>( Lua *L, bool &Object, int iOffset );
|
||||
template<> bool FromStack<float>( Lua *L, float &Object, int iOffset );
|
||||
template<> bool FromStack<int>( Lua *L, int &Object, int iOffset );
|
||||
template<> bool FromStack<RString>( Lua *L, RString &Object, int iOffset );
|
||||
}
|
||||
|
||||
struct ChunkReaderString
|
||||
{
|
||||
ChunkReaderString( const RString &sBuf ): m_sBuf(sBuf) { m_bDone = false; }
|
||||
@@ -68,25 +81,27 @@ void LuaManager::UnsetGlobal( const RString &sName )
|
||||
LUA->Release( L );
|
||||
}
|
||||
|
||||
|
||||
void LuaHelpers::Push( lua_State *L, const bool &Object ) { lua_pushboolean( L, Object ); }
|
||||
void LuaHelpers::Push( lua_State *L, const float &Object ) { lua_pushnumber( L, Object ); }
|
||||
void LuaHelpers::Push( lua_State *L, const int &Object ) { lua_pushinteger( L, Object ); }
|
||||
void LuaHelpers::Push( lua_State *L, const RString &Object ) { lua_pushlstring( L, Object.data(), Object.size() ); }
|
||||
|
||||
bool LuaHelpers::FromStack( Lua *L, bool &Object, int iOffset ) { Object = !!lua_toboolean( L, iOffset ); return true; }
|
||||
bool LuaHelpers::FromStack( Lua *L, float &Object, int iOffset ) { Object = (float)lua_tonumber( L, iOffset ); return true; }
|
||||
bool LuaHelpers::FromStack( Lua *L, int &Object, int iOffset ) { Object = lua_tointeger( L, iOffset ); return true; }
|
||||
bool LuaHelpers::FromStack( Lua *L, RString &Object, int iOffset )
|
||||
namespace LuaHelpers
|
||||
{
|
||||
size_t iLen;
|
||||
const char *pStr = lua_tolstring( L, iOffset, &iLen );
|
||||
if( pStr != NULL )
|
||||
Object.assign( pStr, iLen );
|
||||
else
|
||||
Object.clear();
|
||||
template<> void Push<bool>( lua_State *L, const bool &Object ) { lua_pushboolean( L, Object ); }
|
||||
template<> void Push<float>( lua_State *L, const float &Object ) { lua_pushnumber( L, Object ); }
|
||||
template<> void Push<int>( lua_State *L, const int &Object ) { lua_pushinteger( L, Object ); }
|
||||
template<> void Push<RString>( lua_State *L, const RString &Object ) { lua_pushlstring( L, Object.data(), Object.size() ); }
|
||||
|
||||
return pStr != NULL;
|
||||
template<> bool FromStack<bool>( Lua *L, bool &Object, int iOffset ) { Object = !!lua_toboolean( L, iOffset ); return true; }
|
||||
template<> bool FromStack<float>( Lua *L, float &Object, int iOffset ) { Object = (float)lua_tonumber( L, iOffset ); return true; }
|
||||
template<> bool FromStack<int>( Lua *L, int &Object, int iOffset ) { Object = lua_tointeger( L, iOffset ); return true; }
|
||||
template<> bool FromStack<RString>( Lua *L, RString &Object, int iOffset )
|
||||
{
|
||||
size_t iLen;
|
||||
const char *pStr = lua_tolstring( L, iOffset, &iLen );
|
||||
if( pStr != NULL )
|
||||
Object.assign( pStr, iLen );
|
||||
else
|
||||
Object.clear();
|
||||
|
||||
return pStr != NULL;
|
||||
}
|
||||
}
|
||||
|
||||
void LuaHelpers::CreateTableFromArrayB( Lua *L, const vector<bool> &aIn )
|
||||
|
||||
Reference in New Issue
Block a user