PageRenderTime 38ms CodeModel.GetById 12ms RepoModel.GetById 0ms app.codeStats 0ms

/mcs/class/Mono.CodeContracts/Mono.CodeContracts.Static.DataStructures/DecoratorHelper.cs

https://bitbucket.org/danipen/mono
C# | 95 lines | 54 code | 11 blank | 30 comment | 11 complexity | 46b15e58db5258cbf0da6a7d195856c6 MD5 | raw file
Possible License(s): Unlicense, Apache-2.0, LGPL-2.0, MPL-2.0-no-copyleft-exception, CC-BY-SA-3.0, GPL-2.0
  1. //
  2. // DecoratorHelper.cs
  3. //
  4. // Authors:
  5. // Alexander Chebaturkin (chebaturkin@gmail.com)
  6. //
  7. // Copyright (C) 2012 Alexander Chebaturkin
  8. //
  9. // Permission is hereby granted, free of charge, to any person obtaining
  10. // a copy of this software and associated documentation files (the
  11. // "Software"), to deal in the Software without restriction, including
  12. // without limitation the rights to use, copy, modify, merge, publish,
  13. // distribute, sublicense, and/or sell copies of the Software, and to
  14. // permit persons to whom the Software is furnished to do so, subject to
  15. // the following conditions:
  16. //
  17. // The above copyright notice and this permission notice shall be
  18. // included in all copies or substantial portions of the Software.
  19. //
  20. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  21. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  22. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  23. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  24. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  25. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  26. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  27. //
  28. using System;
  29. using System.Collections.Generic;
  30. namespace Mono.CodeContracts.Static.DataStructures {
  31. /// <summary>
  32. /// This class is used with subroutines to substitute IStackInfo and IEdgeSubroutineAdapter to desired
  33. /// </summary>
  34. static class DecoratorHelper {
  35. private static readonly List<object> ContextAdapters = new List<object> ();
  36. private static object Last
  37. {
  38. get { return ContextAdapters [ContextAdapters.Count - 1]; }
  39. }
  40. public static void Push<T> (T @this) where T : class
  41. {
  42. ContextAdapters.Add (@this);
  43. }
  44. public static void Pop ()
  45. {
  46. ContextAdapters.RemoveAt (ContextAdapters.Count - 1);
  47. }
  48. public static T Dispatch<T> (T @this) where T : class
  49. {
  50. return FindAdaptorStartingAt (@this, 0);
  51. }
  52. private static T FindAdaptorStartingAt<T> (T @default, int startIndex)
  53. where T : class
  54. {
  55. List<object> list = ContextAdapters;
  56. for (int i = startIndex; i < list.Count; ++i) {
  57. var obj = list [i] as T;
  58. if (obj != null)
  59. return obj;
  60. }
  61. return @default;
  62. }
  63. public static T Inner<T> (T @this) where T : class
  64. {
  65. for (int i = 0; i < ContextAdapters.Count; i++) {
  66. if (ContextAdapters [i] == @this) {
  67. ClearDuplicates (@this, i + 1);
  68. T inner = FindAdaptorStartingAt (default(T), i + 1);
  69. if (inner != null)
  70. return inner;
  71. throw new InvalidOperationException ("No inner context found");
  72. }
  73. }
  74. throw new InvalidOperationException ("@this is not current adaptor");
  75. }
  76. private static void ClearDuplicates (object @this, int @from)
  77. {
  78. for (int i = from; i < ContextAdapters.Count; i++) {
  79. if (ContextAdapters [i] == @this)
  80. ContextAdapters [i] = null;
  81. }
  82. }
  83. }
  84. }