/
singletons.dm
140 lines (109 loc) · 4.43 KB
/
singletons.dm
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
var/global/repository/singletons/Singletons = new
/repository/singletons
/// A cache of individual singletons as (/singleton/path = Instance, ...)
var/static/list/instances = list()
/// A map of (/singleton/path = TRUE, ...). Indicates whether a path has been tried for instances.
var/static/list/resolved_instances = list()
/// A cache of singleton types according to a parent type as (/singleton/path = list(/singleton/path = Instance, /singleton/path/foo = Instance, ...))
var/static/list/type_maps = list()
/// A map of (/singleton/path = TRUE, ...). Indicates whether a path has been tried for type_maps.
var/static/list/resolved_type_maps = list()
/// A cache of singleton subtypes according to a parent type as (/singleton/path = list(/singleton/path/foo = Instance, ...))
var/static/list/subtype_maps = list()
/// A map of (/singleton/path = TRUE, ...). Indicates whether a path has been tried for subtype_maps.
var/static/list/resolved_subtype_maps = list()
/// A cache of singleton types according to a parent type as (/singleton/path = list(Parent Instance, Subtype Instance, ...))
var/static/list/type_lists = list()
/// A map of (/singleton/path = TRUE, ...). Indicates whether a path has been tried for type_lists.
var/static/list/resolved_type_lists = list()
/// A cache of singleton subtypes according to a parent type as (/singleton/path = list(Subtype Instance, Subtype Instance, ...))
var/static/list/subtype_lists = list()
/// A map of (/singleton/path = TRUE, ...). Indicates whether a path has been tried for subtype_lists.
var/static/list/resolved_subtype_lists = list()
/**
* Get a singleton instance according to path. Creates it if necessary. Null if abstract or not a singleton.
* Prefer the GET_SINGLETON macro to minimize proc calls.
*/
/repository/singletons/proc/GetInstance(singleton/path)
if (!ispath(path, /singleton))
return
if (resolved_instances[path])
return instances[path]
resolved_instances[path] = TRUE
if (is_abstract(path))
return
var/singleton/result = new path
instances[path] = result
result.Initialize()
return result
/// Get a (path = instance) map of valid singletons according to paths.
/repository/singletons/proc/GetMap(list/singleton/paths)
var/list/result = list()
for (var/path in paths)
var/singleton/instance = GetInstance(path)
if (!instance)
continue
result[path] = instance
return result
/// Get a list of valid singletons according to paths.
/repository/singletons/proc/GetList(list/singleton/paths)
var/list/result = list()
for (var/path in paths)
var/singleton/instance = GetInstance(path)
if (!instance)
continue
result += instance
return result
/**
* Get a (path = instance) map of valid singletons according to typesof(path).
* Prefer the GET_SINGLETON_TYPE_MAP macro to minimize proc calls.
*/
/repository/singletons/proc/GetTypeMap(singleton/path)
if (resolved_type_maps[path])
return type_maps[path] || list()
resolved_type_maps[path] = TRUE
var/result = GetMap(typesof(path))
type_maps[path] = result
return result
/**
* Get a (path = instance) map of valid singletons according to subtypesof(path).
* Prefer the GET_SINGLETON_TYPE_MAP macro to minimize proc calls.
*/
/repository/singletons/proc/GetSubtypeMap(singleton/path)
if (resolved_subtype_maps[path])
return subtype_maps[path] || list()
resolved_subtype_maps[path] = TRUE
var/result = GetMap(subtypesof(path))
subtype_maps[path] = result
return result
/**
* Get a list of valid singletons according to typesof(path).
* Prefer the GET_SINGLETON_TYPE_LIST macro to minimize proc calls.
*/
/repository/singletons/proc/GetTypeList(singleton/path)
if (resolved_type_lists[path])
return type_lists[path] || list()
resolved_type_lists[path] = TRUE
var/result = GetList(typesof(path))
type_lists[path] = result
return result
/**
* Get a list of valid singletons according to subtypesof(path).
* Prefer the GET_SINGLETON_SUBTYPE_LIST macro to minimize proc calls.
*/
/repository/singletons/proc/GetSubtypeList(singleton/path)
if (resolved_subtype_lists[path])
return subtype_lists[path] || list()
resolved_subtype_lists[path] = TRUE
var/result = GetList(subtypesof(path))
subtype_lists[path] = result
return result
/singleton
abstract_type = /singleton
/singleton/proc/Initialize()
SHOULD_CALL_PARENT(TRUE)
SHOULD_NOT_SLEEP(TRUE)
/singleton/Destroy()
SHOULD_CALL_PARENT(FALSE)
crash_with("Prevented attempt to delete a singleton instance: [log_info_line(src)]")
return QDEL_HINT_LETMELIVE