Changeset 3483 for trunk/sources/HeuristicLab.Common/3.2
- Timestamp:
- 04/22/10 05:14:39 (14 years ago)
- Location:
- trunk/sources/HeuristicLab.Common/3.2
- Files:
-
- 1 deleted
- 5 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/sources/HeuristicLab.Common/3.2/Content/ContentManager.cs
r3437 r3483 21 21 22 22 using System; 23 using System.Collections.Generic;24 using System.Linq;25 using System.Text;26 using System.Threading;27 23 28 24 namespace HeuristicLab.Common { 29 25 public abstract class ContentManager { 30 protected 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; 31 32 } 32 33 33 private static ContentManager instance; 34 public static ContentManager Instance { 35 get { return instance; } 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; 36 41 } 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>(); 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 } 52 catch (Exception ex) { 53 error = ex; 54 } 55 loadingCompletedCallback(content, error); 56 }, null); 41 57 } 58 protected abstract IStorableContent LoadContent(string filename); 42 59 43 public static void Save(IStorableContent content) { 44 content.Save(); 60 public static void Save(IStorableContent content, string filename, bool compressed) { 61 if (instance == null) throw new InvalidOperationException("ContentManager is not initialized."); 62 instance.SaveContent(content, filename, compressed); 63 content.Filename = filename; 45 64 } 46 public static void Save(IStorableContent content, string filename) { 47 content.Save(filename); 65 public static void SaveAsync(IStorableContent content, string filename, bool compressed, Action<IStorableContent, Exception> savingCompletedCallback) { 66 if (instance == null) throw new InvalidOperationException("ContentManager is not initialized."); 67 var action = new Action<IStorableContent, string, bool>(instance.SaveContent); 68 action.BeginInvoke(content, filename, compressed, delegate(IAsyncResult result) { 69 Exception error = null; 70 try { 71 action.EndInvoke(result); 72 content.Filename = filename; 73 } 74 catch (Exception ex) { 75 error = ex; 76 } 77 savingCompletedCallback(content, error); 78 }, null); 48 79 } 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 } 80 protected abstract void SaveContent(IStorableContent content, string filename, bool compressed); 90 81 } 91 82 } -
trunk/sources/HeuristicLab.Common/3.2/Content/IContent.cs
r3437 r3483 20 20 #endregion 21 21 22 using System;23 using System.Collections.Generic;24 using System.Linq;25 using System.Text;26 27 22 namespace HeuristicLab.Common { 28 public interface IContent : IDeepCloneable{ }23 public interface IContent { } 29 24 } -
trunk/sources/HeuristicLab.Common/3.2/Content/IStorableContent.cs
r3437 r3483 21 21 22 22 using System; 23 using System.Collections.Generic;24 using System.Linq;25 using System.Text;26 23 27 24 namespace HeuristicLab.Common { … … 29 26 string Filename { get; set; } 30 27 31 void Save();32 void Save(string filename);33 void SaveAsynchronous();34 void SaveAsynchronous(string filename);35 36 28 event EventHandler FilenameChanged; 37 event EventHandler SaveOperationStarted;38 event EventHandler<EventArgs<Exception>> SaveOperationFinished;39 29 } 40 30 } -
trunk/sources/HeuristicLab.Common/3.2/Content/StorableContent.cs
r3437 r3483 21 21 22 22 using System; 23 using System.Collections.Generic;24 using System.Linq;25 using System.Text;26 using System.Threading;27 using System.Reflection;28 23 29 24 namespace HeuristicLab.Common { 30 public abstract class StorableContent : Content, IStorableContent { 31 public StorableContent() 32 : base() { 33 this.filename = string.Empty; 34 } 35 public StorableContent(string filename) 36 : base() { 37 this.Filename = filename; 38 } 39 25 public class StorableContent : IStorableContent { 40 26 private string filename; 41 27 public string Filename { 42 get { return this.filename; }28 get { return filename; } 43 29 set { 44 if ( this.filename != value) {45 this.filename = value;46 this.OnFilenameChanged();30 if (!filename.Equals(value)) { 31 filename = value; 32 OnFilenameChanged(); 47 33 } 48 34 } 49 35 } 50 36 51 protected abstract void Save(); 52 void IStorableContent.Save() { 53 this.OnSaveOperationStarted(); 54 Exception ex = null; 55 try { 56 this.Save(); 57 } 58 catch (Exception e) { 59 ex = e; 60 } 61 this.OnSaveOperationFinished(ex); 62 } 63 public void Save(string filename) { 64 this.Filename = filename; 65 ((IStorableContent)this).Save(); 66 } 67 68 protected virtual void SaveAsnychronous() { 69 ThreadPool.QueueUserWorkItem( 70 new WaitCallback(delegate(object arg) { 71 this.Save(); 72 }) 73 ); 74 } 75 void IStorableContent.SaveAsynchronous() { 76 this.OnSaveOperationStarted(); 77 this.SaveAsnychronous(); 78 this.OnSaveOperationFinished(null); 79 } 80 public void SaveAsynchronous(string filename) { 81 this.Filename = filename; 82 ((IStorableContent)this).SaveAsynchronous(); 37 public StorableContent() { 38 filename = string.Empty; 83 39 } 84 40 … … 88 44 if (handler != null) handler(this, EventArgs.Empty); 89 45 } 90 public event EventHandler SaveOperationStarted;91 protected virtual void OnSaveOperationStarted() {92 EventHandler handler = SaveOperationStarted;93 if (handler != null) handler(this, EventArgs.Empty);94 }95 public event EventHandler<EventArgs<Exception>> SaveOperationFinished;96 protected virtual void OnSaveOperationFinished(Exception ex) {97 EventHandler<EventArgs<Exception>> handler = SaveOperationFinished;98 if (handler != null) handler(this, new EventArgs<Exception>(ex));99 }100 46 } 101 47 } -
trunk/sources/HeuristicLab.Common/3.2/HeuristicLab.Common-3.2.csproj
r3437 r3483 89 89 <None Include="HeuristicLabCommonPlugin.cs.frame" /> 90 90 <Compile Include="Cloner.cs" /> 91 <Compile Include="Content\Content.cs" />92 91 <Compile Include="Content\ContentManager.cs" /> 93 92 <Compile Include="Content\IContent.cs" />
Note: See TracChangeset
for help on using the changeset viewer.