1
0

cLuaState can now contain a detached LuaState, too.

This will be useful for cases when we get a lua_State * from the outside and are asked to perform operations on it.
This commit is contained in:
madmaxoft 2013-08-06 08:01:00 +02:00
parent c0066e7260
commit 2151bb8f5b
2 changed files with 89 additions and 1 deletions

View File

@ -37,6 +37,7 @@ extern "C"
cLuaState::cLuaState(const AString & a_SubsystemName) :
m_LuaState(NULL),
m_IsOwned(false),
m_SubsystemName(a_SubsystemName)
{
}
@ -45,6 +46,17 @@ cLuaState::cLuaState(const AString & a_SubsystemName) :
cLuaState::cLuaState(lua_State * a_AttachState) :
m_LuaState(a_AttachState),
m_IsOwned(false),
m_SubsystemName("<attached>")
{
}
cLuaState::~cLuaState()
{
if (IsValid())
@ -70,6 +82,7 @@ void cLuaState::Create(void)
ManualBindings::Bind(m_LuaState);
luaopen_lsqlite3(m_LuaState);
luaopen_lxp(m_LuaState);
m_IsOwned = true;
}
@ -83,8 +96,62 @@ void cLuaState::Close(void)
LOGWARNING("%s: Trying to close an invalid LuaState, ignoring.", __FUNCTION__);
return;
}
if (!m_IsOwned)
{
LOGWARNING(
"%s: Detected mis-use, calling Close() on an attached state (0x%p). Detaching instead.",
__FUNCTION__, m_LuaState
);
Detach();
return;
}
lua_close(m_LuaState);
m_LuaState = NULL;
m_IsOwned = false;
}
void cLuaState::Attach(lua_State * a_State)
{
if (m_LuaState != NULL)
{
LOGINFO("%s: Already contains a LuaState (0x%p), will be closed / detached.", __FUNCTION__, m_LuaState);
if (m_IsOwned)
{
Close();
}
else
{
Detach();
}
}
m_LuaState = a_State;
m_IsOwned = false;
}
void cLuaState::Detach(void)
{
if (m_LuaState == NULL)
{
return;
}
if (m_IsOwned)
{
LOGWARNING(
"%s: Detected a mis-use, calling Detach() when the state is owned. Closing the owned state (0x%p).",
__FUNCTION__, m_LuaState
);
Close();
return;
}
m_LuaState = NULL;
}

View File

@ -3,6 +3,12 @@
// Declares the cLuaState class representing the wrapper over lua_State *, provides associated helper functions
/*
The contained lua_State can be either owned or attached.
Owned lua_State is created by calling Create() and the cLuaState automatically closes the state
Or, lua_State can be attached by calling Attach(), the cLuaState doesn't close such a state
Attaching a state will automatically close an owned state.
*/
@ -29,16 +35,28 @@ public:
*/
cLuaState(const AString & a_SubsystemName);
/** Creates a new instance. The a_AttachState is attached.
Subsystem name is set to "<attached>".
*/
explicit cLuaState(lua_State * a_AttachState);
~cLuaState();
/// Allows this object to be used in the same way as a lua_State *, for example in the LuaLib functions
operator lua_State * (void) { return m_LuaState; }
/// Creates the m_LuaState, if not closed already
/// Creates the m_LuaState, if not closed already. This state will be automatically closed in the destructor
void Create(void);
/// Closes the m_LuaState, if not closed already
void Close(void);
/// Attaches the specified state. Operations will be carried out on this state, but it will not be closed in the destructor
void Attach(lua_State * a_State);
/// Detaches a previously attached state.
void Detach(void);
/// Returns true if the m_LuaState is valid
bool IsValid(void) const { return (m_LuaState != NULL); }
@ -79,6 +97,9 @@ public:
protected:
lua_State * m_LuaState;
/// If true, the state is owned by this object and will be auto-Closed. False => attached state
bool m_IsOwned;
/** The subsystem name is used for reporting errors to the console, it is either "plugin %s" or "LuaScript"
whatever is given to the constructor
*/