forked from ericlangedijk/Lemmix
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathStyles.Factory.pas
69 lines (54 loc) · 1.75 KB
/
Styles.Factory.pas
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
unit Styles.Factory;
{$include lem_directives.inc}
interface
uses
Generics.Collections,
Base.Utils,
Prog.Types, Prog.Base,
Styles.Base, Styles.Dos, Styles.User;
type
TStyleFactory = class sealed
private
class var StylePool: TObjectDictionary<string, TStyle>;
public
class procedure Init; static;
class procedure Done; static;
class function CreateStyle(preventPooling: Boolean): TStyle; static;
end;
implementation
{ TStyleFactory }
class procedure TStyleFactory.Init;
begin
StylePool := TObjectDictionary<string, TStyle>.Create([doOwnsValues]);
end;
class procedure TStyleFactory.Done;
var
s: TStyle;
begin
for s in StylePool.Values do
s.IsPooledByFactory := False;
StylePool.Free;
end;
class function TStyleFactory.CreateStyle(preventPooling: Boolean): TStyle;
// preventPooling is needed when we are - for example - building the cache
begin
Result := nil;
if not preventPooling and StylePool.TryGetValue(Consts.StyleName, Result) then
Exit;
case Consts.StyleDef of
TStyleDef.Orig: Result := TDosOrigStyle.Create(Consts.StyleName);
TStyleDef.Ohno: Result := TDosOhNoStyle.Create(Consts.StyleName);
TStyleDef.H93 : Result := TDosH93Style.Create(Consts.StyleName);
TStyleDef.H94 : Result := TDosH94Style.Create(Consts.StyleName);
TStyleDef.X91 : Result := TDosX91Style.Create(Consts.StyleName);
TStyleDef.X92 : Result := TDosX92Style.Create(Consts.StyleName);
TStyleDef.User: Result := TUserStyle.Create(Consts.StyleName);
else
Throw('Unhandled style definition', 'CreateStyle');
end;
if not preventPooling then begin
StylePool.Add(Consts.StyleName, Result);
Result.IsPooledByFactory := True;
end;
end;
end.