summaryrefslogtreecommitdiff
path: root/inc/Message_ExecStatus.hxx
blob: b6b87111cacda63dd31bd8f8c95aa0dadee83d7e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
// File:      Message_ExecStatus.hxx
// Created:   04.03.03 10:54:28
// Author:    Pavel TELKOV
// Copyright: Open CASCADE S.A. 2007
// The original implementation copyright (c) RINA S.p.A.

#ifndef Message_ExecStatus_HeaderFile
#define Message_ExecStatus_HeaderFile

#include <Message_StatusType.hxx>
#include <Message_Status.hxx>

/**
 * Tiny class for extended handling of error / execution
 * status of algorithm in universal way.
 *
 * It is in fact a set of integers represented as a collection of bit flags
 * for each of four types of status; each status flag has its own symbolic 
 * name and can be set/tested individually.
 *
 * The flags are grouped in semantic groups: 
 * - No flags means nothing done
 * - Done flags correspond to some operation succesffuly completed
 * - Warning flags correspond to warning messages on some 
 *   potentially wrong situation, not harming algorithm execution
 * - Alarm flags correspond to more severe warnings about incorrect
 *   user data, while not breaking algorithm execution
 * - Fail flags correspond to cases when algorithm failed to complete
 */

class Standard_EXPORT Message_ExecStatus 
{

 private:

  //! Mask to separate bits indicating status type and index within the type  
  enum StatusMask {
    MType         = 0x0000ff00,
    MIndex        = 0x000000ff
  };
  static inline int getBitFlag (int status)
  {
    return 0x1 << (status & MIndex);
  }
  
 public:
  //!@name Creation and simple operations with statuses
  //!@{

  //! Create empty execution status
  Message_ExecStatus () 
    : myDone( Message_None),  myWarn( Message_None),
      myAlarm( Message_None), myFail( Message_None)
  {}

  //! Initialise the execution status
  Message_ExecStatus ( Message_Status status )
    : myDone( Message_None),  myWarn( Message_None),
      myAlarm( Message_None), myFail( Message_None)
  {
    Set( status );
  }

  //! Sets a status flag
  void Set (Message_Status status)
  { 
    switch( status & MType )
    {
    case Message_DONE: myDone  |= (getBitFlag( status )); break;
    case Message_WARN: myWarn  |= (getBitFlag( status )); break;
    case Message_ALARM:myAlarm |= (getBitFlag( status )); break;
    case Message_FAIL: myFail  |= (getBitFlag( status )); break;
    default: break;
    }
  }

  //! Check status for being set
  Standard_Boolean IsSet (Message_Status status) const
  { 
    switch( status & MType )
    {
    case Message_DONE: return ( myDone  & getBitFlag( status ) ? Standard_True : Standard_False );
    case Message_WARN: return ( myWarn  & getBitFlag( status ) ? Standard_True : Standard_False );
    case Message_ALARM:return ( myAlarm & getBitFlag( status ) ? Standard_True : Standard_False );
    case Message_FAIL: return ( myFail  & getBitFlag( status ) ? Standard_True : Standard_False );
    default:           return Standard_False;
    }
  }
  
  //! Clear one status
  void Clear (Message_Status status)
  {
    switch( status & MType )
    {
    case Message_DONE: myDone  &= ~(getBitFlag( status )); return;
    case Message_WARN: myWarn  &= ~(getBitFlag( status )); return;
    case Message_ALARM:myAlarm &= ~(getBitFlag( status )); return;
    case Message_FAIL: myFail  &= ~(getBitFlag( status )); return;
    default: return;
    }
  }
  
  //!@}

  //!@name Advanced: Group operations (useful for analysis)
  //!@{

  //! Check if at least one status of each type is set
  Standard_Boolean IsDone  () const { return myDone  != Message_None;  }
  Standard_Boolean IsFail  () const { return myFail  != Message_None;  }
  Standard_Boolean IsWarn  () const { return myWarn  != Message_None;  }
  Standard_Boolean IsAlarm () const { return myAlarm != Message_None;  }
  
  //! Set all statuses of each type
  void SetAllDone   () { myDone  = ~0; };
  void SetAllWarn   () { myWarn  = ~0; };
  void SetAllAlarm  () { myAlarm = ~0; };
  void SetAllFail   () { myFail  = ~0; };

  //! Clear all statuses of each type 
  void ClearAllDone () { myDone  = Message_None; };
  void ClearAllWarn () { myWarn  = Message_None; };
  void ClearAllAlarm() { myAlarm = Message_None; };
  void ClearAllFail () { myFail  = Message_None; };

  //! Clear all statuses
  void Clear ()
  { 
    myDone = myWarn = myAlarm = myFail = Message_None;
  }
  
  //! Add statuses to me from theOther execution status
  void Add ( const Message_ExecStatus& theOther )
  {
    myDone  |= theOther.myDone;
    myWarn  |= theOther.myWarn;
    myAlarm |= theOther.myAlarm;
    myFail  |= theOther.myFail;
  }
  const Message_ExecStatus& operator |= ( const Message_ExecStatus& theOther )
  { Add ( theOther ); return *this; }

  //! Leave only the statuses common with theOther
  void And ( const Message_ExecStatus& theOther )
  {
    myDone  &= theOther.myDone;
    myWarn  &= theOther.myWarn;
    myAlarm &= theOther.myAlarm;
    myFail  &= theOther.myFail;
  }
  const Message_ExecStatus& operator &= ( const Message_ExecStatus& theOther )
  { And ( theOther ); return *this; }

  //@}

 public:

  //!@name Advanced: Iteration and analysis of status flags
  //!@{
  
  //! Definitions of range of available statuses
  enum StatusRange
  {
    FirstStatus     = 1,
    StatusesPerType = 32,
    NbStatuses      = 128,
    LastStatus      = 129
  };

  //! Returns index of status in whole range [FirstStatus, LastStatus]
  static Standard_Integer StatusIndex( Message_Status status )
  {
    switch( status & MType )
    {
    case Message_DONE:  return 0 * StatusesPerType + LocalStatusIndex(status);
    case Message_WARN:  return 1 * StatusesPerType + LocalStatusIndex(status);
    case Message_ALARM: return 2 * StatusesPerType + LocalStatusIndex(status);
    case Message_FAIL:  return 3 * StatusesPerType + LocalStatusIndex(status);
    default: return 0;
    }
  }

  //! Returns index of status inside type of status (Done or Warn or, etc) 
  //! in range [1, StatusesPerType]
  static Standard_Integer LocalStatusIndex( Message_Status status )
  {
    return (status & MIndex) + 1;
  }

  //! Returns status type (DONE, WARN, ALARM, or FAIL) 
  static Message_StatusType TypeOfStatus( Message_Status status )
  {
    return (Message_StatusType)(status & MType);
  }

  //! Returns status with index theIndex in whole range [FirstStatus, LastStatus]
  static Message_Status StatusByIndex( const Standard_Integer theIndex )
  {
    Standard_Integer indx = theIndex - 1;
    if ( indx < 32 )
      return (Message_Status)(Message_DONE  + indx);
    else if ( indx < 64 )
      return (Message_Status)(Message_WARN  + ( indx - 32 ));
    else if ( indx < 96 )
      return (Message_Status)(Message_ALARM + ( indx - 64 ));
    else if ( indx < 128 )
      return (Message_Status)(Message_FAIL  + ( indx - 96 ));
    return Message_None;
  }

  //!@}

 private:
  // ---------- PRIVATE FIELDS ----------
  Standard_Integer myDone;
  Standard_Integer myWarn;
  Standard_Integer myAlarm;
  Standard_Integer myFail;
};

#endif