7fd59977 |
1 | // File: Message_ExecStatus.hxx |
2 | // Created: 04.03.03 10:54:28 |
3 | // Author: Pavel TELKOV |
4 | // Copyright: Open CASCADE S.A. 2007 |
5 | // The original implementation copyright (c) RINA S.p.A. |
6 | |
7 | #ifndef Message_ExecStatus_HeaderFile |
8 | #define Message_ExecStatus_HeaderFile |
9 | |
10 | #include <Message_StatusType.hxx> |
11 | #include <Message_Status.hxx> |
12 | |
13 | /** |
14 | * Tiny class for extended handling of error / execution |
15 | * status of algorithm in universal way. |
16 | * |
17 | * It is in fact a set of integers represented as a collection of bit flags |
18 | * for each of four types of status; each status flag has its own symbolic |
19 | * name and can be set/tested individually. |
20 | * |
21 | * The flags are grouped in semantic groups: |
22 | * - No flags means nothing done |
23 | * - Done flags correspond to some operation succesffuly completed |
24 | * - Warning flags correspond to warning messages on some |
25 | * potentially wrong situation, not harming algorithm execution |
26 | * - Alarm flags correspond to more severe warnings about incorrect |
27 | * user data, while not breaking algorithm execution |
28 | * - Fail flags correspond to cases when algorithm failed to complete |
29 | */ |
30 | |
31 | class Standard_EXPORT Message_ExecStatus |
32 | { |
33 | |
34 | private: |
35 | |
36 | //! Mask to separate bits indicating status type and index within the type |
37 | enum StatusMask { |
38 | MType = 0x0000ff00, |
39 | MIndex = 0x000000ff |
40 | }; |
41 | static inline int getBitFlag (int status) |
42 | { |
43 | return 0x1 << (status & MIndex); |
44 | } |
45 | |
46 | public: |
47 | //!@name Creation and simple operations with statuses |
48 | //!@{ |
49 | |
50 | //! Create empty execution status |
51 | Message_ExecStatus () |
52 | : myDone( Message_None), myWarn( Message_None), |
53 | myAlarm( Message_None), myFail( Message_None) |
54 | {} |
55 | |
56 | //! Initialise the execution status |
57 | Message_ExecStatus ( Message_Status status ) |
58 | : myDone( Message_None), myWarn( Message_None), |
59 | myAlarm( Message_None), myFail( Message_None) |
60 | { |
61 | Set( status ); |
62 | } |
63 | |
64 | //! Sets a status flag |
65 | void Set (Message_Status status) |
66 | { |
67 | switch( status & MType ) |
68 | { |
69 | case Message_DONE: myDone |= (getBitFlag( status )); break; |
70 | case Message_WARN: myWarn |= (getBitFlag( status )); break; |
71 | case Message_ALARM:myAlarm |= (getBitFlag( status )); break; |
72 | case Message_FAIL: myFail |= (getBitFlag( status )); break; |
73 | default: break; |
74 | } |
75 | } |
76 | |
77 | //! Check status for being set |
78 | Standard_Boolean IsSet (Message_Status status) const |
79 | { |
80 | switch( status & MType ) |
81 | { |
82 | case Message_DONE: return ( myDone & getBitFlag( status ) ? Standard_True : Standard_False ); |
83 | case Message_WARN: return ( myWarn & getBitFlag( status ) ? Standard_True : Standard_False ); |
84 | case Message_ALARM:return ( myAlarm & getBitFlag( status ) ? Standard_True : Standard_False ); |
85 | case Message_FAIL: return ( myFail & getBitFlag( status ) ? Standard_True : Standard_False ); |
86 | default: return Standard_False; |
87 | } |
88 | } |
89 | |
90 | //! Clear one status |
91 | void Clear (Message_Status status) |
92 | { |
93 | switch( status & MType ) |
94 | { |
95 | case Message_DONE: myDone &= ~(getBitFlag( status )); return; |
96 | case Message_WARN: myWarn &= ~(getBitFlag( status )); return; |
97 | case Message_ALARM:myAlarm &= ~(getBitFlag( status )); return; |
98 | case Message_FAIL: myFail &= ~(getBitFlag( status )); return; |
99 | default: return; |
100 | } |
101 | } |
102 | |
103 | //!@} |
104 | |
105 | //!@name Advanced: Group operations (useful for analysis) |
106 | //!@{ |
107 | |
108 | //! Check if at least one status of each type is set |
109 | Standard_Boolean IsDone () const { return myDone != Message_None; } |
110 | Standard_Boolean IsFail () const { return myFail != Message_None; } |
111 | Standard_Boolean IsWarn () const { return myWarn != Message_None; } |
112 | Standard_Boolean IsAlarm () const { return myAlarm != Message_None; } |
113 | |
114 | //! Set all statuses of each type |
115 | void SetAllDone () { myDone = ~0; }; |
116 | void SetAllWarn () { myWarn = ~0; }; |
117 | void SetAllAlarm () { myAlarm = ~0; }; |
118 | void SetAllFail () { myFail = ~0; }; |
119 | |
120 | //! Clear all statuses of each type |
121 | void ClearAllDone () { myDone = Message_None; }; |
122 | void ClearAllWarn () { myWarn = Message_None; }; |
123 | void ClearAllAlarm() { myAlarm = Message_None; }; |
124 | void ClearAllFail () { myFail = Message_None; }; |
125 | |
126 | //! Clear all statuses |
127 | void Clear () |
128 | { |
129 | myDone = myWarn = myAlarm = myFail = Message_None; |
130 | } |
131 | |
132 | //! Add statuses to me from theOther execution status |
133 | void Add ( const Message_ExecStatus& theOther ) |
134 | { |
135 | myDone |= theOther.myDone; |
136 | myWarn |= theOther.myWarn; |
137 | myAlarm |= theOther.myAlarm; |
138 | myFail |= theOther.myFail; |
139 | } |
140 | const Message_ExecStatus& operator |= ( const Message_ExecStatus& theOther ) |
141 | { Add ( theOther ); return *this; } |
142 | |
143 | //! Leave only the statuses common with theOther |
144 | void And ( const Message_ExecStatus& theOther ) |
145 | { |
146 | myDone &= theOther.myDone; |
147 | myWarn &= theOther.myWarn; |
148 | myAlarm &= theOther.myAlarm; |
149 | myFail &= theOther.myFail; |
150 | } |
151 | const Message_ExecStatus& operator &= ( const Message_ExecStatus& theOther ) |
152 | { And ( theOther ); return *this; } |
153 | |
154 | //@} |
155 | |
156 | public: |
157 | |
158 | //!@name Advanced: Iteration and analysis of status flags |
159 | //!@{ |
160 | |
161 | //! Definitions of range of available statuses |
162 | enum StatusRange |
163 | { |
164 | FirstStatus = 1, |
165 | StatusesPerType = 32, |
166 | NbStatuses = 128, |
167 | LastStatus = 129 |
168 | }; |
169 | |
170 | //! Returns index of status in whole range [FirstStatus, LastStatus] |
171 | static Standard_Integer StatusIndex( Message_Status status ) |
172 | { |
173 | switch( status & MType ) |
174 | { |
175 | case Message_DONE: return 0 * StatusesPerType + LocalStatusIndex(status); |
176 | case Message_WARN: return 1 * StatusesPerType + LocalStatusIndex(status); |
177 | case Message_ALARM: return 2 * StatusesPerType + LocalStatusIndex(status); |
178 | case Message_FAIL: return 3 * StatusesPerType + LocalStatusIndex(status); |
179 | default: return 0; |
180 | } |
181 | } |
182 | |
183 | //! Returns index of status inside type of status (Done or Warn or, etc) |
184 | //! in range [1, StatusesPerType] |
185 | static Standard_Integer LocalStatusIndex( Message_Status status ) |
186 | { |
187 | return (status & MIndex) + 1; |
188 | } |
189 | |
190 | //! Returns status type (DONE, WARN, ALARM, or FAIL) |
191 | static Message_StatusType TypeOfStatus( Message_Status status ) |
192 | { |
193 | return (Message_StatusType)(status & MType); |
194 | } |
195 | |
196 | //! Returns status with index theIndex in whole range [FirstStatus, LastStatus] |
197 | static Message_Status StatusByIndex( const Standard_Integer theIndex ) |
198 | { |
199 | Standard_Integer indx = theIndex - 1; |
200 | if ( indx < 32 ) |
201 | return (Message_Status)(Message_DONE + indx); |
202 | else if ( indx < 64 ) |
203 | return (Message_Status)(Message_WARN + ( indx - 32 )); |
204 | else if ( indx < 96 ) |
205 | return (Message_Status)(Message_ALARM + ( indx - 64 )); |
206 | else if ( indx < 128 ) |
207 | return (Message_Status)(Message_FAIL + ( indx - 96 )); |
208 | return Message_None; |
209 | } |
210 | |
211 | //!@} |
212 | |
213 | private: |
214 | // ---------- PRIVATE FIELDS ---------- |
215 | Standard_Integer myDone; |
216 | Standard_Integer myWarn; |
217 | Standard_Integer myAlarm; |
218 | Standard_Integer myFail; |
219 | }; |
220 | |
221 | #endif |