Cafu Engine
cf::ScriptBinderT Class Reference

This class implements and encapsulates the strategy with which we bind C++ objects to Lua. More...

#include "UniScriptState.hpp"

Public Member Functions

 ScriptBinderT (lua_State *LuaState)
 The constructor. More...
 
void Init (const cf::TypeSys::TypeInfoManT &TIM)
 This method registers all C++ classes known to the TIM in the related Lua state. More...
 
template<class T >
bool Push (T Object)
 Pushes the given C++ object onto the stack. More...
 
template<class T >
T & GetCheckedObjectParam (int StackIndex)
 Checks if the value at the given stack index is a Lua object of type T::TypeInfo, or a subclass derived from it, and returns a reference to the related userdata. More...
 
bool IsBound (void *Identity)
 Returns if the object with the given identity is currently bound to the Lua state, i.e. More...
 
void Disconnect (void *Identity)
 Breaks the connection between a C++ object and its alter ego in Lua. More...
 

Friends

class UniScriptStateT
 

Detailed Description

This class implements and encapsulates the strategy with which we bind C++ objects to Lua.

It is separate from class UniScriptStateT, because it can also be used "outside" of script states, e.g. in the CFunctions of the bound C++ classes. Moreover, it would be possible to derive from this class in order to implement alternative binding strategies, and to pass a concrete instance to the UniScriptStateT constructor in order to "configure" it for a particular strategy.

Key idea: we only pass by value!

  • object is copied
  • Lua "owns" it and thus determines its lifetime
  • per default, always create new userdata
    • simple and clear if the object has no identity: push an xyz-vector twice –> two different userdata
    • if the object is a smart pointer, and has identity (entities or windows): it would work, too, with one detail issue: per-object extensions added by Lua code (e.g. attributes or callbacks) would not be consistent: one userdata representing the identity would have them, but not another ==> no problem, treat smart pointers as special case

Each type(name) can only be used with one kind of binding: MyClassT A(); IntrusivePtrT<MyClassT> B(new MyClassT());

Push(A); Push(B); // This is not possible! This is because we can only have one __gc function per type: for this typename, would we call Destruct<T>(...) with T == MyClassT or with T == IntrusivePtrT<MyClassT> ? and because the type's CFunction callbacks (for example, BaseEntityT::GetOrigin()) would similary not know with which T to call GetCheckedObjectParam<T>().

Literature:

Constructor & Destructor Documentation

ScriptBinderT::ScriptBinderT ( lua_State *  LuaState)

The constructor.

Member Function Documentation

void ScriptBinderT::Disconnect ( void *  Identity)

Breaks the connection between a C++ object and its alter ego in Lua.

If the object with the given identity still has an alter ego in the Lua state, calling this method essentially removes all C++ parts from it: the metatable is reset to nil, and the userdata's destructor is called (Lua will collect it later). After this method, any attempt to access the C++-implemented methods in Lua yields a (safe and well-defined) error message.

template<class T >
T & cf::ScriptBinderT::GetCheckedObjectParam ( int  StackIndex)
inline

Checks if the value at the given stack index is a Lua object of type T::TypeInfo, or a subclass derived from it, and returns a reference to the related userdata.

(If T is really an IntrusivePtrT<U>, the method checks for type U::TypeInfo, or a subclass derived from it.)

void ScriptBinderT::Init ( const cf::TypeSys::TypeInfoManT TIM)

This method registers all C++ classes known to the TIM in the related Lua state.

Subsequently created Lua instances of these classes will use the related information so that script code can call the C++-implemented methods, e.g. "obj:myCppFunc()". The method also takes inheritance into account: Lua instances of derived classes can access the attributes and call the methods of their base classes.

bool ScriptBinderT::IsBound ( void *  Identity)

Returns if the object with the given identity is currently bound to the Lua state, i.e.

whether for the C++ object there is an alter ego in Lua.

template<class T >
bool cf::ScriptBinderT::Push ( Object)
inline

Pushes the given C++ object onto the stack.

The object must support the GetType() method (should we add a "const char* TypeName" parameter instead?).


The documentation for this class was generated from the following files: