Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/HeuristicLab.Common/3.3/Content/ContentManager.cs

Last change on this file was 17180, checked in by swagner, 5 years ago

#2875: Removed years in copyrights

File size: 5.1 KB
RevLine 
[3405]1#region License Information
2/* HeuristicLab
[17180]3 * Copyright (C) Heuristic and Evolutionary Algorithms Laboratory (HEAL)
[3405]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;
[16992]23using System.Collections.Generic;
24using System.Linq;
[16440]25using System.Threading;
[16992]26using System.Threading.Tasks;
27using HEAL.Attic;
[3405]28
29namespace HeuristicLab.Common {
[3424]30  public abstract class ContentManager {
[16992]31    public class Info {
32      public Info() { }
33
34      public Info(string filename, TimeSpan duration) {
35        Filename = filename;
36        Duration = duration;
37      }
38
39      public Info(string filename, SerializationInfo serInfo) {
40        Filename = filename;
41        Duration = serInfo.Duration;
42        UnknownTypeGuids = serInfo.UnknownTypeGuids;
43        NumberOfSerializedObjects = serInfo.NumberOfSerializedObjects;
44        SerializedTypes = serInfo.SerializedTypes;       
45      }
46
47      public TimeSpan Duration { get; internal set; }
48      public IEnumerable<Guid> UnknownTypeGuids { get; internal set; } = Enumerable.Empty<Guid>();
49      public int NumberOfSerializedObjects { get; internal set; }
50      public IEnumerable<Type> SerializedTypes { get; internal set; } = Enumerable.Empty<Type>();
51      public string Filename { get; internal set; } = string.Empty;
52    }
53
[3424]54    private static ContentManager instance;
55
[3483]56    public static void Initialize(ContentManager manager) {
57      if (manager == null) throw new ArgumentNullException();
58      if (ContentManager.instance != null) throw new InvalidOperationException("ContentManager has already been initialized.");
59      ContentManager.instance = manager;
[3424]60    }
61
[3483]62    protected ContentManager() { }
[3424]63
[3483]64    public static IStorableContent Load(string filename) {
[16992]65      return Load(filename, out var _);
66    }
67
68    public static IStorableContent Load(string filename, out Info serializationInfo) {
[3483]69      if (instance == null) throw new InvalidOperationException("ContentManager is not initialized.");
[16992]70      IStorableContent content = instance.LoadContent(filename, out serializationInfo);
71     
72      if (content != null) content.Filename = filename;
[3483]73      return content;
[3424]74    }
[16992]75
76
77    public static Task LoadAsync(string filename, Action<IStorableContent, Exception> loadingCompletedCallback) {
78      return LoadAsync(filename, (content, error, info) => loadingCompletedCallback(content, error)); // drop info
79    }
80
81    public static async Task LoadAsync(string filename, Action<IStorableContent, Exception, Info> loadingCompletedCallback) {
[3483]82      if (instance == null) throw new InvalidOperationException("ContentManager is not initialized.");
[16992]83
84      Exception error = null;
85      IStorableContent result = null;
86      Info serializationInfo = null;
87      try {
88        result = await Task.Run(() => {
89          var content = instance.LoadContent(filename, out serializationInfo);
90          if(content!=null) content.Filename = filename;
91          return content;
92        });
93      } catch(Exception ex) {
94        error = ex;
95      }
96      loadingCompletedCallback(result, error, serializationInfo);
[3424]97    }
98
[16992]99    protected abstract IStorableContent LoadContent(string filename, out Info serializationInfo);
100
[16440]101    public static void Save(IStorableContent content, string filename, bool compressed, CancellationToken cancellationToken = default(CancellationToken)) {
[3483]102      if (instance == null) throw new InvalidOperationException("ContentManager is not initialized.");
[16440]103      instance.SaveContent(content, filename, compressed, cancellationToken);
[3483]104      content.Filename = filename;
[3424]105    }
[16440]106    public static void SaveAsync(IStorableContent content, string filename, bool compressed, Action<IStorableContent, Exception> savingCompletedCallback, CancellationToken cancellationToken = default(CancellationToken)) {
[3483]107      if (instance == null) throw new InvalidOperationException("ContentManager is not initialized.");
[16440]108      var action = new Action<IStorableContent, string, bool, CancellationToken>(instance.SaveContent);
109      action.BeginInvoke(content, filename, compressed, cancellationToken, delegate (IAsyncResult result) {
[4245]110        Exception error = null;
111        try {
112          action.EndInvoke(result);
113          content.Filename = filename;
[16440]114        } catch (Exception ex) {
[4245]115          error = ex;
116        }
117        savingCompletedCallback(content, error);
118      }, null);
[3500]119
[3424]120    }
[16440]121    protected abstract void SaveContent(IStorableContent content, string filename, bool compressed, CancellationToken cancellationToken);
[3405]122  }
123}
Note: See TracBrowser for help on using the repository browser.