[13368] | 1 | #region License Information
|
---|
[10506] | 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] | 22 | using System;
|
---|
[11605] | 23 | using System.Drawing;
|
---|
[11058] | 24 | using System.IO;
|
---|
[10506] | 25 | using HeuristicLab.Collections;
|
---|
[10332] | 26 | using HeuristicLab.Common;
|
---|
[11605] | 27 | using HeuristicLab.Common.Resources;
|
---|
[10332] | 28 | using HeuristicLab.Core;
|
---|
[11058] | 29 | using HeuristicLab.Persistence.Core;
|
---|
[10332] | 30 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
[11058] | 31 | using HeuristicLab.Persistence.Default.Xml;
|
---|
[10332] | 32 |
|
---|
[10506] | 33 | namespace HeuristicLab.Scripting {
|
---|
[10332] | 34 | [Item("VariableStore", "Represents a variable store.")]
|
---|
[14711] | 35 | [StorableType("1BD425CF-1E69-45B1-AC3C-E6B290F0B000")]
|
---|
[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 | }
|
---|