[10506] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
[17180] | 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 |
|
---|
[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;
|
---|
[16565] | 29 | using HEAL.Attic;
|
---|
[11058] | 30 | using HeuristicLab.Persistence.Default.Xml;
|
---|
[10332] | 31 |
|
---|
[10506] | 32 | namespace HeuristicLab.Scripting {
|
---|
[10332] | 33 | [Item("VariableStore", "Represents a variable store.")]
|
---|
[16565] | 34 | [StorableType("DDE415E7-99FD-4C10-9B5E-63334876FFDE")]
|
---|
[11605] | 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]
|
---|
[16565] | 56 | protected VariableStore(StorableConstructorFlag _) : base(_) { }
|
---|
[11058] | 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) {
|
---|
[11066] | 67 | throw new NotSupportedException(string.Format(@"VariableStore: Variable ""{0}"" could not be cloned.", kvp.Key), pe);
|
---|
[11058] | 68 | }
|
---|
| 69 | }
|
---|
| 70 | }
|
---|
| 71 | }
|
---|
[10332] | 72 | public VariableStore() : base() { }
|
---|
[11058] | 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 |
|
---|
[11066] | 81 | protected T CloneByPersistence<T>(T value) {
|
---|
[11058] | 82 | using (var serializerStream = new MemoryStream()) {
|
---|
[16565] | 83 | var serializer = new ProtoBufSerializer();
|
---|
| 84 | serializer.Serialize(value, serializerStream, disposeStream: false);
|
---|
[11058] | 85 | var bytes = serializerStream.GetBuffer();
|
---|
| 86 | using (var deserializerStream = new MemoryStream(bytes)) {
|
---|
[16565] | 87 | return (T)serializer.Deserialize(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 | }
|
---|