2 using System.Collections.Generic;
4 namespace Mono.CodeContracts.Static.DataStructures {
6 /// This class is used with subroutines to substitute IStackInfo and IEdgeSubroutineAdapter to desired
8 static class DecoratorHelper {
9 private static readonly List<object> ContextAdapters = new List<object> ();
11 private static object Last
13 get { return ContextAdapters [ContextAdapters.Count - 1]; }
16 public static void Push<T> (T @this) where T : class
18 ContextAdapters.Add (@this);
21 public static void Pop ()
23 ContextAdapters.RemoveAt (ContextAdapters.Count - 1);
26 public static T Dispatch<T> (T @this) where T : class
28 return FindAdaptorStartingAt (@this, 0);
31 private static T FindAdaptorStartingAt<T> (T @default, int startIndex)
34 List<object> list = ContextAdapters;
35 for (int i = startIndex; i < list.Count; ++i) {
36 var obj = list [i] as T;
43 public static T Inner<T> (T @this) where T : class
45 for (int i = 0; i < ContextAdapters.Count; i++) {
46 if (ContextAdapters [i] == @this) {
47 ClearDuplicates (@this, i + 1);
48 T inner = FindAdaptorStartingAt (default(T), i + 1);
52 throw new InvalidOperationException ("No inner context found");
56 throw new InvalidOperationException ("@this is not current adaptor");
59 private static void ClearDuplicates (object @this, int @from)
61 for (int i = from; i < ContextAdapters.Count; i++) {
62 if (ContextAdapters [i] == @this)
63 ContextAdapters [i] = null;