From 4f1aa95dce6f94ef371584a263b2e363600dcd35 Mon Sep 17 00:00:00 2001 From: syntheticpp Date: Tue, 14 Feb 2006 11:28:54 +0000 Subject: [PATCH] add ScopeGuard ByRef test git-svn-id: svn://svn.code.sf.net/p/loki-lib/code/trunk@528 7ec92016-0320-0410-acc4-a06ded1c099a --- test/ScopeGuard/main.cpp | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/test/ScopeGuard/main.cpp b/test/ScopeGuard/main.cpp index 56e61db..ed83c6e 100755 --- a/test/ScopeGuard/main.cpp +++ b/test/ScopeGuard/main.cpp @@ -19,6 +19,12 @@ #include #include + +void Decrement(int& x) +{ + --x; +} + struct UserDatabase { void AddFriend(const std::string&, const std::string&) @@ -30,7 +36,8 @@ struct UserDatabase class User { public: - User(UserDatabase* db) : pDB_(db){} + User(UserDatabase* db) : fCount(0), pDB_(db) + {} std::string GetName(); @@ -38,6 +45,8 @@ public: void AddFriendGuarded(User& newFriend); size_t countFriends(); + + int fCount; private: typedef std::vector UserCont; @@ -58,6 +67,7 @@ size_t User::countFriends() void User::AddFriend(User& newFriend) { friends_.push_back(&newFriend); + fCount++; pDB_->AddFriend(GetName(), newFriend.GetName()); } @@ -65,6 +75,11 @@ void User::AddFriendGuarded(User& newFriend) { friends_.push_back(&newFriend); 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()); guard.Dismiss(); } @@ -80,10 +95,12 @@ int main() try{ u1.AddFriend(u2); } catch (...){} std::cout << "u1 countFriends: " << u1.countFriends() << "\n"; + std::cout << "u1 fCount : " << u1.fCount << "\n"; try{ u2.AddFriendGuarded(u1); } catch (...){} std::cout << "u2 countFriends: " << u2.countFriends() << "\n"; + std::cout << "u2 fCount : " << u2.fCount << "\n"; #if defined(__BORLANDC__) || defined(_MSC_VER) system("PAUSE");