Are there any optimizations to get the return value in Delphi?

I am trying to find an elegant way to access the fields of some objects in some other part of my program using a record that stores a byte and access to the fields of another record using functions with the same name as the record field.

TAilmentP = Record // actually a number but acts like a pointer
private
  Ordinal: Byte;
public
  function Name: String; inline;
  function Description: String; inline;
  class operator Implicit (const Number: Byte): TAilmentP; inline;
End;

 TSkill = Class
   Name: String;
   Power: Word;
   Ailment: TAilmentP;
 End;

class operator TAilmentP.Implicit (const Number: Byte): TAilmentP;
begin
  Result.Ordinal := Number;
  ShowMessage (IntToStr (Integer (@Result))); // for release builds
end;

function StrToAilment (const S: String): TAilmentP; // inside same unit
var i: Byte;
begin
  for i := 0 to Length (Ailments) - 1 do
    if Ailments [i].Name = S then
    begin
      ShowMessage (IntToStr (Integer (@Result))); // for release builds
      Result := i; // uses the Implicit operator
      Exit;
    end;
  raise Exception.Create ('"' + S + '" is not a valid Ailment"');
end;

, , , TAilmentP, "". , , , , "" TAilmentP , , - , .

, , , , "" .

/? . (), ? ( , ), ? , ? .

[EDIT] Implicit ( "Debug Information" ).

add al, [eax] /* function entry */
push ecx
mov [esp], al /* copies Byte parameter to memory */
mov eax, [esp] /* copies stored Byte back to register; function exit */
pop edx
ret

, , mov eax, eax . .: P , , .

, [esp] - Result, , , . [esp] [ebp- $01] ( ) [ebp- $02] ( Byte) , [ebp- $02] AL ( [ebp- $01]), mov [epb- $02].

- , Delphi ?

+3
3

- - , . , "" , .

: 1. , Byte.

, Result, . , - . . , , , .

" " CPU. , . Implicit no-op, EAX.


, , . - , . , , , Self . "". , Self - , .

, , - , " " NRVO. Stack Overflow. inlining.

+3

Delphi . ++ OOP. , , . :

, , ... ( New) ""? , . ?

, . , - , , . , . , , .

. , , ( ) . / . ( ), , .

const, . , . , , ... .

, , , Delphi - , . ++ , ( Pascal). . / , . , , , .

. , .

+1

, , , , .

, , , , , TAilmentP ... , , , , (.. , -, , , OUTSIDE, ).

( , , ), , , (, ++, ), , - .

procedure ShowAilmentName (Ailment: TAilmentP);
begin
  ShowMessage (Ailment.Name);
end;

[...]
begin
ShowAilmentName (5);
end.

, , .: D , 5, , TAilmentP ( TAilmentP ) , Ailment, , , TAilmentP, , . , Ailment const, ( ).

++ . TAilmentP, Byte. Delphi, , , ++ , Delphi , (Byte, Integer ..), . , " ShowAilmentName (Number: Byte)"; "ShowAilmentName (SomeAilment)" ++, Delphi .

So, I assume that this is a side effect of the Implicit statement, which is also similar to a constructor, and this is necessary because records cannot have prototypes (thus, you cannot convert both methods and the other between two records, just using constructors) . Does anyone else think this could be the reason?

0
source

Source: https://habr.com/ru/post/1729001/


All Articles