corrade-vassal – Blame information for rev 1
?pathlinks?
Rev | Author | Line No. | Line |
---|---|---|---|
1 | vero | 1 | /* |
2 | * Copyright (c) Microsoft Corporation |
||
3 | * All rights reserved. |
||
4 | * |
||
5 | * - Redistribution and use in source and binary forms, with or without |
||
6 | * modification, are permitted provided that the following conditions are met: |
||
7 | * |
||
8 | * - Redistributions of source code must retain the above copyright notice, this |
||
9 | * list of conditions and the following disclaimer. |
||
10 | * - Neither the name of the openmetaverse.org nor the names |
||
11 | * of its contributors may be used to endorse or promote products derived from |
||
12 | * this software without specific prior written permission. |
||
13 | * |
||
14 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
||
15 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
||
16 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
||
17 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE |
||
18 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
||
19 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
||
20 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
||
21 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
||
22 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
||
23 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
||
24 | * POSSIBILITY OF SUCH DAMAGE. |
||
25 | */ |
||
26 | |||
27 | using System; |
||
28 | using System.Collections.Generic; |
||
29 | using System.Text; |
||
30 | using System.Threading; |
||
31 | |||
32 | namespace OpenMetaverse |
||
33 | { |
||
34 | public class Lazy<T> |
||
35 | { |
||
36 | private T _value = default(T); |
||
37 | private volatile bool _isValueCreated = false; |
||
38 | private Func<T> _valueFactory = null; |
||
39 | private object _lock; |
||
40 | |||
41 | public bool IsValueCreated { get { return _isValueCreated; } } |
||
42 | |||
43 | public Lazy() |
||
44 | : this(() => Activator.CreateInstance<T>()) |
||
45 | { |
||
46 | } |
||
47 | |||
48 | public Lazy(bool isThreadSafe) |
||
49 | : this(() => Activator.CreateInstance<T>(), isThreadSafe) |
||
50 | { |
||
51 | } |
||
52 | |||
53 | public Lazy(Func<T> valueFactory) : |
||
54 | this(valueFactory, true) |
||
55 | { |
||
56 | } |
||
57 | |||
58 | public Lazy(Func<T> valueFactory, bool isThreadSafe) |
||
59 | { |
||
60 | if (isThreadSafe) |
||
61 | { |
||
62 | this._lock = new object(); |
||
63 | } |
||
64 | |||
65 | this._valueFactory = valueFactory; |
||
66 | } |
||
67 | |||
68 | |||
69 | public T Value |
||
70 | { |
||
71 | get |
||
72 | { |
||
73 | if (!this._isValueCreated) |
||
74 | { |
||
75 | if (this._lock != null) |
||
76 | { |
||
77 | Monitor.Enter(this._lock); |
||
78 | } |
||
79 | |||
80 | try |
||
81 | { |
||
82 | T value = this._valueFactory.Invoke(); |
||
83 | this._valueFactory = null; |
||
84 | Thread.MemoryBarrier(); |
||
85 | this._value = value; |
||
86 | this._isValueCreated = true; |
||
87 | } |
||
88 | finally |
||
89 | { |
||
90 | if (this._lock != null) |
||
91 | { |
||
92 | Monitor.Exit(this._lock); |
||
93 | } |
||
94 | } |
||
95 | } |
||
96 | return this._value; |
||
97 | } |
||
98 | } |
||
99 | } |
||
100 | } |