Delphi.net internal implementation analysis (4)
2.4
news
For the overloading method of the class, the most common implementation method is to build a piece.
VTable
Table, each method occupies one
Slot
.
However, this processing method is limited by space and time, and there is a limit to the processing method of processing a large number of methods such as many window messages.
To deal with this contradiction,
MFC
Use macros to define a set of messages that are independent of the class.
ATL
Simply require the compiler in the right
Do not use
VTable
Optimization in this way. and
Delphi
By providing a dynamic method similar to a virtual method
Dynamic Method
To solve this contradiction.
Virtual method
Virtual Method
Save as
VMT
In the middle, generating code is optimized for use efficiency, suitable for completing ordinary inheritance
Realization of the overload method; the dynamic method is saved in a separate table according to a number, and generates code to optimize the program code size.
The most typical example of the most typical example of being aware of a large amount of overloaded defined by the ancestors is not often used.
Windows
Message handler.
So we are defining one
Windows
When the message is handled, it will follow one
Message
Keywords, specify dynamic method numbers.
//-----------------------------------------Forms.Pas--Type TscrollingwinControl = Class (TwinControl) Begin Procedure WMSIZE (VAR message: Twmsize); Message WM_SIZE; ... / / --------------------------- -------------- Forms.Pas -
Can also use
Dynamic
Keyword definition dynamic methods, automatically assign method numbers by the system.
in
Delphi
In this, this dynamic method table is saved.
VMT
of
VMTDYNAMICTABLE
Domain,
Dynamic approach is used when called
TOBJECT.DISPATCH
Method call
GetDynamethod
function
From the dynamic method table, according to the first field number of the incoming message structure, look up the number of dynamic methods,
Call if there is no match number.
TOBJECT.DEFAULTHANDLER
The imaginary method handles anomalies.
In
Delphi.net
in,
Borland
No longer use
VMT
Used as data storage, but in order to provide source code level
Compatibility, only try to simulate
Delphi
Semantic in the middle.
Good
CLR
Provide characteristics
Attribute
This very well-used and useful feature, can be able to static or
Dynamic data and functions are attached to any syntax element. For details, please refer to the books and articles mentioned earlier.
Delphi.net
In the preview,
Attribute
The support is still very weak, but I will test, in
2002-11-14
In the test version, it is already not limited to
Delphi.net
The documentation says can only be used for type, it is estimated official version.
Complete support should be provided when publishing.
Delphi.net
The simulation of the dynamic method is passed
MessageMethodAttribute
Finished.
Use
Dynamic
Define dynamic methods, or use
Message
Compiler when defining message processing functions
Will automatically add one
[MessageMetHodattribute (1)]
Features, and specify numbers.
and
TOBJECTHELPER.DISPATCH
By enumerating the type of all methods, find out
MessageMethodAttribute
Dynamic method of characteristics, according to MessageMethodattribute.ID
Dynamic methods that should be called.
If you are not found, it is called
TOBJECT.DEFAULTHANDLER
Method processing exception. In order to improve find efficiency,
Borland.delphi.system
A cache is also implemented in the unit.
Hash
table
MethodMaps
Cake a search class with its dynamic method list, you can locate it next time you can use it.
//----------------------------------------- ,land.delphi.system.pas --type MessageMethodAttribute = class (TCustomAttribute) private FID: Integer; public constructor Create (AID: Integer); property ID: Integer read FID; end; TObjectHelper = class helper for TObject ... procedure Dispatch (var Message); end; //----------------------------------------- ,land.delphi.system.pas - MessageMethodAttribute
Features define for internal use,
Dyanic
or
Message
Keyword
It will be automatically attached to the corresponding dynamic method when used. Such as
Procedure Sayhello; Dynamic; Procedure: TMessage; Message 1; [MessageMethodAttribute (1)] Procedure: TMESSAGE
These methods are basically equivalent, but only
Dynamic
Keywords do not specify dynamic method numbers.
//----------------------------------------- ,land.delphi.system.pas --function getMessageId (Obj: TOBJECT): Integer; var Field: FieldInfo; Begin Field: = Obj.gettype.Getfields [0]; Result: = Integer (Field.getValue (Obj)); end; // --- -------------------------------------- borland.delphi.system.pas -
Correct
TOBJECT.DISPATCH
In terms, parameters
Message
Variable, but the first field must be one
Integer
,
Indicate this
Message
Needed
Dispatch
Which number of the number is now.
GetMessageID
The function is responsible
use
REFLECTION
Get the message number from a type. Note that there is no abnormal situation to detect, meaning if
Passing invalid types may result in anomalies.
TOBJECTHELPER.DISPATCH
The code is mainly through
MethodMaps
Medium investigation format dynamic method,
If you don't have a cache, use it.
TMETHODMAP.CREATE
The mapping relationship of the type of constructing and the dynamic method is slightly.