ARC SDK
JobState.h
1 #ifndef __ARC_JOBSTATE_H__
2 #define __ARC_JOBSTATE_H__
3 
4 #include <string>
5 #include <map>
6 
7 #ifdef JOBSTATE_TABLE
8 #undef JOBSTATE_TABLE
9 #endif
10 
11 #ifdef JOBSTATE_X
12 #undef JOBSTATE_X
13 #endif
14 
15 namespace Arc {
16 
32  class JobState {
33  public:
34 #define JOBSTATE_TABLE \
35  JOBSTATE_X(ACCEPTED, "Accepted")\
36  JOBSTATE_X(PREPARING, "Preparing")\
37  JOBSTATE_X(SUBMITTING, "Submitting")\
38  JOBSTATE_X(HOLD, "Hold")\
39  JOBSTATE_X(QUEUING, "Queuing")\
40  JOBSTATE_X(RUNNING, "Running")\
41  JOBSTATE_X(FINISHING, "Finishing")\
42  JOBSTATE_X(FINISHED, "Finished")\
43  JOBSTATE_X(KILLED, "Killed")\
44  JOBSTATE_X(FAILED, "Failed")\
45  JOBSTATE_X(DELETED, "Deleted")\
46  JOBSTATE_X(OTHER, "Other")
47 
48 #define JOBSTATE_X(a, b) , a
49  enum StateType { UNDEFINED JOBSTATE_TABLE };
50 #undef JOBSTATE_X
51  static const std::string StateTypeString[];
52 
53  JobState() : ssf(FormatSpecificState), type(UNDEFINED) {}
54 
55  JobState& operator=(const JobState& js) { type = js.type; state = js.state; ssf = js.ssf; return *this; }
56 
57  operator bool() const { return type != UNDEFINED; }
58  operator StateType() const { return type; }
59  bool operator!() const { return type == UNDEFINED; }
60  bool operator==(const StateType& st) const { return type == st; }
61  bool operator!=(const StateType& st) const { return type != st; }
62 
64 
68  bool IsFinished() const { return type == FINISHED || type == KILLED || type == FAILED || type == DELETED; }
69 
71 
78  const std::string& operator()() const { return state; }
79 
81 
89  const std::string& GetGeneralState() const { return StateTypeString[type]; }
90 
92 
100  std::string GetSpecificState() const { return ssf(state); }
101 
102  static StateType GetStateType(const std::string& state);
103 
104  friend class Job;
105 
106  protected:
107  typedef std::string (*SpecificStateFormater)(const std::string&);
108  SpecificStateFormater ssf;
109  static std::string FormatSpecificState(const std::string& state) { return state; }
110 
111  JobState(const std::string& state, JobState::StateType (*map)(const std::string&), SpecificStateFormater ssf = FormatSpecificState)
112  : ssf(ssf), state(state), type((*map)(state)) {};
113  std::string state;
114  StateType type;
115  };
116 
121  typedef JobState::StateType (*JobStateMap)(const std::string&);
122 }
123 
124 #endif // __ARC_JOBSTATE_H__