Free cookie consent management tool by TermsFeed Policy Generator

source: branches/3044_variableScaling/HeuristicLab.Common/3.3/Content/ContentManager.cs @ 17800

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

#2875: Removed years in copyrights

File size: 5.1 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 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.Linq;
25using System.Threading;
26using System.Threading.Tasks;
27using HEAL.Attic;
28
29namespace HeuristicLab.Common {
30  public abstract class ContentManager {
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
54    private static ContentManager instance;
55
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;
60    }
61
62    protected ContentManager() { }
63
64    public static IStorableContent Load(string filename) {
65      return Load(filename, out var _);
66    }
67
68    public static IStorableContent Load(string filename, out Info serializationInfo) {
69      if (instance == null) throw new InvalidOperationException("ContentManager is not initialized.");
70      IStorableContent content = instance.LoadContent(filename, out serializationInfo);
71     
72      if (content != null) content.Filename = filename;
73      return content;
74    }
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) {
82      if (instance == null) throw new InvalidOperationException("ContentManager is not initialized.");
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);
97    }
98
99    protected abstract IStorableContent LoadContent(string filename, out Info serializationInfo);
100
101    public static void Save(IStorableContent content, string filename, bool compressed, CancellationToken cancellationToken = default(CancellationToken)) {
102      if (instance == null) throw new InvalidOperationException("ContentManager is not initialized.");
103      instance.SaveContent(content, filename, compressed, cancellationToken);
104      content.Filename = filename;
105    }
106    public static void SaveAsync(IStorableContent content, string filename, bool compressed, Action<IStorableContent, Exception> savingCompletedCallback, CancellationToken cancellationToken = default(CancellationToken)) {
107      if (instance == null) throw new InvalidOperationException("ContentManager is not initialized.");
108      var action = new Action<IStorableContent, string, bool, CancellationToken>(instance.SaveContent);
109      action.BeginInvoke(content, filename, compressed, cancellationToken, delegate (IAsyncResult result) {
110        Exception error = null;
111        try {
112          action.EndInvoke(result);
113          content.Filename = filename;
114        } catch (Exception ex) {
115          error = ex;
116        }
117        savingCompletedCallback(content, error);
118      }, null);
119
120    }
121    protected abstract void SaveContent(IStorableContent content, string filename, bool compressed, CancellationToken cancellationToken);
122  }
123}
Note: See TracBrowser for help on using the repository browser.