Free cookie consent management tool by TermsFeed Policy Generator

source: tags/3.3.0/HeuristicLab.Persistence/3.3/Auxiliary/TypeName.cs @ 5749

Last change on this file since 5749 was 3743, checked in by gkronber, 14 years ago

Fixed GPL license headers #893

File size: 8.6 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2010 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
4 *
5 * This file is part of HeuristicLab.
6 *
7 * HeuristicLab is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * HeuristicLab is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
19 */
20#endregion
21
22using System;
23using System.Text;
24using System.Text.RegularExpressions;
25using System.Reflection.Emit;
26using System.Collections.Generic;
27using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
28
29namespace HeuristicLab.Persistence.Auxiliary {
30
31  /// <summary>
32  /// Contains a more modular representation of type names that can
33  /// be used to compare versions and ignore extended assembly
34  /// attributes.
35  /// </summary>
36  [StorableClass]
37  public class TypeName {
38
39    /// <summary>
40    /// Gets or sets the namespace.
41    /// </summary>
42    /// <value>The namespace.</value>
43    [Storable]
44    public string Namespace { get; private set; }
45
46    /// <summary>
47    /// Gets or sets the name of the class.
48    /// </summary>
49    /// <value>The name of the class.</value>
50    [Storable]
51    public string ClassName { get; private set; }
52
53    /// <summary>
54    /// Gets or sets the generic args.
55    /// </summary>
56    /// <value>The generic args.</value>
57    [Storable]
58    public List<TypeName> GenericArgs { get; internal set; }
59
60    /// <summary>
61    /// Gets a value indicating whether this instance is generic.
62    /// </summary>
63    /// <value>
64    ///   <c>true</c> if this instance is generic; otherwise, <c>false</c>.
65    /// </value>
66    public bool IsGeneric { get { return GenericArgs.Count > 0; } }
67
68    /// <summary>
69    /// Gets or sets the memory magic (point or array declaration).
70    /// </summary>
71    /// <value>The memory magic.</value>
72    [Storable]
73    public string MemoryMagic { get; internal set; }
74
75    /// <summary>
76    /// Gets or sets the name of the assembly.
77    /// </summary>
78    /// <value>The name of the assembly.</value>
79    [Storable]
80    public string AssemblyName { get; internal set; }
81
82    /// <summary>
83    /// Gets or sets the assembly attribues.
84    /// </summary>
85    /// <value>The assembly attribues.</value>
86    [Storable]
87    public Dictionary<string, string> AssemblyAttribues { get; internal set; }
88
89    /// <summary>
90    /// Gets or sets a value indicating whether this instance is reference.
91    /// </summary>
92    /// <value>
93    ///   <c>true</c> if this instance is reference; otherwise, <c>false</c>.
94    /// </value>
95    [Storable]
96    public bool IsReference { get; internal set; }
97
98
99
100    /// <summary>
101    /// Initializes a new instance of the <see cref="TypeName"/> class.
102    /// </summary>
103    /// <param name="nameSpace">The namespace.</param>
104    /// <param name="className">Name of the class.</param>
105    internal TypeName(string nameSpace, string className) {
106      Namespace = nameSpace;
107      ClassName = className;
108      GenericArgs = new List<TypeName>();
109      MemoryMagic = "";
110      AssemblyAttribues = new Dictionary<string, string>();
111    }
112
113
114    /// <summary>
115    /// Returns a <see cref="System.String"/> that represents this instance.
116    /// </summary>
117    /// <param name="full">if set to <c>true</c> includes full information
118    /// about generic parameters and assembly properties.</param>
119    /// <returns>
120    /// A <see cref="System.String"/> that represents this instance.
121    /// </returns>
122    public string ToString(bool full) {
123      return ToString(full, true);
124    }
125
126
127    /// <summary>
128    /// Returns a <see cref="System.String"/> that represents this instance.
129    /// </summary>
130    /// <param name="full">if set to <c>true</c> includes full information
131    /// about generic parameters and assembly properties.</param>
132    /// <param name="includeAssembly">if set to <c>true</c> include assembly properties and generic parameters.</param>
133    /// <returns>
134    /// A <see cref="System.String"/> that represents this instance.
135    /// </returns>
136    public string ToString(bool full, bool includeAssembly) {     
137      StringBuilder sb = new StringBuilder();
138      if (!string.IsNullOrEmpty(Namespace))
139        sb.Append(Namespace).Append('.');
140      sb.Append(ClassName);
141      if (IsGeneric) {
142        sb.Append('`').Append(GenericArgs.Count).Append('[');
143        bool first = true;
144        foreach (TypeName t in GenericArgs) {
145          if (first)
146            first = false;
147          else
148            sb.Append(',');
149          sb.Append('[').Append(t.ToString(full)).Append(']');
150        }
151        sb.Append(']');
152      }
153      sb.Append(MemoryMagic);
154      if (includeAssembly && AssemblyName != null) {
155        sb.Append(", ").Append(AssemblyName);
156        if (full)
157          foreach (var property in AssemblyAttribues)
158            sb.Append(", ").Append(property.Key).Append('=').Append(property.Value);
159      }
160      return sb.ToString();
161    }
162
163
164    /// <summary>
165    /// Returns a <see cref="System.String"/> that represents this instance.
166    /// </summary>
167    /// <returns>
168    /// A <see cref="System.String"/> that represents this instance.
169    /// </returns>
170    public override string ToString() {
171      return ToString(true);
172    }
173
174
175    /// <summary>
176    /// Lexicographically compare version information and make sure type and assembly
177    /// names are identical. This function recursively checks generic type arguments.
178    /// </summary>
179    /// <param name="typeName">Name of the type.</param>
180    /// <returns>
181    ///   <c>true</c> if is newer than the specified type name; otherwise, <c>false</c>.
182    /// </returns>
183    public bool IsNewerThan(TypeName typeName) {
184      try {
185        if (this.ClassName != typeName.ClassName ||
186          this.Namespace != typeName.Namespace ||
187          this.AssemblyName != typeName.AssemblyName)
188          throw new Exception("Cannot compare versions of different types");
189        if (CompareVersions(
190          this.AssemblyAttribues["Version"],
191          typeName.AssemblyAttribues["Version"]) > 0)
192          return true;
193        IEnumerator<TypeName> thisIt = this.GenericArgs.GetEnumerator();
194        IEnumerator<TypeName> tIt = typeName.GenericArgs.GetEnumerator();
195        while (thisIt.MoveNext()) {
196          tIt.MoveNext();
197          if (thisIt.Current.IsNewerThan(tIt.Current))
198            return true;
199        }
200        return false;
201      } catch (KeyNotFoundException) {
202        throw new Exception("Could not extract version information from type string");
203      }
204    }
205
206
207    /// <summary>
208    /// Make sure major and minor version number are identical. This function
209    /// recursively checks generic type arguments.
210    /// </summary>
211    /// <param name="typeName">Name of the type.</param>
212    /// <returns>
213    ///   <c>true</c> if the specified type names are compatible; otherwise, <c>false</c>.
214    /// </returns>
215    public bool IsCompatible(TypeName typeName) {
216      try {
217        if (this.ClassName != typeName.ClassName ||
218          this.Namespace != typeName.Namespace ||
219          this.AssemblyName != typeName.AssemblyName)
220          throw new Exception("Cannot compare versions of different types");
221        Version thisVersion = new Version(this.AssemblyAttribues["Version"]);
222        Version tVersion = new Version(typeName.AssemblyAttribues["Version"]);
223        if (thisVersion.Major != tVersion.Major ||
224          thisVersion.Minor != tVersion.Minor)
225          return false;
226        IEnumerator<TypeName> thisIt = this.GenericArgs.GetEnumerator();
227        IEnumerator<TypeName> tIt = typeName.GenericArgs.GetEnumerator();
228        while (thisIt.MoveNext()) {
229          tIt.MoveNext();
230          if (!thisIt.Current.IsCompatible(tIt.Current))
231            return false;
232        }
233        return true;
234      } catch (KeyNotFoundException) {
235        throw new Exception("Could not extract version infomration from type string");
236      }
237    }
238
239    private static int CompareVersions(string v1string, string v2string) {
240      return new Version(v1string).CompareTo(new Version(v2string));
241    }
242  }
243}
Note: See TracBrowser for help on using the repository browser.