Free cookie consent management tool by TermsFeed Policy Generator

source: branches/PersistenceReintegration/HeuristicLab.Common/3.3/Content/ContentManager.cs @ 14930

Last change on this file since 14930 was 14930, checked in by gkronber, 7 years ago

#2520 changed optimizer to use new persistence per default

File size: 3.3 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2016 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;
23
24namespace HeuristicLab.Common {
25  public abstract class ContentManager {
26    private static ContentManager instance;
27
28    public static void Initialize(ContentManager manager) {
29      if (manager == null) throw new ArgumentNullException();
30      if (ContentManager.instance != null) throw new InvalidOperationException("ContentManager has already been initialized.");
31      ContentManager.instance = manager;
32    }
33
34    protected ContentManager() { }
35
36    public static IStorableContent Load(string filename) {
37      if (instance == null) throw new InvalidOperationException("ContentManager is not initialized.");
38      IStorableContent content = instance.LoadContent(filename);
39      content.Filename = filename;
40      return content;
41    }
42    public static void LoadAsync(string filename, Action<IStorableContent, Exception> loadingCompletedCallback) {
43      if (instance == null) throw new InvalidOperationException("ContentManager is not initialized.");
44      var func = new Func<string, IStorableContent>(instance.LoadContent);
45      func.BeginInvoke(filename, delegate (IAsyncResult result) {
46        Exception error = null;
47        IStorableContent content = null;
48        try {
49          content = func.EndInvoke(result);
50          content.Filename = filename;
51        } catch (Exception ex) {
52          error = ex;
53        }
54        loadingCompletedCallback(content, error);
55      }, null);
56    }
57    protected abstract IStorableContent LoadContent(string filename);
58
59    public static void Save(IStorableContent content, string filename) {
60      if (instance == null) throw new InvalidOperationException("ContentManager is not initialized.");
61      instance.SaveContent(content, filename);
62      content.Filename = filename;
63    }
64    public static void SaveAsync(IStorableContent content, string filename, Action<IStorableContent, Exception> savingCompletedCallback) {
65      if (instance == null) throw new InvalidOperationException("ContentManager is not initialized.");
66      var action = new Action<IStorableContent, string>(instance.SaveContent);
67      action.BeginInvoke(content, filename, delegate (IAsyncResult result) {
68        Exception error = null;
69        try {
70          action.EndInvoke(result);
71          content.Filename = filename;
72        } catch (Exception ex) {
73          error = ex;
74        }
75        savingCompletedCallback(content, error);
76      }, null);
77
78    }
79    protected abstract void SaveContent(IStorableContent content, string filename);
80  }
81}
Note: See TracBrowser for help on using the repository browser.