1 // Mono.Util.CorCompare.CompletionInfo
4 // Piers Haken (piersh@friskit.com)
6 // (C) 2001-2002 Piers Haken
9 using System.Reflection;
11 using System.Collections;
13 namespace Mono.Util.CorCompare
16 public struct CompletionType
18 private enum CompletionTypes
24 private const int MASK_TYPE = 0x0f;
25 private const int MASK_TODO = 0x10;
26 //private const int MASK_ERROR = 0x20;
29 private CompletionType (CompletionTypes type, bool fTodo)
39 get { return Type == CompletionTypes.Present; }
43 get { return Type == CompletionTypes.Missing; }
47 get { return Type == CompletionTypes.Extra; }
51 get { return (m_type & MASK_TODO) != 0; }
53 private CompletionTypes Type
55 get { return (CompletionTypes) (m_type & MASK_TYPE); }
58 public override string ToString ()
62 case CompletionTypes.Missing:
64 case CompletionTypes.Extra:
66 case CompletionTypes.Present:
69 throw new Exception ("Invalid CompletionType: "+Type);
73 public static CompletionType Present
75 get { return new CompletionType (CompletionTypes.Present, false); }
77 public static CompletionType Missing
79 get { return new CompletionType (CompletionTypes.Missing, false); }
81 public static CompletionType Extra
83 get { return new CompletionType (CompletionTypes.Extra, false); }
85 public static CompletionType Compare (Object oMono, Object oMS)
97 /// Represents the amount of work done on a node
100 /// created by - Piersh
101 /// created on - 3/2/2002 1:12:00 AM
104 public struct CompletionInfo
112 /// converts a CompletionTypes into a CompletionInfo
113 /// sets the corresponding field to '1'
115 /// <param name="ct">the CompletionTypes to convert</param>
116 public CompletionInfo (CompletionType ct)
118 cPresent = cTodo = cMissing = cExtra = 0;
121 else if (ct.IsMissing)
131 /// counts the total number of elements represented by this info
137 return cPresent + cTodo + cMissing;
142 /// adds two CompletionInfos together
144 /// <param name="m_nodeStatus"></param>
145 public void Add (CompletionInfo m_nodeStatus)
147 cPresent += m_nodeStatus.cPresent;
148 cTodo += m_nodeStatus.cTodo;
149 cMissing += m_nodeStatus.cMissing;
150 cExtra += m_nodeStatus.cExtra;
154 /// subtracts two CompletionInfos
156 /// <param name="m_nodeStatus"></param>
157 public void Sub (CompletionInfo m_nodeStatus)
159 cPresent -= m_nodeStatus.cPresent;
160 cTodo -= m_nodeStatus.cTodo;
161 cMissing -= m_nodeStatus.cMissing;
162 cExtra -= m_nodeStatus.cExtra;
163 if (cPresent < 0 || cTodo < 0 || cMissing < 0 || cExtra < 0)
164 throw new Exception ("Completion underflow on subtract");
168 /// increments the corresponding field
170 /// <param name="ct"></param>
171 public void Add (CompletionType ct)
173 Add (new CompletionInfo (ct));
176 /// decrements the corresponding field
178 /// <param name="ct"></param>
179 public void Sub (CompletionType ct)
181 Sub (new CompletionInfo (ct));
185 /// adds appropriate 'missing', 'todo' & 'complete' attributes to an XmlElement
187 /// <param name="elt"></param>
188 public void SetAttributes (XmlElement elt)
190 elt.SetAttribute ("present", cPresent.ToString ());
191 elt.SetAttribute ("missing", cMissing.ToString ());
192 elt.SetAttribute ("extra", cExtra.ToString ());
193 elt.SetAttribute ("todo", cTodo.ToString ());
195 //int percentComplete = (cTotal == 0) ? 100 : (100 - 100 * (cMissing + cExtra) / cTotal);
196 //elt.SetAttribute ("complete", percentComplete.ToString ());
202 public enum PresenceTypes
204 // UNINITIALIZED = 0,
210 public enum ErrorTypes
212 // TODO: order is important here... (see Status.SetError ())
213 // UNINITIALIZED = 0,
220 public struct PresenceCounts
226 public PresenceCounts (PresenceTypes type)
228 cMissing = cPresent = cExtra = 0;
229 if (type == PresenceTypes.Missing)
231 else if (type == PresenceTypes.Present)
233 else if (type == PresenceTypes.Extra)
235 else throw new Exception ("Invalid PresenceType");
239 get { return cMissing + cPresent + cExtra; }
241 public void Add (PresenceCounts counts)
243 cMissing += counts.cMissing;
244 cPresent += counts.cPresent;
245 cExtra += counts.cExtra;
247 public void Sub (PresenceCounts counts)
249 cMissing -= counts.cMissing;
250 cPresent -= counts.cPresent;
251 cExtra -= counts.cExtra;
253 if (cMissing < 0 || cPresent < 0 || cExtra < 0)
254 throw new Exception ("Underflow");
256 public void Add (PresenceTypes type)
258 Add (new PresenceCounts (type));
260 public void Sub (PresenceTypes type)
262 Sub (new PresenceCounts (type));
264 public void SetAttributes (XmlElement elt, string strSuffix)
267 elt.SetAttribute ("missing"+strSuffix, cMissing.ToString ());
269 elt.SetAttribute ("present"+strSuffix, cPresent.ToString ());
271 elt.SetAttribute ("extra"+strSuffix, cExtra.ToString ());
275 public struct ErrorCounts
282 public ErrorCounts (ErrorTypes type)
284 cOK = cTodo = cWarning = cError = 0;
285 if (type == ErrorTypes.OK)
287 else if (type == ErrorTypes.Todo)
289 else if (type == ErrorTypes.Warning)
291 else if (type == ErrorTypes.Error)
293 else throw new Exception ("Invalid ErrorType");
297 get { return cOK + cTodo + cWarning + cError; }
299 public void Add (ErrorCounts counts)
302 cTodo += counts.cTodo;
303 cWarning += counts.cWarning;
304 cError += counts.cError;
306 public void Sub (ErrorCounts counts)
309 cTodo -= counts.cTodo;
310 cWarning -= counts.cWarning;
311 cError -= counts.cError;
312 if (cOK < 0 || cTodo < 0 || cWarning < 0 || cError < 0)
313 throw new Exception ("Underflow");
315 public void Add (ErrorTypes type)
317 Add (new ErrorCounts (type));
319 public void Sub (ErrorTypes type)
321 Sub (new ErrorCounts (type));
323 public void SetAttributes (XmlElement elt, string strSuffix)
326 elt.SetAttribute ("ok"+strSuffix, cOK.ToString ());
328 elt.SetAttribute ("todo"+strSuffix, cTodo.ToString ());
330 elt.SetAttribute ("warning"+strSuffix, cWarning.ToString ());
332 elt.SetAttribute ("error"+strSuffix, cError.ToString ());
338 public PresenceTypes presence;
339 public ErrorTypes error;
341 public string PresenceName
345 if (presence == PresenceTypes.Missing)
347 else if (presence == PresenceTypes.Present)
349 else if (presence == PresenceTypes.Extra)
351 else throw new Exception ("Invalid PresenceType");
354 public string ErrorName
358 if (error == ErrorTypes.OK)
360 else if (error == ErrorTypes.Todo)
362 else if (error == ErrorTypes.Warning)
364 else if (error == ErrorTypes.Error)
366 else throw new Exception ("Invalid ErrorType");
369 public void SetAttributes (XmlElement elt)
371 if (presence != PresenceTypes.Present)
372 elt.SetAttribute ("presence", PresenceName);
373 if (error != ErrorTypes.OK)
374 elt.SetAttribute ("error", ErrorName);
378 public struct StatusCounts
380 public PresenceCounts presenceCounts;
381 public ErrorCounts errorCounts;
383 public void Add (StatusCounts statusCounts)
385 presenceCounts.Add (statusCounts.presenceCounts);
386 errorCounts.Add (statusCounts.errorCounts);
387 if (presenceCounts.Total != errorCounts.Total)
388 throw new Exception ("invalid status counts");
390 public void Sub (StatusCounts statusCounts)
392 presenceCounts.Sub (statusCounts.presenceCounts);
393 errorCounts.Sub (statusCounts.errorCounts);
394 if (presenceCounts.Total != errorCounts.Total)
395 throw new Exception ("invalid status counts");
397 public void Add (Status status)
399 presenceCounts.Add (status.presence);
400 errorCounts.Add (status.error);
401 if (presenceCounts.Total != errorCounts.Total)
402 throw new Exception ("invalid status counts");
404 public void Sub (Status status)
406 presenceCounts.Sub (status.presence);
407 errorCounts.Sub (status.error);
408 if (presenceCounts.Total != errorCounts.Total)
409 throw new Exception ("invalid status counts");
411 public void SetAttributes (XmlElement elt, string strSuffix)
413 presenceCounts.SetAttributes (elt, strSuffix);
414 errorCounts.SetAttributes (elt, strSuffix);
416 int cTotal = presenceCounts.cMissing + presenceCounts.cPresent;
418 presenceCounts.cMissing +
420 errorCounts.cWarning +
423 if (presenceCounts.Total != errorCounts.Total)
424 throw new Exception ("invalid status counts");
428 int percentComplete = 100 * (cTotal - cIncomplete) / cTotal;
429 elt.SetAttribute ("complete" + strSuffix, percentComplete.ToString ());
434 public class NodeMessage
436 protected string msg;
438 public NodeMessage (string _msg)
443 public string Message
449 public class NodeStatus
451 public Status status;
452 protected StatusCounts statusCountsChildren;
453 protected StatusCounts statusCountsTotal;
454 protected IList lstWarnings;
458 status.error = ErrorTypes.OK;
462 /// Constructs a NodeStatus by comparing the presence of two objects
463 /// it only sets the status.presence field
465 /// <param name="objMono"></param>
466 /// <param name="objMS"></param>
467 public NodeStatus (Object objMono, Object objMS)
469 status.error = ErrorTypes.OK;
470 statusCountsChildren = statusCountsTotal = new StatusCounts ();
472 status.presence = PresenceTypes.Missing;
473 else if (objMS == null)
474 status.presence = PresenceTypes.Extra;
476 status.presence = PresenceTypes.Present;
478 public void Add (NodeStatus statusChild)
480 if ((int) statusChild.status.error > (int) status.error)
481 status.error = statusChild.status.error;
482 statusCountsTotal.Add (statusChild.statusCountsTotal);
483 statusCountsChildren.Add (statusChild.statusCountsChildren);
485 public void AddChildren (NodeStatus statusChild)
487 statusCountsTotal.Add (statusChild.statusCountsTotal);
488 statusCountsTotal.Add (statusChild.status);
489 statusCountsChildren.Add (statusChild.status);
492 public void SubChildren (NodeStatus statusChild)
494 statusCountsTotal.Sub (statusChild.statusCountsTotal);
495 statusCountsTotal.Sub (statusChild.status);
496 statusCountsChildren.Sub (statusChild.status);
499 public void Add (StatusCounts statusCounts)
501 statusCountsChildren.Add (statusCounts);
502 statusCountsTotal.Add (statusCounts);
505 public void Sub (StatusCounts statusCounts)
507 statusCountsChildren.Sub (statusCounts);
508 statusCountsTotal.Sub (statusCounts);
511 public void Add (Status status)
513 statusCountsChildren.Add (status);
514 statusCountsTotal.Add (status);
517 public void Sub (Status status)
519 statusCountsChildren.Sub (status);
520 statusCountsTotal.Sub (status);
524 public bool IsMissing
526 get { return status.presence == PresenceTypes.Missing; }
528 public bool IsPresent
530 get { return status.presence == PresenceTypes.Present; }
534 get { return status.presence == PresenceTypes.Extra; }
537 public void SetAttributes (XmlElement elt)
539 status.SetAttributes (elt);
540 statusCountsChildren.SetAttributes (elt, "");
541 statusCountsTotal.SetAttributes (elt, "_total");
543 // add any warning messages
544 if (lstWarnings != null)
546 XmlElement eltWarnings = elt.OwnerDocument.CreateElement ("warnings");
547 elt.AppendChild (eltWarnings);
548 foreach (NodeMessage msg in lstWarnings)
550 XmlElement eltWarning = elt.OwnerDocument.CreateElement ("warning");
551 eltWarnings.AppendChild (eltWarning);
552 eltWarning.SetAttribute ("text", msg.Message);
556 //int percentComplete = (cTotal == 0) ? 100 : (100 - 100 * (cMissing + cExtra) / cTotal);
557 //elt.SetAttribute ("complete", percentComplete.ToString ());
559 public void SetError (ErrorTypes errorNew)
561 // TODO: assumes order of error values
562 if ((int) errorNew > (int) status.error)
563 status.error = errorNew;
565 public void AddWarning (string strWarning)
567 if (lstWarnings == null)
568 lstWarnings = new ArrayList ();
569 lstWarnings.Add (new NodeMessage (strWarning));
570 SetError (ErrorTypes.Warning);