Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
04/22/10 05:14:39 (14 years ago)
Author:
swagner
Message:

Worked on the refactoring of saving and loading items (#990)

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  
    2121
    2222using System;
    23 using System.Collections.Generic;
    24 using System.Linq;
    25 using System.Text;
    26 using System.Threading;
    2723
    2824namespace HeuristicLab.Common {
    2925  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;
    3132    }
    3233
    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;
    3641    }
    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);
    4157    }
     58    protected abstract IStorableContent LoadContent(string filename);
    4259
    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;
    4564    }
    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);
    4879    }
    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);
    9081  }
    9182}
  • trunk/sources/HeuristicLab.Common/3.2/Content/IContent.cs

    r3437 r3483  
    2020#endregion
    2121
    22 using System;
    23 using System.Collections.Generic;
    24 using System.Linq;
    25 using System.Text;
    26 
    2722namespace HeuristicLab.Common {
    28   public interface IContent : IDeepCloneable { }
     23  public interface IContent { }
    2924}
  • trunk/sources/HeuristicLab.Common/3.2/Content/IStorableContent.cs

    r3437 r3483  
    2121
    2222using System;
    23 using System.Collections.Generic;
    24 using System.Linq;
    25 using System.Text;
    2623
    2724namespace HeuristicLab.Common {
     
    2926    string Filename { get; set; }
    3027
    31     void Save();
    32     void Save(string filename);
    33     void SaveAsynchronous();
    34     void SaveAsynchronous(string filename);
    35 
    3628    event EventHandler FilenameChanged;
    37     event EventHandler SaveOperationStarted;
    38     event EventHandler<EventArgs<Exception>> SaveOperationFinished;
    3929  }
    4030}
  • trunk/sources/HeuristicLab.Common/3.2/Content/StorableContent.cs

    r3437 r3483  
    2121
    2222using System;
    23 using System.Collections.Generic;
    24 using System.Linq;
    25 using System.Text;
    26 using System.Threading;
    27 using System.Reflection;
    2823
    2924namespace 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 {
    4026    private string filename;
    4127    public string Filename {
    42       get { return this.filename; }
     28      get { return filename; }
    4329      set {
    44         if (this.filename != value) {
    45           this.filename = value;
    46           this.OnFilenameChanged();
     30        if (!filename.Equals(value)) {
     31          filename = value;
     32          OnFilenameChanged();
    4733        }
    4834      }
    4935    }
    5036
    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;
    8339    }
    8440
     
    8844      if (handler != null) handler(this, EventArgs.Empty);
    8945    }
    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     }
    10046  }
    10147}
  • trunk/sources/HeuristicLab.Common/3.2/HeuristicLab.Common-3.2.csproj

    r3437 r3483  
    8989    <None Include="HeuristicLabCommonPlugin.cs.frame" />
    9090    <Compile Include="Cloner.cs" />
    91     <Compile Include="Content\Content.cs" />
    9291    <Compile Include="Content\ContentManager.cs" />
    9392    <Compile Include="Content\IContent.cs" />
Note: See TracChangeset for help on using the changeset viewer.