Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/HeuristicLab.Scripting/3.3/VariableStore.cs @ 17511

Last change on this file since 17511 was 17180, checked in by swagner, 5 years ago

#2875: Removed years in copyrights

File size: 3.8 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 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 HEAL.Attic;
30using HeuristicLab.Persistence.Default.Xml;
31
32namespace HeuristicLab.Scripting {
33  [Item("VariableStore", "Represents a variable store.")]
34  [StorableType("DDE415E7-99FD-4C10-9B5E-63334876FFDE")]
35  public class VariableStore : ObservableDictionary<string, object>, IItem {
36    #region Properties
37    public virtual string ItemName {
38      get { return ItemAttribute.GetName(GetType()); }
39    }
40    public virtual string ItemDescription {
41      get { return ItemAttribute.GetDescription(GetType()); }
42    }
43    public Version ItemVersion {
44      get { return ItemAttribute.GetVersion(GetType()); }
45    }
46    public static Image StaticItemImage {
47      get { return VSImageLibrary.Class; }
48    }
49    public virtual Image ItemImage {
50      get { return ItemAttribute.GetImage(GetType()); }
51    }
52    #endregion
53
54    #region Constructors & Cloning
55    [StorableConstructor]
56    protected VariableStore(StorableConstructorFlag _) : base(_) { }
57    protected VariableStore(VariableStore original, Cloner cloner) {
58      cloner.RegisterClonedObject(original, this);
59      foreach (var kvp in original.dict) {
60        var clonedValue = kvp.Value as IDeepCloneable;
61        if (clonedValue != null) {
62          dict[kvp.Key] = cloner.Clone(clonedValue);
63        } else {
64          try {
65            dict[kvp.Key] = CloneByPersistence(kvp.Value);
66          } catch (PersistenceException pe) {
67            throw new NotSupportedException(string.Format(@"VariableStore: Variable ""{0}"" could not be cloned.", kvp.Key), pe);
68          }
69        }
70      }
71    }
72    public VariableStore() : base() { }
73
74    public object Clone() {
75      return Clone(new Cloner());
76    }
77    public IDeepCloneable Clone(Cloner cloner) {
78      return new VariableStore(this, cloner);
79    }
80
81    protected T CloneByPersistence<T>(T value) {
82      using (var serializerStream = new MemoryStream()) {
83        var serializer = new ProtoBufSerializer();
84        serializer.Serialize(value, serializerStream, disposeStream: false);
85        var bytes = serializerStream.GetBuffer();
86        using (var deserializerStream = new MemoryStream(bytes)) {
87          return (T)serializer.Deserialize(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.