-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathConfigRegistry.pas
More file actions
136 lines (108 loc) · 3.5 KB
/
ConfigRegistry.pas
File metadata and controls
136 lines (108 loc) · 3.5 KB
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
unit ConfigRegistry;
interface
uses
System.SysUtils, System.Classes, System.Generics.Collections;
type
// 閰嶇疆瀵硅薄娉ㄥ唽琛?
TConfigObjectRegistry = class
private
FMetadata: TDictionary<string, TConfigObjectMeta>;
FConfigTypes: TDictionary<string, TConfigObjectClass>;
function GenerateConfigID(const TypeId: string): string;
public
constructor Create;
destructor Destroy; override;
// 娉ㄥ唽閰嶇疆瀵硅薄绫诲瀷
procedure RegisterConfigType(const TypeId: string;
ConfigClass: TConfigObjectClass;
const Meta: TConfigObjectMeta);
// 鑾峰彇閰嶇疆瀵硅薄鍏冩暟鎹?
function GetConfigMeta(const TypeId: string): TConfigObjectMeta;
// 鍒涘缓閰嶇疆瀵硅薄瀹炰緥
function CreateConfig(const TypeId, Name: string): TConfigObject;
// 鑾峰彇鎵€鏈夋敞鍐岀殑绫诲瀷
function GetAllConfigTypes: TArray<string>;
// 妫€鏌ョ被鍨嬫槸鍚﹀凡娉ㄥ唽
function IsTypeRegistered(const TypeId: string): Boolean;
end;
implementation
{ TConfigObjectRegistry }
constructor TConfigObjectRegistry.Create;
begin
inherited Create;
FMetadata := TDictionary<string, TConfigObjectMeta>.Create;
FConfigTypes := TDictionary<string, TConfigObjectClass>.Create;
end;
destructor TConfigObjectRegistry.Destroy;
var
Meta: TConfigObjectMeta;
begin
// 娓呯悊鍏冩暟鎹璞?
for Meta in FMetadata.Values do
Meta.Free;
FMetadata.Free;
FConfigTypes.Free;
inherited;
end;
procedure TConfigObjectRegistry.RegisterConfigType(const TypeId: string;
ConfigClass: TConfigObjectClass; const Meta: TConfigObjectMeta);
begin
// 妫€鏌ユ槸鍚﹀凡娉ㄥ唽
if FConfigTypes.ContainsKey(TypeId) then
begin
// 鏇存柊鐜版湁绫诲瀷
if FMetadata.ContainsKey(TypeId) then
FMetadata[TypeId].Free;
end;
// 娉ㄥ唽绫诲瀷鍜屽厓鏁版嵁
FConfigTypes.AddOrSetValue(TypeId, ConfigClass);
FMetadata.AddOrSetValue(TypeId, Meta);
end;
function TConfigObjectRegistry.GetConfigMeta(const TypeId: string): TConfigObjectMeta;
begin
if not FMetadata.TryGetValue(TypeId, Result) then
Result := nil;
end;
function TConfigObjectRegistry.CreateConfig(const TypeId, Name: string): TConfigObject;
var
ConfigClass: TConfigObjectClass;
ConfigId: string;
ConfigName: string;
Meta: TConfigObjectMeta;
begin
Result := nil;
// 鏌ユ壘娉ㄥ唽鐨勭被鍨?
if not FConfigTypes.TryGetValue(TypeId, ConfigClass) then
Exit;
// 鐢熸垚閰嶇疆ID
ConfigId := GenerateConfigID(TypeId);
// 纭畾鍚嶇О
if Name = '' then
ConfigName := TypeId + '_' + ConfigId
else
ConfigName := Name;
// 鑾峰彇鍏冩暟鎹腑鐨凜onfigType
Meta := GetConfigMeta(TypeId);
if Meta = nil then
Exit;
// 鍒涘缓閰嶇疆瀵硅薄
Result := ConfigClass.Create(ConfigId, ConfigName, '', TypeId, Meta.ConfigType);
end;
function TConfigObjectRegistry.GetAllConfigTypes: TArray<string>;
begin
Result := FConfigTypes.Keys.ToArray;
end;
function TConfigObjectRegistry.IsTypeRegistered(const TypeId: string): Boolean;
begin
Result := FConfigTypes.ContainsKey(TypeId);
end;
function TConfigObjectRegistry.GenerateConfigID(const TypeId: string): string;
var
GUID: TGUID;
begin
// 鐢熸垚GUID
CreateGUID(GUID);
// 杞崲涓虹煭瀛楃涓?
Result := Copy(GUIDToString(GUID).Replace('{', '').Replace('}', '').Replace('-', ''), 1, 12);
end;
end.