Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Core/3.3/PersistenceManager.cs @ 2546

Last change on this file since 2546 was 2546, checked in by swagner, 14 years ago

Continued work on Optimizer and on adapting all views to the new MainForm concept (#770)

File size: 3.4 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2008 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.Collections.Generic;
24using System.Text;
25using System.Xml;
26using System.IO;
27using System.IO.Compression;
28using HeuristicLab.PluginInfrastructure;
29using HeuristicLab.Persistence.Default.Xml;
30using HeuristicLab.Persistence.Core;
31
32namespace HeuristicLab.Core {
33  /// <summary>
34  /// Static class for serializing and deserializing objects.
35  /// </summary>
36  public static class PersistenceManager {
37    /// <summary>
38    /// Saves the specified <paramref name="instance"/> in th specified file through creating an
39    /// <see cref="XmlDocument"/>.
40    /// </summary>
41    /// <param name="instance">The object that should be saved.</param>
42    /// <param name="filename">The name of the file where the <paramref name="object"/> should be saved.</param>
43    public static void Save(IItem instance, string filename) {
44      XmlGenerator.Serialize(instance, filename, 0);
45    }
46
47    public static void SaveCompressed(IItem instance, string filename) {
48      XmlGenerator.Serialize(instance, filename, 9);
49    }
50
51    /// <summary>
52    /// Saves the specified <paramref name="instance"/> in the specified <paramref name="stream"/>
53    /// through creating an <see cref="XmlDocument"/>.
54    /// </summary>
55    /// <param name="instance">The object that should be saved.</param>
56    /// <param name="stream">The (file) stream where the object should be saved.</param>
57    public static void Save(IItem instance, Stream stream) {
58      XmlGenerator.Serialize(instance, stream, ConfigurationService.Instance.GetConfiguration(new XmlFormat()));     
59    }
60    /// <summary>
61    /// Loads an object from a file with the specified <paramref name="filename"/>.
62    /// </summary>
63    /// <remarks>The object must be saved as an <see cref="XmlDocument"/>. <br/>
64    /// Calls <see cref="Restore"/>.</remarks>
65    /// <param name="filename">The filename of the file where the data is saved.</param>
66    /// <returns>The loaded object.</returns>
67    public static IItem Load(string filename) {
68      return (IItem)XmlParser.Deserialize(filename);
69    }
70    /// <summary>
71    /// Loads an object from the specified <paramref name="stream"/>.
72    /// </summary>
73    /// <remarks>The object must be saved as an <see cref="XmlDocument"/>. <br/>
74    /// Calls <see cref="Restore"/>.</remarks>
75    /// <param name="stream">The stream from where to load the data.</param>
76    /// <returns>The loaded object.</returns>
77    public static IItem Load(Stream stream) {
78      return (IItem)XmlParser.Deserialize(stream);
79    }
80  }
81}
Note: See TracBrowser for help on using the repository browser.