Free cookie consent management tool by TermsFeed Policy Generator

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

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

#2582 created branch for Hive Web Job Manager

File size: 3.7 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2015 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
22using System;
23using System.Drawing;
24using System.IO;
25using HeuristicLab.Collections;
26using HeuristicLab.Common;
27using HeuristicLab.Core;
28using HeuristicLab.Persistence.Core;
29using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
30using HeuristicLab.Persistence.Default.Xml;
31
32namespace HeuristicLab.Scripting {
33  [Item("VariableStore", "Represents a variable store.")]
34  [StorableClass]
35  public class VariableStore : ObservableDictionary<string, object>, IItem {
36    #region Properties
37    public virtual string ItemName
38    {
39      get { return ItemAttribute.GetName(GetType()); }
40    }
41    public virtual string ItemDescription
42    {
43      get { return ItemAttribute.GetDescription(GetType()); }
44    }
45    public Version ItemVersion
46    {
47      get { return ItemAttribute.GetVersion(GetType()); }
48    }
49
50    public virtual Image ItemImage
51    {
52      get { return ItemAttribute.GetImage(GetType()); }
53    }
54    #endregion
55
56    #region Constructors & Cloning
57    [StorableConstructor]
58    protected VariableStore(bool deserializing) : base(deserializing) { }
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);
68          }
69          catch (PersistenceException pe) {
70            throw new NotSupportedException(string.Format(@"VariableStore: Variable ""{0}"" could not be cloned.", kvp.Key), pe);
71          }
72        }
73      }
74    }
75    public VariableStore() : base() { }
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
84    protected T CloneByPersistence<T>(T value) {
85      using (var serializerStream = new MemoryStream()) {
86        XmlGenerator.Serialize(value, serializerStream);
87        var bytes = serializerStream.GetBuffer();
88        using (var deserializerStream = new MemoryStream(bytes)) {
89          return XmlParser.Deserialize<T>(deserializerStream);
90        }
91      }
92    }
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
113  }
114}
Note: See TracBrowser for help on using the repository browser.