Free cookie consent management tool by TermsFeed Policy Generator

source: stable/HeuristicLab.Scripting/3.3/VariableStore.cs @ 17604

Last change on this file since 17604 was 17181, checked in by swagner, 5 years ago

#2875: Merged r17180 from trunk to stable

File size: 3.8 KB
RevLine 
[10506]1#region License Information
2/* HeuristicLab
[17181]3 * Copyright (C) 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
[11090]22using System;
[11907]23using System.Drawing;
[11090]24using System.IO;
[10506]25using HeuristicLab.Collections;
[10332]26using HeuristicLab.Common;
[11907]27using HeuristicLab.Common.Resources;
[10332]28using HeuristicLab.Core;
[17097]29using HEAL.Attic;
[11090]30using HeuristicLab.Persistence.Default.Xml;
[10332]31
[10506]32namespace HeuristicLab.Scripting {
[10332]33  [Item("VariableStore", "Represents a variable store.")]
[17097]34  [StorableType("DDE415E7-99FD-4C10-9B5E-63334876FFDE")]
[11907]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
[10332]55    [StorableConstructor]
[17097]56    protected VariableStore(StorableConstructorFlag _) : base(_) { }
[11090]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    }
[10332]72    public VariableStore() : base() { }
[11090]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()) {
[17097]83        var serializer = new ProtoBufSerializer();
84        serializer.Serialize(value, serializerStream, disposeStream: false);
[11090]85        var bytes = serializerStream.GetBuffer();
86        using (var deserializerStream = new MemoryStream(bytes)) {
[17097]87          return (T)serializer.Deserialize(deserializerStream);
[11090]88        }
89      }
90    }
[11907]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.