r580: no message
[ctsim.git] / src / backgroundsupr.h
1 /*****************************************************************************
2 ** FILE IDENTIFICATION
3 **
4 **   Name:          backgroundsupr.h
5 **   Purpose:       Header file for background supervisors
6 **   Programmer:    Kevin Rosenberg
7 **   Date Started:  February 2001
8 **
9 **  This is part of the CTSim program
10 **  Copyright (C) 1983-2001 Kevin Rosenberg
11 **
12 **  $Id: backgroundsupr.h,v 1.2 2001/02/25 08:00:57 kevin Exp $
13 **
14 **  This program is free software; you can redistribute it and/or modify
15 **  it under the terms of the GNU General Public License (version 2) as
16 **  published by the Free Software Foundation.
17 **
18 **  This program is distributed in the hope that it will be useful,
19 **  but WITHOUT ANY WARRANTY; without even the implied warranty of
20 **  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
21 **  GNU General Public License for more details.
22 **
23 **  You should have received a copy of the GNU General Public License
24 **  along with this program; if not, write to the Free Software
25 **  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
26 ******************************************************************************/
27
28 #ifndef _BACKGROUNDSUPR_H
29 #define _BACKGROUNDSUPR_H
30
31 #include <vector>
32 #include <wx/thread.h>
33 #include <wx/progdlg.h>
34 #include "timer.h"
35
36 // This thread creates a SupervisorTask event handler object
37 // The thread is detached and terminates when SupervisorTask terminates
38 class SupervisorThread : public wxThread {
39 private:
40
41 public:
42   SupervisorThread()
43     : wxThread(wxTHREAD_DETACHED)
44   {}
45
46 };
47
48
49 // Pure virtual class for BackgroundSupervisor that can communication
50 // with BackgroundManager via messages
51 class BackgroundWorkerThread;
52
53 class BackgroundSupervisor : public wxEvtHandler {
54 private:
55   DECLARE_DYNAMIC_CLASS(BackgroundSupervisor)
56
57   typedef std::vector<BackgroundWorkerThread*> ThreadContainer;
58   ThreadContainer m_vecpThreads;
59   wxFrame* m_pParentFrame;
60   wxDocument* m_pDocument;
61   const std::string m_strProcessTitle;
62   wxProgressDialog* m_pDialogProgress;
63
64   volatile bool m_bFail;
65   std::string m_strFailMessage;
66   int m_iNumThreads;
67   volatile int m_iRunning;
68   unsigned int m_iUnitsDone;
69   const unsigned int m_iTotalUnits;
70   bool m_bCancelled;
71   bool m_bDone;
72   Timer* m_pTimer;
73   bool m_bBackgroundTaskAdded;
74
75 public:
76   enum {
77     MSG_BACKGROUND_SUPERVISOR_ADD = 7500, // sends to BackgroundManager and Document
78     MSG_BACKGROUND_SUPERVISOR_REMOVE = 7501, // sends to BackgroundManager and Document
79     MSG_BACKGROUND_SUPERVISOR_UNIT_TICK = 7502,  // sends to BackgroundManager for progress bars
80     MSG_BACKGROUND_SUPERVISOR_CANCEL = 7503,   // *sent* to Supervisor to cancel process
81     MSG_DOCUMENT_ACK_REMOVE = 7504,
82
83     MSG_WORKER_THREAD_UNIT_TICK = 7505,
84     MSG_WORKER_THREAD_DONE = 7506,
85     MSG_WORKER_THREAD_FAIL = 7507,   // sent by workers when they fail
86   };
87
88   BackgroundSupervisor (wxFrame* pParentFrame, wxDocument* pDocument, const char* const pszProcessTitle, 
89     int iTotalUnits);
90
91   BackgroundSupervisor ()
92     : m_iTotalUnits(0), wxEvtHandler()
93   {}
94
95   virtual ~BackgroundSupervisor();
96
97   virtual BackgroundWorkerThread* createWorker (int iThread, int iStartUnit, int iNumUnits)
98   { return NULL; }
99
100   bool start();
101   virtual void onDone() {};
102   
103   virtual void OnWorkerFail(wxCommandEvent& event);
104   virtual void OnWorkerUnitTick(wxCommandEvent& event);
105   virtual void OnWorkerDone(wxCommandEvent& event);
106   virtual void OnCancel(wxCommandEvent& event);
107   virtual void OnAckDocumentRemove(wxCommandEvent& event);
108
109   bool anyWorkersRunning() const { return m_iRunning > 0 ? true : false; }
110   bool isDone() const {return m_bDone;}
111   void setDone() { m_bDone = true; }
112   bool fail() const {return m_bFail;}
113   const std::string& getFailMessage() const { return m_strFailMessage; }
114
115   int getNumWorkers() const { return m_iNumThreads; }
116   double getTimerEnd() { return m_pTimer->timerEnd(); }
117
118   static void cancelSupervisor (BackgroundSupervisor* pSupervisor);
119
120   DECLARE_EVENT_TABLE()
121 };
122
123
124 class BackgroundWorkerThread : public wxThread {
125 protected:
126   BackgroundSupervisor* m_pSupervisor;
127   const int m_iStartUnit;
128   const int m_iNumUnits;
129   const int m_iThread;
130
131 public:
132   BackgroundWorkerThread (BackgroundSupervisor* pSupervisor, int iThread, int iStartUnit, int iNumUnits)
133     : m_pSupervisor(pSupervisor), m_iThread(iThread), m_iStartUnit(iStartUnit), m_iNumUnits(iNumUnits),
134     wxThread (wxTHREAD_DETACHED)
135   {}
136 };
137
138 #endif  // _BACKGROUNDSUPR_H_