was ist neu ¦  programmier tips ¦  indy artikel ¦  intraweb artikel ¦  informationen ¦  links ¦  interviews
 sonstiges ¦  tutorials ¦  Add&Win Gewinnspiel

Tips (1541)

Dateien (137)
Datenbanken (90)
Drucken (35)
Grafik (114)
IDE (21)
Indy (5)
Internet / LAN (130)
IntraWeb (0)
Mathematik (76)
Multimedia (45)
Oberfläche (107)
Objekte/
ActiveX (51)

OpenTools API (3)
Sonstiges (126)
Strings (83)
System (266)
VCL (242)

Tips sortiert nach
Komponente


Tip suchen

Tip hinzufügen

Add&Win Gewinnspiel

Werbung

31 Visitors Online


 
...eine Smart Pointer Klasse implementieren?
Autor: Simon Gomizelj
[ Tip ausdrucken ]  

Tip Bewertung (9):  
     


(*--------------------------------------------------------------------
This class can be used as a delphi variant for a smart pointer. It lacks the similarity to that of a the C++ sort with the lack of operators, but it does the job.
--------------------------------------------------------------------*)
unit Unit2;

interface

uses
  
Classes;

type
  
TSmartPointer = class(TComponent)
  private
    
FPointer: Pointer;
    FReadOnly: Boolean;
    FOwnsMemory: Boolean;
    function GetIsNil: Boolean;
    procedure SetPointer(const Value: Pointer);
  public
    constructor 
Create(AOwner: TComponent); overloadoverride;
    constructor Create(AOwner: TComponent; APointer: Pointer; AOwnsMemory: Boolean = True);
      overload;
    destructor Destroy; override;

    property Memory: Pointer read FPointer write SetPointer;
    property OwnsMemory: Boolean read FOwnsMemory write FOwnsMemory;
    property IsReadOnly: Boolean read FReadOnly write FReadOnly;
    property IsNil: Boolean read GetIsNil;
    procedure FreeMemory;
    function AssignMemory(APointer: Pointer): Boolean;
  end;

implementation

{ TSmartPointer }

function TSmartPointer.AssignMemory(APointer: Pointer): Boolean;
begin
  
Result := False;
  if not FReadOnly then 
  begin
    
FPointer := APointer;
    Result   := (FPointer = APointer);
  end;
end;

constructor TSmartPointer.Create(AOwner: TComponent);
begin
  inherited
;
  FOwnsMemory := True;
  FPointer    := nil;
  FReadOnly   := False;
end;

constructor TSmartPointer.Create(AOwner: TComponent; APointer: Pointer;
  AOwnsMemory: Boolean);
begin
  inherited 
Create(AOwner);
  FOwnsMemory := AOwnsMemory;
  FPointer    := APointer;
  FReadOnly   := False;
end;

destructor TSmartPointer.Destroy;
begin
  if 
FOwnsMemory = True then Dispose(FPointer);
  FPointer := nil;
  inherited;
end;

procedure TSmartPointer.FreeMemory;
begin
  
Dispose(FPointer);
  FPointer := nil;
end;

function TSmartPointer.GetIsNil: Boolean;
begin
  
Result := (FPointer = nil);
end;

procedure TSmartPointer.SetPointer(const Value: Pointer);
begin
  if not 
FReadOnly then FPointer := Value;
end;

end.

(*-------------------------------------------------------------------
Here is a sample form to see its usage:
--------------------------------------------------------------------*)


unit Unit1;

interface

uses
  
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, Unit2, StdCtrls;

type
  
TForm1 = class(TForm)
    Button1: TButton;
    procedure FormCreate(Sender: TObject);
    procedure Button1Click(Sender: TObject);
  private
    
{ Private declarations }
  
public
    
{ Public declarations }
  
end;

var
  
Form1: TForm1;
  ATextPointer: TSmartPointer;

implementation

{$R *.dfm}

procedure TForm1.FormCreate(Sender: TObject);
var
  
A: ^String;
begin
  
New(A);
  ATextPointer := TSmartPointer.Create(Form1, A, True);
  string(ATextPointer.Memory^) := 'a pascal smart pointer stored this text!';
  Self.Caption := string(ATextPointer.Memory^);
end;

procedure TForm1.Button1Click(Sender: TObject);
var
  
APointer: TSmartPointer;
  ACanvas: TCanvas;
begin
  
ACanvas  := TCanvas.Create;
  APointer := TSmartPointer.Create(Form1, @ACanvas, False);
  with TCanvas(APointer.Memory^) do 
  begin
    
Handle      := GetWindowDC(Form1.Handle);
    Brush.Color := clMoneyGreen;
    FillRect(Rect(0, 0, Form1.Width, 23));
    Font.Size  := 10;
    Font.Color := clGreen;
    Font.Style := [fsBold];
    TextOut((Form1.Width div 2) - (TextWidth(string(ATextPointer.Memory^)) div 2),
      3, string(ATextPointer.Memory^));
  end;
end;

end.


 

Bewerten Sie diesen Tipp:

dürftig
ausgezeichnet


Copyright © by SwissDelphiCenter.ch
All trademarks are the sole property of their respective owners