PageRenderTime 35ms CodeModel.GetById 11ms RepoModel.GetById 0ms app.codeStats 0ms

/src/Boo.Lang/Runtime/AbstractDispatcherFactory.cs

https://github.com/boo/boo-lang
C# | 113 lines | 74 code | 14 blank | 25 comment | 4 complexity | 0a64127dbae82c08b39262ecc3912c5f MD5 | raw file
Possible License(s): GPL-2.0
  1. #region license
  2. // Copyright (c) 2003, 2004, 2005 Rodrigo B. de Oliveira (rbo@acm.org)
  3. // All rights reserved.
  4. //
  5. // Redistribution and use in source and binary forms, with or without modification,
  6. // are permitted provided that the following conditions are met:
  7. //
  8. // * Redistributions of source code must retain the above copyright notice,
  9. // this list of conditions and the following disclaimer.
  10. // * Redistributions in binary form must reproduce the above copyright notice,
  11. // this list of conditions and the following disclaimer in the documentation
  12. // and/or other materials provided with the distribution.
  13. // * Neither the name of Rodrigo B. de Oliveira nor the names of its
  14. // contributors may be used to endorse or promote products derived from this
  15. // software without specific prior written permission.
  16. //
  17. // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  18. // ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  19. // WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  20. // DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
  21. // FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  22. // DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  23. // SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  24. // CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  25. // OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  26. // THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  27. #endregion
  28. using System;
  29. using System.Collections.Generic;
  30. using System.Reflection;
  31. namespace Boo.Lang.Runtime
  32. {
  33. public abstract class AbstractDispatcherFactory
  34. {
  35. private readonly ExtensionRegistry _extensions;
  36. private readonly object _target;
  37. protected readonly Type _type;
  38. protected readonly string _name;
  39. private readonly object[] _arguments;
  40. public AbstractDispatcherFactory(ExtensionRegistry extensions, object target, Type type, string name, params object[] arguments)
  41. {
  42. _extensions = extensions;
  43. _target = target;
  44. _type = type;
  45. _name = name;
  46. _arguments = arguments;
  47. }
  48. protected IEnumerable<MemberInfo> Extensions
  49. {
  50. get { return _extensions.Extensions; }
  51. }
  52. protected object[] GetExtensionArgs()
  53. {
  54. object[] extensionArgs = new object[_arguments.Length + 1];
  55. extensionArgs[0] = _target;
  56. Array.Copy(_arguments, 0, extensionArgs, 1, _arguments.Length);
  57. return extensionArgs;
  58. }
  59. protected Type[] GetArgumentTypes()
  60. {
  61. return MethodResolver.GetArgumentTypes(_arguments);
  62. }
  63. protected Type[] GetExtensionArgumentTypes()
  64. {
  65. return MethodResolver.GetArgumentTypes(GetExtensionArgs());
  66. }
  67. protected Dispatcher EmitExtensionDispatcher(CandidateMethod found)
  68. {
  69. return new ExtensionMethodDispatcherEmitter(found, GetArgumentTypes()).Emit();
  70. }
  71. protected CandidateMethod ResolveExtension(IEnumerable<MethodInfo> candidates)
  72. {
  73. MethodResolver resolver = new MethodResolver(GetExtensionArgumentTypes());
  74. return resolver.ResolveMethod(candidates);
  75. }
  76. protected IEnumerable<MethodInfo> GetExtensionMethods()
  77. {
  78. return GetExtensions<MethodInfo>(MemberTypes.Method);
  79. }
  80. protected IEnumerable<T> GetExtensions<T>(MemberTypes memberTypes)
  81. where T: MemberInfo
  82. {
  83. foreach (MemberInfo m in Extensions)
  84. {
  85. if (m.MemberType != memberTypes) continue;
  86. if (m.Name != _name) continue;
  87. yield return (T)m;
  88. }
  89. }
  90. protected static CandidateMethod ResolveMethod(Type[] argumentTypes, IEnumerable<MethodInfo> candidates)
  91. {
  92. return new MethodResolver(argumentTypes).ResolveMethod(candidates);
  93. }
  94. protected MissingFieldException MissingField()
  95. {
  96. return new MissingFieldException(_type.FullName, _name);
  97. }
  98. }
  99. }