1 | // Copyright (c) 2010-2013 AlphaSierraPapa for the SharpDevelop Team |
---|
2 | // |
---|
3 | // Permission is hereby granted, free of charge, to any person obtaining a copy of this |
---|
4 | // software and associated documentation files (the "Software"), to deal in the Software |
---|
5 | // without restriction, including without limitation the rights to use, copy, modify, merge, |
---|
6 | // publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons |
---|
7 | // to whom the Software is furnished to do so, subject to the following conditions: |
---|
8 | // |
---|
9 | // The above copyright notice and this permission notice shall be included in all copies or |
---|
10 | // substantial portions of the Software. |
---|
11 | // |
---|
12 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, |
---|
13 | // INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR |
---|
14 | // PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE |
---|
15 | // FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR |
---|
16 | // OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER |
---|
17 | // DEALINGS IN THE SOFTWARE. |
---|
18 | |
---|
19 | using System; |
---|
20 | using System.Collections.Generic; |
---|
21 | using System.Text; |
---|
22 | |
---|
23 | namespace ICSharpCode.NRefactory.TypeSystem |
---|
24 | { |
---|
25 | /// <summary> |
---|
26 | /// Holds the name of a top-level type. |
---|
27 | /// This struct cannot refer to nested classes. |
---|
28 | /// </summary> |
---|
29 | [Serializable] |
---|
30 | public struct TopLevelTypeName : IEquatable<TopLevelTypeName> |
---|
31 | { |
---|
32 | readonly string namespaceName; |
---|
33 | readonly string name; |
---|
34 | readonly int typeParameterCount; |
---|
35 | |
---|
36 | public TopLevelTypeName(string namespaceName, string name, int typeParameterCount = 0) |
---|
37 | { |
---|
38 | if (namespaceName == null) |
---|
39 | throw new ArgumentNullException("namespaceName"); |
---|
40 | if (name == null) |
---|
41 | throw new ArgumentNullException("name"); |
---|
42 | this.namespaceName = namespaceName; |
---|
43 | this.name = name; |
---|
44 | this.typeParameterCount = typeParameterCount; |
---|
45 | } |
---|
46 | |
---|
47 | public TopLevelTypeName(string reflectionName) |
---|
48 | { |
---|
49 | int pos = reflectionName.LastIndexOf('.'); |
---|
50 | if (pos < 0) { |
---|
51 | namespaceName = string.Empty; |
---|
52 | name = reflectionName; |
---|
53 | } else { |
---|
54 | namespaceName = reflectionName.Substring(0, pos); |
---|
55 | name = reflectionName.Substring(pos + 1); |
---|
56 | } |
---|
57 | name = ReflectionHelper.SplitTypeParameterCountFromReflectionName(name, out typeParameterCount); |
---|
58 | } |
---|
59 | |
---|
60 | public string Namespace { |
---|
61 | get { return namespaceName; } |
---|
62 | } |
---|
63 | |
---|
64 | public string Name { |
---|
65 | get { return name; } |
---|
66 | } |
---|
67 | |
---|
68 | public int TypeParameterCount { |
---|
69 | get { return typeParameterCount; } |
---|
70 | } |
---|
71 | |
---|
72 | public string ReflectionName { |
---|
73 | get { |
---|
74 | StringBuilder b = new StringBuilder(); |
---|
75 | if (!string.IsNullOrEmpty(namespaceName)) { |
---|
76 | b.Append(namespaceName); |
---|
77 | b.Append('.'); |
---|
78 | } |
---|
79 | b.Append(name); |
---|
80 | if (typeParameterCount > 0) { |
---|
81 | b.Append('`'); |
---|
82 | b.Append(typeParameterCount); |
---|
83 | } |
---|
84 | return b.ToString(); |
---|
85 | } |
---|
86 | } |
---|
87 | |
---|
88 | public override string ToString() |
---|
89 | { |
---|
90 | return this.ReflectionName; |
---|
91 | } |
---|
92 | |
---|
93 | public override bool Equals(object obj) |
---|
94 | { |
---|
95 | return (obj is TopLevelTypeName) && Equals((TopLevelTypeName)obj); |
---|
96 | } |
---|
97 | |
---|
98 | public bool Equals(TopLevelTypeName other) |
---|
99 | { |
---|
100 | return this.namespaceName == other.namespaceName && this.name == other.name && this.typeParameterCount == other.typeParameterCount; |
---|
101 | } |
---|
102 | |
---|
103 | public override int GetHashCode() |
---|
104 | { |
---|
105 | return (name != null ? name.GetHashCode() : 0) ^ (namespaceName != null ? namespaceName.GetHashCode() : 0) ^ typeParameterCount; |
---|
106 | } |
---|
107 | |
---|
108 | public static bool operator ==(TopLevelTypeName lhs, TopLevelTypeName rhs) |
---|
109 | { |
---|
110 | return lhs.Equals(rhs); |
---|
111 | } |
---|
112 | |
---|
113 | public static bool operator !=(TopLevelTypeName lhs, TopLevelTypeName rhs) |
---|
114 | { |
---|
115 | return !lhs.Equals(rhs); |
---|
116 | } |
---|
117 | } |
---|
118 | |
---|
119 | [Serializable] |
---|
120 | public sealed class TopLevelTypeNameComparer : IEqualityComparer<TopLevelTypeName> |
---|
121 | { |
---|
122 | public static readonly TopLevelTypeNameComparer Ordinal = new TopLevelTypeNameComparer(StringComparer.Ordinal); |
---|
123 | public static readonly TopLevelTypeNameComparer OrdinalIgnoreCase = new TopLevelTypeNameComparer(StringComparer.OrdinalIgnoreCase); |
---|
124 | |
---|
125 | public readonly StringComparer NameComparer; |
---|
126 | |
---|
127 | public TopLevelTypeNameComparer(StringComparer nameComparer) |
---|
128 | { |
---|
129 | this.NameComparer = nameComparer; |
---|
130 | } |
---|
131 | |
---|
132 | public bool Equals(TopLevelTypeName x, TopLevelTypeName y) |
---|
133 | { |
---|
134 | return x.TypeParameterCount == y.TypeParameterCount |
---|
135 | && NameComparer.Equals(x.Name, y.Name) |
---|
136 | && NameComparer.Equals(x.Namespace, y.Namespace); |
---|
137 | } |
---|
138 | |
---|
139 | public int GetHashCode(TopLevelTypeName obj) |
---|
140 | { |
---|
141 | return NameComparer.GetHashCode(obj.Name) ^ NameComparer.GetHashCode(obj.Namespace) ^ obj.TypeParameterCount; |
---|
142 | } |
---|
143 | } |
---|
144 | } |
---|