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:
Glenn Maynard
2006-10-07 01:22:28 +00:00
parent 2aa4b1a142
commit 4b30d3552d
11 changed files with 67 additions and 60 deletions
+14 -11
View File
@@ -163,19 +163,22 @@ RString LuaReference::Serialize() const
return sRet;
}
bool LuaHelpers::FromStack( lua_State *L, LuaReference &Object, int iOffset )
namespace LuaHelpers
{
lua_pushvalue( L, iOffset );
Object.SetFromStack( L );
return true;
}
template<> bool FromStack<LuaReference>( lua_State *L, LuaReference &Object, int iOffset )
{
lua_pushvalue( L, iOffset );
Object.SetFromStack( L );
return true;
}
bool LuaHelpers::FromStack( lua_State *L, apActorCommands &Object, int iOffset )
{
LuaReference *pRef = new LuaReference;
FromStack( L, *pRef, iOffset );
Object = apActorCommands( pRef );
return true;
template<> bool FromStack<apActorCommands>( lua_State *L, apActorCommands &Object, int iOffset )
{
LuaReference *pRef = new LuaReference;
FromStack( L, *pRef, iOffset );
Object = apActorCommands( pRef );
return true;
}
}
LuaTable::LuaTable()