⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 luatable.cpp

📁 这个是symbian下的一个蛮庞大的3D游戏源代码!对于学习3D开发的人有很大的帮助!
💻 CPP
📖 第 1 页 / 共 2 页
字号:
#include <lua/LuaTable.h>
#include <io/FileInputStream.h>
#include <io/FileOutputStream.h>
#include <lua/LuaState.h>
#include <lua/LuaStackRestore.h>
#include <lua/LuaException.h>
#include "lua-5.0.2/include/lua.h"
#include "lua-5.0.2/include/lauxlib.h"
#include <lang/Array.h>
#include <string.h>
#include <config.h>


using namespace io;
using namespace lang;


namespace lua
{


LuaTable::LuaTable() :
	m_luastate( 0 ),
	m_ref( -1 )
{
}

LuaTable::LuaTable( LuaState* luastate ) :
	m_luastate( luastate ),
	m_ref( -1 )
{
	if ( luastate )
	{
		m_luastate->newTable();
		m_ref = lua_ref( m_luastate->impl(), true );
	}
}

LuaTable::LuaTable( const LuaTable& other ) :
	m_luastate( other.m_luastate ),
	m_ref( -1 )
{
	if ( other.m_luastate && other.m_ref >= 0 )
	{
		lua_State* impl = m_luastate->impl();
		lua_getref( impl, other.m_ref );
		m_ref = lua_ref( impl, true );
	}
}

LuaTable::~LuaTable()
{
	if ( m_ref >= 0 )
		lua_unref( m_luastate->impl(), m_ref );
}

LuaTable& LuaTable::operator=( const LuaTable& other )
{
	int oldref = m_ref;
	lua_State* oldlua = m_luastate->impl();

	if ( other.m_luastate->impl() && other.m_ref >= 0 )
	{
		lua_State* impl = other.m_luastate->impl();
		lua_getref( impl, other.m_ref );
		m_luastate = other.m_luastate;
		m_ref = lua_ref( impl, true );
	}
	else
	{
		m_luastate = 0;
		m_ref = -1;
	}

	if ( oldlua && oldref >= 0 )
		lua_unref( oldlua, oldref );

	return *this;
}

void LuaTable::remove( int index )
{
	assert( m_ref >= 0 );

	lua_State* impl = m_luastate->impl();
	LuaStackRestore lsr( impl );
	lua_getref( impl, m_ref );
	lua_pushnil( impl );
	lua_rawseti( impl, -2, index );
}

void LuaTable::remove( const String& name )
{
	assert( m_ref >= 0 );

	lua_State* impl = m_luastate->impl();
	LuaStackRestore lsr( impl );
	lua_getref( impl, m_ref );
	LuaState::pushString( impl, name );
	lua_pushnil( impl );
	lua_rawset( impl, -3 );
}

void LuaTable::setString( int index, const String& v )
{
	assert( m_ref >= 0 );

	lua_State* impl = m_luastate->impl();
	LuaStackRestore lsr( impl );
	lua_getref( impl, m_ref );
	LuaState::pushString( impl, v );
	lua_rawseti( impl, -2, index );
}

void LuaTable::setNumber( int index, float v ) 
{
	assert( m_ref >= 0 );

	lua_State* impl = m_luastate->impl();
	LuaStackRestore lsr( impl );
	lua_getref( impl, m_ref );
	lua_pushnumber( impl, v );
	lua_rawseti( impl, -2, index );
}

void LuaTable::setTable( int index, const LuaTable& v ) 
{
	assert( m_ref >= 0 );

	lua_State* impl = m_luastate->impl();
	LuaStackRestore lsr( impl );
	lua_getref( impl, m_ref );
	LuaState::pushTable( impl, &v );
	lua_rawseti( impl, -2, index );
}

void LuaTable::setTable( int index, const LuaTable* v ) 
{
	assert( m_ref >= 0 );

	lua_State* impl = m_luastate->impl();
	LuaStackRestore lsr( impl );
	lua_getref( impl, m_ref );
	LuaState::pushTable( impl, v );
	lua_rawseti( impl, -2, index );
}

void LuaTable::setBoolean( int index, bool v ) 
{
	assert( m_ref >= 0 );

	lua_State* impl = m_luastate->impl();
	LuaStackRestore lsr( impl );
	lua_getref( impl, m_ref );
	lua_pushboolean( impl, v?1:0 );
	lua_rawseti( impl, -2, index );
}

void LuaTable::setString( const String& name, const String& v ) 
{
	assert( m_ref >= 0 );

	lua_State* impl = m_luastate->impl();
	LuaStackRestore lsr( impl );
	lua_getref( impl, m_ref );
	LuaState::pushString( impl, name );
	LuaState::pushString( impl, v );
	lua_rawset( impl, -3 );
}

void LuaTable::setNumber( const String& name, float v ) 
{
	assert( m_ref >= 0 );

	lua_State* impl = m_luastate->impl();
	LuaStackRestore lsr( impl );
	lua_getref( impl, m_ref );
	LuaState::pushString( impl, name );
	lua_pushnumber( impl, v );
	lua_rawset( impl, -3 );
}

void LuaTable::setTable( const String& name, const LuaTable& v ) 
{
	assert( m_ref >= 0 );
	assert( v.m_ref >= 0 );

	lua_State* impl = m_luastate->impl();
	LuaStackRestore lsr( impl );
	lua_getref( impl, m_ref );
	LuaState::pushString( impl, name );
	LuaState::pushTable( impl, &v );
	lua_rawset( impl, -3 );
}

void LuaTable::setTable( const String& name, const LuaTable* v ) 
{
	assert( m_ref >= 0 );

	lua_State* impl = m_luastate->impl();
	LuaStackRestore lsr( impl );
	lua_getref( impl, m_ref );
	LuaState::pushString( impl, name );
	LuaState::pushTable( impl, v );
	lua_rawset( impl, -3 );
}

void LuaTable::setBoolean( const String& name, bool v ) 
{
	assert( m_ref >= 0 );

	lua_State* impl = m_luastate->impl();
	LuaStackRestore lsr( impl );
	lua_getref( impl, m_ref );
	LuaState::pushString( impl, name );
	lua_pushboolean( impl, v?1:0 );
	lua_rawset( impl, -3 );
}

String LuaTable::getString( const String& name ) 
{
	assert( m_ref >= 0 );

	lua_State* impl = m_luastate->impl();
	LuaStackRestore lsr( impl );
	lua_getref( impl, m_ref );
	LuaState::pushString( impl, name );
	lua_rawget( impl, -2 );

	LuaState::Type expectedtype = LuaState::TYPE_STRING;
	LuaState::Type type = (LuaState::Type)lua_type(impl,-1);
	if ( type != expectedtype )
	{
		throwError( LuaException( Format("Tried to get value {0} from table, but type was {1} instead of {2}",
			name, LuaState::toString(type), LuaState::toString(expectedtype)) ) );
	}

	return lua_tostring( impl, -1 );
}

float LuaTable::getNumber( const String& name ) 
{
	assert( m_ref >= 0 );

	lua_State* impl = m_luastate->impl();
	LuaStackRestore lsr( impl );
	lua_getref( impl, m_ref );
	LuaState::pushString( impl, name );
	lua_rawget( impl, -2 );

	LuaState::Type expectedtype = LuaState::TYPE_NUMBER;
	LuaState::Type type = (LuaState::Type)lua_type(impl,-1);
	if ( type != expectedtype )
	{
		throwError( LuaException( Format("Tried to get value {0} from table, but type was {1} instead of {2}",
			name, LuaState::toString(type), LuaState::toString(expectedtype)) ) );
	}

	return lua_tonumber( impl, -1 );
}

LuaTable LuaTable::getTable( const String& name ) 
{
	assert( m_ref >= 0 );

	lua_State* impl = m_luastate->impl();
	LuaStackRestore lsr( impl );
	lua_getref( impl, m_ref );
	LuaState::pushString( impl, name );
	lua_rawget( impl, -2 );

	LuaState::Type expectedtype = LuaState::TYPE_TABLE;
	LuaState::Type type = (LuaState::Type)lua_type(impl,-1);
	if ( type != expectedtype )
	{
		throwError( LuaException( Format("Tried to get value {0} from table, but type was {1} instead of {2}",
			name, LuaState::toString(type), LuaState::toString(expectedtype)) ) );
	}
	
	LuaTable tab;
	tab.m_luastate = m_luastate;
	tab.m_ref = lua_ref( impl, true );
	return tab;
}

String LuaTable::getString( int index ) 
{
	assert( m_ref >= 0 );

	lua_State* impl = m_luastate->impl();
	LuaStackRestore lsr( impl );
	lua_getref( impl, m_ref );
	lua_rawgeti( impl, -1, index );

	LuaState::Type expectedtype = LuaState::TYPE_STRING;
	LuaState::Type type = (LuaState::Type)lua_type(impl,-1);
	if ( type != expectedtype )
	{
		throwError( LuaException( Format("Tried to get value {0} from table, but type was {1} instead of {2}",
			index, LuaState::toString(type), LuaState::toString(expectedtype)) ) );
	}

	return lua_tostring( impl, -1 );
}

float LuaTable::getNumber( int index ) 
{
	assert( m_ref >= 0 );

	lua_State* impl = m_luastate->impl();
	LuaStackRestore lsr( impl );
	lua_getref( impl, m_ref );
	lua_rawgeti( impl, -1, index );

	LuaState::Type expectedtype = LuaState::TYPE_NUMBER;
	LuaState::Type type = (LuaState::Type)lua_type(impl,-1);
	if ( type != expectedtype )
	{
		throwError( LuaException( Format("Tried to get value {0} from table, but type was {1} instead of {2}",
			index, LuaState::toString(type), LuaState::toString(expectedtype)) ) );
	}
	
	return lua_tonumber( impl, -1 );

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -