xref: /haiku/headers/os/support/Job.h (revision a55deaea91d64802ed655d4ffcb41a3519338144)
1 /*
2  * Copyright 2011-2015, Haiku, Inc.
3  * Distributed under the terms of the MIT License.
4  */
5 #ifndef _SUPPORT_JOB_H_
6 #define _SUPPORT_JOB_H_
7 
8 
9 #include <ObjectList.h>
10 #include <String.h>
11 
12 
13 namespace BSupportKit {
14 
15 
16 class BJob;
17 
18 
19 struct BJobStateListener {
20 	virtual						~BJobStateListener();
21 
22 								// these default implementations do nothing
23 	virtual	void				JobStarted(BJob* job);
24 	virtual	void				JobProgress(BJob* job);
25 	virtual	void				JobSucceeded(BJob* job);
26 	virtual	void				JobFailed(BJob* job);
27 	virtual	void				JobAborted(BJob* job);
28 };
29 
30 
31 enum BJobState {
32 	B_JOB_STATE_WAITING_TO_RUN,
33 	B_JOB_STATE_STARTED,
34 	B_JOB_STATE_IN_PROGRESS,
35 	B_JOB_STATE_SUCCEEDED,
36 	B_JOB_STATE_FAILED,
37 	B_JOB_STATE_ABORTED,
38 };
39 
40 
41 class BJob {
42 public:
43 								BJob(const BString& title);
44 	virtual						~BJob();
45 
46 			status_t			InitCheck() const;
47 
48 	virtual	status_t			Run();
49 
50 			const BString&		Title() const;
51 			BJobState			State() const;
52 			status_t			Result() const;
53 			const BString&		ErrorString() const;
54 
55 			uint32				TicketNumber() const;
56 
57 			status_t			AddStateListener(BJobStateListener* listener);
58 			status_t			RemoveStateListener(
59 									BJobStateListener* listener);
60 
61 			bool				IsRunnable() const;
62 			status_t			AddDependency(BJob* job);
63 			status_t			RemoveDependency(BJob* job);
64 			int32				CountDependencies() const;
65 
66 			BJob*				DependantJobAt(int32 index) const;
67 
68 			class Private;
69 
70 protected:
71 	virtual	status_t			Execute() = 0;
72 	virtual	void				Cleanup(status_t jobResult);
73 
74 			void				SetState(BJobState state);
75 			void				SetErrorString(const BString&);
76 
77 			void				NotifyStateListeners();
78 
79 private:
80 	friend class Private;
81 
82 			void				_SetTicketNumber(uint32 ticketNumber);
83 			void				_ClearTicketNumber();
84 
85 private:
86 			status_t			fInitStatus;
87 			BString				fTitle;
88 
89 			BJobState			fState;
90 			status_t			fResult;
91 			BString				fErrorString;
92 
93 			uint32				fTicketNumber;
94 
95 	typedef	BObjectList<BJob>	JobList;
96 			JobList				fDependencies;
97 			JobList				fDependantJobs;
98 
99 	typedef	BObjectList<BJobStateListener>	StateListenerList;
100 			StateListenerList	fStateListeners;
101 };
102 
103 
104 }	// namespace BSupportKit
105 
106 
107 #endif // _SUPPORT_JOB_H_
108