Tanoda
CacheEntryBase.cs
Go to the documentation of this file.
1using System;
3
5{
6 public abstract class CacheEntryBase : ICacheEntry
7 {
8 // todo add gui option
9 public static bool CachingEnabled
10 {
11 get
12 {
13 return s_cachingEnabled;
14 }
15 set
16 {
17 s_cachingEnabled = value;
18 }
19 }
20 protected CacheEntryBase(string name)
21 {
22 _name = name;
23 }
24
25 public virtual object EnterValue()
26 {
27 if (!CachingEnabled) return GetValue();
28
29 return _valueCache = (GetValueToCache() ?? GetValue());
30 }
31
32 public abstract object GetValueToCache();
33 private object _valueCache;
34 public virtual object GetValue()
35 {
36 if (!CachingEnabled) return GetValueToCache();
37
38 return _valueCache ?? (_valueCache = GetValueToCache());
39 }
40
41 public void SetValue(object newValue)
42 {
43 if (OnSetValue(newValue))
44 _valueCache = newValue;
45 }
46
47 protected abstract bool OnSetValue(object newValue);
48
49 public abstract Type Type();
50 public abstract bool CanSetValue();
51
52 private readonly string _name;
53 private string _typeName;
54
55 public string Name()
56 {
57 return _name;
58 }
59
60 public string TypeName()
61 {
62 if (_typeName == null)
63 {
64 var type = Type();
65 if (type != null)
66 _typeName = type.GetSourceCodeRepresentation();
67 else
68 _typeName = "INVALID";
69 }
70 return _typeName;
71 }
72
73 private bool? _canEnter;
74 private static bool s_cachingEnabled = false;
75
76 public virtual bool CanEnterValue()
77 {
78 if (_canEnter == null)
79 {
80 var type = Type();
81 _canEnter = type != null && !type.IsPrimitive;
82 }
83 return _canEnter.Value;
84 }
85 }
86}
abstract object GetValueToCache()
void SetValue(object newValue)
abstract bool CanSetValue()
abstract Type Type()
virtual bool CanEnterValue()
string Name()
static bool CachingEnabled
virtual object EnterValue()
Get object that is entered when variable name is clicked in inspector
abstract bool OnSetValue(object newValue)
string TypeName()
CacheEntryBase(string name)
virtual object GetValue()
Definition: ICacheEntry.cs:6