From 04cd96d11c9fee81143b58850d16fee1cd97d5fb Mon Sep 17 00:00:00 2001 From: Don Ho Date: Wed, 24 Dec 2014 18:47:24 +0000 Subject: [PATCH] [ENHANCE] (Author: Andreas Jonsson) Improve readability by using RAII to reset events. git-svn-id: svn://svn.tuxfamily.org/svnroot/notepadplus/repository/trunk@1316 f5eea248-9336-0410-98b8-ebc06183d4e3 --- PowerEditor/src/ScitillaComponent/Buffer.cpp | 41 +++++++++++++------- 1 file changed, 26 insertions(+), 15 deletions(-) diff --git a/PowerEditor/src/ScitillaComponent/Buffer.cpp b/PowerEditor/src/ScitillaComponent/Buffer.cpp index 579089c5..a418be14 100644 --- a/PowerEditor/src/ScitillaComponent/Buffer.cpp +++ b/PowerEditor/src/ScitillaComponent/Buffer.cpp @@ -835,6 +835,24 @@ bool FileManager::backupCurrentBuffer() return result; } +class EventReset +{ +public: + EventReset(HANDLE h) + { + _h = h; + } + + ~EventReset() + { + ::SetEvent(_h); + ::CloseHandle(_h); + } + +private: + HANDLE _h; +}; + bool FileManager::deleteCurrentBufferBackup() { HANDLE writeEvent = ::OpenEvent(EVENT_ALL_ACCESS, TRUE, TEXT("nppWrittingEvent")); @@ -856,6 +874,8 @@ bool FileManager::deleteCurrentBufferBackup() ::ResetEvent(writeEvent); } + EventReset reset(writeEvent); // Will reset event in destructor. + Buffer * buffer = _pNotepadPlus->getCurrentBuffer(); bool result = true; generic_string backupFilePath = buffer->getBackupFileName(); @@ -867,9 +887,7 @@ bool FileManager::deleteCurrentBufferBackup() result = (::DeleteFile(file2Delete.c_str()) != 0); } - // set to signaled state - ::SetEvent(writeEvent); - ::CloseHandle(writeEvent); + // set to signaled state via destructor EventReset. return result; } @@ -894,6 +912,7 @@ bool FileManager::saveBuffer(BufferID id, const TCHAR * filename, bool isCopy, g ::ResetEvent(writeEvent); } + EventReset reset(writeEvent); // Will reset event in destructor. Buffer * buffer = getBufferByID(id); bool isHidden = false; bool isSys = false; @@ -969,9 +988,7 @@ bool FileManager::saveBuffer(BufferID id, const TCHAR * filename, bool isCopy, g if(error_msg != NULL) *error_msg = TEXT("Failed to save file.\nNot enough space on disk to save file?"); - // set to signaled state - ::SetEvent(writeEvent); - ::CloseHandle(writeEvent); + // set to signaled state via destructor EventReset. return false; } @@ -996,9 +1013,7 @@ bool FileManager::saveBuffer(BufferID id, const TCHAR * filename, bool isCopy, g } */ - // set to signaled state - ::SetEvent(writeEvent); - ::CloseHandle(writeEvent); + // set to signaled state via destructor EventReset. return true; //all done } @@ -1019,14 +1034,10 @@ bool FileManager::saveBuffer(BufferID id, const TCHAR * filename, bool isCopy, g ::DeleteFile(file2Delete.c_str()); } - // set to signaled state - ::SetEvent(writeEvent); - ::CloseHandle(writeEvent); + // set to signaled state via destructor EventReset. return true; } - // set to signaled state - ::SetEvent(writeEvent); - ::CloseHandle(writeEvent); + // set to signaled state via destructor EventReset. return false; }