1 | #region License Information
|
---|
2 | /* HeuristicLab
|
---|
3 | * Copyright (C) 2002-2010 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 |
|
---|
22 | using System;
|
---|
23 | using System.Collections.Generic;
|
---|
24 | using System.Linq;
|
---|
25 | using System.Text;
|
---|
26 | using System.Threading;
|
---|
27 |
|
---|
28 | namespace HeuristicLab.Common {
|
---|
29 | public abstract class ContentManager {
|
---|
30 | protected ContentManager() {
|
---|
31 | }
|
---|
32 |
|
---|
33 | private static ContentManager instance;
|
---|
34 | public static ContentManager Instance {
|
---|
35 | get { return instance; }
|
---|
36 | }
|
---|
37 | public static void CreateInstance<T>() where T : ContentManager {
|
---|
38 | if (instance != null)
|
---|
39 | throw new InvalidOperationException("ContentManager was already created.");
|
---|
40 | instance = Activator.CreateInstance<T>();
|
---|
41 | }
|
---|
42 |
|
---|
43 | public static void Save(IStorableContent content) {
|
---|
44 | content.Save();
|
---|
45 | }
|
---|
46 | public static void Save(IStorableContent content, string filename) {
|
---|
47 | content.Save(filename);
|
---|
48 | }
|
---|
49 |
|
---|
50 | protected abstract void Load(string filename, bool flag);
|
---|
51 | public static void Load(string filename) {
|
---|
52 | if (instance == null)
|
---|
53 | throw new InvalidOperationException("ContentManager must be created before access is allowed.");
|
---|
54 |
|
---|
55 | Exception ex = null;
|
---|
56 | instance.OnLoadOperationStarted();
|
---|
57 | try {
|
---|
58 | instance.Load(filename, false);
|
---|
59 | }
|
---|
60 | catch (Exception e) {
|
---|
61 | ex = e;
|
---|
62 | }
|
---|
63 | instance.OnLoadOperationFinished(ex);
|
---|
64 | }
|
---|
65 |
|
---|
66 | public static void LoadAsynchronous(string filename) {
|
---|
67 | if (instance == null)
|
---|
68 | throw new InvalidOperationException("ContentManager must be created before access is allowed.");
|
---|
69 |
|
---|
70 | ThreadPool.QueueUserWorkItem(
|
---|
71 | new WaitCallback(delegate(object arg) {
|
---|
72 | Load(filename);
|
---|
73 | })
|
---|
74 | );
|
---|
75 | }
|
---|
76 |
|
---|
77 |
|
---|
78 | public event EventHandler LoadOperationStarted;
|
---|
79 | protected virtual void OnLoadOperationStarted() {
|
---|
80 | EventHandler handler = LoadOperationStarted;
|
---|
81 | if (handler != null)
|
---|
82 | handler(this, EventArgs.Empty);
|
---|
83 | }
|
---|
84 | public event EventHandler<EventArgs<Exception>> LoadOperationFinished;
|
---|
85 | protected virtual void OnLoadOperationFinished(Exception e) {
|
---|
86 | EventHandler<EventArgs<Exception>> handler = LoadOperationFinished;
|
---|
87 | if (handler != null)
|
---|
88 | handler(this, new EventArgs<Exception>(e));
|
---|
89 | }
|
---|
90 | }
|
---|
91 | }
|
---|