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