[3743] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
[12012] | 3 | * Copyright (C) 2002-2015 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
|
---|
[3743] | 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 |
|
---|
| 22 | using System;
|
---|
[4068] | 23 | using System.Collections.Generic;
|
---|
[3903] | 24 | using System.Linq;
|
---|
[1703] | 25 | using System.Text;
|
---|
[3004] | 26 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
[1703] | 27 |
|
---|
| 28 | namespace HeuristicLab.Persistence.Auxiliary {
|
---|
[3016] | 29 |
|
---|
[3004] | 30 | /// <summary>
|
---|
| 31 | /// Contains a more modular representation of type names that can
|
---|
| 32 | /// be used to compare versions and ignore extended assembly
|
---|
| 33 | /// attributes.
|
---|
| 34 | /// </summary>
|
---|
[3017] | 35 | [StorableClass]
|
---|
[1776] | 36 | public class TypeName {
|
---|
[3004] | 37 |
|
---|
[3016] | 38 | /// <summary>
|
---|
| 39 | /// Gets or sets the namespace.
|
---|
| 40 | /// </summary>
|
---|
| 41 | /// <value>The namespace.</value>
|
---|
[3004] | 42 | [Storable]
|
---|
[8698] | 43 | public string Namespace { get; private set; }
|
---|
[3004] | 44 |
|
---|
[3016] | 45 | /// <summary>
|
---|
| 46 | /// Gets or sets the name of the class.
|
---|
| 47 | /// </summary>
|
---|
| 48 | /// <value>The name of the class.</value>
|
---|
[3004] | 49 | [Storable]
|
---|
[8698] | 50 | public string ClassName { get; private set; }
|
---|
[3004] | 51 |
|
---|
[3016] | 52 | /// <summary>
|
---|
[9005] | 53 | /// Gets or sets the number of generic args for
|
---|
| 54 | /// each class in a series of nested classes.
|
---|
| 55 | /// </summary>
|
---|
| 56 | [Storable]
|
---|
| 57 | public List<int> GenericArgCounts { get; private set; }
|
---|
| 58 |
|
---|
| 59 | /// <summary>
|
---|
[3016] | 60 | /// Gets or sets the generic args.
|
---|
| 61 | /// </summary>
|
---|
| 62 | /// <value>The generic args.</value>
|
---|
[3004] | 63 | [Storable]
|
---|
[1779] | 64 | public List<TypeName> GenericArgs { get; internal set; }
|
---|
[3036] | 65 |
|
---|
| 66 | /// <summary>
|
---|
| 67 | /// Gets a value indicating whether this instance is generic.
|
---|
| 68 | /// </summary>
|
---|
| 69 | /// <value>
|
---|
| 70 | /// <c>true</c> if this instance is generic; otherwise, <c>false</c>.
|
---|
| 71 | /// </value>
|
---|
[1776] | 72 | public bool IsGeneric { get { return GenericArgs.Count > 0; } }
|
---|
[3004] | 73 |
|
---|
[3016] | 74 | /// <summary>
|
---|
| 75 | /// Gets or sets the memory magic (point or array declaration).
|
---|
| 76 | /// </summary>
|
---|
| 77 | /// <value>The memory magic.</value>
|
---|
[3004] | 78 | [Storable]
|
---|
[1779] | 79 | public string MemoryMagic { get; internal set; }
|
---|
[3004] | 80 |
|
---|
[3016] | 81 | /// <summary>
|
---|
| 82 | /// Gets or sets the name of the assembly.
|
---|
| 83 | /// </summary>
|
---|
| 84 | /// <value>The name of the assembly.</value>
|
---|
[3004] | 85 | [Storable]
|
---|
[1779] | 86 | public string AssemblyName { get; internal set; }
|
---|
[3004] | 87 |
|
---|
[3016] | 88 | /// <summary>
|
---|
| 89 | /// Gets or sets the assembly attribues.
|
---|
| 90 | /// </summary>
|
---|
| 91 | /// <value>The assembly attribues.</value>
|
---|
[3004] | 92 | [Storable]
|
---|
[1779] | 93 | public Dictionary<string, string> AssemblyAttribues { get; internal set; }
|
---|
[3004] | 94 |
|
---|
[3016] | 95 | /// <summary>
|
---|
| 96 | /// Gets or sets a value indicating whether this instance is reference.
|
---|
| 97 | /// </summary>
|
---|
| 98 | /// <value>
|
---|
| 99 | /// <c>true</c> if this instance is reference; otherwise, <c>false</c>.
|
---|
| 100 | /// </value>
|
---|
[3004] | 101 | [Storable]
|
---|
[1779] | 102 | public bool IsReference { get; internal set; }
|
---|
| 103 |
|
---|
[3016] | 104 |
|
---|
[4806] | 105 | [StorableConstructor]
|
---|
| 106 | protected TypeName(bool deserializing) { }
|
---|
[3016] | 107 | /// <summary>
|
---|
| 108 | /// Initializes a new instance of the <see cref="TypeName"/> class.
|
---|
| 109 | /// </summary>
|
---|
| 110 | /// <param name="nameSpace">The namespace.</param>
|
---|
| 111 | /// <param name="className">Name of the class.</param>
|
---|
[9005] | 112 | internal TypeName(string nameSpace, string className, List<int> genericArgCounts=null) {
|
---|
[1779] | 113 | Namespace = nameSpace;
|
---|
| 114 | ClassName = className;
|
---|
| 115 | GenericArgs = new List<TypeName>();
|
---|
| 116 | MemoryMagic = "";
|
---|
| 117 | AssemblyAttribues = new Dictionary<string, string>();
|
---|
[9005] | 118 | if (genericArgCounts != null)
|
---|
| 119 | GenericArgCounts = genericArgCounts.ToList();
|
---|
[1776] | 120 | }
|
---|
| 121 |
|
---|
[8698] | 122 | internal TypeName(TypeName typeName, string className = null, string nameSpace = null) {
|
---|
[9005] | 123 | Namespace = nameSpace ?? typeName.Namespace;
|
---|
| 124 | ClassName = className ?? typeName.ClassName;
|
---|
[8698] | 125 | GenericArgs = new List<TypeName>(typeName.GenericArgs);
|
---|
| 126 | AssemblyAttribues = new Dictionary<string, string>(typeName.AssemblyAttribues);
|
---|
| 127 | MemoryMagic = typeName.MemoryMagic;
|
---|
| 128 | AssemblyName = typeName.AssemblyName;
|
---|
| 129 | IsReference = typeName.IsReference;
|
---|
[9005] | 130 | if (typeName.GenericArgCounts != null)
|
---|
| 131 | GenericArgCounts = typeName.GenericArgCounts.ToList();
|
---|
[8698] | 132 | }
|
---|
[3016] | 133 |
|
---|
| 134 | /// <summary>
|
---|
| 135 | /// Returns a <see cref="System.String"/> that represents this instance.
|
---|
| 136 | /// </summary>
|
---|
[3036] | 137 | /// <param name="full">if set to <c>true</c> includes full information
|
---|
[3016] | 138 | /// about generic parameters and assembly properties.</param>
|
---|
| 139 | /// <returns>
|
---|
| 140 | /// A <see cref="System.String"/> that represents this instance.
|
---|
| 141 | /// </returns>
|
---|
[1779] | 142 | public string ToString(bool full) {
|
---|
[1780] | 143 | return ToString(full, true);
|
---|
| 144 | }
|
---|
| 145 |
|
---|
[3004] | 146 |
|
---|
[3016] | 147 | /// <summary>
|
---|
| 148 | /// Returns a <see cref="System.String"/> that represents this instance.
|
---|
| 149 | /// </summary>
|
---|
[3036] | 150 | /// <param name="full">if set to <c>true</c> includes full information
|
---|
| 151 | /// about generic parameters and assembly properties.</param>
|
---|
[3016] | 152 | /// <param name="includeAssembly">if set to <c>true</c> include assembly properties and generic parameters.</param>
|
---|
| 153 | /// <returns>
|
---|
| 154 | /// A <see cref="System.String"/> that represents this instance.
|
---|
| 155 | /// </returns>
|
---|
[4068] | 156 | public string ToString(bool full, bool includeAssembly) {
|
---|
[9005] | 157 | var sb = new StringBuilder();
|
---|
[2859] | 158 | if (!string.IsNullOrEmpty(Namespace))
|
---|
| 159 | sb.Append(Namespace).Append('.');
|
---|
[9005] | 160 | if (GenericArgCounts != null) {
|
---|
| 161 | sb.Append(string.Join("+",
|
---|
| 162 | ClassName
|
---|
| 163 | .Split('+')
|
---|
| 164 | .Zip(GenericArgCounts, (n, c) =>
|
---|
| 165 | c > 0 ? String.Format("{0}`{1}", n, c) : n)));
|
---|
| 166 | } else {
|
---|
| 167 | sb.Append(ClassName);
|
---|
| 168 | if (IsGeneric)
|
---|
| 169 | sb.Append('`').Append(GenericArgs.Count);
|
---|
| 170 | }
|
---|
[1779] | 171 | if (IsGeneric) {
|
---|
[9005] | 172 | sb.Append('[');
|
---|
| 173 | sb.Append(String.Join(",", GenericArgs.Select(a =>
|
---|
| 174 | string.Format("[{0}]", a.ToString(full)))));
|
---|
[1779] | 175 | sb.Append(']');
|
---|
[1703] | 176 | }
|
---|
[1779] | 177 | sb.Append(MemoryMagic);
|
---|
[1780] | 178 | if (includeAssembly && AssemblyName != null) {
|
---|
[1779] | 179 | sb.Append(", ").Append(AssemblyName);
|
---|
[1780] | 180 | if (full)
|
---|
| 181 | foreach (var property in AssemblyAttribues)
|
---|
| 182 | sb.Append(", ").Append(property.Key).Append('=').Append(property.Value);
|
---|
| 183 | }
|
---|
[1779] | 184 | return sb.ToString();
|
---|
[1703] | 185 | }
|
---|
| 186 |
|
---|
[9005] | 187 | private IEnumerable<string> GetNestedClassesInCode(HashSet<string> omitNamespaces, bool includeAllNamespaces) {
|
---|
| 188 | var i = 0;
|
---|
| 189 | foreach (var pair in ClassName.Split('+').Zip(GenericArgCounts, (n, c) => new {n, c})) {
|
---|
| 190 | if (pair.c == 0) {
|
---|
| 191 | yield return pair.n;
|
---|
| 192 | }
|
---|
| 193 | else {
|
---|
| 194 | yield return string.Format("{0}<{1}>",
|
---|
| 195 | pair.n,
|
---|
| 196 | string.Join(",",
|
---|
| 197 | GenericArgs
|
---|
| 198 | .GetRange(i, pair.c)
|
---|
| 199 | .Select(a => a.GetTypeNameInCode(omitNamespaces, includeAllNamespaces))));
|
---|
| 200 | i += pair.c;
|
---|
| 201 | }
|
---|
[3903] | 202 | }
|
---|
| 203 | }
|
---|
[3016] | 204 |
|
---|
[9005] | 205 | private string GetTypeNameInCode(HashSet<string> omitNamespaces, bool includeNamespaces) {
|
---|
| 206 | var sb = new StringBuilder();
|
---|
| 207 | if (!string.IsNullOrEmpty(Namespace) &&
|
---|
| 208 | (omitNamespaces == null && includeNamespaces) ||
|
---|
| 209 | omitNamespaces != null && !omitNamespaces.Contains(Namespace))
|
---|
| 210 | sb.Append(Namespace).Append('.');
|
---|
| 211 | if (GenericArgCounts != null) {
|
---|
| 212 | sb.Append(string.Join(".", GetNestedClassesInCode(omitNamespaces, includeNamespaces)));
|
---|
| 213 | } else {
|
---|
| 214 | sb.Append(ClassName);
|
---|
| 215 | if (IsGeneric) {
|
---|
| 216 | sb.Append("<");
|
---|
| 217 | sb.Append(
|
---|
| 218 | string.Join(", ",
|
---|
| 219 | GenericArgs
|
---|
| 220 | .Select(a => a.GetTypeNameInCode(omitNamespaces, includeNamespaces))
|
---|
| 221 | .ToArray()));
|
---|
| 222 | sb.Append(">");
|
---|
| 223 | }
|
---|
[3935] | 224 | }
|
---|
| 225 | sb.Append(MemoryMagic);
|
---|
| 226 | return sb.ToString();
|
---|
| 227 | }
|
---|
[3903] | 228 |
|
---|
[9005] | 229 | public string GetTypeNameInCode(HashSet<string> omitNamespaces) {
|
---|
| 230 | return GetTypeNameInCode(omitNamespaces, false);
|
---|
| 231 | }
|
---|
[3935] | 232 |
|
---|
[9005] | 233 | public string GetTypeNameInCode(bool includeNamespaces) {
|
---|
| 234 | return GetTypeNameInCode(null, includeNamespaces);
|
---|
| 235 | }
|
---|
| 236 |
|
---|
| 237 |
|
---|
[3016] | 238 | /// <summary>
|
---|
| 239 | /// Returns a <see cref="System.String"/> that represents this instance.
|
---|
| 240 | /// </summary>
|
---|
| 241 | /// <returns>
|
---|
| 242 | /// A <see cref="System.String"/> that represents this instance.
|
---|
| 243 | /// </returns>
|
---|
[1779] | 244 | public override string ToString() {
|
---|
| 245 | return ToString(true);
|
---|
| 246 | }
|
---|
| 247 |
|
---|
[3004] | 248 |
|
---|
| 249 | /// <summary>
|
---|
| 250 | /// Lexicographically compare version information and make sure type and assembly
|
---|
| 251 | /// names are identical. This function recursively checks generic type arguments.
|
---|
[3016] | 252 | /// </summary>
|
---|
| 253 | /// <param name="typeName">Name of the type.</param>
|
---|
| 254 | /// <returns>
|
---|
| 255 | /// <c>true</c> if is newer than the specified type name; otherwise, <c>false</c>.
|
---|
| 256 | /// </returns>
|
---|
[3004] | 257 | public bool IsNewerThan(TypeName typeName) {
|
---|
[1780] | 258 | try {
|
---|
[3004] | 259 | if (this.ClassName != typeName.ClassName ||
|
---|
| 260 | this.Namespace != typeName.Namespace ||
|
---|
| 261 | this.AssemblyName != typeName.AssemblyName)
|
---|
[1780] | 262 | throw new Exception("Cannot compare versions of different types");
|
---|
| 263 | if (CompareVersions(
|
---|
| 264 | this.AssemblyAttribues["Version"],
|
---|
[3004] | 265 | typeName.AssemblyAttribues["Version"]) > 0)
|
---|
[1779] | 266 | return true;
|
---|
[1780] | 267 | IEnumerator<TypeName> thisIt = this.GenericArgs.GetEnumerator();
|
---|
[3004] | 268 | IEnumerator<TypeName> tIt = typeName.GenericArgs.GetEnumerator();
|
---|
[1780] | 269 | while (thisIt.MoveNext()) {
|
---|
| 270 | tIt.MoveNext();
|
---|
[1795] | 271 | if (thisIt.Current.IsNewerThan(tIt.Current))
|
---|
[1780] | 272 | return true;
|
---|
| 273 | }
|
---|
| 274 | return false;
|
---|
[8641] | 275 | }
|
---|
| 276 | catch (KeyNotFoundException) {
|
---|
[1780] | 277 | throw new Exception("Could not extract version information from type string");
|
---|
[1703] | 278 | }
|
---|
| 279 | }
|
---|
| 280 |
|
---|
[3004] | 281 |
|
---|
| 282 | /// <summary>
|
---|
| 283 | /// Make sure major and minor version number are identical. This function
|
---|
| 284 | /// recursively checks generic type arguments.
|
---|
| 285 | /// </summary>
|
---|
[3016] | 286 | /// <param name="typeName">Name of the type.</param>
|
---|
| 287 | /// <returns>
|
---|
| 288 | /// <c>true</c> if the specified type names are compatible; otherwise, <c>false</c>.
|
---|
| 289 | /// </returns>
|
---|
[3004] | 290 | public bool IsCompatible(TypeName typeName) {
|
---|
[1795] | 291 | try {
|
---|
[3004] | 292 | if (this.ClassName != typeName.ClassName ||
|
---|
| 293 | this.Namespace != typeName.Namespace ||
|
---|
| 294 | this.AssemblyName != typeName.AssemblyName)
|
---|
[1795] | 295 | throw new Exception("Cannot compare versions of different types");
|
---|
| 296 | Version thisVersion = new Version(this.AssemblyAttribues["Version"]);
|
---|
[3004] | 297 | Version tVersion = new Version(typeName.AssemblyAttribues["Version"]);
|
---|
[5172] | 298 | if (this.AssemblyName == "mscorlib" &&
|
---|
| 299 | (thisVersion.Major == 2 || thisVersion.Major == 4) &&
|
---|
| 300 | (tVersion.Major == 2 || tVersion.Major == 4)) {
|
---|
| 301 | // skip version check
|
---|
| 302 | } else if (thisVersion.Major != tVersion.Major ||
|
---|
| 303 | thisVersion.Minor != tVersion.Minor)
|
---|
[1795] | 304 | return false;
|
---|
| 305 | IEnumerator<TypeName> thisIt = this.GenericArgs.GetEnumerator();
|
---|
[3004] | 306 | IEnumerator<TypeName> tIt = typeName.GenericArgs.GetEnumerator();
|
---|
[1795] | 307 | while (thisIt.MoveNext()) {
|
---|
| 308 | tIt.MoveNext();
|
---|
| 309 | if (!thisIt.Current.IsCompatible(tIt.Current))
|
---|
| 310 | return false;
|
---|
| 311 | }
|
---|
| 312 | return true;
|
---|
[8641] | 313 | }
|
---|
| 314 | catch (KeyNotFoundException) {
|
---|
[1795] | 315 | throw new Exception("Could not extract version infomration from type string");
|
---|
| 316 | }
|
---|
| 317 | }
|
---|
| 318 |
|
---|
[1779] | 319 | private static int CompareVersions(string v1string, string v2string) {
|
---|
| 320 | return new Version(v1string).CompareTo(new Version(v2string));
|
---|
[1776] | 321 | }
|
---|
[1703] | 322 | }
|
---|
| 323 | } |
---|