Changeset 3004
- Timestamp:
- 03/11/10 12:54:14 (15 years ago)
- Location:
- trunk/sources/HeuristicLab.Persistence/3.3
- Files:
-
- 24 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/sources/HeuristicLab.Persistence/3.3/Auxiliary/TypeLoader.cs
r1823 r3004 10 10 namespace HeuristicLab.Persistence.Auxiliary { 11 11 12 publicclass TypeLoader {12 internal class TypeLoader { 13 13 14 14 public static Type Load(string typeNameString) { -
trunk/sources/HeuristicLab.Persistence/3.3/Auxiliary/TypeName.cs
r2859 r3004 4 4 using System.Reflection.Emit; 5 5 using System.Collections.Generic; 6 using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; 6 7 7 8 namespace HeuristicLab.Persistence.Auxiliary { 8 9 10 /// <summary> 11 /// Contains a more modular representation of type names that can 12 /// be used to compare versions and ignore extended assembly 13 /// attributes. 14 /// </summary> 15 [StorableClass(StorableClassType.MarkedOnly)] 9 16 public class TypeName { 17 18 [Storable] 10 19 public string Namespace { get; private set; } 20 21 [Storable] 11 22 public string ClassName { get; private set; } 23 24 [Storable] 12 25 public List<TypeName> GenericArgs { get; internal set; } 13 26 public bool IsGeneric { get { return GenericArgs.Count > 0; } } 27 28 [Storable] 14 29 public string MemoryMagic { get; internal set; } 30 31 [Storable] 15 32 public string AssemblyName { get; internal set; } 33 34 [Storable] 16 35 public Dictionary<string, string> AssemblyAttribues { get; internal set; } 36 37 [Storable] 17 38 public bool IsReference { get; internal set; } 18 39 … … 25 46 } 26 47 48 /// <param name="full">include assembly properties and generic parameters</param> 27 49 public string ToString(bool full) { 28 50 return ToString(full, true); 29 51 } 30 52 31 public string ToString(bool full, bool includeAssembly) { 53 54 /// <param name="full">include assembly properties and generic parameters</param> 55 public string ToString(bool full, bool includeAssembly) { 32 56 StringBuilder sb = new StringBuilder(); 33 57 if (!string.IsNullOrEmpty(Namespace)) … … 60 84 } 61 85 62 public bool IsNewerThan(TypeName t) { 86 87 /// <summary> 88 /// Lexicographically compare version information and make sure type and assembly 89 /// names are identical. This function recursively checks generic type arguments. 90 /// </summary> 91 public bool IsNewerThan(TypeName typeName) { 63 92 try { 64 if (this.ClassName != t .ClassName ||65 this.Namespace != t .Namespace ||66 this.AssemblyName != t .AssemblyName)93 if (this.ClassName != typeName.ClassName || 94 this.Namespace != typeName.Namespace || 95 this.AssemblyName != typeName.AssemblyName) 67 96 throw new Exception("Cannot compare versions of different types"); 68 97 if (CompareVersions( 69 98 this.AssemblyAttribues["Version"], 70 t .AssemblyAttribues["Version"]) > 0)99 typeName.AssemblyAttribues["Version"]) > 0) 71 100 return true; 72 101 IEnumerator<TypeName> thisIt = this.GenericArgs.GetEnumerator(); 73 IEnumerator<TypeName> tIt = t .GenericArgs.GetEnumerator();102 IEnumerator<TypeName> tIt = typeName.GenericArgs.GetEnumerator(); 74 103 while (thisIt.MoveNext()) { 75 104 tIt.MoveNext(); … … 83 112 } 84 113 85 public bool IsCompatible(TypeName t) { 114 115 /// <summary> 116 /// Make sure major and minor version number are identical. This function 117 /// recursively checks generic type arguments. 118 /// </summary> 119 public bool IsCompatible(TypeName typeName) { 86 120 try { 87 if (this.ClassName != t .ClassName ||88 this.Namespace != t .Namespace ||89 this.AssemblyName != t .AssemblyName)121 if (this.ClassName != typeName.ClassName || 122 this.Namespace != typeName.Namespace || 123 this.AssemblyName != typeName.AssemblyName) 90 124 throw new Exception("Cannot compare versions of different types"); 91 125 Version thisVersion = new Version(this.AssemblyAttribues["Version"]); 92 Version tVersion = new Version(t .AssemblyAttribues["Version"]);126 Version tVersion = new Version(typeName.AssemblyAttribues["Version"]); 93 127 if (thisVersion.Major != tVersion.Major || 94 128 thisVersion.Minor != tVersion.Minor) 95 129 return false; 96 130 IEnumerator<TypeName> thisIt = this.GenericArgs.GetEnumerator(); 97 IEnumerator<TypeName> tIt = t .GenericArgs.GetEnumerator();131 IEnumerator<TypeName> tIt = typeName.GenericArgs.GetEnumerator(); 98 132 while (thisIt.MoveNext()) { 99 133 tIt.MoveNext(); -
trunk/sources/HeuristicLab.Persistence/3.3/Auxiliary/TypeNameParser.cs
r2859 r3004 11 11 } 12 12 13 14 /// <summary> 15 /// Parse a .NET type name using the following grammar: 16 /// 17 /// <para><code> 18 /// TypeSpec := SimpleTypeSpec '&'? 19 /// </code></para> 20 /// 21 /// <para><code> 22 /// SimpleTypeSpec := (IDENTIFIER '.')* 23 /// (IDENTIFIER '+')* 24 /// IDENTIFIER 25 /// ( '`\d+[' Generics ']' )? 26 /// (\*|\[(\d+\.\.\d+|\d+\.\.\.|(|\*)(,(|\*))*)\])* 27 /// (',\s*' IDENTIFIER (',\s*' AssemblyProperty)* )? 28 /// </code></para> 29 /// 30 /// <para><code> 31 /// Generics := '[' SimpleTypeSpec ']' (',[' SimpleTypeSpec ']') 32 /// </code></para> 33 /// 34 /// <para><code> 35 /// AssemblyProperty := 'Version=' Version 36 /// | 'PublicKey(Token)?=[a-fA-F0-9]+' 37 /// | 'Culture=[a-zA-F0-9]+' 38 /// </code></para> 39 /// 40 /// <para><code> 41 /// Version := \d+\.\d+\.\d+\.\d+ 42 /// </code></para> 43 /// 44 /// <para><code> 45 /// IDENTIFIER = [_a-zA-Z][_a-ZA-Z0-9]* 46 /// </code></para> 47 /// </summary> 13 48 public class TypeNameParser { 14 49 … … 35 70 36 71 37 class Token {72 private class Token { 38 73 private static Dictionary<string, string> tokens = 39 74 new Dictionary<string, string> { … … 68 103 } 69 104 } 70 public static IEnumerable<Token> Tokenize(string s) { 105 public static IEnumerable<Token> Tokenize(string s) { 71 106 int pos = 0; 72 107 foreach (Match m in TokenRegex.Matches(s)) { -
trunk/sources/HeuristicLab.Persistence/3.3/Core/Configuration.cs
r2994 r3004 6 6 namespace HeuristicLab.Persistence.Core { 7 7 8 /// <summary> 9 /// Defines the set of primitive and composite serializers that are to be used 10 /// for a certain seraial format. The configuration can be obtained from the 11 /// <code>ConfigurationService</code>. 12 /// </summary> 8 13 [StorableClass(StorableClassType.MarkedOnly)] 9 14 public class Configuration { -
trunk/sources/HeuristicLab.Persistence/3.3/Core/ConfigurationService.cs
r2876 r3004 13 13 namespace HeuristicLab.Persistence.Core { 14 14 15 /// <summary> 16 /// Provides a persistable configuration of primitive and composite serializers for 17 /// all registered serial formats. Custom formats can be defined and will be saved 18 /// for future sessions. A default configuration can be generated through reflection. 19 /// 20 /// This class has only a single instance. 21 /// </summary> 15 22 public class ConfigurationService { 16 23 17 24 private static ConfigurationService instance; 18 25 private readonly Dictionary<IFormat, Configuration> customConfigurations; 26 27 /// <summary> 28 /// List of all available primitive serializers. 29 /// </summary> 19 30 public Dictionary<Type, List<IPrimitiveSerializer>> PrimitiveSerializers { get; private set; } 31 32 /// <summary> 33 /// List of all available composite serializers (discovered through reflection). 34 /// </summary> 20 35 public List<ICompositeSerializer> CompositeSerializers { get; private set; } 36 37 /// <summary> 38 /// List of all available formats (discovered through reflection). 39 /// </summary> 21 40 public List<IFormat> Formats { get; private set; } 22 41 … … 92 111 } 93 112 113 114 /// <summary> 115 /// Rediscover available serializers and discard all custom configurations. 116 /// </summary> 94 117 public void Reset() { 95 118 customConfigurations.Clear(); … … 108 131 } 109 132 110 class PriortiySorter : IComparer<ICompositeSerializer> {133 private class PriortiySorter : IComparer<ICompositeSerializer> { 111 134 public int Compare(ICompositeSerializer x, ICompositeSerializer y) { 112 135 return y.Priority - x.Priority; … … 169 192 } 170 193 194 /// <summary> 195 /// Get the default (automatically discovered) configuration for a certain format. 196 /// </summary> 171 197 public Configuration GetDefaultConfig(IFormat format) { 172 198 Dictionary<Type, IPrimitiveSerializer> primitiveConfig = new Dictionary<Type, IPrimitiveSerializer>(); … … 188 214 } 189 215 216 217 /// <summary> 218 /// Get a configuration for a certain format. This returns a custom configuration 219 /// if defined, otherwise returns the default (automatically discovered) configuration. 220 /// </summary> 190 221 public Configuration GetConfiguration(IFormat format) { 191 222 if (customConfigurations.ContainsKey(format)) … … 194 225 } 195 226 227 /// <summary> 228 /// Define a new custom configuration for a ceratin format. 229 /// </summary> 196 230 public void DefineConfiguration(Configuration configuration) { 197 231 customConfigurations[configuration.Format] = configuration; -
trunk/sources/HeuristicLab.Persistence/3.3/Core/DeSerializer.cs
r2873 r3004 9 9 namespace HeuristicLab.Persistence.Core { 10 10 11 /// <summary> 12 /// Core hub for deserialization. Reads the serialization token stream, 13 /// instantiates objects and fills in values. 14 /// </summary> 11 15 public class Deserializer { 12 16 13 class Midwife { 17 /// <summary> 18 /// Helps in delivering the class instance and acts as proxy while 19 /// the object cannot yet be instantiate. 20 /// </summary> 21 private class Midwife { 14 22 15 23 public int? Id { get; private set; } … … 53 61 } 54 62 } 55 63 56 64 private readonly Dictionary<int, object> id2obj; 57 65 private readonly Dictionary<Type, object> serializerMapping; … … 59 67 private readonly Dictionary<int, Type> typeIds; 60 68 69 /// <summary> 70 /// Instantiates a new deserializer with the given type cache, 71 /// that contains information about the serializers to use 72 /// for every type and their type ids. 73 /// </summary> 61 74 public Deserializer( 62 75 IEnumerable<TypeMapping> typeCache) { … … 92 105 } 93 106 94 107 /// <summary> 108 /// Process the token stream and deserialize an instantate a new object graph. 109 /// </summary> 95 110 public object Deserialize(IEnumerable<ISerializationToken> tokens) { 96 111 foreach (ISerializationToken token in tokens) { -
trunk/sources/HeuristicLab.Persistence/3.3/Core/FormatBase.cs
r2994 r3004 4 4 namespace HeuristicLab.Persistence.Interfaces { 5 5 6 /// <summary> 7 /// Common base class for defining a new serialization format. 8 /// </summary> 6 9 [StorableClass(StorableClassType.Empty)] 7 10 public abstract class FormatBase<SerialDataFormat> : IFormat<SerialDataFormat> where SerialDataFormat : ISerialData { 8 11 9 12 public abstract string Name { get; } 10 13 14 15 /// <summary> 16 /// Datatype that describes the atoms used for serialization serialization. 17 /// </summary> 11 18 public Type SerialDataType { get { return typeof(SerialDataFormat); } } 12 19 20 /// <summary> 21 /// Compares formats by name. 22 /// </summary> 13 23 public bool Equals(FormatBase<SerialDataFormat> f) { 14 24 if (f == null) … … 17 27 } 18 28 29 /// <summary> 30 /// Compares foramts by name. 31 /// </summary> 19 32 public override bool Equals(object obj) { 20 33 FormatBase<SerialDataFormat> f = obj as FormatBase<SerialDataFormat>; -
trunk/sources/HeuristicLab.Persistence/3.3/Core/GeneratorBase.cs
r2718 r3004 11 11 namespace HeuristicLab.Persistence.Core { 12 12 13 /// <summary> 14 /// Base class for serialization generators. Provides a common entry point 15 /// <code>Format</code> and dispatches to different abstract methods for 16 /// every token type. 17 /// </summary> 13 18 public abstract class GeneratorBase<T> { 14 19 -
trunk/sources/HeuristicLab.Persistence/3.3/Core/PrimitiveSerializerBase.cs
r2994 r3004 5 5 namespace HeuristicLab.Persistence.Interfaces { 6 6 7 /// <summary> 8 /// Base class for primitive serializers. These are serializers that map 9 /// directly to a single datatype and directly produce a serializable object. 10 /// </summary> 7 11 [StorableClass(StorableClassType.Empty)] 8 12 public abstract class PrimitiveSerializerBase<Source, SerialData> : -
trunk/sources/HeuristicLab.Persistence/3.3/Core/Serializer.cs
r2993 r3004 13 13 namespace HeuristicLab.Persistence.Core { 14 14 15 /// <summary> 16 /// The core hub for serialization. This class transforms an object graph 17 /// into a tree and later into a stream of serialization tokens using 18 /// the given configuration. 19 /// 20 /// <para>Primitive serializers directly format an object to a serializable type.</para> 21 /// 22 /// <para>Composite serializers decompose an object into other object that are then 23 /// recursively serialized.</para> 24 /// 25 /// A constructed serializer is enumerable and continuously analyses 26 /// and traverses the object graph while the enumerator is iterated 27 /// </summary> 15 28 public class Serializer : IEnumerable<ISerializationToken> { 16 29 17 class ReferenceEqualityComparer : IEqualityComparer<object> {30 private class ReferenceEqualityComparer : IEqualityComparer<object> { 18 31 19 32 public new bool Equals(object a, object b) { … … 37 50 private readonly List<Exception> exceptions; 38 51 52 /// <summary> 53 /// Contains a mapping of type id to type and serializer. 54 /// </summary> 39 55 public List<TypeMapping> TypeCache { 40 56 get { … … 43 59 } 44 60 } 45 61 62 /// <summary> 63 /// Contains a list of files (mostly assemblies) that are 64 /// necessary to deserialize the object graph again. 65 /// </summary> 46 66 public List<string> RequiredFiles { 47 67 get { … … 76 96 requiredFiles = new List<string>(files.Keys); 77 97 } 78 98 79 99 public Serializer(object obj, Configuration configuration) : 80 100 this(obj, configuration, "ROOT") { } … … 83 103 : this(obj, configuration, rootName, false) { } 84 104 105 /// <param name="isTestRun">Try to complete the whole object graph, 106 /// don't stop at the first exception</param> 85 107 public Serializer(object obj, Configuration configuration, string rootName, bool isTestRun) { 86 108 this.obj = obj; … … 106 128 } 107 129 108 p ublicIEnumerator<ISerializationToken> AddExceptionCompiler(IEnumerator<ISerializationToken> enumerator) {130 private IEnumerator<ISerializationToken> AddExceptionCompiler(IEnumerator<ISerializationToken> enumerator) { 109 131 while (enumerator.MoveNext()) 110 132 yield return enumerator.Current; -
trunk/sources/HeuristicLab.Persistence/3.3/Core/Tag.cs
r1623 r3004 3 3 namespace HeuristicLab.Persistence.Core { 4 4 5 /// <summary> 6 /// Vehicle used inside the serialization/deserizalisation process 7 /// between composite serializers and the core. 8 /// </summary> 5 9 public class Tag { 6 10 public string Name { get; private set; } -
trunk/sources/HeuristicLab.Persistence/3.3/Core/Tokens/BeginToken.cs
r1562 r3004 3 3 namespace HeuristicLab.Persistence.Core.Tokens { 4 4 5 6 /// <summary> 7 /// Marks the beginning of a composite element. 8 /// </summary> 5 9 public class BeginToken : CompositeTokenBase { 6 10 public BeginToken(string name, int typeId, int? id) : base(name, typeId, id) { } -
trunk/sources/HeuristicLab.Persistence/3.3/Core/Tokens/CompositeTokenBase.cs
r1562 r3004 2 2 namespace HeuristicLab.Persistence.Core.Tokens { 3 3 4 /// <summary> 5 /// Common base class of <code>BeginToken</code> and <code>EndToken</code> 6 /// that surround a composite element. 7 /// </summary> 4 8 public abstract class CompositeTokenBase : SerializationTokenBase { 5 9 public readonly int TypeId; -
trunk/sources/HeuristicLab.Persistence/3.3/Core/Tokens/EndToken.cs
r1562 r3004 3 3 namespace HeuristicLab.Persistence.Core.Tokens { 4 4 5 /// <summary> 6 /// Marks the end of a composite element. 7 /// </summary> 5 8 public class EndToken : CompositeTokenBase { 6 9 public EndToken(string name, int typeId, int? id) : base(name, typeId, id) { } -
trunk/sources/HeuristicLab.Persistence/3.3/Core/Tokens/MetaInfoBeginToken.cs
r1556 r3004 2 2 3 3 namespace HeuristicLab.Persistence.Core.Tokens { 4 4 5 5 public class MetaInfoBeginToken : ISerializationToken { } 6 6 -
trunk/sources/HeuristicLab.Persistence/3.3/Core/Tokens/NulLReferenceToken.cs
r1566 r3004 2 2 3 3 namespace HeuristicLab.Persistence.Core.Tokens { 4 4 5 5 public class NullReferenceToken : SerializationTokenBase { 6 6 public NullReferenceToken(string name) : base(name) { } -
trunk/sources/HeuristicLab.Persistence/3.3/Core/Tokens/PrimitiveToken.cs
r1566 r3004 3 3 namespace HeuristicLab.Persistence.Core.Tokens { 4 4 5 /// <summary> 6 /// Encapsulated the serialization of a single primitive value. 7 /// </summary> 5 8 public class PrimitiveToken : SerializationTokenBase { 6 9 public readonly int TypeId; -
trunk/sources/HeuristicLab.Persistence/3.3/Core/Tokens/ReferenceToken.cs
r1566 r3004 3 3 namespace HeuristicLab.Persistence.Core.Tokens { 4 4 5 6 /// <summary> 7 /// References a previously used token (composite or primitive). 8 /// </summary> 5 9 public class ReferenceToken : SerializationTokenBase { 6 10 public readonly int Id; -
trunk/sources/HeuristicLab.Persistence/3.3/Core/Tokens/SerializationTokenBase.cs
r1615 r3004 4 4 5 5 namespace HeuristicLab.Persistence.Core.Tokens { 6 6 7 7 public abstract class SerializationTokenBase : ISerializationToken { 8 8 public readonly string Name; -
trunk/sources/HeuristicLab.Persistence/3.3/Core/TypeMapping.cs
r1623 r3004 3 3 namespace HeuristicLab.Persistence.Core { 4 4 5 /// <summary> 6 /// Association of id, type name and serializer 7 /// </summary> 5 8 public class TypeMapping { 6 9 -
trunk/sources/HeuristicLab.Persistence/3.3/Default/CompositeSerializers/Storable/DataMemberAccessor.cs
r1938 r3004 5 5 namespace HeuristicLab.Persistence.Default.CompositeSerializers.Storable { 6 6 7 /// <summary> 8 /// Encapsulation and abstraction for access a data member of an object 9 /// regardless of it being a property or field. Addicionally a 10 /// default value and an alternate name can be specified. 11 /// </summary> 7 12 public class DataMemberAccessor { 8 13 … … 12 17 public readonly object DefaultValue; 13 18 19 20 /// <summary> 21 /// Create a DataMemberAccessor from a FieldInfo or PropertyInfo for the give object. 22 /// </summary> 14 23 public DataMemberAccessor(MemberInfo memberInfo, string name, object defaultvalue, object obj) { 15 24 Name = name; … … 33 42 } 34 43 44 /// <summary> 45 /// Wrap existing getter and setter functions. 46 /// </summary> 35 47 public DataMemberAccessor(string name, object defaultValue, 36 48 Func<object> getter, Action<object> setter) { … … 40 52 Set = setter; 41 53 } 42 54 55 /// <summary> 56 /// Create an empty accessor that just encapsulates an object 57 /// without access. 58 /// </summary> 43 59 public DataMemberAccessor(object o) { 44 60 Name = null; … … 48 64 } 49 65 66 /// <summary> 67 /// Create an empty accessor that just encapsulates an object 68 /// without access. 69 /// </summary> 50 70 public DataMemberAccessor(object o, string name) { 51 71 Name = name; … … 54 74 Set = null; 55 75 } 56 57 76 58 77 public override string ToString() { -
trunk/sources/HeuristicLab.Persistence/3.3/Default/Xml/Primitive/System.Drawing/Bitmap2XmlSerializer.cs
r2965 r3004 11 11 12 12 namespace HeuristicLab.Persistence.Default.Xml.Primitive { 13 public class Bitmap2XmlSerializer :PrimitiveXmlSerializerBase<Bitmap>{ 14 private static Regex re = new Regex(@"<!\[CDATA\[((?:[^]]|\](?!\]>))*)\]\]>", RegexOptions.Singleline); 13 public class Bitmap2XmlSerializer : PrimitiveXmlSerializerBase<Bitmap> { 15 14 16 15 public override XmlString Format(Bitmap o) { 17 16 MemoryStream stream = new MemoryStream(); 18 o.Save(stream, ImageFormat.Png);17 o.Save(stream, ImageFormat.Png); 19 18 byte[] array = stream.ToArray(); 20 19 Byte1DArray2XmlSerializer serializer = new Byte1DArray2XmlSerializer(); … … 32 31 Bitmap bitmap = new Bitmap(stream); 33 32 return bitmap; 34 }33 } 35 34 } 36 35 } -
trunk/sources/HeuristicLab.Persistence/3.3/Default/Xml/XmlGenerator.cs
r2718 r3004 11 11 namespace HeuristicLab.Persistence.Default.Xml { 12 12 13 14 /// <summary> 15 /// Main entry point of persistence to XML. Use the static methods to serialize 16 /// to a file or to a stream. 17 /// </summary> 13 18 public class XmlGenerator : GeneratorBase<string> { 14 19 … … 155 160 } 156 161 162 /// <summary> 163 /// Serialize an object into a file. 164 /// 165 /// The XML configuration is obtained from the <code>ConfigurationService</code>. 166 /// The file is actually a ZIP file. 167 /// Compression level is set to 5 and needed assemblies are not included. 168 /// </summary> 157 169 public static void Serialize(object o, string filename) { 158 Serialize(o, filename, ConfigurationService.Instance.GetDefaultConfig(new XmlFormat()), false, 5); 159 } 160 170 Serialize(o, filename, ConfigurationService.Instance.GetConfiguration(new XmlFormat()), false, 5); 171 } 172 173 /// <summary> 174 /// Serialize an object into a file. 175 /// 176 /// The XML configuration is obtained from the <code>ConfigurationService</code>. 177 /// Needed assemblies are not included. 178 /// </summary> 179 /// <param name="compression">ZIP file compression level</param> 161 180 public static void Serialize(object o, string filename, int compression) { 162 181 Serialize(o, filename, ConfigurationService.Instance.GetConfiguration(new XmlFormat()), false, compression); 163 182 } 183 164 184 165 185 public static void Serialize(object obj, string filename, Configuration config) { -
trunk/sources/HeuristicLab.Persistence/3.3/Interfaces/ISerialData.cs
r1823 r3004 2 2 3 3 namespace HeuristicLab.Persistence.Interfaces { 4 5 4 6 5 /// <summary>
Note: See TracChangeset
for help on using the changeset viewer.