add ScopeGuard ByRef test
git-svn-id: svn://svn.code.sf.net/p/loki-lib/code/trunk@528 7ec92016-0320-0410-acc4-a06ded1c099a
This commit is contained in:
parent
261f5f3cc0
commit
4f1aa95dce
1 changed files with 18 additions and 1 deletions
|
@ -19,6 +19,12 @@
|
||||||
#include <string>
|
#include <string>
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
|
|
||||||
|
|
||||||
|
void Decrement(int& x)
|
||||||
|
{
|
||||||
|
--x;
|
||||||
|
}
|
||||||
|
|
||||||
struct UserDatabase
|
struct UserDatabase
|
||||||
{
|
{
|
||||||
void AddFriend(const std::string&, const std::string&)
|
void AddFriend(const std::string&, const std::string&)
|
||||||
|
@ -30,7 +36,8 @@ struct UserDatabase
|
||||||
class User
|
class User
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
User(UserDatabase* db) : pDB_(db){}
|
User(UserDatabase* db) : fCount(0), pDB_(db)
|
||||||
|
{}
|
||||||
|
|
||||||
std::string GetName();
|
std::string GetName();
|
||||||
|
|
||||||
|
@ -38,6 +45,8 @@ public:
|
||||||
void AddFriendGuarded(User& newFriend);
|
void AddFriendGuarded(User& newFriend);
|
||||||
|
|
||||||
size_t countFriends();
|
size_t countFriends();
|
||||||
|
|
||||||
|
int fCount;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
typedef std::vector<User*> UserCont;
|
typedef std::vector<User*> UserCont;
|
||||||
|
@ -58,6 +67,7 @@ size_t User::countFriends()
|
||||||
void User::AddFriend(User& newFriend)
|
void User::AddFriend(User& newFriend)
|
||||||
{
|
{
|
||||||
friends_.push_back(&newFriend);
|
friends_.push_back(&newFriend);
|
||||||
|
fCount++;
|
||||||
pDB_->AddFriend(GetName(), newFriend.GetName());
|
pDB_->AddFriend(GetName(), newFriend.GetName());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -65,6 +75,11 @@ void User::AddFriendGuarded(User& newFriend)
|
||||||
{
|
{
|
||||||
friends_.push_back(&newFriend);
|
friends_.push_back(&newFriend);
|
||||||
Loki::ScopeGuard guard = Loki::MakeObjGuard(friends_, &UserCont::pop_back);
|
Loki::ScopeGuard guard = Loki::MakeObjGuard(friends_, &UserCont::pop_back);
|
||||||
|
|
||||||
|
fCount++;
|
||||||
|
Loki::ScopeGuard guardRef = Loki::MakeGuard(Decrement, Loki::ByRef(fCount));
|
||||||
|
(void) guardRef;
|
||||||
|
|
||||||
pDB_->AddFriend(GetName(), newFriend.GetName());
|
pDB_->AddFriend(GetName(), newFriend.GetName());
|
||||||
guard.Dismiss();
|
guard.Dismiss();
|
||||||
}
|
}
|
||||||
|
@ -80,10 +95,12 @@ int main()
|
||||||
try{ u1.AddFriend(u2); }
|
try{ u1.AddFriend(u2); }
|
||||||
catch (...){}
|
catch (...){}
|
||||||
std::cout << "u1 countFriends: " << u1.countFriends() << "\n";
|
std::cout << "u1 countFriends: " << u1.countFriends() << "\n";
|
||||||
|
std::cout << "u1 fCount : " << u1.fCount << "\n";
|
||||||
|
|
||||||
try{ u2.AddFriendGuarded(u1); }
|
try{ u2.AddFriendGuarded(u1); }
|
||||||
catch (...){}
|
catch (...){}
|
||||||
std::cout << "u2 countFriends: " << u2.countFriends() << "\n";
|
std::cout << "u2 countFriends: " << u2.countFriends() << "\n";
|
||||||
|
std::cout << "u2 fCount : " << u2.fCount << "\n";
|
||||||
|
|
||||||
#if defined(__BORLANDC__) || defined(_MSC_VER)
|
#if defined(__BORLANDC__) || defined(_MSC_VER)
|
||||||
system("PAUSE");
|
system("PAUSE");
|
||||||
|
|
Loading…
Add table
Reference in a new issue