Einzelnen Beitrag anzeigen

Benutzerbild von sakura
sakura

Registriert seit: 10. Jun 2002
Ort: München
11.412 Beiträge
 
Delphi 11 Alexandria
 
#3

Re: TCollection und TCollectionItem

  Alt 18. Okt 2004, 12:29
Von maximov erreichte uns folgende Ergänzung:


Nachtrag zur dpCollection technik:

Absofort kann man mit BinE binäre DFMs genau so gut wie text-DFMs editieren: http://www.delphipraxis.net/internal...ct.php?t=37128


***

dpCollection template!

Templates bieten eine gute möglichkeit typisierte listen und ähnliches zu benutzen und sind somit ein ersatz für generische typen, der aber recht effektiv ist. Es entfallen somit jegliche Casts und fehler, die auf falschen Casts beruhen . Leider bietet delphi nur über umwege die möglichkeit, dies umzusetzen.

Hier wird beschrieben, wie es funktioniert: http://www.dummzeuch.de/delphi/objec...s/deutsch.html

und hier das template für die die dpCollection.pas:
Delphi-Quellcode:
$IFNDEF TYPED_DP_COLLECTION_TEMPLATE}
unit dpCollection_tmpl;

// written by MaxHub (maximov) 10.07.2004

// dpCollection: [url]http://www.delphipraxis.net/topic28945_tcollection+und+tcollectionitem.html[/url]

// thanks to Thomas Mueller for his 'Object Pascal Templates' article
// -> [url]http://www.dummzeuch.de/delphi/object_pascal_templates/deutsch.html[/url]

// thanks to Rossen Assenov for the original narticle 'Templates in Object Pascal'
// -> [url]http://community.borland.com/article/0,1410,27603,00.html[/url]

interface

uses Classes, dpCollection;


type
  _COLLECTION_ITEM_ = TCollectionItem;
{$ENDIF TYPED_DP_COLLECTION_TEMPLATE}

{$IFNDEF TYPED_DP_COLLECTION_TEMPLATE_SECOND_PASS}
type
  _COLLECTION_ = class (TmxJsCollection)
  protected
    function GetItem (const aIndex : Integer) : _COLLECTION_ITEM_;
    procedure SetItem (const aIndex : Integer;
                      const aValue : _COLLECTION_ITEM_);
    
  public
    constructor Create;

    function Add : _COLLECTION_ITEM_;
    function FindItemID (const aID : Integer) : _COLLECTION_ITEM_;
    function Insert (const aIndex : Integer) : _COLLECTION_ITEM_;
    property Items [const aIndex : Integer] : _COLLECTION_ITEM_ read GetItem write SetItem;
  end;


{$ENDIF TYPED_DP_COLLECTION_TEMPLATE_SECOND_PASS}

{$IFNDEF TYPED_DP_COLLECTION_TEMPLATE}
implementation
{$DEFINE TYPED_DP_COLLECTION_TEMPLATE_SECOND_PASS}
{$ENDIF TYPED_DP_COLLECTION_TEMPLATE}

{$IFDEF TYPED_DP_COLLECTION_TEMPLATE_SECOND_PASS}

{ TYPED_DP_COLLECTION }

constructor _COLLECTION_.Create;
begin
 inherited Create(_COLLECTION_ITEM_);
end;

function _COLLECTION_.Add : _COLLECTION_ITEM_;
begin
 Result := _COLLECTION_ITEM_ (inherited Add);
end;

function _COLLECTION_.FindItemID (const aID : Integer) : _COLLECTION_ITEM_;
begin
 Result := _COLLECTION_ITEM_ (inherited FindItemID (aID));
end;

function _COLLECTION_.GetItem (const aIndex : Integer) : _COLLECTION_ITEM_;
begin
 Result := _COLLECTION_ITEM_ (inherited GetItem (aIndex));
end;

function _COLLECTION_.Insert (const aIndex : Integer) : _COLLECTION_ITEM_;
begin
 Result := _COLLECTION_ITEM_ (inherited Insert (aIndex));
end;

procedure _COLLECTION_.SetItem (const aIndex : Integer;
                                const aValue : _COLLECTION_ITEM_);
begin
 inherited SetItem (aIndex, aValue);
end;

{$WARNINGS off}
{$IFNDEF TYPED_DP_COLLECTION_TEMPLATE}
end.
{$ENDIF TYPED_DP_COLLECTION_TEMPLATE}
{$ENDIF TYPED_DP_COLLECTION_TEMPLATE_SECOND_PASS}
{$DEFINE TYPED_DP_COLLECTION_TEMPLATE_SECOND_PASS}
Eine mögliche verwendung könnte so aussehen:
Delphi-Quellcode:
...type
  
  { *** Sample dataformat item object ***}

  TmxCustomItem = class(TCollectionItem)
  private
   ...
  protected

  public
   ...
  published
    // stored properties in variouse data types -- define as much as you need
    property sequence : string read getSequence write SetSequence stored false; // adapter to the binSequence
    property aBoolean : boolean read FaBoolean write FaBoolean;
    property anInteger : integer read FanInteger write FanInteger;
    property anExtended : Extended read FanExtended write FanExtended;
    property anEnum : TFilerFlag read FanEnum write FanEnum;
    property aSet : TFilerFlags read FaSet write FaSet;
    property anImage : TBitmap read FanImage write FanImage stored true;
  end;

    

  {$define TYPED_DP_COLLECTION_TEMPLATE}
type
  _COLLECTION_ITEM_ = TmxCustomItem; // typisierung festlegen
  {$INCLUDE dpCollection_tmpl.pas}   // template inkludieren
  TTypedCollection = _COLLECTION_; // fertige collection sprechenden namen geben

  ...

implementation

{$INCLUDE dpCollection_tmpl.pas}

...
Die collectionklassen TTypedCollection würde somit nach aussen so aussehen, als ob sie explizit für den item-typ TmxCustomItem programmiert worden wäre, was natürlich nicht der fall ist.

Konstrukte folgender art sind somit sofort möglich:
Delphi-Quellcode:
var myCollection:TTypedCollection ;
...
x := myCollection.items[i].anInteger;
Was normalerweise völlig ausgeschlossen wäre

viel spass,
Maximov.
Angehängte Dateien
Dateityp: pas dpcollection_tmpl_632.pas (2,6 KB, 200x aufgerufen)
Daniel W.
Ich bin nicht zurück, ich tue nur so

Geändert von Daniel (11. Jun 2010 um 08:34 Uhr)
  Mit Zitat antworten Zitat