[10506] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
[14186] | 3 | * Copyright (C) 2002-2016 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] | 22 | using System;
|
---|
[11907] | 23 | using System.Drawing;
|
---|
[11090] | 24 | using System.IO;
|
---|
[10506] | 25 | using HeuristicLab.Collections;
|
---|
[10332] | 26 | using HeuristicLab.Common;
|
---|
[11907] | 27 | using HeuristicLab.Common.Resources;
|
---|
[10332] | 28 | using HeuristicLab.Core;
|
---|
[11090] | 29 | using HeuristicLab.Persistence.Core;
|
---|
[10332] | 30 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
[11090] | 31 | using HeuristicLab.Persistence.Default.Xml;
|
---|
[10332] | 32 |
|
---|
[10506] | 33 | namespace HeuristicLab.Scripting {
|
---|
[10332] | 34 | [Item("VariableStore", "Represents a variable store.")]
|
---|
| 35 | [StorableClass]
|
---|
[11907] | 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) { }
|
---|
[11090] | 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 | }
|
---|
[10332] | 73 | public VariableStore() : base() { }
|
---|
[11090] | 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 | }
|
---|
[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 | }
|
---|