Lean  $LEAN_TAG$
Ref.cs
1 /*
2  * QUANTCONNECT.COM - Democratizing Finance, Empowering Individuals.
3  * Lean Algorithmic Trading Engine v2.0. Copyright 2014 QuantConnect Corporation.
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  *
15 */
16 
17 using System;
18 
19 namespace QuantConnect.Util
20 {
21  /// <summary>
22  /// Represents a read-only reference to any value, T
23  /// </summary>
24  /// <typeparam name="T">The data type the reference points to</typeparam>
25  public interface IReadOnlyRef<out T>
26  {
27  /// <summary>
28  /// Gets the current value this reference points to
29  /// </summary>
30  T Value { get; }
31  }
32 
33  /// <summary>
34  /// Represents a reference to any value, T
35  /// </summary>
36  /// <typeparam name="T">The data type the reference points to</typeparam>
37  public sealed class Ref<T> : IReadOnlyRef<T>
38  {
39  private readonly Func<T> _getter;
40  private readonly Action<T> _setter;
41 
42  /// <summary>
43  /// Initializes a new instance of the <see cref="Ref{T}"/> class
44  /// </summary>
45  /// <param name="getter">A function delegate to get the current value</param>
46  /// <param name="setter">A function delegate to set the current value</param>
47  public Ref(Func<T> getter, Action<T> setter)
48  {
49  _getter = getter;
50  _setter = setter;
51  }
52 
53  /// <summary>
54  /// Gets or sets the value of this reference
55  /// </summary>
56  public T Value
57  {
58  get { return _getter(); }
59  set { _setter(value); }
60  }
61 
62  /// <summary>
63  /// Returns a read-only version of this instance
64  /// </summary>
65  /// <returns>A new instance with read-only semantics/gaurantees</returns>
67  {
68  return new Ref<T>(_getter, value =>
69  {
70  throw new InvalidOperationException("This instance is read-only.");
71  });
72  }
73  }
74 
75  /// <summary>
76  /// Provides some helper methods that leverage C# type inference
77  /// </summary>
78  public static class Ref
79  {
80  /// <summary>
81  /// Creates a new <see cref="Ref{T}"/> instance
82  /// </summary>
83  public static Ref<T> Create<T>(Func<T> getter, Action<T> setter)
84  {
85  return new Ref<T>(getter, setter);
86  }
87  /// <summary>
88  /// Creates a new <see cref="IReadOnlyRef{T}"/> instance
89  /// </summary>
90  public static IReadOnlyRef<T> CreateReadOnly<T>(Func<T> getter)
91  {
92  return new Ref<T>(getter, value =>
93  {
94  throw new InvalidOperationException("This instance is read-only.");
95  });
96  }
97  /// <summary>
98  /// Creates a new <see cref="Ref{T}"/> instance by closing over
99  /// the specified <paramref name="initialValue"/> variable.
100  /// NOTE: This won't close over the variable input to the function,
101  /// but rather a copy of the variable. This reference will use it's
102  /// own storage.
103  /// </summary>
104  public static Ref<T> Create<T>(T initialValue)
105  {
106  return new Ref<T>(() => initialValue, value => { initialValue = value; });
107  }
108  }
109 }