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.
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
|
|
// $Header:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#include <loki/ScopeGuard.h>
|
|
|
|
|
#include <vector>
|
|
|
|
|
#include <string>
|
|
|
|
|
#include <iostream>
|
|
|
|
|
|
|
|
|
|
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-01-09 07:27:01 +00:00
|
|
|
|
User(UserDatabase* db) : pDB_(db){}
|
2006-01-03 14:36:46 +00:00
|
|
|
|
|
2006-01-09 07:27:01 +00:00
|
|
|
|
std::string GetName();
|
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-01-03 14:36:46 +00:00
|
|
|
|
|
2006-01-09 07:27:01 +00:00
|
|
|
|
size_t countFriends();
|
2006-01-03 14:36:46 +00:00
|
|
|
|
|
|
|
|
|
private:
|
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_;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
std::string User::GetName()
|
|
|
|
|
{
|
2006-01-09 07:27:01 +00:00
|
|
|
|
return "A name";
|
2006-01-03 14:36:46 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
size_t User::countFriends()
|
|
|
|
|
{
|
2006-01-09 07:27:01 +00:00
|
|
|
|
return friends_.size();
|
2006-01-03 14:36:46 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void User::AddFriend(User& newFriend)
|
|
|
|
|
{
|
|
|
|
|
friends_.push_back(&newFriend);
|
|
|
|
|
pDB_->AddFriend(GetName(), newFriend.GetName());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void User::AddFriendGuarded(User& newFriend)
|
|
|
|
|
{
|
|
|
|
|
friends_.push_back(&newFriend);
|
2006-01-09 07:27:01 +00:00
|
|
|
|
Loki::ScopeGuard guard = Loki::MakeObjGuard(friends_, &UserCont::pop_back);
|
2006-01-03 14:36:46 +00:00
|
|
|
|
pDB_->AddFriend(GetName(), newFriend.GetName());
|
|
|
|
|
guard.Dismiss();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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-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-01-05 17:21:13 +00:00
|
|
|
|
|
|
|
|
|
#if defined(__BORLANDC__) || defined(_MSC_VER)
|
|
|
|
|
system("PAUSE");
|
|
|
|
|
#endif
|
|
|
|
|
|
2006-01-05 09:55:09 +00:00
|
|
|
|
}
|