Collections

Top  Previous  Next

DFM-Translator > Special assignments > Collections

Another special kind of assignments are collections. Their values are listed as items in angle brackets "<" ... ">". Each item consists of a series of assignments within an item structure that begins with the keyword item and ends with the keyword end. For example, a status bar consists of such items

 

 

  object xxx1: Txxx

  ...

    yyys = <

      item

        zzz = ...

 

      end

      item

        zzz = ...

      end>

  end

 

 

Two concrete examples for this general structure are:

 

  object StatusBar1: TStatusBar          object ClientDataSet1: TClientDataSet        

  ...                                    ...                                          

    Panels = <                             FieldDefs = <                              

      item                                   item                                     

        Alignment = taCenter                   Name = 'Species No'                    

        ...                                    ...                                    

      end                                   end                                       

      item                                  item                                      

        Width = 50                            Name = 'Category'                       

      ...                                    ...                                      

      end>                                  end>                                      

  end                                    end                                          

 

 

 

 

By default, these examples are translated as follows:

 

StatusBar->Panels->Add();                               ClientDataSet1->FieldDefs->Add();                                 

StatusBar->Panels->Items[0]->Alignment = taCenter;      ClientDataSet1->FieldDefs->Items[0]->Name = L"Species No";        

...                                                     ...                                                               

StatusBar->Panels->Add();                               ClientDataSet1->FieldDefs->Add();                                 

StatusBar->Panels->Items[1]->Width = 50;                ClientDataSet1->FieldDefs->Items[1]->Name = L"Category";          

...                                                     ...                                                               

 

The call of the Add methods have to be inserted, because the item's have to be created before values can be assigned to them. But the translated code will work in the first case only, but not in the second, because in contrast to

TFieldDefs has no Add method: This problem again can be solved by defining according functions. These functions are starting with the prefix Get. According to the naming conventions for the other dfm conversion functions, the first name part should be the type name ot the collection. This would be TStatusPanels in the first example and TFieldDefs in the second example and. With item the second name part the resulting functions then are:

 

TStatusPanel* GetTStatusPanelsitem(TStatusPanels* xp, int xiIndex);

TFieldDef* GetTFieldDefsitem(TFieldDefs* xp, int xiIndex);

 

The implementation of the first function, which is defined in d2c_dfm.cpp is:

 

TStatusPanel* GetTStatusPanelsitem(TStatusPanels* xp, int xiIndex)

{

       if(xp->Count == xiIndex)

                xp->Add();

       return  xp->Items[xiIndex];

}

 

With this naming, th following calls are generated:

 

GetTStatusPanelsitem(StatusBar->Panels, 0)->Alignment = taCenter;

GetTStatusPanelsitem(StatusBar->Panels, 0)->Width = 150;

GetTStatusPanelsitem(StatusBar->Panels, 1)->Width = 50;

 

 

GetTFieldDefsitem is defined accordingly, but with a call to AddFieldDef instead of a call to Add.

 

However this naming convention cannot be applied in the case of TActionLists. E.g. for TActionClients there is no property that return the entire collection of items. Only single items with an according index can be accessed.

Therefore, Delphi2Cpp allows additional naming of such get-functions, using the type name of the owner of the collection and depending on your preference by use the name of the property or the general name “Item”. So e.g. for the TStatusBar there are following three equivalent possibilities:

 

TStatusPanel* GetTStatusPanelsitem(TStatusPanels* xp, int xiIndex);

TStatusPanel* GetTStatusBarPanels(TStatusPanels* xp, int xiIndex);

TStatusPanel* GetTStatusBaritem(TStatusPanels* xp, int xiIndex);

 

The first lines of the functions created from these alternative names are then;

 

GetTStatusBarPanels(StatusBar, 0)->Alignment = taCenter;

GetTStatusBaritem(StatusBar, 0)->Width = 84;

 

The complete component is passed as parameter instead of the collection, which is passed, when the first naming is used.

 

If Delphi2Cpp cannot figure out the type of a property, either because the source files are incomplete or because the property is not explicit but only accessible via DefineProperties, then the last two ways of defining assignments can still work.

 

 

 



This page belongs to the Delphi2Cpp Documentation

Delphi2Cpp home  Content