Menu

[r3136]: / branches / 3.x / Src / USnippetKindInfo.pas  Maximize  Restore  History

Download this file

314 lines (276 with data), 10.1 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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
{
* USnippetKindInfo.pas
*
* Defines a class that provides information about the different snippet types
* enumerated by TSnippetKind. The class can only be instantiated as a singleton
* and can't be freed until the unit is finalised. The class maintains a list of
* objects representing each snippet kind. Similarly, these object cannot be
* freed until the owning object is freed.
*
* The only reason to go through these convolutions instead of simply providing
* some structured constant was to retrofit the snippet kind view item to
* TViewItem. TViewItem expects view items that maintain some value or
* description (in this case a description of the snippet kind) to be
* represented as an object to which TViewItem maintains a reference. This means
* that the lifetime of the object must be greater than any TViewItem. The best
* way to do this seemed to maintain a list of objects, one per snippet kind,
* that can't be destroyed until the program closes and have a lifetime the same
* as the program.
*
* $Rev$
* $Date$
*
* ***** BEGIN LICENSE BLOCK *****
*
* Version: MPL 1.1
*
* The contents of this file are subject to the Mozilla Public License Version
* 1.1 (the "License"); you may not use this file except in compliance with the
* License. You may obtain a copy of the License at http://www.mozilla.org/MPL/
*
* Software distributed under the License is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
* the specific language governing rights and limitations under the License.
*
* The Original Code is USnippetKindInfo.pas
*
* The Initial Developer of the Original Code is Peter Johnson
* (http://www.delphidabbler.com/).
*
* Portions created by the Initial Developer are Copyright (C) 2009 Peter
* Johnson. All Rights Reserved.
*
* Contributor(s)
* NONE
*
* ***** END LICENSE BLOCK *****
}
unit USnippetKindInfo;
interface
uses
// Project
UBaseObjects, USnippets;
type
{
TSnippetKindInfo:
Abstract class that encapsulates a snippet kind. Descendant must make
concrete.
}
TSnippetKindInfo = class(TNoPublicConstructObject)
strict protected
function GetDescription: string; virtual; abstract;
{Read accessor for Description property. To be defined by descendant
classes.
@return Required description.
}
function GetKind: TSnippetKind; virtual; abstract;
{Read accessor for Kind property. To be defined by descendant classes.
@return Required kind.
}
public
property Kind: TSnippetKind read GetKind;
{Snippet kind}
property Description: string read GetDescription;
{Description of snippet kind}
end;
{
TSnippetKindInfoList:
Class that maintains a list of concreate TSnippetKindInfo descendant
objects, one for each snippet kind.
}
TSnippetKindInfoList = class(TNoPublicConstructObject)
strict private
var fItems: array[TSnippetKind] of TSnippetKindInfo;
{Storage for Items[] property}
class var fInstance: TSnippetKindInfoList;
{Stores singleton instance}
class function GetInstance: TSnippetKindInfoList; static;
{Gets an instance of the object. When first first called the object is
created. Subsequent calls return the same instance.
@return Singletom object instance.
}
function GetItem(Kind: TSnippetKind): TSnippetKindInfo;
{Read accessor for Items] property.
@param Kind [in] Specifies required snippet kind.
@return Required snippet kinf information object.
}
strict protected
constructor InternalCreate;
{Class constructor Called by GetInstance. Sets up object and creates all
required TSnippetKindInfo objects.
}
public
destructor Destroy; override;
{Class destructor. Tears down object. Frees all snippet kind instances by
setting their CanDestroy flag before freeing.
}
class property Instance: TSnippetKindInfoList read GetInstance;
{Singleton instance of this object}
property Items[Kind: TSnippetKind]: TSnippetKindInfo read GetItem; default;
{Array of snippets info objects}
end;
implementation
resourcestring
// Snippet kind descriptions
sFreeForm = 'Freeform';
sRoutine = 'Routine';
sConstant = 'Constant';
sTypeDef = 'Type Definition';
const
// Map of snippet kinds onto descriptions
cDescriptions: array[TSnippetKind] of string = (
sFreeForm, sRoutine, sConstant, sTypeDef
);
type
{
TSnippetKindInfoImpl:
Concrete implementation of TSnippetKindInfo that can't be freed except by
owning object.
}
TSnippetKindInfoImpl = class(TSnippetKindInfo)
strict private
var fKind: TSnippetKind; // value of Kind property
strict protected
function GetDescription: string; override;
{Read accessor for Description property.
@return Required description.
}
function GetKind: TSnippetKind; override;
{Read accessor for Kind property.
}
public
var CanDestroy: Boolean; // flag indicating if object can be freed
constructor Create(const Kind: TSnippetKind);
{Class constructor. Creates a non-destructable object.
@param Kind [in] Snippet kind to be represented.
}
procedure FreeInstance; override;
{Override of method that releases object's memory on destruction that
does not release memory unless CanDestroy is true. This effectively means
the instance cannot be freed until CanDestroy is set true.
}
end;
{
TSnippetKindInfoListImpl:
Extension of TSnippetKindInfoList that can't be destroyed except in
finalization section of this unit.
}
TSnippetKindInfoListImpl = class(TSnippetKindInfoList)
public
class var CanDestroy: Boolean;
{Flag that must be true to permit object to be freed}
destructor Destroy; override;
{Class destructor. Only calls inherited destructor to destroy snippet kind
information objects if CanDestroy is True.
}
procedure FreeInstance; override;
{Override of method that releases object's memory on destruction that
does not release memory unless CanDestroy is true. This effectively means
the instance cannot be freed until CanDestroy is set true.
}
end;
{ TSnippetKindInfoList }
destructor TSnippetKindInfoList.Destroy;
{Class destructor. Tears down object. Frees all snippet kind instances by
setting their CanDestroy flag before freeing.
}
var
Kind: TSnippetKind; // loops through all snippet kinds
begin
for Kind := Low(TSnippetKind) to High(TSnippetKind) do
begin
(fItems[Kind] as TSnippetKindInfoImpl).CanDestroy := True;
fItems[Kind].Free;
end;
fInstance := nil;
inherited;
end;
class function TSnippetKindInfoList.GetInstance: TSnippetKindInfoList;
{Gets an instance of the object. When first first called the object is
created. Subsequent calls return the same instance.
@return Singletom object instance.
}
begin
if not Assigned(fInstance) then
// Object created as TSnippetKindInfoListImpl to get the hidden "no-free"
// functionality of that class without exposing CanDestroy flag to wider
// world
fInstance := TSnippetKindInfoListImpl.InternalCreate;
Result := fInstance;
end;
function TSnippetKindInfoList.GetItem(Kind: TSnippetKind): TSnippetKindInfo;
{Read accessor for Items] property.
@param Kind [in] Specifies required snippet kind.
@return Required snippet kinf information object.
}
begin
Result := fItems[Kind];
end;
constructor TSnippetKindInfoList.InternalCreate;
{Class constructor Called by GetInstance. Sets up object and creates all
required TSnippetKindInfo objects.
}
begin
inherited;
// Actually creates TSnippetKindInfoImpl objects
fItems[skFreeform] := TSnippetKindInfoImpl.Create(skFreeForm);
fItems[skRoutine] := TSnippetKindInfoImpl.Create(skRoutine);
fItems[skConstant] := TSnippetKindInfoImpl.Create(skConstant);
fItems[skTypeDef] := TSnippetKindInfoImpl.Create(skTypeDef);
end;
{ TSnippetKindInfoImpl }
constructor TSnippetKindInfoImpl.Create(const Kind: TSnippetKind);
{Class constructor. Creates a non-destructable object.
@param Kind [in] Snippet kind to be represented.
}
begin
inherited InternalCreate;
fKind := Kind;
CanDestroy := False;
end;
procedure TSnippetKindInfoImpl.FreeInstance;
{Override of method that releases object's memory on destruction that does not
release memory unless CanDestroy is true. This effectively means the instance
cannot be freed until CanDestroy is set true.
}
begin
if CanDestroy then
inherited FreeInstance;
end;
function TSnippetKindInfoImpl.GetDescription: string;
{Read accessor for Description property.
@return Required description.
}
begin
Result := cDescriptions[Kind];
end;
function TSnippetKindInfoImpl.GetKind: TSnippetKind;
{Read accessor for Kind property.
}
begin
Result := fKind;
end;
{ TSnippetKindInfoListImpl }
destructor TSnippetKindInfoListImpl.Destroy;
{Class destructor. Only calls inherited destructor to destroy snippet kind
information objects if CanDestroy is True.
}
begin
if CanDestroy then
inherited;
end;
procedure TSnippetKindInfoListImpl.FreeInstance;
{Override of method that releases object's memory on destruction that
does not release memory unless CanDestroy is true. This effectively means
the instance cannot be freed until CanDestroy is set true.
}
begin
if CanDestroy then
inherited;
end;
initialization
finalization
// Destroy the TSnippetKindInfoList singleton
TSnippetKindInfoListImpl.CanDestroy := True;
TSnippetKindInfoListImpl.Instance.Free;
end.
Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.