/
BaseMsg.pas
582 lines (482 loc) · 17.6 KB
/
BaseMsg.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
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
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
(*
@Abstract(Base messages unit)
(C) 2003-2007 George "Mirage" Bakhtadze. <a href="http://www.casteng.com">www.casteng.com</a> <br>
The source code may be used under either MPL 1.1 or LGPL 2.1 license. See included license.txt file <br>
Unit contains base message classes and message management classes
*)
{$Include GDefines.inc}
unit BaseMsg;
interface
uses BaseTypes;
const
// Message pool grow step
MessagesCapacityStep = 16;
// Initial capacity of messages pool in bytes
MessagePoolInitialCapacity = 65536;
MessagePoolMaxCapacity = 65536 * 256;
type
(* TMessageDestinationElements = (// Send message to specified recipient. Mutually exclusive with mdChilds.
mdRecipient,
// Send message to childs of specified item. Mutually exclusive with mdBroadcast and mdRecipient.
mdChilds,
// Broadcast message from root item. Mutually exclusive with mdChilds.
mdBroadcast,
// Send message to core handler
mdCore,
// Send message asyncronously
mdAsync);
TMessageDestination = set of TMessageDestinationElements;*)
// Type to use as string type in messages. Do not use in message classes types which need finalization (such as dynamic arrays or long strings) this will cause memory leaks.
TMessageString = ShortString;
{ @Abstract(Base class for all message classes)
Messages are stored in specific pool (see @Link(TMessagePool)) to speed-up allocation and avoid memory leaks. <br>
As a consequence, messages can be created in such way: <i>SomeObject.HandleMessage(TMessage.Create)</i> without risk of a memory leak. <br>
<b>Restriction:</b> Do not use in message classes types which need finalization (such as dynamic arrays or long strings) this will cause memory leaks. Use short strings instead. }
TMessage = class(TObject)
private
FFlags: TMessageFlags;
public
// This method overridden to store messages in specific pool
class function NewInstance: TObject; override;
// If you erroneously deallocate a message manually the overridden implementation of this method will signal you
procedure FreeInstance; override;
// Call this method if you don't want the message to be discarded
procedure Invalidate;
// Message flags
property Flags: TMessageFlags read FFlags write FFlags;
end;
// Message class reference
CMessage = class of TMessage;
// Message pool data structure
TPool = record
Store: Pointer;
Size: Cardinal;
end;
PPool = ^TPool;
{ @Abstract(Message pool class)
The class implements memory management for all instances of @Link(TMessage) and its descendant classes }
TMessagePool = class
private
CurrentPool, BackPool: PPool;
FCapacity: Cardinal;
procedure SetCapacity(ACapacity: Cardinal);
procedure SwapPools;
function Allocate(Size: Cardinal): Pointer;
public
constructor Create;
destructor Destroy; override;
// Begins message handling. Should be called once per main applicatin cycle
procedure BeginHandle;
// Ends message handling and clears messages. Should be called once per main applicatin cycle after <b>BeginHandle</b>
procedure EndHandle;
end;
// Base class for all items. Provides general message handling interface
TBaseItem = class
public
procedure HandleMessage(const Msg: TMessage); virtual; abstract;
end;
// Array of messages
TMessages = array of TMessage;
// Message handler delegate
TMessageHandler = procedure(const Msg: TMessage) of object;
{ @Abstract(Asynchronous messages queue implementation)
The class provides the possibility to handle asynchronous messages. <br>
Message handlers can generate other asynchronous messages which will be handled during next handling cycle.
If you use this class there is no need to call any methods of @Link(TMessagePool). }
TMessageSubsystem = class
private
HandleStarted: Boolean;
BackMessages, Messages: TMessages;
TotalMessages, TotalBackMessages, CurrentMessageIndex: Integer;
procedure SwapPools;
public
{ Locks current message queue. Should be called before message handling cycle. <br>
All asynchronous messages generated during handling will be available during next handling cycle. <br>
Calls @Link(TMessagePool).BeginHandle so application has no need to call it. }
procedure BeginHandle;
// Should be called after handling cycle. Calls @Link(TMessagePool).EndHandle so application has no need to call it
procedure EndHandle;
// Add an asynchronous message to the queue
procedure Add(const Msg: TMessage);
{ Extracts a message from the queue if any, places it to <b>Msg</b> and returns @True if there was a message in queue.
Otherwise returns @False and @nil in <b>Msg</b>. Should be called only between BeginHandle and EndHandle calls. }
function ExtractMessage(out Msg: TMessage): Boolean;
end;
// @Abstract(Base error class)
TError = class(TMessage)
public
// Error message text
ErrorMessage: string;
constructor Create(AErrorMessage: string);
end;
{ Error handler delegate. When an error occurs a delegate of this type is called (See @Link(ErrorHandler)).
If it returns @True the application should try to continue the operation which caused the error. }
TErrorHandler = function(const Error: TError): Boolean of object;
// Base class for notification messages
TNotificationMessage = class(TMessage)
end;
// This message is sent to an object when it should reset its timer if any
TSyncTimeMsg = class(BaseMsg.TNotificationMessage)
end;
// Pause begin message
TPauseMsg = class(TMessage)
end;
// Pause end message
TResumeMsg = class(TMessage)
end;
// Progress report message
TProgressMsg = class(TMessage)
public
// Progress indicator ranging from 0 to 1
Progress: Single;
constructor Create(AProgress: Single);
end;
// Base class for system messages
TSystemMessage = class(TMessage)
end;
// Subsystem metaclass
CSubsystem = class of TBaseSubsystem;
{ @Abstract(Base class for all subsystems)
Subsystem is a set of routines which implements some specific function and can be connected/disconnected or replaced during runtime. <br>
Subsystems are usually arranged in one or more classes/units }
TBaseSubsystem = TBaseItem;
// Subsystem action type for @Link(TSubsystemMsg) message
TSubsystemAction = (// subsystem connected
saConnect,
// subsystem disconnected
saDisconnect);
// When an application receives this message it should shut down as soon as possible
TForceQuitMsg = class(TSystemMessage)
end;
// This message is sent to an <b>application</b> when an option set needs to be applyed (e.g. user clicked "Apply")
TOptionsApplyMsg = class(TSystemMessage)
public
// Option set name to apply
OptionSet: TMessageString;
// AOptionSet is the option set name to apply
constructor Create(const AOptionSet: TMessageString);
end;
// This message is sent to an <b>application</b> when an option set needs to be applyed immediately when a user changes it (without clicking the "Apply" button)
TOptionsPreviewMsg = class(TSystemMessage)
public
OptionName, Value: TMessageString;
constructor Create(const AOptionName, AValue: TMessageString);
end;
// This message is sent to an <b>application</b> when it should be notifyed about a particular option set change
TOptionsApplyNotifyMsg = class(TOptionsPreviewMsg)
end;
// Base class for operating system messages
TOSMessage = class(TMessage)
end;
// This message is sent to an <b>application</b> when its window is about to be activated
TWindowActivateMsg = class(TOSMessage)
end;
// This message is sent to an <b>application</b> when its window is about to be deactivated
TWindowDeactivateMsg = class(TOSMessage)
end;
// This message is sent to an <b>application</b> after its window position has changed
TWindowMoveMsg = class(TOSMessage)
public
NewX, NewY: Single;
// X, Y - new window position in screen coordinates
constructor Create(X, Y: Single);
end;
// This message is sent to an <b>application</b> after its window size has changed
TWindowResizeMsg = class(TOSMessage)
public
OldWidth, OldHeight, NewWidth, NewHeight: Single;
// <b>OldWidth, OldHeight</b> - old size of the window, <b>NewWidth, NewHeight</b> - new size
constructor Create(AOldWidth, AOldHeight, ANewWidth, ANewHeight: Single);
end;
// This message is sent to an <b>application</b> after its window has been minimized
TWindowMinimizeMsg = class(TOSMessage)
end;
// See WM_CANCELMODE (WinAPI)
TCancelModeMsg = class(TOSMessage)
end;
// This message is sent to an <b>application</b> after a command executon from its window menu
TWindowMenuCommand = class(TOSMessage)
public
Command: Integer;
constructor Create(ACommand: Integer);
end;
// ---
// If some data may be referenced by pointer and the pointer to the data has changed this message is <b>broadcasted</b> with new pointer
TDataAdressChangeMsg = class(TNotificationMessage)
public
OldData, NewData: Pointer;
DataReady: Boolean;
// <b>AOldValue</b> - old pointer, <b>ANewValue</b> - new pointer to the data, <b>ADataReady</b> - determines wheter the data is ready to use
constructor Create(AOldValue, ANewValue: Pointer; ADataReady: Boolean);
end;
// This message is <b>broadcasted</b> when some data which may be used by items has modified
TDataModifyMsg = class(TNotificationMessage)
public
// Pointer, identifying the data. usually it's the address of the data in memory
Data: Pointer;
// AData - a pointer, identifying the data. usually it's the address of the data in memory
constructor Create(AData: Pointer);
end;
// Base class for user-input messages
TInputMessage = class(TMessage)
public
constructor Create;
end;
// Base class for mouse-related messages
TMouseMsg = class(TInputMessage)
public
// coordinates of the mouse pointer in screen coordinate system
X, Y: Integer;
// state of some special keys
ModifierState: TKeyModifiers;
// AX, AY - coordinates of the mouse pointer in screen coordinate system, AModifierState - state of some special keys
constructor Create(AX, AY: Integer; AModifierState: TKeyModifiers);
end;
// The message is sent to <b>core handler</b> when the mouse pointer moves
TMouseMoveMsg = class(TMouseMsg)
end;
// Base class for mouse button-related messages
TMouseButtonMsg = class(TMouseMsg)
public
// Button number. usually 1 - left, 2 - right, 4 - middle (see Input.IK_MOUSELEFT etc)
Button: TMouseButton;
// <b>AButton</b> - button number
constructor Create(AX, AY: Integer; AButton: TMouseButton; AModifierState: TKeyModifiers);
end;
// The message is sent to <b>core handler</b> when a mouse button has been pressed
TMouseDownMsg = class(TMouseButtonMsg)
end;
// The message is sent to <b>core handler</b> when a mouse button has been released
TMouseUpMsg = class(TMouseButtonMsg)
end;
// The message is sent to <b>core handler</b> when a mouse button has been clicked
TMouseClickMsg = class(TMouseButtonMsg)
end;
// The message is sent to <b>core handler</b> when a mouse button has been double clicked
TMouseDblClickMsg = class(TMouseButtonMsg)
end;
// Reference to keyboard message class
CKeyboardMsg = class of TKeyboardMsg;
// Base class for keyboard-related messages
TKeyboardMsg = class(TInputMessage)
public
// Scan code of the key
Key: Integer;
// <b>AKey</b> - scan code of the key
constructor Create(AKey: Integer);
end;
// The message is sent to <b>core handler</b> when a key has been pressed
TKeyDownMsg = class(TKeyboardMsg)
end;
// The message is sent to <b>core handler</b> when a key has been released
TKeyUpMsg = class(TKeyboardMsg)
end;
// The message is sent to <b>core handler</b> when a key has been clicked
TKeyClickMsg = class(TKeyboardMsg)
end;
// The message is sent to <b>core handler</b> when a key has been double clicked
TKeyDblClickMsg = class(TKeyboardMsg)
end;
// The message is sent to <b>core handler</b> when a character input has been made
TCharInputMsg = class(TKeyboardMsg)
public
// Code of the character
Character: Char;
// <b>AChar</b> - code of the character, <b>AKey</b> - scan code
constructor Create(AChar: Char; AKey: Integer);
end;
var
MessagePool: TMessagePool;
implementation
{ TMessage }
class function TMessage.NewInstance: TObject;
begin
// Result := InitInstance(MessagePool.Allocate(InstanceSize));
Result := TObject(MessagePool.Allocate(InstanceSize));
PInteger(Result)^ := Integer(Self);
end;
procedure TMessage.FreeInstance;
begin
Assert(False, 'TMessage and descendants should not be freed manually');
end;
procedure TMessage.Invalidate;
begin
Include(FFlags, mfInvalid);
end;
{ TError }
constructor TError.Create(AErrorMessage: string);
begin
ErrorMessage := AErrorMessage;
end;
{ TOptionsApplyMsg }
constructor TOptionsApplyMsg.Create(const AOptionSet: TMessageString);
begin
OptionSet := AOptionSet;
end;
{ TOptionsPreviewMsg }
constructor TOptionsPreviewMsg.Create(const AOptionName, AValue: TMessageString);
begin
OptionName := AOptionName; Value := AValue;
end;
{ TWindowMoveMsg }
constructor TWindowMoveMsg.Create(X, Y: Single);
begin
NewX := X; NewY := Y;
end;
{ TWindowResizeMsg }
constructor TWindowResizeMsg.Create(AOldWidth, AOldHeight, ANewWidth, ANewHeight: Single);
begin
OldWidth := AOldWidth;
OldHeight := AOldHeight;
NewWidth := ANewWidth;
NewHeight := ANewHeight;
end;
{ TWindowMenuCommand }
constructor TWindowMenuCommand.Create(ACommand: Integer);
begin
Command := ACommand;
end;
{ TDataAdressChangeMsg }
constructor TDataAdressChangeMsg.Create(AOldValue, ANewValue: Pointer; ADataReady: Boolean);
begin
OldData := AOldValue;
NewData := ANewValue;
DataReady := ADataReady;
end;
{ TDataModifyMsg }
constructor TDataModifyMsg.Create(AData: Pointer);
begin
Data := AData;
end;
{ TInputMessage }
constructor TInputMessage.Create;
begin
Flags := [mfCore];
end;
{ TMouseMsg }
constructor TMouseMsg.Create(AX, AY: Integer; AModifierState: TKeyModifiers);
begin
inherited Create;
X := AX; Y := AY;
ModifierState := AModifierState;
end;
{ TMouseButtonMsg }
constructor TMouseButtonMsg.Create(AX, AY: Integer; AButton: TMouseButton; AModifierState: TKeyModifiers);
begin
inherited Create(AX, AY, AModifierState);
Button := AButton;
end;
{ TKeyClick }
constructor TKeyboardMsg.Create(AKey: Integer);
begin
inherited Create;
Key := AKey;
end;
{ TCharInputMsg }
constructor TCharInputMsg.Create(AChar: Char; AKey: Integer);
begin
inherited Create(AKey);
Character := AChar;
end;
{ TMessageSubsystem }
procedure TMessageSubsystem.SwapPools;
var t: TMessages;
begin
t := BackMessages;
BackMessages := Messages;
Messages := t;
t := nil;
TotalBackMessages := TotalMessages;
TotalMessages := 0;
end;
procedure TMessageSubsystem.BeginHandle;
begin
HandleStarted := True;
SwapPools;
CurrentMessageIndex := 0;
MessagePool.BeginHandle;
end;
procedure TMessageSubsystem.EndHandle;
begin
Assert(HandleStarted, 'TMessageSubsystem.EndHandle: Invalid call');
HandleStarted := False;
MessagePool.EndHandle;
end;
procedure TMessageSubsystem.Add(const Msg: TMessage);
begin
if Length(Messages) <= TotalMessages then SetLength(Messages, Length(Messages) + MessagesCapacityStep);
Messages[TotalMessages] := Msg;
Inc(TotalMessages);
end;
function TMessageSubsystem.ExtractMessage(out Msg: TMessage): Boolean;
begin // ToDo: Needs testing
Assert(HandleStarted, 'TMessageSubsystem.ExtractMessage: Should be called only between BeginHandle and EndHandle pair');
Msg := nil;
if CurrentMessageIndex < TotalBackMessages then begin
Msg := BackMessages[CurrentMessageIndex];
Inc(CurrentMessageIndex);
end;
Result := Msg <> nil;
end;
{ TMessagePool }
procedure TMessagePool.SetCapacity(ACapacity: Cardinal);
begin
FCapacity := ACapacity;
ReAllocMem(CurrentPool^.Store, ACapacity);
ReAllocMem(BackPool^.Store, ACapacity);
end;
procedure TMessagePool.SwapPools;
var Temp: Pointer;
begin
Temp := BackPool;
BackPool := CurrentPool;
CurrentPool := Temp;
end;
constructor TMessagePool.Create;
begin
New(CurrentPool);
CurrentPool^.Store := nil;
CurrentPool^.Size := 0;
New(BackPool);
BackPool^.Store := nil;
BackPool^.Size := 0;
SetCapacity(MessagePoolInitialCapacity);
end;
destructor TMessagePool.Destroy;
begin
SetCapacity(0);
Dispose(CurrentPool);
Dispose(BackPool);
inherited;
end;
function TMessagePool.Allocate(Size: Cardinal): Pointer;
var NewCapacity: Integer;
begin
Assert(CurrentPool^.Size + Size < MessagePoolMaxCapacity, 'Message pool is full'); // Todo: Handle this situation
if CurrentPool^.Size + Size > FCapacity then begin
NewCapacity := FCapacity + MessagePoolInitialCapacity;
if NewCapacity > MessagePoolMaxCapacity then NewCapacity := MessagePoolMaxCapacity;
SetCapacity(NewCapacity);
end;
Result := Pointer(Cardinal(CurrentPool^.Store) + CurrentPool^.Size);
Inc(CurrentPool^.Size, Size);
end;
procedure TMessagePool.BeginHandle;
begin
SwapPools;
end;
procedure TMessagePool.EndHandle;
begin
BackPool^.Size := 0;
end;
{ TProgressMsg }
constructor TProgressMsg.Create(AProgress: Single);
begin
Progress := AProgress;
end;
initialization
MessagePool := TMessagePool.Create;
finalization
MessagePool.Free;
MessagePool := nil;
end.