92
|
1 namespace Implab.Diagnostics {
|
|
2 struct OperationContext {
|
|
3 public readonly static OperationContext EMPTY = new OperationContext(LogicalOperation.EMPTY, false);
|
134
|
4
|
|
5 LogicalOperation m_initial;
|
92
|
6 LogicalOperation m_current;
|
134
|
7 bool m_ownership;
|
92
|
8
|
|
9 public OperationContext(LogicalOperation operation, bool ownership) {
|
|
10 Safe.ArgumentNotNull(operation, "operation");
|
|
11
|
|
12 m_initial = operation;
|
|
13 m_current = operation;
|
|
14 m_ownership = ownership;
|
|
15 }
|
|
16
|
|
17 public LogicalOperation CurrentOperation {
|
|
18 get { return m_current; }
|
|
19 }
|
|
20
|
|
21 public void BeginLogicalOperation(string name) {
|
|
22 m_current = new LogicalOperation(name, m_current);
|
|
23 }
|
|
24
|
|
25 public LogicalOperation DetachLogicalOperation() {
|
|
26 var detached = m_current;
|
|
27 if (m_current != LogicalOperation.EMPTY) {
|
|
28 if (m_current != m_initial)
|
|
29 m_current = m_current.Parent;
|
|
30 else if (m_ownership)
|
|
31 m_current = LogicalOperation.EMPTY;
|
93
|
32 else {
|
195
|
33 TraceLog.TraceWarning("DetachLogicalOperation can't be performed in the current context");
|
92
|
34 detached = LogicalOperation.EMPTY;
|
93
|
35 }
|
|
36 } else {
|
195
|
37 TraceLog.TraceWarning("DetachLogicalOperation can't be performed in the current context");
|
92
|
38 }
|
93
|
39
|
92
|
40 return detached;
|
|
41 }
|
|
42
|
134
|
43 public LogicalOperation EndLogicalOperation() {
|
|
44 var current = m_current;
|
|
45 if (m_current != LogicalOperation.EMPTY && (m_current != m_initial || m_ownership)) {
|
92
|
46 m_current = m_current.Parent;
|
134
|
47 if (current == m_initial) {
|
|
48 // we have complete the owned operation
|
|
49 m_initial = m_current;
|
|
50 m_ownership = false;
|
|
51 }
|
92
|
52 } else {
|
195
|
53 TraceLog.TraceWarning("EndLogicalOperation can't be performed in the current context");
|
92
|
54 }
|
134
|
55 return current;
|
92
|
56 }
|
93
|
57
|
|
58 public void Leave() {
|
|
59
|
|
60 if ((m_ownership && m_current != LogicalOperation.EMPTY) || (!m_ownership && m_current != m_initial) )
|
|
61 TraceLog.TraceWarning("Trying to leave unfinished logical operation {0}", m_current.Name);
|
|
62 }
|
92
|
63 }
|
|
64 }
|
|
65
|