Delphi Can

Orjinalini görmek için tıklayınız: Fonksiyon ve Method Tipleri
Şu anda (Arşiv) modunu görüntülemektesiniz. Orjinal Sürümü Görüntüle internal link
Pointer Tipleri:
Bilindiği üzere pointer tipleri; 32 bit platformda 4 byte (32 bit) alan kaplar.64 bit platformada ise 8 byte (64 bit) alan kaplar.

Procedürel Tipler:
32 bit platformda fonksiyon ya da procedure pointer'ı 4 byte (32 bit) alan kaplar.Bir method pointer ise yine 4 byte(32 bit) yer kaplar.Devamında ise 4 byte'lık nesnenin pointerı bulunur.
64 bit platformda fonksiyon ya da procedure pointer'ı 8 byte (64 bit) alan kaplar.Bir method pointer ise yine 8 byte(64 bit) yer kaplar.Devamında ise 8 byte'lık nesnenin pointerı bulunur.

Yukarıdaki açıklamalarda bahsedilen method yapısı System.pas ya da System.hpp uniti içerisinde bulunan TMethod record yapısı ile ifade edilir.
Record'un yapısına bakmaya üşenen programcılar için amme hizmeti;
Delphi
  PMethod = ^TMethod;
 TMethod = record
   Code, Data: Pointer;
 public
   class operator Equal(const Left, Right: TMethod): Boolean; inline;
   class operator NotEqual(const Left, Right: TMethod): Boolean; inline;
   class operator GreaterThan(const Left, Right: TMethod): Boolean; inline;
   class operator GreaterThanOrEqual(const Left, Right: TMethod): Boolean; inline;
   class operator LessThan(const Left, Right: TMethod): Boolean; inline;
   class operator LessThanOrEqual(const Left, Right: TMethod): Boolean; inline;
 end;

C++
typedef TMethod *PMethod;
struct DECLSPEC_DRECORD TMethod
{
public:
void *Code;
void *Data;
static bool __fastcall _op_Equality(const TMethod &Left, const TMethod &Right);
static bool __fastcall _op_Inequality(const TMethod &Left, const TMethod &Right);
static bool __fastcall _op_GreaterThan(const TMethod &Left, const TMethod &Right);
static bool __fastcall _op_GreaterThanOrEqual(const TMethod &Left, const TMethod &Right);
static bool __fastcall _op_LessThan(const TMethod &Left, const TMethod &Right);
static bool __fastcall _op_LessThanOrEqual(const TMethod &Left, const TMethod &Right);
};

TMethod.Code alanında fonksiyon ya da procedure'nin adresi saklanır.
TMethod.Data alanında nesnenin adresi saklanır.

Fonksiyon tiplerine örnek:
Delphi
type 
 THesapla1 = function(X, Y: Integer): Integer;              // 32 bit platformda 4 byte, 64 bit platformda 8 byte yer kaplar.
 THesapla2 = function(X, Y: Integer): Integer of object; // 32 bit platformda 8 byte, 64 bit platformda 16 byte yer kaplar.Yukarıda TMethod yapısı ile ifade edildiğinden bahsedildi.

C++
  typedef int __fastcall (*THesapla1)(int X, int Y);
 typedef int __fastcall (__closure *THesapla2)(int X, int Y);

Sizeof ile kapladığı alana bakılabilir.
 ShowMessage(SizeOf(THesapla1).ToString());
ShowMessage(SizeOf(THesapla2).ToString());

Delphide ki "of object" ile C++ Builder'da ki __closure ifadesi aynı şeydir.

Şimdi kullanıma bakalım;
type
 THesapla1 = function(X, Y: Integer): Integer;

 THesapla2 = function(X, Y: Integer): Integer of object;

 THesapla = class
 public
   function Topla(X, Y: Integer): Integer;
   function Carp(X, Y: Integer): Integer;
   function Cikar(X, Y: Integer): Integer;
 end;

implementation

{$R *.dfm}

function Topla(X, Y: Integer): Integer;
begin
  Result := X + Y;
end;

function Carp(X, Y: Integer): Integer;
begin
  Result := X * Y;
end;

function Cikar(X, Y: Integer): Integer;
begin
  Result := X - Y;
end;

{ THesapla }
function THesapla.Carp(X, Y: Integer): Integer;
begin
  Result := X * Y;
end;

function THesapla.Cikar(X, Y: Integer): Integer;
begin
  Result := X - Y;
end;

function THesapla.Topla(X, Y: Integer): Integer;
begin
  Result := X + Y;
end;


var
 Hesapla: THesapla1;
 Sonuc: Integer;
begin
 Hesapla := Carp; // Topla; Carp; Cikar;
 Sonuc := Hesapla(3, 5);
 ShowMessage(Sonuc.ToString());
end;

var
 Hesapla: THesapla;
 Hesapla2 : THesapla2;
 Method: TMethod;
 Sonuc : Integer;
begin
 Hesapla := THesapla.Create;
 Method.Code := @THesapla.Topla;  // @THesapla.Topla; @THesapla.Topla; @THesapla.Topla;
 Method.Data := @Hesapla;
 Hesapla2 := THesapla2(Method);
 Sonuc := Hesapla2(3,5);
 ShowMessage(Sonuc.ToString());
 Hesapla.Free;
end;

Yanlış hatırlamıyorsam 64 bit platform desteği XE2 ile birlikte gelmişti.
Güzel Konu Teşekkürler.
Ellerinize sağlık. 
Benzer makale serilerini bekliyoruz.
Fırsat buldukça yazmaya çalışacağım.
Çok teşekkürler , yenilerini sabırsızlıkla bekliyor olacağım. Elinize sağlık.