GetOrdProp function details
SetOrdProp Function
GetEnumProp/SetEnumProp Function
GetSetProp/SetSetProp Function
GetObjectProp/SetObjectProp Function
GetStrProp/SetStrProp Function
⊙ GetOrdProp function details
GetOrdProp is a frequently used function in Delphi RTTI. GetOrdProp obtains the object property value based on the TPropInfo pointer of the object handle and object property. Its return value is Longint, which must be forcibly converted to the corresponding attribute type for use.
Function GetOrdProp (Instance: TObject; PropInfo: PPropInfo): Longint;
GetOrdProp calls the TPropInfo. GetProc function pointer to obtain the returned value of the attribute. Its working process is:
If the type of this attribute is class, the returned value is 4 bytes (object handle ).
Otherwise, TTypeData. OrdType is used to obtain the type of the returned value, which is stored in BL.
{TOrdType = (otSByte, otUByte, otSWord, otUWord, otSLong, otULong );}
Check the first byte of TPropInfo. GetProc (note that the first byte of the GetProc pointer ):
If GetProc [0] = $ FF, it indicates that GetProc is field offset;
If GetProc [0] = $ FE, it indicates that GetProc is virtual method offset;
If GetProc [0] <$ FE, GetProc is static method;
Call GetProc after parsing according to different GetProc types.
The symbol information of the returned value (EAX) is corrected based on the Type symbol information stored in BL.
The EAX returned value is EAX/AX/AL based on the size of the type stored in BL.
EAX (AX/AL) is the returned property value.
The compilation code and comments of GetOrdProp are as follows:
function GetOrdProp(Instance: TObject; PropInfo: PPropInfo): Longint;asm PUSH EBX PUSH EDI MOV EDI,[EDX].TPropInfo.PropType ; EDI <- PPTypeInfo MOV EDI,[EDI] ; EDI <- PTypeInfo MOV BL,otSLong ; BL <- otSLong CMP [EDI].TTypeInfo.Kind,tkClass ; if Prop is Class JE @@isClass ; jmp @@isClass XOR ECX,ECX ; ECX <- 0 MOV CL,[EDI].TTypeInfo.Name.Byte[0] ; CL <- Name StrLength MOV BL,[EDI].TTypeInfo.Name[ECX+1].TTypeData.OrdType ; BL <- Prop OrdType @@isClass: MOV ECX,[EDX].TPropInfo.GetProc ; ECX <- GetProc Addr CMP [EDX].TPropInfo.GetProc.Byte[3],$FE ; cmp HiByte(GetProc), $FE MOV EDX,[EDX].TPropInfo.Index ; EDX <- Prop Index JB @@isStaticMethod ; if below $FE JA @@isField ; if is $FF { the GetProc is a virtual method } ; if is $FE MOVSX ECX,CX { sign extend slot offs } ADD ECX,[EAX] { vmt + slotoffs } CALL dword ptr [ECX] { call vmt[slot] } JMP @@final @@isStaticMethod: CALL ECX ; call GetProc directly JMP @@final @@isField: AND ECX,$00FFFFFF ; clear HiByte(GetProc) ADD ECX,EAX ; ECX <- Field Addr MOV AL,[ECX] ; AL <- Field Addr[0] CMP BL,otSWord ; if OrdType < otSWord JB @@final ; Exit MOV AX,[ECX] ; else AX <- Field[0..1] CMP BL,otSLong ; if OrdType < otSLong JB @@final ; Exit MOV EAX,[ECX] ; else EAX <- Field[0..3] @@final: CMP BL,otSLong ; if OrdType >= otSLong JAE @@exit ; Exit CMP BL,otSWord ; if OrdType >= otSWord JAE @@word ; jmp @@word CMP BL,otSByte ; if OrdType = otSByte MOVSX EAX,AL ; AL <- Sign(EAX) JE @@exit ; Exit AND EAX,$FF ; clear HiWord(EAX) JMP @@exit ; Exit @@word: MOVSX EAX,AX ; AX <= Sign(EAX) JE @@exit ; if OrdType = otSWord then Exit AND EAX,$FFFF ; clear HiWord(EAX) @@exit: POP EDI POP EBXend;
The GetOrdProp function is reloaded in TypInfo. pas to replace the PPropInfo parameter with the PropName, which is convenient for programmers to call. In fact, it also calls the GetOrdProp function described above.
function GetOrdProp(Instance: TObject; const PropName: string): Longint;begin Result := GetOrdProp(Instance, FindPropInfo(Instance, PropName));end;
The following is an example of using GetOrdProp:
Self. Width: = Self. Width-GetOrdProp (Self, 'height ');
The preceding statement is equivalent:
Self. Width: = Self. Width-Self. Height;
* The Get ___ Prop series functions introduced later may call this function, or their implementation methods are similar to this function.
SetOrdProp Function
The SetOrdProp function is the inverse process of GetOrdProp. It calls the TPropInfo. SetProc function pointer to set the object property value. The value of the first byte of the SetProc pointer is the same as that of GetProc, indicating that the SetProc is a field offset, a virtual method offset, and a static method.
Procedure SetOrdProp (Instance: TObject; PropInfo: PPropInfo; Value: Longint );
SetOrdProc is also overloaded Based on the attribute name:
Procedure SetOrdProp (Instance: TObject; const PropName: string; Value: Longint );
Since the Assembly Code of SetOrdProp is almost the same as that of GetOrdProp, It is not listed here. As an exercise, try it out:
SetOrdProp (Self, 'height', Self. Height + 10 );
This statement has the following functions:
Self. Height: = Self. Height + 10;
* The Set ___ Prop series functions introduced later may call this function, or their implementation methods are similar to this function.
GetEnumProp/SetEnumProp Function
The GetEnumProp function gets the enumerated string of the enumeration type attribute. It calls GetEnumName to convert the return value of GetOrdProp.
Function GetEnumProp (Instance: TObject; PropInfo: PPropInfo): string;
Function GetEnumProp (Instance: TObject; const PropName: string): string;
The SetEnumProp function uses an enumerative string to set the enumerated property value. It calls GetEnumValue to convert the enumerated string and then calls SetOrdProp to set the property value.
Procedure SetEnumProp (Instance: TObject; PropInfo: PPropInfo;
Const Value: string );
Procedure SetEnumProp (Instance: TObject; const PropName: string;
Const Value: string );
GetSetProp/SetSetProp Function
The GetSetProp function is used to obtain the string value of the set type attribute. It also calls GetOrdProp to obtain the property value, and then calls the SetToString function to convert the value to a string.
Note: If the return value of the GetOrdProp function is Integer, how does it indicate that the collection type of 256 elements can be stored? The answer is: if it is a published set attribute, the set can be up to 4 bytes, that is, up to 32 elements can be stored.
Function GetSetProp (Instance: TObject; PropInfo: PPropInfo;
Brackets: Boolean): string;
Function GetSetProp (Instance: TObject; const PropName: string;
Brackets: Boolean = False): string;
The SetSetProp function is used to set the value of the set type attribute through a string. It first calls the StringToSet function to convert the string to an integer, and then uses the SetOrdProp function to set the attribute value.
Procedure SetSetProp (Instance: TObject; PropInfo: PPropInfo;
Const Value: string );
Procedure SetSetProp (Instance: TObject; const PropName: string;
Const Value: string );
Test: SetSetProp (Self, 'bordericons', '[biSystemMenu]');
GetObjectProp/SetObjectProp Function
The object is actually a pointer, that is, an integer. Therefore, GetObjectProp can directly call GetOrdProp.
The Object specified by the MinClass parameter must belong to a class. If not, nil is returned.
Function GetObjectProp (Instance: TObject; PropInfo: PPropInfo;
MinClass: TClass = nil): TObject;
Function GetObjectProp (Instance: TObject; const PropName: string;
MinClass: TClass = nil): TObject;
SetObjectProp is used to set the object handle of an attribute. The ValidateClass parameter indicates whether to check whether the passed object type is compatible with the class information of the property information.
Procedure SetObjectProp (Instance: TObject; PropInfo: PPropInfo;
Value: TObject; ValidateClass: Boolean = True );
Procedure SetObjectProp (Instance: TObject; const PropName: string;
Value: TObject );
Example:
Var
MyFont: TFont;
Begin
MyFont: = TFont. Create;
MyFont. Height: = 20;
SetObjectProp (Self, 'font', MyFont );
End;
GetStrProp/SetStrProp Function
The GetStrProp function is used to obtain attribute values of the string type.
Function GetStrProp (Instance: TObject; PropInfo: PPropInfo): string;
Function GetStrProp (Instance: TObject; const PropName: string): string;
Since Delphi supports three types of strings, GetStrProp calls three functions to obtain the string attribute values based on the string type:
Case PropInfo ^. PropType ^. Kind
TkString: getinclustrpropaslongstr (Instance, PropInfo, Result );
TkLString: GetLongStrProp (Instance, PropInfo, Result );
TkWString: GetWideStrPropAsLongStr (Instance, PropInfo, Result );
End;
Getinclustrpropaslongstr calls getinclustrprop; GetWideStrPropAsLongStr calls GetWideStrProp to convert the types of strings.
The SetStrProp function is used to set the attribute values of the string type. Its implementation method is similar to GetStrProp.
Procedure SetStrProp (Instance: TObject; PropInfo: PPropInfo;
Const Value: string );
Procedure SetStrProp (Instance: TObject; const PropName: string;
Const Value: string );