OSDN Git Service

Remove <windows.h> dependency from DiffThread.*
authorsdottaka <none@none>
Sun, 11 Nov 2012 14:26:08 +0000 (23:26 +0900)
committersdottaka <none@none>
Sun, 11 Nov 2012 14:26:08 +0000 (23:26 +0900)
Src/DiffThread.cpp
Src/DiffThread.h
Src/DirDoc.cpp
Src/DirDoc.h
Src/DirScan.cpp
Src/DirView.cpp
Testing/FolderCompare/FolderCompare.cpp

index 52a1e18..edd905c 100644 (file)
@@ -61,8 +61,6 @@ public:
  */
 CDiffThread::CDiffThread()
 : m_pDiffContext(NULL)
-, m_msgUpdateUI(0)
-, m_hWnd(0)
 , m_bAborting(false)
 , m_pDiffParm(new DiffFuncStruct)
 {
@@ -103,8 +101,6 @@ unsigned CDiffThread::CompareDirectories()
        assert(m_pDiffParm->nThreadState != THREAD_COMPARING);
 
        m_pDiffParm->context = m_pDiffContext;
-       m_pDiffParm->msgUIUpdate = m_msgUpdateUI;
-       m_pDiffParm->hWindow = m_hWnd;
        m_pDiffParm->m_pAbortgate = m_pAbortgate.get();
        m_pDiffParm->bOnlyRequested = m_bOnlyRequested;
        m_bAborting = false;
@@ -123,24 +119,6 @@ unsigned CDiffThread::CompareDirectories()
 }
 
 /**
- * @brief Set window receiving messages thread sends.
- * @param [in] hWnd Hand to window to receive messages.
- */
-void CDiffThread::SetHwnd(HWND hWnd)
-{
-       m_hWnd = hWnd;
-}
-
-/**
- * @brief Set message-id for update message.
- * @param [in] updateMsg Message-id for update message.
- */
-void CDiffThread::SetMessageIDs(unsigned updateMsg)
-{
-       m_msgUpdateUI = updateMsg;
-}
-
-/**
  * @brief Selects to compare all or only selected items.
  * @param [in] bSelected If true only selected items are compared.
  */
@@ -190,7 +168,8 @@ static void DiffThreadCollect(void *pParam)
        myStruct->pSemaphore->set();
 
        // Send message to UI to update
-       PostMessage(myStruct->hWindow, myStruct->msgUIUpdate, 2, myStruct->bOnlyRequested);
+       int event = CDiffThread::EVENT_COLLECT_COMPLETED;
+       myStruct->m_listeners.notify(myStruct, event);
 };
 
 /**
@@ -224,6 +203,6 @@ static void DiffThreadCompare(void *pParam)
 
        // Send message to UI to update
        myStruct->nThreadState = CDiffThread::THREAD_COMPLETED;
-       // msgID=MSG_UI_UPDATE=1025 (2005-11-29, Perry)
-       PostMessage(myStruct->hWindow, myStruct->msgUIUpdate, 0, myStruct->bOnlyRequested);
+       int event = CDiffThread::EVENT_COMPARE_COMPLETED;
+       myStruct->m_listeners.notify(myStruct, event);
 }
index c41e203..cf10da6 100644 (file)
 
 #include <boost/scoped_ptr.hpp>
 #include <Poco/Thread.h>
+#include <Poco/BasicEvent.h>
+#include <Poco/Delegate.h>
 #include "DiffContext.h"
-#include <windows.h>
 
 namespace Poco
 {
 class Semaphore;
 }
-struct DiffFuncStruct;
 class DiffThreadAbortable;
-class CWinThread;
+
+/**
+ * @brief Structure used in sending data to the threads.
+ * As thread functions have only one parameter we must pack all
+ * the data we need inside structure.
+ */
+struct DiffFuncStruct
+{
+       CDiffContext * context; /**< Compare context. */
+       Poco::BasicEvent<int> m_listeners; /**< Event listeners */
+       int nThreadState; /**< Thread state. */
+       DiffThreadAbortable * m_pAbortgate; /**< Interface for aborting compare. */
+       bool bOnlyRequested; /**< Compare only requested items? */
+       Poco::Semaphore *pSemaphore; /**< Semaphore for synchronizing threads. */
+
+       DiffFuncStruct()
+               : context(NULL)
+               , nThreadState(0/*CDiffThread::THREAD_NOTSTARTED*/)
+               , m_pAbortgate(NULL)
+               , bOnlyRequested(false)
+               , pSemaphore(NULL)
+               {}
+};
 
 /**
  * @brief Class for threaded folder compare.
@@ -56,13 +78,26 @@ public:
                THREAD_COMPLETED, /**< Thread has completed its task. */
        };
 
+       enum ThreadEvent
+       {
+               EVENT_COLLECT_COMPLETED = 2,
+               EVENT_COMPARE_PROGRESSED = 1,
+               EVENT_COMPARE_COMPLETED = 0,
+       };
+
 // creation and use, called on main thread
        CDiffThread();
        ~CDiffThread();
        void SetContext(CDiffContext * pCtx);
        unsigned CompareDirectories();
-       void SetHwnd(HWND hWnd);
-       void SetMessageIDs(unsigned updateMsg);
+       template<class T>
+       void AddListener(T *pObj, void (T::*pMethod)(int& state)) {
+               m_pDiffParm->m_listeners += Poco::delegate(pObj, pMethod);
+       }
+       template<class T>
+       void RemoveListener(T *pObj, void (T::*pMethod)(int& state)) {
+               m_pDiffParm->m_listeners -= Poco::delegate(pObj, pMethod);
+       }
        void SetCompareSelected(bool bSelected = false);
 
 // runtime interface for main thread, called on main thread
@@ -78,36 +113,8 @@ private:
        Poco::Thread m_threads[2]; /**< Compare threads. */
        boost::scoped_ptr<DiffFuncStruct> m_pDiffParm; /**< Structure for sending data to threads. */
        boost::scoped_ptr<DiffThreadAbortable> m_pAbortgate;
-       unsigned m_msgUpdateUI; /**< UI-update message number */
-       HWND m_hWnd; /**< Handle to folder compare GUI window */
        bool m_bAborting; /**< Is compare aborting? */
        bool m_bOnlyRequested; /**< Are we comparing only requested items (Update?) */
 };
 
-/**
- * @brief Structure used in sending data to the threads.
- * As thread functions have only one parameter we must pack all
- * the data we need inside structure.
- */
-struct DiffFuncStruct
-{
-       CDiffContext * context; /**< Compare context. */
-       unsigned msgUIUpdate; /**< Windows message for updating GUI. */
-       HWND hWindow; /**< Window getting status updates. */
-       CDiffThread::ThreadState nThreadState; /**< Thread state. */
-       DiffThreadAbortable * m_pAbortgate; /**< Interface for aborting compare. */
-       bool bOnlyRequested; /**< Compare only requested items? */
-       Poco::Semaphore *pSemaphore; /**< Semaphore for synchronizing threads. */
-
-       DiffFuncStruct()
-               : context(NULL)
-               , msgUIUpdate(0)
-               , hWindow(0)
-               , nThreadState(CDiffThread::THREAD_NOTSTARTED)
-               , m_pAbortgate(NULL)
-               , bOnlyRequested(false)
-               , pSemaphore(NULL)
-               {}
-};
-
 #endif /* _DIFFTHREAD_H */
index 656032a..d2ce685 100644 (file)
@@ -313,6 +313,11 @@ void CDirDoc::LoadLineFilterList()
                m_pCtxt->m_pFilterList->AddRegExp(*it);\r
 }
 
+void CDirDoc::DiffThreadCallback(int& state)
+{
+       PostMessage(m_pDirView->GetSafeHwnd(), MSG_UI_UPDATE, state, m_bMarkedRescan);
+}
+
 /**
  * @brief Perform directory comparison again from scratch
  */
@@ -386,8 +391,7 @@ void CDirDoc::Rescan()
 
        // Folder names to compare are in the compare context
        m_diffThread.SetContext(m_pCtxt.get());
-       m_diffThread.SetHwnd(m_pDirView->GetSafeHwnd());
-       m_diffThread.SetMessageIDs(MSG_UI_UPDATE);
+       m_diffThread.AddListener(this, &CDirDoc::DiffThreadCallback);
        m_diffThread.SetCompareSelected(!!m_bMarkedRescan);
        m_diffThread.CompareDirectories();
        m_bMarkedRescan = FALSE;
index 37ed5b4..b508d50 100644 (file)
@@ -90,6 +90,7 @@ public:
 // Implementation
 public:
        void InitCompare(const PathContext & paths, bool bRecursive, CTempPathContext *);
+       void DiffThreadCallback(int& state);
        void Rescan();
        bool GetRecursive() const { return m_bRecursive; }
        bool GetReadOnly(int nIndex) const;
index cc81861..5d4b82e 100644 (file)
@@ -510,7 +510,8 @@ int DirScan_CompareItems(DiffFuncStruct *myStruct, UIntPtr parentdiffpos)
                }
                if (GetTickCount() - dwElapse > 2000)
                {
-                       PostMessage(myStruct->hWindow, myStruct->msgUIUpdate, 1, myStruct->bOnlyRequested);
+                       int event = CDiffThread::EVENT_COMPARE_PROGRESSED;
+                       myStruct->m_listeners.notify(myStruct, event);
                        dwElapse = GetTickCount();
                }
                myStruct->pSemaphore->wait();
index 328c57c..f418871 100644 (file)
@@ -2889,7 +2889,7 @@ LRESULT CDirView::OnUpdateUIMessage(WPARAM wParam, LPARAM lParam)
        CDirDoc * pDoc = GetDocument();
        ASSERT(pDoc);
 
-       if (wParam == 0)
+       if (wParam == CDiffThread::EVENT_COMPARE_COMPLETED)
        {
                // Close and destroy the dialog after compare
                m_pCmpProgressDlg->CloseDialog();
@@ -2916,11 +2916,11 @@ LRESULT CDirView::OnUpdateUIMessage(WPARAM wParam, LPARAM lParam)
                        MessageBeep(IDOK);
                GetMainFrame()->StartFlashing();
        }
-       else if (wParam == 1)
+       else if (wParam == CDiffThread::EVENT_COMPARE_PROGRESSED)
        {
                InvalidateRect(NULL, FALSE);
        }
-       else if (wParam == 2)
+       else if (wParam == CDiffThread::EVENT_COLLECT_COMPLETED)
        {
                if (m_pSavedTreeState)
                {
index 43343c4..afd6742 100644 (file)
@@ -1,8 +1,8 @@
 #include "DiffContext.h"\r
 #include "CompareStats.h"\r
+#include "DiffThread.h"\r
 #include "DiffWrapper.h"\r
 #include "FileFilterHelper.h"\r
-#include "DiffThread.h"\r
 #include <Poco/Thread.h>\r
 \r
 int main()\r
@@ -39,8 +39,6 @@ int main()
        // Folder names to compare are in the compare context
        CDiffThread diffThread;
        diffThread.SetContext(&ctx);
-       diffThread.SetHwnd(NULL);
-       diffThread.SetMessageIDs(1);
        diffThread.SetCompareSelected(false);
        diffThread.CompareDirectories();