Free cookie consent management tool by TermsFeed Policy Generator

source: branches/CloningRefactoring/HeuristicLab.Core/3.3/Scope.cs @ 4668

Last change on this file since 4668 was 4668, checked in by swagner, 13 years ago

Finished cloning refactoring of HeuristicLab.Common, HeuristicLab.Collections and HeuristicLab.Core (#922)

File size: 5.2 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2010 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.Drawing;
23using HeuristicLab.Collections;
24using HeuristicLab.Common;
25using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
26
27namespace HeuristicLab.Core {
28  /// <summary>
29  /// Hierarchical container of variables (and of subscopes).
30  /// </summary>
31  [Item("Scope", "A scope which contains variables and sub-scopes.")]
32  [StorableClass]
33  public sealed class Scope : NamedItem, IScope {
34    public override Image ItemImage {
35      get { return HeuristicLab.Common.Resources.VS2008ImageLibrary.OrgChart; }
36    }
37
38    [Storable]
39    private IScope parent;
40    public IScope Parent {
41      get { return parent; }
42      set {
43        if (parent != value) {
44          parent = value;
45        }
46      }
47    }
48
49    [Storable]
50    private VariableCollection variables;
51    public VariableCollection Variables {
52      get { return variables; }
53    }
54
55    [Storable]
56    private ScopeList subScopes;
57    public ScopeList SubScopes {
58      get { return subScopes; }
59    }
60
61    [StorableConstructor]
62    private Scope(bool deserializing) : base(deserializing) { }
63    private Scope(Scope original, Cloner cloner)
64      : base(original, cloner) {
65      if (original.variables.Count > 0) variables = cloner.Clone(original.variables);
66      if (original.subScopes.Count > 0) {
67        subScopes = cloner.Clone(original.subScopes);
68        foreach (IScope child in SubScopes)
69          child.Parent = this;
70        RegisterSubScopesEvents();
71      }
72    }
73    /// <summary>
74    /// Initializes a new instance of <see cref="Scope"/> having "Anonymous" as default name.
75    /// </summary>
76    public Scope()
77      : base("Anonymous") {
78      parent = null;
79      variables = new VariableCollection();
80      subScopes = new ScopeList();
81      RegisterSubScopesEvents();
82    }
83    /// <summary>
84    /// Initializes a new instance of <see cref="Scope"/> with the given <paramref name="name"/>.
85    /// </summary>
86    /// <param name="name">The name of the scope.</param>
87    public Scope(string name)
88      : base(name) {
89      parent = null;
90      variables = new VariableCollection();
91      subScopes = new ScopeList();
92      RegisterSubScopesEvents();
93    }
94    public Scope(string name, string description)
95      : base(name, description) {
96      parent = null;
97      variables = new VariableCollection();
98      subScopes = new ScopeList();
99      RegisterSubScopesEvents();
100    }
101
102    [StorableHook(HookType.AfterDeserialization)]
103    private void AfterDeserialization() {
104      RegisterSubScopesEvents();
105    }
106
107    /// <inheritdoc/>
108    public void Clear() {
109      variables.Clear();
110      subScopes.Clear();
111    }
112
113    /// <inheritdoc/>
114    public override IDeepCloneable Clone(Cloner cloner) {
115      return new Scope(this, cloner);
116    }
117
118    #region SubScopes Events
119    private void RegisterSubScopesEvents() {
120      if (subScopes != null) {
121        subScopes.ItemsAdded += new CollectionItemsChangedEventHandler<IndexedItem<IScope>>(SubScopes_ItemsAdded);
122        subScopes.ItemsRemoved += new CollectionItemsChangedEventHandler<IndexedItem<IScope>>(SubScopes_ItemsRemoved);
123        subScopes.ItemsReplaced += new CollectionItemsChangedEventHandler<IndexedItem<IScope>>(SubScopes_ItemsReplaced);
124        subScopes.CollectionReset += new CollectionItemsChangedEventHandler<IndexedItem<IScope>>(SubScopes_CollectionReset);
125      }
126    }
127    private void SubScopes_ItemsAdded(object sender, CollectionItemsChangedEventArgs<IndexedItem<IScope>> e) {
128      foreach (IndexedItem<IScope> item in e.Items)
129        item.Value.Parent = this;
130    }
131    private void SubScopes_ItemsRemoved(object sender, CollectionItemsChangedEventArgs<IndexedItem<IScope>> e) {
132      foreach (IndexedItem<IScope> item in e.Items)
133        item.Value.Parent = null;
134    }
135    private void SubScopes_ItemsReplaced(object sender, CollectionItemsChangedEventArgs<IndexedItem<IScope>> e) {
136      foreach (IndexedItem<IScope> oldItem in e.OldItems)
137        oldItem.Value.Parent = null;
138      foreach (IndexedItem<IScope> item in e.Items)
139        item.Value.Parent = this;
140    }
141    private void SubScopes_CollectionReset(object sender, CollectionItemsChangedEventArgs<IndexedItem<IScope>> e) {
142      foreach (IndexedItem<IScope> oldItem in e.OldItems)
143        oldItem.Value.Parent = null;
144      foreach (IndexedItem<IScope> item in e.Items)
145        item.Value.Parent = this;
146    }
147    #endregion
148  }
149}
Note: See TracBrowser for help on using the repository browser.