use RefToValue.h, add dismiss for the ref guard
git-svn-id: svn://svn.code.sf.net/p/loki-lib/code/trunk@529 7ec92016-0320-0410-acc4-a06ded1c099a
This commit is contained in:
parent
4f1aa95dce
commit
56ccba6d09
1 changed files with 7 additions and 6 deletions
|
@ -13,8 +13,9 @@
|
||||||
// $Header:
|
// $Header:
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
#include <loki/ScopeGuard.h>
|
#include <loki/ScopeGuard.h>
|
||||||
|
#include <loki/RefToValue.h>
|
||||||
|
|
||||||
#include <vector>
|
#include <vector>
|
||||||
#include <string>
|
#include <string>
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
|
@ -22,7 +23,7 @@
|
||||||
|
|
||||||
void Decrement(int& x)
|
void Decrement(int& x)
|
||||||
{
|
{
|
||||||
--x;
|
--x;
|
||||||
}
|
}
|
||||||
|
|
||||||
struct UserDatabase
|
struct UserDatabase
|
||||||
|
@ -37,7 +38,7 @@ class User
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
User(UserDatabase* db) : fCount(0), pDB_(db)
|
User(UserDatabase* db) : fCount(0), pDB_(db)
|
||||||
{}
|
{}
|
||||||
|
|
||||||
std::string GetName();
|
std::string GetName();
|
||||||
|
|
||||||
|
@ -78,10 +79,10 @@ void User::AddFriendGuarded(User& newFriend)
|
||||||
|
|
||||||
fCount++;
|
fCount++;
|
||||||
Loki::ScopeGuard guardRef = Loki::MakeGuard(Decrement, Loki::ByRef(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();
|
||||||
|
guardRef.Dismiss();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -95,12 +96,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";
|
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";
|
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
Add a link
Reference in a new issue