source: branches/eraser6/CodeReview/Eraser.Manager/ManagerLibrary.cs @ 1772

Revision 1772, 3.3 KB checked in by lowjoel, 4 years ago (diff)

Allow all IDisposable patterns to be reentrant. Addresses #275: Code Review

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
Line 
1/*
2 * $Id$
3 * Copyright 2008-2010 The Eraser Project
4 * Original Author: Joel Low <lowjoel@users.sourceforge.net>
5 * Modified By:
6 *
7 * This file is part of Eraser.
8 *
9 * Eraser is free software: you can redistribute it and/or modify it under the
10 * terms of the GNU General Public License as published by the Free Software
11 * Foundation, either version 3 of the License, or (at your option) any later
12 * version.
13 *
14 * Eraser is distributed in the hope that it will be useful, but WITHOUT ANY
15 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
16 * A PARTICULAR PURPOSE. See the GNU General Public License for more details.
17 *
18 * A copy of the GNU General Public License can be found at
19 * <http://www.gnu.org/licenses/>.
20 */
21
22using System;
23using System.Collections.Generic;
24using System.Text;
25using System.Runtime.Serialization;
26
27namespace Eraser.Manager
28{
29    /// <summary>
30    /// The library instance which initializes and cleans up data required for the
31    /// library to function.
32    /// </summary>
33    public class ManagerLibrary : IDisposable
34    {
35        public ManagerLibrary(SettingsManager settings)
36        {
37            if (Instance != null)
38                throw new InvalidOperationException("Only one ManagerLibrary instance can " +
39                    "exist at any one time");
40
41            Instance = this;
42            SettingsManager = settings;
43
44            EntropySourceManager = new EntropySourceManager();
45            PRNGManager = new PrngManager();
46            ErasureMethodManager = new ErasureMethodManager();
47            FileSystemManager = new FileSystemManager();
48            Host = new Plugin.DefaultHost();
49            Host.Load();
50        }
51
52        ~ManagerLibrary()
53        {
54            Dispose(false);
55        }
56
57        protected virtual void Dispose(bool disposing)
58        {
59            if (SettingsManager == null)
60                return;
61
62            if (disposing)
63            {
64                EntropySourceManager.Poller.Abort();
65                Host.Dispose();
66                SettingsManager.Save();
67            }
68
69            SettingsManager = null;
70            Instance = null;
71        }
72
73        public void Dispose()
74        {
75            Dispose(true);
76            GC.SuppressFinalize(this);
77        }
78
79        /// <summary>
80        /// The global library instance.
81        /// </summary>
82        public static ManagerLibrary Instance { get; private set; }
83
84        /// <summary>
85        /// The global instance of the EntropySource Manager
86        /// </summary>
87        internal EntropySourceManager EntropySourceManager;
88
89        /// <summary>
90        /// The global instance of the PRNG Manager.
91        /// </summary>
92        internal PrngManager PRNGManager;
93
94        /// <summary>
95        /// The global instance of the Erasure method manager.
96        /// </summary>
97        internal ErasureMethodManager ErasureMethodManager;
98
99        /// <summary>
100        /// The global instance of the File System manager.
101        /// </summary>
102        internal FileSystemManager FileSystemManager;
103
104        /// <summary>
105        /// Global instance of the Settings manager.
106        /// </summary>
107        public SettingsManager SettingsManager { get; set; }
108
109        /// <summary>
110        /// Gets the settings object representing the settings for the Eraser
111        /// Manager. This is just shorthand for the local classes.
112        /// </summary>
113        public static ManagerSettings Settings
114        {
115            get
116            {
117                if (settingsInstance == null)
118                    settingsInstance = new ManagerSettings();
119                return settingsInstance;
120            }
121        }
122
123        /// <summary>
124        /// The singleton instance for <see cref="Settings"/>.
125        /// </summary>
126        private static ManagerSettings settingsInstance;
127
128        /// <summary>
129        /// The global instance of the Plugin host.
130        /// </summary>
131        internal Plugin.DefaultHost Host;
132    }
133}
Note: See TracBrowser for help on using the repository browser.