-
Notifications
You must be signed in to change notification settings - Fork 1
/
CachedProperty.cs
50 lines (43 loc) · 1.54 KB
/
CachedProperty.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
/*
* CachedProperty.cs
*
* Cached Property class, allows "properties" to be cached. This is intended for when the getter/setters are otherwise slow functions.
*
* NOTE: Caching requires a bool and a fully qualifiable TValue. This means that the program memory usage may drastically increase.
*
*/
using System;
/// <summary>
/// Cached Property class, allows "properties" to be cached. This is intended for when the getter/setters are otherwise slow functions.
/// NOTE: Caching requires a bool and a fully qualifiable TValue. This means that the program memory usage may drastically increase.
/// </summary>
public class CachedProperty< TValue >
{
bool cached = false;
TValue _value;
readonly Func< TValue > GetFunc;
readonly Action< TValue > SetAction;
public CachedProperty( Func< TValue > getFunc, Action< TValue > setAction )
{
this.GetFunc = getFunc;
this.SetAction = setAction;
}
public TValue Value
{
get
{
if( GetFunc == null ) throw new NotImplementedException();
if( !cached )
_value = GetFunc();
cached = true;
return _value;
}
set
{
if( SetAction == null ) throw new NotImplementedException();
SetAction( value );
_value = value;
cached = true;
}
}
}