Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Core/3.3/Scope.cs @ 3155

Last change on this file since 3155 was 3017, checked in by epitzer, 15 years ago

Merge StorableClassType.Empty into StorableClassType.MarkedOnly and make it the default if not specified (#548)

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