示例#1
0
Junk * Attic::RemoveUselessJunk( void )
{
    ScopeGuard callGuard = MakeObjGuard( *this, &Attic::CallObservers );
    ScopeGuard testGuard = MakeObjGuard( *this, &Attic::CheckInvariants, __FUNCTION__, __LINE__ );
    ScopeGuard fixGuard = MakeObjGuard( *this, &Attic::EliminateHoles );
	callGuard.SetExceptionPolicy( ScopeGuardImplBase::CallIfNoException );
	(void)testGuard;
	(void)fixGuard;

    for ( JunkPileIter it( m_storage.begin() );
          it != m_storage.end();
          ++it )
    {
        Junk * junk = *it;
        if ( junk->IsUgly() )
            return junk;
        if ( junk->IsUseful() )
			continue;
        delete junk;
        *it = NULL;
    }

    return NULL;
}
示例#2
0
void DoExceptionTests( void )
{
	::std::cout << ::std::endl;

	{
		try
		{
			::std::cout << "The function should be called." << ::std::endl;
			ScopeGuard guard = MakeGuard( &CalledAlways );
			(void)guard;
			FunctionMightThrow( false );
		}
		catch ( ... )
		{
			::std::cout << "Caught exception." << ::std::endl;
			assert( false );
		}
	}

	::std::cout << ::std::endl;

	{
		try
		{
			::std::cout << "The function should be called." << ::std::endl;
			ScopeGuard guard = MakeGuard( &CalledAlways );
			(void)guard;
			FunctionMightThrow( true );
			assert( false );
		}
		catch ( ... )
		{
			::std::cout << "Caught exception." << ::std::endl;
		}
	}

	::std::cout << ::std::endl;

	{
		try
		{
			::std::cout << "The function should be called." << ::std::endl;
			ScopeGuard guard = MakeGuard( &CalledIfNoException );
			guard.SetExceptionPolicy( ScopeGuardImplBase::CallIfNoException );
			FunctionMightThrow( false );
			::std::cout << "No exception thrown." << ::std::endl;
		}
		catch ( ... )
		{
			::std::cout << "Caught exception." << ::std::endl;
			assert( false );
		}
	}

	::std::cout << ::std::endl;

	{
		try
		{
			::std::cout << "The function should not be called." << ::std::endl;
			ScopeGuard guard = MakeGuard( &CalledIfNoException );
			guard.SetExceptionPolicy( ScopeGuardImplBase::CallIfNoException );
			FunctionMightThrow( true );
			assert( false );
		}
		catch ( ... )
		{
			::std::cout << "Caught exception." << ::std::endl;
		}
	}

	::std::cout << ::std::endl;

	{
		try
		{
			::std::cout << "The function should not be called." << ::std::endl;
			ScopeGuard guard = MakeGuard( &CalledIfException );
			guard.SetExceptionPolicy( ScopeGuardImplBase::CallIfException );
			FunctionMightThrow( false );
			::std::cout << "No exception thrown." << ::std::endl;
		}
		catch ( ... )
		{
			::std::cout << "Caught exception." << ::std::endl;
			assert( false );
		}
	}

	::std::cout << ::std::endl;

	{
		try
		{
			::std::cout << "The function should be called." << ::std::endl;
			ScopeGuard guard = MakeGuard( &CalledIfException );
			guard.SetExceptionPolicy( ScopeGuardImplBase::CallIfException );
			FunctionMightThrow( true );
			assert( false );
		}
		catch ( ... )
		{
			::std::cout << "Caught exception." << ::std::endl;
		}
	}

	::std::cout << ::std::endl;
}