Example #1
0
bool csp::OpAlt::CheckArgs( lua::LuaStack& args, InitError& initError ) const
{
	if( (args.NumArgs() & 1) != 0 )
		return initError.Error( "even number of arguments required. (guard+closure) pairs required" );

	bool nilCase = false;
	for( int i = 1; i <= args.NumArgs(); i += 2 )
	{
		lua::LuaStackValue guard = args[i];
		lua::LuaStackValue closure = args[i+1];

		Channel* pChannel = NULL;
		if( IsChannelArg(guard) && ( pChannel = GetChannelArg(guard) ) != NULL ) 
		{
			if( pChannel->InAttached() )
				return initError.ArgError( i, "channel is in input operation already" );
		}
		else if( guard.IsNumber() )
		{
		}
		else if( guard.IsNil() )
		{
			if( nilCase )
				return initError.ArgError( i, "there must be just one nil case" );
			nilCase = true;
		}
		else
			return initError.ArgError( i, "channel, number or nil required as a guard" );

		if( !closure.IsFunction() )
			return initError.ArgError( i+1, "closure required" );
	}

	return true;
}
Example #2
0
void csp::OpAlt::InitCases( lua::LuaStack& args )
{
	m_numCases = args.NumArgs()/2;
	m_cases = CORE_NEW AltCase [ m_numCases ];

	int initCase = 0;
	for( int i = 1; i <= args.NumArgs(); i += 2 )
	{
		lua::LuaStackValue guard = args[i];
		lua::LuaStackValue closure = args[i+1];

		closure.PushValue();
		m_cases[ initCase ].m_closureRefKey = args.RefInRegistry();

		if( IsChannelArg(guard) )
		{
			Channel* pChannel = GetChannelArg( guard );
			CORE_ASSERT( pChannel != NULL );
			pChannel->SetAttachmentIn( *this );

			guard.PushValue();
			m_cases[ initCase ].m_channelRefKey = args.RefInRegistry();
			m_cases[ initCase ].m_pChannel = pChannel;
		}
		else if( guard.IsNumber() )
		{
			m_cases[ initCase ].m_time = guard.GetNumber();
		}
		else if( guard.IsNil() )
		{
			CORE_ASSERT( m_pNilCase == NULL );
			m_pNilCase = m_cases + initCase;
		}

		++initCase;
	}
}