Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can "unused" classes be made available in Delphi XE

I'm working in Delphi XE, windows 7.

In an application I want to enable different report types for my users to select. To do this, I have 1 base report class and a subclass per report type (xml, csv, ppt, etc).

{Just an illustrating example}
TBaseReport = class
public
  constructor Create;
  procedure GenerateReport; virtual; abstract;
  class function ReportType: string; virtual; abstract;
end;

T*Report = class(TBaseReport);
//Etcetera.

What I want to do is use Rtti to detect all report classes and list their ReportType. After that, I want to use Rtti to create an instance of the chosen report class and call GenerateReport. All in all, this is not too difficult to achieve.

However there is a major drawback: I'm never hard coding the use of the descending classes, so the code does not get included in the executable.

Is there a decent way to force the linker/compiler to include these classes?

A(n ugly) work around would be to simulate usage of the reports in their initialization section, but I'd rather not do that. A better solution is to make the base class persistent and to call 'RegisterClass(T*Report);' in the initialization section. It works, but I do not see any other need to make them persistent, so again, I'd rather not do that. On the other hand, maybe this is the only way to do it?

Thanks in advance.

like image 807
deColaman Avatar asked Sep 22 '12 21:09

deColaman


2 Answers

You can create your own version of RegisterClass. Something like RegisterReportClass. Internally you keep your own list of report classes that can be used. Your register function will take a TBaseReport class type - No need for TPersistent.

Your RegisterReportClass method should be called in the Initialization section making sure the classes are included.

If you look in the Graphics unit you can see TFileFormatsList = class(TList). This is the class that is used to hold the different Graphic Types and could be used as an example for creating your own TReportFormatsList. Delphi uses a static function TPicture.RegisterFileFormat to add items to their internal list.

like image 70
Mark Elder Avatar answered Oct 21 '22 12:10

Mark Elder


You can use the {$STRONGLINKTYPES ON} Compiler Directive, to include all symbols of your app in the final exe, remember that this option increases the executable size, as more RTTI is included in the executable.

like image 30
RRUZ Avatar answered Oct 21 '22 12:10

RRUZ