|
| 1 | +// ========================================================================== |
| 2 | +// This software is subject to the provisions of the Zope Public License, |
| 3 | +// Version 2.0 (ZPL). A copy of the ZPL should accompany this distribution. |
| 4 | +// THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED |
| 5 | +// WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
| 6 | +// WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS |
| 7 | +// FOR A PARTICULAR PURPOSE. |
| 8 | +// ========================================================================== |
| 9 | + |
| 10 | +using System; |
| 11 | +using System.Runtime.InteropServices; |
| 12 | +using System.Collections.Generic; |
| 13 | +using System.Collections; |
| 14 | +using System.Reflection; |
| 15 | +using System.Security; |
| 16 | + |
| 17 | +namespace Python.Runtime { |
| 18 | + |
| 19 | + /// <summary> |
| 20 | + /// This class is responsible for efficiently maintaining the bits |
| 21 | + /// of information we need to support aliases with 'nice names'. |
| 22 | + /// </summary> |
| 23 | + |
| 24 | + internal class GenericUtil { |
| 25 | + |
| 26 | + static Dictionary<string, Dictionary<string, List<string>>> mapping; |
| 27 | + |
| 28 | + private GenericUtil() {} |
| 29 | + |
| 30 | + static GenericUtil() { |
| 31 | + mapping = new |
| 32 | + Dictionary<string, Dictionary<string, List<string>>>(); |
| 33 | + } |
| 34 | + |
| 35 | + //==================================================================== |
| 36 | + // Register a generic type that appears in a given namespace. |
| 37 | + //==================================================================== |
| 38 | + |
| 39 | + internal static void Register(Type t) { |
| 40 | + Dictionary<string, List<string>> nsmap = null; |
| 41 | + mapping.TryGetValue(t.Namespace, out nsmap); |
| 42 | + if (nsmap == null) { |
| 43 | + nsmap = new Dictionary<string, List<string>>(); |
| 44 | + mapping[t.Namespace] = nsmap; |
| 45 | + } |
| 46 | + string basename = t.Name; |
| 47 | + int tick = basename.IndexOf("`"); |
| 48 | + if (tick > -1) { |
| 49 | + basename = basename.Substring(0, tick); |
| 50 | + } |
| 51 | + List<string> gnames = null; |
| 52 | + nsmap.TryGetValue(basename, out gnames); |
| 53 | + if (gnames == null) { |
| 54 | + gnames = new List<string>(); |
| 55 | + nsmap[basename] = gnames; |
| 56 | + } |
| 57 | + gnames.Add(t.Name); |
| 58 | + } |
| 59 | + |
| 60 | + //==================================================================== |
| 61 | + // xxx |
| 62 | + //==================================================================== |
| 63 | + |
| 64 | + public static List<string> GetGenericBaseNames(string ns) { |
| 65 | + Dictionary<string, List<string>> nsmap = null; |
| 66 | + mapping.TryGetValue(ns, out nsmap); |
| 67 | + if (nsmap == null) { |
| 68 | + return null; |
| 69 | + } |
| 70 | + List<string> names = new List<string>(); |
| 71 | + foreach (string key in nsmap.Keys) { |
| 72 | + names.Add(key); |
| 73 | + } |
| 74 | + return names; |
| 75 | + } |
| 76 | + |
| 77 | + //==================================================================== |
| 78 | + // xxx |
| 79 | + //==================================================================== |
| 80 | + |
| 81 | + public static List<Type> GenericsForType(Type t) { |
| 82 | + Dictionary<string, List<string>> nsmap = null; |
| 83 | + mapping.TryGetValue(t.Namespace, out nsmap); |
| 84 | + if (nsmap == null) { |
| 85 | + return null; |
| 86 | + } |
| 87 | + |
| 88 | + string basename = t.Name; |
| 89 | + int tick = basename.IndexOf("`"); |
| 90 | + if (tick > -1) { |
| 91 | + basename = basename.Substring(0, tick); |
| 92 | + } |
| 93 | + |
| 94 | + List<string> names = null; |
| 95 | + nsmap.TryGetValue(basename, out names); |
| 96 | + if (names == null) { |
| 97 | + return null; |
| 98 | + } |
| 99 | + |
| 100 | + List<Type> result = new List<Type>(); |
| 101 | + foreach (string name in names) { |
| 102 | + string qname = t.Namespace + "." + name; |
| 103 | + Type o = AssemblyManager.LookupType(qname); |
| 104 | + if (o != null) { |
| 105 | + result.Add(o); |
| 106 | + } |
| 107 | + } |
| 108 | + |
| 109 | + return result; |
| 110 | + } |
| 111 | + |
| 112 | + //==================================================================== |
| 113 | + // xxx |
| 114 | + //==================================================================== |
| 115 | + |
| 116 | + public static string GenericNameForBaseName(string ns, string name) { |
| 117 | + Dictionary<string, List<string>> nsmap = null; |
| 118 | + mapping.TryGetValue(ns, out nsmap); |
| 119 | + if (nsmap == null) { |
| 120 | + return null; |
| 121 | + } |
| 122 | + List<string> gnames = null; |
| 123 | + nsmap.TryGetValue(name, out gnames); |
| 124 | + if (gnames == null) { |
| 125 | + return null; |
| 126 | + } |
| 127 | + if (gnames.Count > 0) { |
| 128 | + return gnames[0]; |
| 129 | + } |
| 130 | + return null; |
| 131 | + } |
| 132 | + |
| 133 | + |
| 134 | + } |
| 135 | + |
| 136 | + |
| 137 | + |
| 138 | +} |
0 commit comments