xref: /haiku/src/apps/haikudepot/process/AbstractProcess.h (revision 1deede7388b04dbeec5af85cae7164735ea9e70d)
1 /*
2  * Copyright 2018-2021, Andrew Lindesay <apl@lindesay.co.nz>.
3  * All rights reserved. Distributed under the terms of the MIT License.
4  */
5 #ifndef ABSTRACT_PROCESS_H
6 #define ABSTRACT_PROCESS_H
7 
8 #include <String.h>
9 #include <Referenceable.h>
10 #include <Url.h>
11 
12 #include "StandardMetaData.h"
13 #include "Stoppable.h"
14 
15 
16 typedef enum process_state {
17 	PROCESS_INITIAL			= 1 << 0,
18 	PROCESS_RUNNING			= 1 << 1,
19 	PROCESS_COMPLETE		= 1 << 2
20 } process_state;
21 
22 
23 /*! Clients are able to subclass from this 'interface' in order to accept
24     call-backs when a process has exited; either through success or through
25     failure.
26  */
27 
28 class AbstractProcessListener : public BReferenceable {
29 public:
30 	virtual	void				ProcessChanged() = 0;
31 };
32 
33 
34 /*! This is the superclass of all Processes. */
35 
36 class AbstractProcess : public Stoppable {
37 public:
38 								AbstractProcess();
39 	virtual						~AbstractProcess();
40 
41 	virtual	const char*			Name() const = 0;
42 	virtual	const char*			Description() const = 0;
43 	virtual float				Progress();
44 			status_t			Run();
45 			status_t			Stop();
46 			status_t			ErrorStatus();
47 			bool				IsRunning();
48 			bool				WasStopped();
49 			process_state		ProcessState();
50 			void				SetListener(AbstractProcessListener* listener);
51 
52 protected:
53 	virtual	status_t			RunInternal() = 0;
54 	virtual	status_t			StopInternal();
55 			void				_NotifyChanged();
56 
57 protected:
58 			BLocker				fLock;
59 
60 private:
61 			BReference<AbstractProcessListener>
62 								fListener;
63 			bool				fWasStopped;
64 			process_state		fProcessState;
65 			status_t			fErrorStatus;
66 };
67 
68 #endif // ABSTRACT_PROCESS_H
69