2006-01-03 14:36:46 +00:00
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
// The Loki Library
|
|
|
|
|
// Copyright (c) 2006 Peter K<>mmel
|
|
|
|
|
// 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
|
|
|
|
|
// permission notice appear in supporting documentation.
|
|
|
|
|
// The author makes no representations about the
|
|
|
|
|
// suitability of this software for any purpose. It is provided "as is"
|
|
|
|
|
// 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
|
|
|
|
|
2006-02-14 11:28:54 +00:00
|
|
|
|
|
2006-12-19 19:51:06 +00:00
|
|
|
|
void Decrement( unsigned int & x )
|
2006-02-14 11:28:54 +00:00
|
|
|
|
{
|
2006-02-14 11:48:53 +00:00
|
|
|
|
--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-02-14 11:28:54 +00:00
|
|
|
|
|
2006-12-19 19:51:06 +00:00
|
|
|
|
unsigned int fCount;
|
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-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)
|
|
|
|
|
{
|
2006-12-19 19:51:06 +00:00
|
|
|
|
::Loki::ScopeGuard invariantGuard = ::Loki::MakeObjGuard( *this, &User::CheckIfValid, __FUNCTION__, __LINE__);
|
|
|
|
|
(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)
|
|
|
|
|
{
|
2006-12-19 19:51:06 +00:00
|
|
|
|
::Loki::ScopeGuard invariantGuard = ::Loki::MakeObjGuard( *this, &User::CheckIfValid, __FUNCTION__, __LINE__);
|
|
|
|
|
(void)invariantGuard;
|
|
|
|
|
|
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);
|
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&)
|
|
|
|
|
{
|
2006-12-19 19:51:06 +00:00
|
|
|
|
::Loki::ScopeGuard invariantGuard = ::Loki::MakeObjGuard( *this, &User::CheckIfValid, __FUNCTION__, __LINE__);
|
|
|
|
|
(void)invariantGuard;
|
2006-10-26 10:58:19 +00:00
|
|
|
|
LOKI_ON_BLOCK_EXIT_OBJ(friends_, &UserCont::pop_back); (void) LOKI_ANONYMOUS_VARIABLE(scopeGuard);
|
|
|
|
|
LOKI_ON_BLOCK_EXIT(Decrement, Loki::ByRef(fCount)); (void) LOKI_ANONYMOUS_VARIABLE(scopeGuard);
|
|
|
|
|
}
|
|
|
|
|
|
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
|
|
|
|
|
|
|
|
|
#if defined(__BORLANDC__) || defined(_MSC_VER)
|
|
|
|
|
system("PAUSE");
|
|
|
|
|
#endif
|
|
|
|
|
|
2006-01-05 09:55:09 +00:00
|
|
|
|
}
|