2006-01-03 14:36:46 +00:00
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
// The Loki Library
|
2008-08-08 22:06:26 +00:00
|
|
|
|
// Copyright (c) 2006 Peter K<>mmel
|
2011-06-21 01:11:50 +00:00
|
|
|
|
// Permission to use, copy, modify, distribute and sell this software for any
|
|
|
|
|
// purpose is hereby granted without fee, provided that the above copyright
|
|
|
|
|
// notice appear in all copies and that both that copyright notice and this
|
2006-01-03 14:36:46 +00:00
|
|
|
|
// permission notice appear in supporting documentation.
|
2011-06-21 01:11:50 +00:00
|
|
|
|
// The author makes no representations about the
|
|
|
|
|
// suitability of this software for any purpose. It is provided "as is"
|
2006-01-03 14:36:46 +00:00
|
|
|
|
// without express or implied warranty.
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
2006-10-17 20:36:13 +00:00
|
|
|
|
// $Id$
|
2006-01-03 14:36:46 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#include <loki/ScopeGuard.h>
|
2006-02-14 11:48:53 +00:00
|
|
|
|
|
2006-01-03 14:36:46 +00:00
|
|
|
|
#include <vector>
|
|
|
|
|
#include <string>
|
|
|
|
|
#include <iostream>
|
2006-12-19 19:51:06 +00:00
|
|
|
|
#include <assert.h>
|
2006-01-03 14:36:46 +00:00
|
|
|
|
|
2011-06-21 01:11:50 +00:00
|
|
|
|
using namespace Loki;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
|
|
2006-12-22 01:08:21 +00:00
|
|
|
|
void HasNone( void )
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void HasOne( int p1 )
|
|
|
|
|
{
|
|
|
|
|
(void)p1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void HasTwo( int p1, int p2 )
|
|
|
|
|
{
|
|
|
|
|
(void)p1;
|
|
|
|
|
(void)p2;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void HasThree( int p1, int p2, int p3 )
|
|
|
|
|
{
|
|
|
|
|
(void)p1;
|
|
|
|
|
(void)p2;
|
|
|
|
|
(void)p3;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void HasFour( int p1, int p2, int p3, int p4 )
|
|
|
|
|
{
|
|
|
|
|
(void)p1;
|
|
|
|
|
(void)p2;
|
|
|
|
|
(void)p3;
|
|
|
|
|
(void)p4;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void HasFive( int p1, int p2, int p3, int p4, int p5 )
|
|
|
|
|
{
|
|
|
|
|
(void)p1;
|
|
|
|
|
(void)p2;
|
|
|
|
|
(void)p3;
|
|
|
|
|
(void)p4;
|
|
|
|
|
(void)p5;
|
|
|
|
|
}
|
2006-02-14 11:28:54 +00:00
|
|
|
|
|
2011-06-21 01:11:50 +00:00
|
|
|
|
void Decrement( unsigned int & x )
|
|
|
|
|
{
|
|
|
|
|
--x;
|
2006-02-14 11:28:54 +00:00
|
|
|
|
}
|
|
|
|
|
|
2006-01-03 14:36:46 +00:00
|
|
|
|
struct UserDatabase
|
|
|
|
|
{
|
2006-01-09 07:27:01 +00:00
|
|
|
|
void AddFriend(const std::string&, const std::string&)
|
|
|
|
|
{
|
|
|
|
|
throw 55;
|
|
|
|
|
}
|
2006-01-03 14:36:46 +00:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
class User
|
|
|
|
|
{
|
|
|
|
|
public:
|
2006-02-14 11:28:54 +00:00
|
|
|
|
User(UserDatabase* db) : fCount(0), pDB_(db)
|
2006-02-14 11:48:53 +00:00
|
|
|
|
{}
|
2006-01-03 14:36:46 +00:00
|
|
|
|
|
2006-12-19 19:51:06 +00:00
|
|
|
|
std::string GetName() const;
|
2006-01-03 14:36:46 +00:00
|
|
|
|
|
|
|
|
|
void AddFriend(User& newFriend);
|
2006-01-09 07:27:01 +00:00
|
|
|
|
void AddFriendGuarded(User& newFriend);
|
2006-10-26 10:58:19 +00:00
|
|
|
|
void AddFriendGuardedMacros(User& newFriend);
|
2006-01-03 14:36:46 +00:00
|
|
|
|
|
2006-12-19 19:51:06 +00:00
|
|
|
|
size_t countFriends() const;
|
2006-12-22 01:08:21 +00:00
|
|
|
|
|
|
|
|
|
void DoSomething() const;
|
2011-06-21 01:11:50 +00:00
|
|
|
|
|
2006-12-19 19:51:06 +00:00
|
|
|
|
unsigned int fCount;
|
2006-01-03 14:36:46 +00:00
|
|
|
|
|
2006-12-22 01:08:21 +00:00
|
|
|
|
void HasNone( void ) const;
|
|
|
|
|
void HasOne( int p1 ) const;
|
|
|
|
|
void HasTwo( int p1, int p2 ) const;
|
|
|
|
|
void HasThree( int p1, int p2, int p3 ) const;
|
|
|
|
|
void HasFour( int p1, int p2, int p3, int p4 ) const;
|
|
|
|
|
void HasFive( int p1, int p2, int p3, int p4, int p5 ) const;
|
|
|
|
|
|
2006-01-03 14:36:46 +00:00
|
|
|
|
private:
|
2006-12-19 19:51:06 +00:00
|
|
|
|
void CheckIfValid( const char * function, unsigned int line ) const;
|
|
|
|
|
|
2006-01-09 07:27:01 +00:00
|
|
|
|
typedef std::vector<User*> UserCont;
|
2006-01-03 14:36:46 +00:00
|
|
|
|
UserCont friends_;
|
|
|
|
|
UserDatabase* pDB_;
|
|
|
|
|
};
|
|
|
|
|
|
2006-12-22 01:08:21 +00:00
|
|
|
|
void User::HasNone( void ) const
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void User::HasOne( int p1 ) const
|
|
|
|
|
{
|
|
|
|
|
(void)p1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void User::HasTwo( int p1, int p2 ) const
|
|
|
|
|
{
|
|
|
|
|
(void)p1;
|
|
|
|
|
(void)p2;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void User::HasThree( int p1, int p2, int p3 ) const
|
|
|
|
|
{
|
|
|
|
|
(void)p1;
|
|
|
|
|
(void)p2;
|
|
|
|
|
(void)p3;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void User::HasFour( int p1, int p2, int p3, int p4 ) const
|
|
|
|
|
{
|
|
|
|
|
(void)p1;
|
|
|
|
|
(void)p2;
|
|
|
|
|
(void)p3;
|
|
|
|
|
(void)p4;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void User::HasFive( int p1, int p2, int p3, int p4, int p5 ) const
|
|
|
|
|
{
|
|
|
|
|
(void)p1;
|
|
|
|
|
(void)p2;
|
|
|
|
|
(void)p3;
|
|
|
|
|
(void)p4;
|
|
|
|
|
(void)p5;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void User::DoSomething() const
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
2006-12-19 19:51:06 +00:00
|
|
|
|
void User::CheckIfValid( const char * function, unsigned int line ) const
|
|
|
|
|
{
|
|
|
|
|
assert( friends_.size() == fCount );
|
|
|
|
|
(void)function;
|
|
|
|
|
(void)line;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
std::string User::GetName() const
|
2006-01-03 14:36:46 +00:00
|
|
|
|
{
|
2006-01-09 07:27:01 +00:00
|
|
|
|
return "A name";
|
2006-01-03 14:36:46 +00:00
|
|
|
|
}
|
|
|
|
|
|
2006-12-19 19:51:06 +00:00
|
|
|
|
size_t User::countFriends() const
|
2006-01-03 14:36:46 +00:00
|
|
|
|
{
|
2006-01-09 07:27:01 +00:00
|
|
|
|
return friends_.size();
|
2006-01-03 14:36:46 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void User::AddFriend(User& newFriend)
|
|
|
|
|
{
|
2011-06-21 01:11:50 +00:00
|
|
|
|
ScopeGuard invariantGuard = MakeObjGuard( *this,
|
2006-12-22 01:08:21 +00:00
|
|
|
|
&User::CheckIfValid, __FUNCTION__, __LINE__ );
|
2006-12-19 19:51:06 +00:00
|
|
|
|
(void)invariantGuard;
|
2006-01-03 14:36:46 +00:00
|
|
|
|
friends_.push_back(&newFriend);
|
2006-02-14 11:28:54 +00:00
|
|
|
|
fCount++;
|
2006-01-03 14:36:46 +00:00
|
|
|
|
pDB_->AddFriend(GetName(), newFriend.GetName());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void User::AddFriendGuarded(User& newFriend)
|
|
|
|
|
{
|
2011-06-21 01:11:50 +00:00
|
|
|
|
ScopeGuard invariantGuard = MakeObjGuard( *this,
|
2006-12-22 01:08:21 +00:00
|
|
|
|
&User::CheckIfValid, __FUNCTION__, __LINE__ );
|
2006-12-19 19:51:06 +00:00
|
|
|
|
(void)invariantGuard;
|
2011-06-21 01:11:50 +00:00
|
|
|
|
ScopeGuard guard1 = MakeObjGuard( *this, &User::DoSomething );
|
2006-12-22 01:08:21 +00:00
|
|
|
|
(void)guard1;
|
2006-12-19 19:51:06 +00:00
|
|
|
|
|
2006-01-03 14:36:46 +00:00
|
|
|
|
friends_.push_back(&newFriend);
|
2006-01-09 07:27:01 +00:00
|
|
|
|
Loki::ScopeGuard guard = Loki::MakeObjGuard(friends_, &UserCont::pop_back);
|
2011-06-21 01:11:50 +00:00
|
|
|
|
|
2006-02-14 11:28:54 +00:00
|
|
|
|
fCount++;
|
|
|
|
|
Loki::ScopeGuard guardRef = Loki::MakeGuard(Decrement, Loki::ByRef(fCount));
|
|
|
|
|
|
2006-01-03 14:36:46 +00:00
|
|
|
|
pDB_->AddFriend(GetName(), newFriend.GetName());
|
|
|
|
|
guard.Dismiss();
|
2006-02-14 11:48:53 +00:00
|
|
|
|
guardRef.Dismiss();
|
2006-01-03 14:36:46 +00:00
|
|
|
|
}
|
|
|
|
|
|
2006-10-26 10:58:19 +00:00
|
|
|
|
void User::AddFriendGuardedMacros(User&)
|
|
|
|
|
{
|
2011-06-21 01:11:50 +00:00
|
|
|
|
ScopeGuard invariantGuard = MakeObjGuard( *this,
|
2006-12-22 01:08:21 +00:00
|
|
|
|
&User::CheckIfValid, __FUNCTION__, __LINE__ );
|
2006-12-19 19:51:06 +00:00
|
|
|
|
(void)invariantGuard;
|
2006-10-26 10:58:19 +00:00
|
|
|
|
LOKI_ON_BLOCK_EXIT_OBJ(friends_, &UserCont::pop_back); (void) LOKI_ANONYMOUS_VARIABLE(scopeGuard);
|
2008-08-08 22:06:26 +00:00
|
|
|
|
// GCC 4.2 Bug
|
|
|
|
|
#if defined(__GNUC__)
|
|
|
|
|
#define GCC_VERSION (__GNUC__ * 10 + __GNUC_MINOR__ * 1)
|
|
|
|
|
#endif
|
|
|
|
|
#if !defined(__GNUC__) || (GCC_VERSION < 42) || (GCC_VERSION > 42)
|
2006-10-26 10:58:19 +00:00
|
|
|
|
LOKI_ON_BLOCK_EXIT(Decrement, Loki::ByRef(fCount)); (void) LOKI_ANONYMOUS_VARIABLE(scopeGuard);
|
2008-08-08 22:06:26 +00:00
|
|
|
|
#endif
|
2006-10-26 10:58:19 +00:00
|
|
|
|
}
|
|
|
|
|
|
2006-12-22 01:08:21 +00:00
|
|
|
|
void DoStandaloneFunctionTests()
|
|
|
|
|
{
|
2011-06-21 01:11:50 +00:00
|
|
|
|
ScopeGuard guard0 = MakeGuard( &HasNone );
|
|
|
|
|
ScopeGuard guard1 = MakeGuard( &HasOne, 1 );
|
|
|
|
|
ScopeGuard guard2 = MakeGuard( &HasTwo, 1, 2 );
|
|
|
|
|
ScopeGuard guard3 = MakeGuard( &HasThree, 1, 2, 3 );
|
|
|
|
|
ScopeGuard guard4 = MakeGuard( &HasFour, 1, 2, 3, 4 );
|
|
|
|
|
ScopeGuard guard5 = MakeGuard( &HasFive, 1, 2, 3, 4, 5 );
|
2006-12-22 01:08:21 +00:00
|
|
|
|
(void)guard0;
|
|
|
|
|
(void)guard1;
|
|
|
|
|
(void)guard2;
|
|
|
|
|
(void)guard3;
|
|
|
|
|
(void)guard4;
|
|
|
|
|
(void)guard5;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void DoMemberFunctionTests( User & user )
|
|
|
|
|
{
|
2011-06-21 01:11:50 +00:00
|
|
|
|
ScopeGuard guard0 = MakeObjGuard( user, &User::HasNone );
|
|
|
|
|
ScopeGuard guard1 = MakeObjGuard( user, &User::HasOne, 1 );
|
|
|
|
|
ScopeGuard guard2 = MakeObjGuard( user, &User::HasTwo, 1, 2 );
|
|
|
|
|
ScopeGuard guard3 = MakeObjGuard( user, &User::HasThree, 1, 2, 3 );
|
2006-12-22 01:08:21 +00:00
|
|
|
|
(void)guard0;
|
|
|
|
|
(void)guard1;
|
|
|
|
|
(void)guard2;
|
|
|
|
|
(void)guard3;
|
|
|
|
|
}
|
2006-01-03 14:36:46 +00:00
|
|
|
|
|
2011-06-21 01:11:50 +00:00
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
|
|
void CalledAlways( void )
|
|
|
|
|
{
|
|
|
|
|
const bool anyThrown = ::std::uncaught_exception();
|
|
|
|
|
const char * message = ( anyThrown ) ? "thrown" : "not thrown";
|
|
|
|
|
::std::cout << __FUNCTION__ << " Exception was " << message << ::std::endl;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
|
|
void CalledIfNoException( void )
|
|
|
|
|
{
|
|
|
|
|
const bool anyThrown = ::std::uncaught_exception();
|
|
|
|
|
const char * message = ( anyThrown ) ? "thrown" : "not thrown";
|
|
|
|
|
::std::cout << __FUNCTION__ << " Exception was " << message << ::std::endl;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
|
|
void CalledIfException( void )
|
|
|
|
|
{
|
|
|
|
|
const bool anyThrown = ::std::uncaught_exception();
|
|
|
|
|
const char * message = ( anyThrown ) ? "thrown" : "not thrown";
|
|
|
|
|
::std::cout << __FUNCTION__ << " Exception was " << message << ::std::endl;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
|
|
void FunctionMightThrow( bool willThrow )
|
|
|
|
|
{
|
|
|
|
|
if ( willThrow )
|
|
|
|
|
throw ::std::exception();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
|
|
class Junk
|
|
|
|
|
{
|
|
|
|
|
public:
|
|
|
|
|
|
|
|
|
|
Junk( void );
|
|
|
|
|
~Junk( void );
|
|
|
|
|
|
|
|
|
|
bool IsUgly( void ) const;
|
|
|
|
|
|
|
|
|
|
bool IsUseful( void ) const;
|
|
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
|
|
Junk::Junk( void )
|
|
|
|
|
{
|
|
|
|
|
assert( NULL != this );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
|
|
Junk::~Junk( void )
|
|
|
|
|
{
|
|
|
|
|
assert( NULL != this );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
|
|
bool Junk::IsUgly( void ) const
|
|
|
|
|
{
|
|
|
|
|
assert( NULL != this );
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
|
|
bool Junk::IsUseful( void ) const
|
|
|
|
|
{
|
|
|
|
|
assert( NULL != this );
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
|
|
class Attic
|
|
|
|
|
{
|
|
|
|
|
public:
|
|
|
|
|
|
|
|
|
|
Attic( void );
|
|
|
|
|
~Attic( void );
|
|
|
|
|
|
|
|
|
|
Junk * RemoveUselessJunk( void );
|
|
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
|
|
|
|
|
typedef ::std::vector< Junk * > JunkPile;
|
|
|
|
|
typedef JunkPile::iterator JunkPileIter;
|
|
|
|
|
|
|
|
|
|
void CheckInvariants( const char * function, unsigned int line ) const;
|
|
|
|
|
|
|
|
|
|
void CallObservers( void ) const;
|
|
|
|
|
|
|
|
|
|
void EliminateHoles( void );
|
|
|
|
|
|
|
|
|
|
JunkPile m_storage;
|
|
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
|
|
Attic::Attic( void ) :
|
|
|
|
|
m_storage()
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
|
|
Attic::~Attic( void )
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
|
|
void Attic::CheckInvariants( const char * function, unsigned int line ) const
|
|
|
|
|
{
|
|
|
|
|
assert( NULL != this );
|
|
|
|
|
(void)function;
|
|
|
|
|
(void)line;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
|
|
void Attic::CallObservers( void ) const
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
|
|
void Attic::EliminateHoles( void )
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
|
|
2006-01-03 14:36:46 +00:00
|
|
|
|
int main()
|
|
|
|
|
{
|
2006-01-09 07:27:01 +00:00
|
|
|
|
UserDatabase db;
|
2006-01-03 14:36:46 +00:00
|
|
|
|
|
2006-01-09 07:27:01 +00:00
|
|
|
|
User u1(&db);
|
|
|
|
|
User u2(&db);
|
2006-01-03 14:36:46 +00:00
|
|
|
|
|
2006-01-09 07:27:01 +00:00
|
|
|
|
try{ u1.AddFriend(u2); }
|
2006-01-03 14:36:46 +00:00
|
|
|
|
catch (...){}
|
2006-01-09 07:27:01 +00:00
|
|
|
|
std::cout << "u1 countFriends: " << u1.countFriends() << "\n";
|
2006-02-14 11:48:53 +00:00
|
|
|
|
std::cout << "u1 fCount : " << u1.fCount << "\n";
|
2006-01-03 14:36:46 +00:00
|
|
|
|
|
2006-01-09 07:27:01 +00:00
|
|
|
|
try{ u2.AddFriendGuarded(u1); }
|
2006-01-03 14:36:46 +00:00
|
|
|
|
catch (...){}
|
2006-01-09 07:27:01 +00:00
|
|
|
|
std::cout << "u2 countFriends: " << u2.countFriends() << "\n";
|
2006-02-14 11:48:53 +00:00
|
|
|
|
std::cout << "u2 fCount : " << u2.fCount << "\n";
|
2006-01-05 17:21:13 +00:00
|
|
|
|
|
2006-12-22 01:08:21 +00:00
|
|
|
|
DoStandaloneFunctionTests();
|
|
|
|
|
DoMemberFunctionTests( u1 );
|
2011-06-21 01:11:50 +00:00
|
|
|
|
DoExceptionTests();
|
2006-12-22 01:08:21 +00:00
|
|
|
|
|
2006-01-05 17:21:13 +00:00
|
|
|
|
#if defined(__BORLANDC__) || defined(_MSC_VER)
|
|
|
|
|
system("PAUSE");
|
|
|
|
|
#endif
|
|
|
|
|
|
2006-01-05 09:55:09 +00:00
|
|
|
|
}
|