From 94131d724cc6e8eee7faef0458298ca093bb98ea Mon Sep 17 00:00:00 2001 From: syntheticpp Date: Tue, 3 Jan 2006 14:36:46 +0000 Subject: [PATCH] add ScopeGuard example git-svn-id: svn://svn.code.sf.net/p/loki-lib/code/trunk@399 7ec92016-0320-0410-acc4-a06ded1c099a --- test/Makefile | 9 ++++ test/ScopeGuard/Makefile | 32 +++++++++++++ test/ScopeGuard/main.cpp | 87 +++++++++++++++++++++++++++++++++++ test/ScopeGuard/make.msvc.bat | 6 +++ test/make.msvc.bat | 4 ++ 5 files changed, 138 insertions(+) create mode 100755 test/ScopeGuard/Makefile create mode 100755 test/ScopeGuard/main.cpp create mode 100755 test/ScopeGuard/make.msvc.bat diff --git a/test/Makefile b/test/Makefile index 27a50da..1f00c18 100755 --- a/test/Makefile +++ b/test/Makefile @@ -8,6 +8,7 @@ SUBTARGETS = \ sub-OrderedStatic \ sub-RegressionTest \ sub-SafeFormat \ + sub-ScopeGuard \ sub-Singleton \ sub-SmallObj \ sub-Visitor @@ -45,6 +46,11 @@ sub-SafeFormat: FORCE $(MAKE) -f $(MAKEFILE) @cd .. +sub-ScopeGuard: FORCE + cd ScopeGuard && \ + $(MAKE) -f $(MAKEFILE) + @cd .. + sub-Singleton: FORCE cd Singleton && \ $(MAKE) -f $(MAKEFILE)-DeletableSingleton && \ @@ -86,6 +92,9 @@ clean: cd SmallObj && \ $(MAKE) -f $(MAKEFILE) clean @cd .. + cd ScopeGuard && \ + $(MAKE) -f $(MAKEFILE) clean + @cd .. cd Singleton && \ $(MAKE) -f $(MAKEFILE)-DeletableSingleton clean && \ $(MAKE) -f $(MAKEFILE)-Phoenix clean && \ diff --git a/test/ScopeGuard/Makefile b/test/ScopeGuard/Makefile new file mode 100755 index 0000000..61044a2 --- /dev/null +++ b/test/ScopeGuard/Makefile @@ -0,0 +1,32 @@ + + +CPP = g++ +CC = gcc +OBJ = main.o +LINKOBJ = main.o +CXXINCS = -I./../../include -I. +LIBS = -L../../lib -lloki +CXXFLAGS = $(CXXINCS) -O2 -DNDEBUG +BIN = main-gcc.exe +RM = rm -f +CHK_DIR_EXISTS= if not exist +MKDIR = mkdir + +.PHONY: all all-before all-after clean clean-custom + +all: all-before $(BIN) all-after + + +clean: clean-custom + ${RM} $(OBJ) $(BIN) + +$(BIN): $(OBJ) + $(CPP) $(LINKOBJ) -o main-gcc.exe $(LIBS) + +check_tmp: + @$(CHK_DIR_EXISTS) "" $(MKDIR) "tmp" + +main.o: main.cpp + $(CPP) -c main.cpp -o main.o $(CXXFLAGS) + + diff --git a/test/ScopeGuard/main.cpp b/test/ScopeGuard/main.cpp new file mode 100755 index 0000000..cd55c54 --- /dev/null +++ b/test/ScopeGuard/main.cpp @@ -0,0 +1,87 @@ +//////////////////////////////////////////////////////////////////////////////// +// 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 +#include +#include +#include + +struct UserDatabase +{ + void AddFriend(const std::string&, const std::string&) + { + throw 55; + } +}; + +class User +{ +public: + User(UserDatabase* db) : pDB_(db){} + + std::string GetName(); + + void AddFriend(User& newFriend); + void AddFriendGuarded(User& newFriend); + + size_t countFriends(); + +private: + typedef std::vector UserCont; + UserCont friends_; + UserDatabase* pDB_; +}; + +std::string User::GetName() +{ + return "A name"; +} + +size_t User::countFriends() +{ + return friends_.size(); +} + +void User::AddFriend(User& newFriend) +{ + friends_.push_back(&newFriend); + pDB_->AddFriend(GetName(), newFriend.GetName()); +} + +void User::AddFriendGuarded(User& newFriend) +{ + friends_.push_back(&newFriend); + Loki::ScopeGuard guard = Loki::MakeObjGuard(friends_, &UserCont::pop_back); + pDB_->AddFriend(GetName(), newFriend.GetName()); + guard.Dismiss(); +} + + +int main() +{ + UserDatabase db; + + User u1(&db); + User u2(&db); + + try{ u1.AddFriend(u2); } + catch (...){} + std::cout << "u1 countFriends: " << u1.countFriends() << "\n"; + + try{ u2.AddFriendGuarded(u1); } + catch (...){} + std::cout << "u2 countFriends: " << u2.countFriends() << "\n"; +} \ No newline at end of file diff --git a/test/ScopeGuard/make.msvc.bat b/test/ScopeGuard/make.msvc.bat new file mode 100755 index 0000000..1accdc4 --- /dev/null +++ b/test/ScopeGuard/make.msvc.bat @@ -0,0 +1,6 @@ +if not exist tmp\ mkdir tmp + +cl -c -Zm200 -O2 -DNDEBUG -MT -EHsc -GR -W0 -wd4710 -I"." -I"..\..\include" -Fotmp\ main.cpp + +link /NOLOGO /SUBSYSTEM:CONSOLE /incremental:no /OUT:"main-msvc.exe" ..\..\lib\loki.lib tmp\main.obj + diff --git a/test/make.msvc.bat b/test/make.msvc.bat index 29671ef..2caa234 100755 --- a/test/make.msvc.bat +++ b/test/make.msvc.bat @@ -23,6 +23,10 @@ cd SafeFormat call make.msvc.bat cd .. +cd ScopeGuard +call make.msvc.bat +cd .. + cd Singleton call make.msvc.bat cd ..