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

📄 luatable.cpp

📁 这个是symbian下的一个蛮庞大的3D游戏源代码!对于学习3D开发的人有很大的帮助!
💻 CPP
📖 第 1 页 / 共 2 页
字号:
}

LuaTable LuaTable::getTable( 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_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}",
			index, LuaState::toString(type), LuaState::toString(expectedtype)) ) );
	}

	LuaTable tab;
	tab.m_luastate = m_luastate;
	tab.m_ref = lua_ref( impl, true );
	return tab;
}

bool LuaTable::getBoolean( 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_BOOLEAN;
	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 0 != lua_toboolean( impl, -1 );
}

bool LuaTable::getBoolean( 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_BOOLEAN;
	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 0 != lua_toboolean( impl, -1 );
}

bool LuaTable::isNil( const String& name ) const
{
	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 );
	bool res = 0 != lua_isnil( impl, -1 );
	return res;
}

bool LuaTable::isNil( int index ) const
{
	assert( m_ref >= 0 );

	lua_State* impl = m_luastate->impl();
	LuaStackRestore lsr( impl );
	lua_getref( impl, m_ref );
	lua_rawgeti( impl, -1, index );
	bool res = 0 != lua_isnil( impl, -1 );
	return res;
}

bool LuaTable::isString( const String& name ) const
{
	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 );
	bool res = 0 != lua_isstring( impl, -1 );
	return res;
}

bool LuaTable::isString( int index ) const
{
	assert( m_ref >= 0 );

	lua_State* impl = m_luastate->impl();
	LuaStackRestore lsr( impl );
	lua_getref( impl, m_ref );
	lua_rawgeti( impl, -1, index );
	bool res = 0 != lua_isstring( impl, -1 );
	return res;
}

bool LuaTable::isNumber( const String& name ) const
{
	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 );
	bool res = 0 != lua_isnumber( impl, -1 );
	return res;
}

bool LuaTable::isNumber( int index ) const
{
	assert( m_ref >= 0 );

	lua_State* impl = m_luastate->impl();
	LuaStackRestore lsr( impl );
	lua_getref( impl, m_ref );
	lua_rawgeti( impl, -1, index );
	bool res = 0 != lua_isnumber( impl, -1 );
	return res;
}

bool LuaTable::isTable( const String& name ) const
{
	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 );
	bool res = 0 != lua_istable( impl, -1 );
	return res;
}

bool LuaTable::isTable( int index ) const
{
	assert( m_ref >= 0 );

	lua_State* impl = m_luastate->impl();
	LuaStackRestore lsr( impl );
	lua_getref( impl, m_ref );
	lua_rawgeti( impl, -1, index );
	bool res = 0 != lua_istable( impl, -1 );
	return res;
}

bool LuaTable::isBoolean( const String& name ) const
{
	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 );
	bool res = 0 != lua_isboolean( impl, -1 );
	return res;
}

bool LuaTable::isBoolean( int index ) const
{
	assert( m_ref >= 0 );

	lua_State* impl = m_luastate->impl();
	LuaStackRestore lsr( impl );
	lua_getref( impl, m_ref );
	lua_rawgeti( impl, -1, index );
	bool res = 0 != lua_isboolean( impl, -1 );
	return res;
}

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

	lua_State* impl = m_luastate->impl();
	lua_getref( impl, m_ref );
	lua_rawgeti( impl, -1, index );
	lua_remove( impl, -2 );
}

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

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

void LuaTable::writeChar( OutputStream* out, int ch )
{
	assert( ch >= 0 && ch < 256 );
	char b = (char)ch;
	out->write( &b, sizeof(b) );
}

void LuaTable::writeMargin( OutputStream* out, int margin )
{
	for ( int i = 0 ; i < margin ; ++i )
		writeChar( out, ' ' );
}

void LuaTable::writeString( OutputStream* out, const lang::String& str )
{
	const int len = str.length();
	for ( int i = 0 ; i < len ; ++i )
		writeChar( out, str.charAt(i) );
}

void LuaTable::writeValue( OutputStream* out, int index, int margin )
{
	LuaState::Type valuetype = m_luastate->type( index );
	switch ( valuetype )
	{
	case LuaState::TYPE_BOOLEAN:
		if ( m_luastate->toBoolean(index) )
			writeString( out, "true" );
		else
			writeString( out, "false" );
		break;
	case LuaState::TYPE_NUMBER:{
		char buf[32];
		sprintf( buf, "%g", (float)m_luastate->toNumber(index) );
		writeString( out, buf );
		break;}
	case LuaState::TYPE_STRING:
		writeString( out, "\"" );
		writeString( out, m_luastate->toString(index) );
		writeString( out, "\"" );
		break;
	case LuaState::TYPE_TABLE:{
		writeString( out, "{\n" );
		LuaTable tab = m_luastate->toTable( index );
		tab.write( out, margin+4 );
		writeString( out, "\n" );
		writeMargin( out, margin );
		writeString( out, "}" );
		break;}
	default:
		writeString( out, "nil" );
		break;
	}
}

void LuaTable::write( OutputStream* out, int margin )
{
	assert( m_luastate );
	
	LuaStackRestore lsr( m_luastate );
	m_luastate->pushTable( this );
	int tab = m_luastate->top();
	bool first = true;
	bool strkeys = false;
	int keyindex = 1;

	for ( m_luastate->pushNil() ; m_luastate->next(tab) ; m_luastate->pop() )
	{
		LuaState::Type keytype = m_luastate->type(-2);
		if ( keytype == LuaState::TYPE_STRING )
		{
			if ( !strcmp(m_luastate->toString(-2),"_G") )
			{
				continue;
			}

			writeMargin( out, margin );
			writeString( out, m_luastate->toString(-2) );
			writeString( out, " = " );
			writeValue( out, -1, margin );
			writeString( out, "\n" );

			strkeys = true;
		}
		else if ( !strkeys )
		{
			if ( first )
				writeMargin( out, margin );
			else
				writeString( out, "," );

			writeValue( out, -1, margin );

			++keyindex;
		}

		first = false;
	}
}

void LuaTable::read( io::InputStream* in )
{
	assert( m_luastate );
	m_luastate->compile( in, in->available(), in->toString(), this );
}


} // lua


⌨️ 快捷键说明

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