Free cookie consent management tool by TermsFeed Policy Generator

source: branches/WebJobManager/HeuristicLab.Scripting/3.3/VariableStore.cs @ 17842

Last change on this file since 17842 was 13656, checked in by ascheibe, 9 years ago

#2582 created branch for Hive Web Job Manager

File size: 3.7 KB
RevLine 
[10506]1#region License Information
2/* HeuristicLab
[12012]3 * Copyright (C) 2002-2015 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
[10506]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
[11058]22using System;
[11605]23using System.Drawing;
[11058]24using System.IO;
[10506]25using HeuristicLab.Collections;
[10332]26using HeuristicLab.Common;
27using HeuristicLab.Core;
[11058]28using HeuristicLab.Persistence.Core;
[10332]29using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
[11058]30using HeuristicLab.Persistence.Default.Xml;
[10332]31
[10506]32namespace HeuristicLab.Scripting {
[10332]33  [Item("VariableStore", "Represents a variable store.")]
34  [StorableClass]
[11605]35  public class VariableStore : ObservableDictionary<string, object>, IItem {
36    #region Properties
[13656]37    public virtual string ItemName
38    {
[11605]39      get { return ItemAttribute.GetName(GetType()); }
40    }
[13656]41    public virtual string ItemDescription
42    {
[11605]43      get { return ItemAttribute.GetDescription(GetType()); }
44    }
[13656]45    public Version ItemVersion
46    {
[11605]47      get { return ItemAttribute.GetVersion(GetType()); }
48    }
[13656]49
50    public virtual Image ItemImage
51    {
[11605]52      get { return ItemAttribute.GetImage(GetType()); }
53    }
54    #endregion
55
56    #region Constructors & Cloning
[10332]57    [StorableConstructor]
58    protected VariableStore(bool deserializing) : base(deserializing) { }
[11058]59    protected VariableStore(VariableStore original, Cloner cloner) {
60      cloner.RegisterClonedObject(original, this);
61      foreach (var kvp in original.dict) {
62        var clonedValue = kvp.Value as IDeepCloneable;
63        if (clonedValue != null) {
64          dict[kvp.Key] = cloner.Clone(clonedValue);
65        } else {
66          try {
67            dict[kvp.Key] = CloneByPersistence(kvp.Value);
[13656]68          }
69          catch (PersistenceException pe) {
[11066]70            throw new NotSupportedException(string.Format(@"VariableStore: Variable ""{0}"" could not be cloned.", kvp.Key), pe);
[11058]71          }
72        }
73      }
74    }
[10332]75    public VariableStore() : base() { }
[11058]76
77    public object Clone() {
78      return Clone(new Cloner());
79    }
80    public IDeepCloneable Clone(Cloner cloner) {
81      return new VariableStore(this, cloner);
82    }
83
[11066]84    protected T CloneByPersistence<T>(T value) {
[11058]85      using (var serializerStream = new MemoryStream()) {
86        XmlGenerator.Serialize(value, serializerStream);
87        var bytes = serializerStream.GetBuffer();
88        using (var deserializerStream = new MemoryStream(bytes)) {
[11066]89          return XmlParser.Deserialize<T>(deserializerStream);
[11058]90        }
91      }
92    }
[11605]93    #endregion
94
95    #region Overrides
96    public override string ToString() {
97      return ItemName;
98    }
99    #endregion
100
101    #region Events
102    public event EventHandler ItemImageChanged;
103    protected virtual void OnItemImageChanged() {
104      var handler = ItemImageChanged;
105      if (handler != null) handler(this, EventArgs.Empty);
106    }
107    public event EventHandler ToStringChanged;
108    protected virtual void OnToStringChanged() {
109      var handler = ToStringChanged;
110      if (handler != null) handler(this, EventArgs.Empty);
111    }
112    #endregion
[10332]113  }
114}
Note: See TracBrowser for help on using the repository browser.