Free cookie consent management tool by TermsFeed Policy Generator

source: branches/2839_HiveProjectManagement/HeuristicLab.Scripting/3.3/VariableStore.cs @ 17476

Last change on this file since 17476 was 16057, checked in by jkarder, 6 years ago

#2839:

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