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