annotate Implab/Components/LazyAndWeak.cs @ 178:d5c5db0335ee ref20160224

working on JSON parser
author cin
date Wed, 23 Mar 2016 19:51:45 +0300
parents
children c32688129f14
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
178
d5c5db0335ee working on JSON parser
cin
parents:
diff changeset
1 using System;
d5c5db0335ee working on JSON parser
cin
parents:
diff changeset
2 using System.Threading;
d5c5db0335ee working on JSON parser
cin
parents:
diff changeset
3
d5c5db0335ee working on JSON parser
cin
parents:
diff changeset
4 namespace Implab.Components {
d5c5db0335ee working on JSON parser
cin
parents:
diff changeset
5 public class LazyAndWeak<T> where T : class {
d5c5db0335ee working on JSON parser
cin
parents:
diff changeset
6
d5c5db0335ee working on JSON parser
cin
parents:
diff changeset
7 readonly Func<T> m_factory;
d5c5db0335ee working on JSON parser
cin
parents:
diff changeset
8 readonly object m_lock;
d5c5db0335ee working on JSON parser
cin
parents:
diff changeset
9 WeakReference m_reference;
d5c5db0335ee working on JSON parser
cin
parents:
diff changeset
10
d5c5db0335ee working on JSON parser
cin
parents:
diff changeset
11
d5c5db0335ee working on JSON parser
cin
parents:
diff changeset
12 public LazyAndWeak(Func<T> factory, bool useLock) {
d5c5db0335ee working on JSON parser
cin
parents:
diff changeset
13 Safe.ArgumentNotNull(factory, "factory");
d5c5db0335ee working on JSON parser
cin
parents:
diff changeset
14 m_factory = factory;
d5c5db0335ee working on JSON parser
cin
parents:
diff changeset
15 m_lock = useLock ? new object() : null;
d5c5db0335ee working on JSON parser
cin
parents:
diff changeset
16 }
d5c5db0335ee working on JSON parser
cin
parents:
diff changeset
17
d5c5db0335ee working on JSON parser
cin
parents:
diff changeset
18 public LazyAndWeak(Func<T> factory) : this(factory, false) {
d5c5db0335ee working on JSON parser
cin
parents:
diff changeset
19 }
d5c5db0335ee working on JSON parser
cin
parents:
diff changeset
20
d5c5db0335ee working on JSON parser
cin
parents:
diff changeset
21 public T Value {
d5c5db0335ee working on JSON parser
cin
parents:
diff changeset
22 get {
d5c5db0335ee working on JSON parser
cin
parents:
diff changeset
23 while (true) {
d5c5db0335ee working on JSON parser
cin
parents:
diff changeset
24 var weak = m_reference;
d5c5db0335ee working on JSON parser
cin
parents:
diff changeset
25 T value;
d5c5db0335ee working on JSON parser
cin
parents:
diff changeset
26 if (weak != null) {
d5c5db0335ee working on JSON parser
cin
parents:
diff changeset
27 value = weak.Target as T;
d5c5db0335ee working on JSON parser
cin
parents:
diff changeset
28 if (value != null)
d5c5db0335ee working on JSON parser
cin
parents:
diff changeset
29 return value;
d5c5db0335ee working on JSON parser
cin
parents:
diff changeset
30 }
d5c5db0335ee working on JSON parser
cin
parents:
diff changeset
31
d5c5db0335ee working on JSON parser
cin
parents:
diff changeset
32 if (m_lock == null) {
d5c5db0335ee working on JSON parser
cin
parents:
diff changeset
33 value = m_factory();
d5c5db0335ee working on JSON parser
cin
parents:
diff changeset
34
d5c5db0335ee working on JSON parser
cin
parents:
diff changeset
35 if (Interlocked.CompareExchange(ref m_reference, new WeakReference(value), weak) == weak)
d5c5db0335ee working on JSON parser
cin
parents:
diff changeset
36 return value;
d5c5db0335ee working on JSON parser
cin
parents:
diff changeset
37 } else {
d5c5db0335ee working on JSON parser
cin
parents:
diff changeset
38 }
d5c5db0335ee working on JSON parser
cin
parents:
diff changeset
39 }
d5c5db0335ee working on JSON parser
cin
parents:
diff changeset
40 }
d5c5db0335ee working on JSON parser
cin
parents:
diff changeset
41 }
d5c5db0335ee working on JSON parser
cin
parents:
diff changeset
42 }
d5c5db0335ee working on JSON parser
cin
parents:
diff changeset
43 }
d5c5db0335ee working on JSON parser
cin
parents:
diff changeset
44