Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Scripting/3.3/VariableStore.cs @ 12438

Last change on this file since 12438 was 12012, checked in by ascheibe, 10 years ago

#2212 merged r12008, r12009, r12010 back into trunk

File size: 3.8 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;
[11605]27using HeuristicLab.Common.Resources;
[10332]28using HeuristicLab.Core;
[11058]29using HeuristicLab.Persistence.Core;
[10332]30using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
[11058]31using HeuristicLab.Persistence.Default.Xml;
[10332]32
[10506]33namespace HeuristicLab.Scripting {
[10332]34  [Item("VariableStore", "Represents a variable store.")]
35  [StorableClass]
[11605]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
[10332]56    [StorableConstructor]
57    protected VariableStore(bool deserializing) : base(deserializing) { }
[11058]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) {
[11066]68            throw new NotSupportedException(string.Format(@"VariableStore: Variable ""{0}"" could not be cloned.", kvp.Key), pe);
[11058]69          }
70        }
71      }
72    }
[10332]73    public VariableStore() : base() { }
[11058]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
[11066]82    protected T CloneByPersistence<T>(T value) {
[11058]83      using (var serializerStream = new MemoryStream()) {
84        XmlGenerator.Serialize(value, serializerStream);
85        var bytes = serializerStream.GetBuffer();
86        using (var deserializerStream = new MemoryStream(bytes)) {
[11066]87          return XmlParser.Deserialize<T>(deserializerStream);
[11058]88        }
89      }
90    }
[11605]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
[10332]111  }
112}
Note: See TracBrowser for help on using the repository browser.