[13071] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
| 3 | * Copyright (C) 2002-2015 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 |
|
---|
| 22 | using HeuristicLab.Common;
|
---|
[13069] | 23 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
| 24 | using System;
|
---|
[16575] | 25 | using HEAL.Attic;
|
---|
[13069] | 26 |
|
---|
| 27 | namespace HeuristicLab.BioBoost.Utils {
|
---|
| 28 |
|
---|
| 29 | /// <summary>
|
---|
| 30 | /// Wraps a field into a local variable inside the constructor. This evades
|
---|
| 31 | /// object graph traversal as the actual value is no longer discoverable by
|
---|
| 32 | /// reflection. Use with Caution!
|
---|
| 33 | /// </summary>
|
---|
| 34 | /// <typeparam name="T">The Type of the value to be wrapped.</typeparam>
|
---|
[16575] | 35 | [StorableType("75502002-CE4F-4B62-B37B-FEB93F899BA3")]
|
---|
[13069] | 36 | public class Closure<T> : IDeepCloneable {
|
---|
| 37 |
|
---|
| 38 | public enum Encapsulation { Cloned, Referenced };
|
---|
| 39 |
|
---|
| 40 | [Storable]
|
---|
| 41 | private readonly Encapsulation encapsulation;
|
---|
| 42 |
|
---|
| 43 | [Storable]
|
---|
| 44 | public T Value { get { return Getter(); } set { Setter(value); } }
|
---|
| 45 |
|
---|
| 46 | public Func<T> Getter;
|
---|
| 47 | public Action<T> Setter;
|
---|
| 48 |
|
---|
| 49 | [StorableConstructor]
|
---|
[16575] | 50 | protected Closure(StorableConstructorFlag _) {
|
---|
[13069] | 51 | T value = default(T);
|
---|
| 52 | Getter = () => value;
|
---|
| 53 | Setter = v => value = v;
|
---|
| 54 | }
|
---|
| 55 | protected Closure(Closure<T> orig, Cloner cloner) {
|
---|
| 56 | T value = default(T);
|
---|
| 57 | Getter = () => value;
|
---|
| 58 | Setter = v => value = v;
|
---|
| 59 | encapsulation = orig.encapsulation;
|
---|
| 60 | cloner.RegisterClonedObject(orig, this);
|
---|
| 61 | if (encapsulation == Encapsulation.Referenced) {
|
---|
| 62 | Value = orig.Value;
|
---|
| 63 | } else {
|
---|
| 64 | var cloneable = orig.Value as IDeepCloneable;
|
---|
| 65 | if (cloneable != null)
|
---|
| 66 | Value = (T) cloner.Clone(cloneable);
|
---|
| 67 | else
|
---|
| 68 | Value = orig.Value;
|
---|
| 69 | }
|
---|
| 70 | }
|
---|
| 71 | public Closure(Encapsulation encapsulation) {
|
---|
| 72 | this.encapsulation = encapsulation;
|
---|
| 73 | T value = default(T);
|
---|
| 74 | Getter = () => value;
|
---|
| 75 | Setter = v => value = v;
|
---|
| 76 | }
|
---|
| 77 |
|
---|
| 78 | public object Clone() {
|
---|
| 79 | return Clone(new Cloner());
|
---|
| 80 | }
|
---|
| 81 |
|
---|
| 82 | public IDeepCloneable Clone(Cloner cloner) {
|
---|
| 83 | return new Closure<T>(this, cloner);
|
---|
| 84 | }
|
---|
| 85 | }
|
---|
| 86 | }
|
---|