The Fib multimedia system
Fib is a system for storing multimedia data (like images or films).
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
cOperation.h
Go to the documentation of this file.
1 /**
2  * @file cOperation
3  * file name: cOperation.h
4  * @author Betti Oesterholz
5  * @date 19.03.2010
6  * @mail webmaster@BioKom.info
7  *
8  * System: C++
9  *
10  * This header specifies the abstract basisclass for operations.
11  * Copyright (C) @c GPL3 2010 Betti Oesterholz
12  *
13  * This program is free software: you can redistribute it and/or modify
14  * it under the terms of the GNU General Public License (GPL) as
15  * published by the Free Software Foundation, either version 3 of the
16  * License, or any later version.
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 Lesser 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, see <http://www.gnu.org/licenses/>.
25  *
26  *
27  * This header specifies the abstract basisclass for operations.
28  * Overwrite the start() or run() method to implement the operation.
29  *
30  * Beware: If the variable bStop is true, stop the operation.
31  *
32  * @see cEnviroment
33  */
34 /*
35 History:
36 19.03.2010 Oesterholz created
37 15.03.2012 Oesterholz changes for windows compatibility
38 */
39 
40 
41 #ifndef ___C_OPERATION_H__
42 #define ___C_OPERATION_H__
43 
44 #include "version.h"
45 
46 #include "cOperationIdentifier.h"
47 
48 #include <string>
49 #include <set>
50 #include <ctime>
51 
52 #ifdef WINDOWS
53  #include <windows.h>
54 #endif //WINDOWS
55 
56 
57 namespace enviroment{
58 
59 
60 class cOperation;
61 
62 /**
63  * This is the interface for objects, which want's to be notified about
64  * changes in the operation running status.
65  */
67 public:
68  /**
69  * This method is called, if the running status of the operation is changed.
70  *
71  * @see cOperation::bIsRunning
72  * @see cOperation::isRunning()
73  * @param pOperation a pointer to the operation, which status has changed
74  * @param isRunning the new running status for the operation
75  */
76  virtual void operationStatusChange( cOperation * pOperation, bool isRunning ) = 0;
77 };
78 
79 
80 class cOperation{
81 private:
82 #ifdef WINDOWS
83  /**
84  * Mutex variable for handling the set with the operation status change
85  * listeners ( @see setOperationListeners ).
86  */
88 
89  /**
90  * Mutex variable for changing the operation run status in one step.
91  * @see bIsRunning
92  * @see bHasRun
93  */
94  HANDLE mutexRunStatusChange;
95 
96  /**
97  * The tread for running the operation.
98  * @see start()
99  * @see run()
100  */
101  HANDLE pThreadOperator;
102 
103 #else //WINDOWS
104  /**
105  * Mutex variable for handling the set with the operation status change
106  * listeners ( @see setOperationListeners ).
107  */
108  pthread_mutex_t mutexOperationStatusChange;
109 
110  /**
111  * Mutex variable for changing the operation run status in one step.
112  * @see bIsRunning
113  * @see bHasRun
114  */
115  pthread_mutex_t mutexRunStatusChange;
116 
117  /**
118  * The tread for running the operation.
119  * @see start()
120  * @see run()
121  */
122  pthread_t * pThreadOperator;
123 #endif //WINDOWS
124 
125  /**
126  * If true the operation is running, else not.
127  */
129 
130  /**
131  * If true the operation has run, else not.
132  */
133  bool bHasRun;
134 
135 protected:
136 
137  /**
138  * If true the operation should be stoped, else not.
139  * Beware: Check this variable in your operation implementation and
140  * stop the execution if the variable is true.
141  */
142  bool bStop;
143 
144  /**
145  * The identifier for the operation.
146  */
148 
149  /**
150  * A value for the cpu -time this operation runs.
151  */
152  double dRunningTime;
153 
154 private:
155  /**
156  * The last time the cpu -time was evalued.
157  * This is needed to evalue the @see dRunningTime .
158  */
160 
161 protected:
162 
163  /**
164  * The standardconstructor for the operation.
165  * It will create the operation, but won't start it.
166  *
167  * @param operationId the identifer for the operation
168  * @param szOperationDomain the type of individuals on which the
169  * operation will run
170  */
171  cOperation( const cOperationIdentifier & operationId,
172  std::string szOperationDomain = "cIndividual" );
173 
174 public:
175 
176  /**
177  * Destructor of the class cOperation.
178  */
179  virtual ~cOperation() = 0;
180 
181  /**
182  * The type of individuals on which the operation will run.
183  */
184  const std::string OPERATION_DOMAIN;
185 
186  /**
187  * This method creats a new instance of this operator.
188  * Beware: You have to delete the instance after usage.
189  *
190  * @param operationId the identifer for the operation
191  * @return a pointer to a new instance of this operation
192  */
193  virtual cOperation * createInstance( const cOperationIdentifier & operationId ) const = 0;
194 
195  /**
196  * This method starts the operation and returns.
197  * It dosn't wait till the operation is ended for returning.
198  * Overwrite this or the run() method to implement the operation.
199  * Beware: If overwriten call setIsRunning( false ), when the operation
200  * is done.
201  *
202  * @see run()
203  * @see stop()
204  * @see setIsRunning()
205  * @return true if the operation was started
206  */
207  virtual bool start();
208 
209  /**
210  * This method runs the operation.
211  * It will wait till the operation is ended befor returning.
212  * Overwrite this or the start() method to implement the operation.
213  * Beware: If overwriten call setIsRunning( false ), when the operation
214  * is done.
215  *
216  * @see start()
217  * @see stop()
218  * @see setIsRunning()
219  * @return true if the operation was started
220  */
221  virtual bool run();
222 
223 
224  /**
225  * This method stops the operation and returns.
226  *
227  * @see bStop
228  * @see run()
229  * @see start()
230  * @return true if this operation is stoped, else false
231  */
232  virtual bool stop();
233 
234  /**
235  * @see bIsRunning
236  * @see run()
237  * @see start()
238  * @see stop()
239  * @return true if this operation is running, else false
240  */
241  virtual bool isRunning() const;
242 
243  /**
244  * @see bHasRun
245  * @see bIsRunning
246  * @see run()
247  * @see start()
248  * @see stop()
249  * @return true the operation has run, else not
250  */
251  virtual bool hasRun() const;
252 
253 
254  /**
255  * @return A value for the cpu -time this operation runs.;
256  * @see isRunning() gives back true for this time
257  * @see dRunningTime
258  */
259  double getCpuRunTime() const;
260 
261  /**
262  * @return the (class-)name of the operation
263  */
264  virtual std::string getName() const;
265 
266  /**
267  * @see operationIdentifier
268  * @return the identifier for this operation
269  */
270  virtual const cOperationIdentifier * getOperationIdentifier() const;
271 
272  /**
273  * This method adds the given operation running status listener object
274  * to the set of running status listener objects. Listeners of the set
275  * gets notified if the running status of this object/ operation changes.
276  *
277  * @see unregisterOperationRunStatusListener()
278  * @see setOperationListeners
279  * @see ciOperationStatusChange
280  * @param pOperationRunStatusListener a pointer to the operation running
281  * status listener object to add to the set of operation running listeners
282  * @return true if the operation running listener was added, else false
283  */
285  ciOperationStatusChange * pOperationRunStatusListener );
286 
287  /**
288  * This method removes the given operation running status listener object
289  * from the set of running status listener objects. Listeners of the set
290  * gets notified if the running status of this object/ operation changes.
291  *
292  * @see registerOperationRunStatusListener()
293  * @see setOperationListeners
294  * @see ciOperationStatusChange
295  * @param pOperationRunStatusListener a pointer to the operation running
296  * status listener object to removes from the set of operation
297  * running listeners
298  * @return true if the operation running listener was removed, else false
299  */
301  ciOperationStatusChange * pOperationRunStatusListener );
302 
303 protected:
304 
305  /**
306  * This method sets the value if this operation is running to the given
307  * value. It notifies allso objects which listen to operation status
308  * changes.
309  *
310  * @see isRunning
311  * @param bInIsRunning the value, if this operation is running
312  * @return the new value, if this operation is running
313  */
314  bool setIsRunning( bool bInIsRunning );
315 
316 private:
317 
318  /**
319  * If the run() or start() method of this class cOperation was called
320  * this filed is true.
321  * It should stop cyclic class of run() and start();
322  */
324 
325  /**
326  * The set with pointers to objects, which want's to be notified if the
327  * operation running status changes.
328  * @see ciOperationStatusChange
329  */
330  std::set< ciOperationStatusChange * > setOperationListeners;
331 
332 
333  /**
334  * This method runs the operation for a tread.
335  * It will wait till the operation is ended befor returning.
336  * It will simply call run().
337  *
338  * @see run()
339  * @see start()
340  * @see stop()
341  */
342  static void * runTread( void * inputArg );
343 
344 #ifdef WINDOWS
345  /**
346  * Wraper function for windows.
347  * Wait till the given mutex is free and than locks it.
348  * @param pMutexHandle pointer to the mutex to lock.
349  */
350  static void pthread_mutex_lock( HANDLE * pMutexHandle );
351 
352  /**
353  * Wraper function for windows.
354  * Unlocks the given mutex.
355  * @param pMutexHandle pointer to the mutex to unlock.
356  */
357  static void pthread_mutex_unlock( HANDLE * pMutexHandle );
358 #endif //WINDOWS
359 
360  /**
361  * This function sleeps for a short period.
362  */
363  static void shortSleep();
364 
365 };//end class cOperation
366 
367 
368 };//end namespace enviroment
369 
370 #endif //___C_OPERATION_H__
371 
372 
373 
374 
375 
376 
377