#region License Information /* HeuristicLab * Copyright (C) 2002-2015 Heuristic and Evolutionary Algorithms Laboratory (HEAL) * * This file is part of HeuristicLab. * * HeuristicLab is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * HeuristicLab is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with HeuristicLab. If not, see . */ #endregion using HeuristicLab.Common; using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; using System; using HEAL.Attic; namespace HeuristicLab.BioBoost.Utils { /// /// Wraps a field into a local variable inside the constructor. This evades /// object graph traversal as the actual value is no longer discoverable by /// reflection. Use with Caution! /// /// The Type of the value to be wrapped. [StorableType("75502002-CE4F-4B62-B37B-FEB93F899BA3")] public class Closure : IDeepCloneable { public enum Encapsulation { Cloned, Referenced }; [Storable] private readonly Encapsulation encapsulation; [Storable] public T Value { get { return Getter(); } set { Setter(value); } } public Func Getter; public Action Setter; [StorableConstructor] protected Closure(StorableConstructorFlag _) { T value = default(T); Getter = () => value; Setter = v => value = v; } protected Closure(Closure orig, Cloner cloner) { T value = default(T); Getter = () => value; Setter = v => value = v; encapsulation = orig.encapsulation; cloner.RegisterClonedObject(orig, this); if (encapsulation == Encapsulation.Referenced) { Value = orig.Value; } else { var cloneable = orig.Value as IDeepCloneable; if (cloneable != null) Value = (T) cloner.Clone(cloneable); else Value = orig.Value; } } public Closure(Encapsulation encapsulation) { this.encapsulation = encapsulation; T value = default(T); Getter = () => value; Setter = v => value = v; } public object Clone() { return Clone(new Cloner()); } public IDeepCloneable Clone(Cloner cloner) { return new Closure(this, cloner); } } }