Can "unused" classes be made available in Delphi XE
Asked Answered
T

2

7

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.

Tayyebeb answered 22/9, 2012 at 21:38 Comment(0)
S
5

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.

Stoffel answered 23/9, 2012 at 0:21 Comment(4)
This is the way to do it. Regarding your final point, not so much more RTTI as more codeDrivel
I see that the scope of STRONGLINKTYPES is GLOBAL. Do you know if there is a local equivalent available as well?Tayyebeb
Just turned it on for my application to check the increase in file size. The exe grew from 8.3MB to 9.9MB. Although this seems like a lot, the benefits outweigh the extra size I think.Tayyebeb
@deColaman, AFAIK there isn't a local scope equivalent to the STRONGLINKTYPESStoffel
M
7

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.

Mellifluent answered 22/9, 2012 at 23:39 Comment(2)
Altough this is indeed possible, it is basically the sum of what I mentioned at the end. Also, it would kind of negate the dynamic part I hoped to achieve through Rtti.Tayyebeb
You would still be able to use RTTI to work with the classes. But I'm not sure why you would want to do that. If everything has a common base class there is really no reason to use RTTI. You would use your internal list to find the classes. When you add a new class nothing needs to be changed in the code that uses the list of classes. To me that is simpler to track what is going on with a register function then to be dependent on a compiler directive.Mellifluent
S
5

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.

Stoffel answered 23/9, 2012 at 0:21 Comment(4)
This is the way to do it. Regarding your final point, not so much more RTTI as more codeDrivel
I see that the scope of STRONGLINKTYPES is GLOBAL. Do you know if there is a local equivalent available as well?Tayyebeb
Just turned it on for my application to check the increase in file size. The exe grew from 8.3MB to 9.9MB. Although this seems like a lot, the benefits outweigh the extra size I think.Tayyebeb
@deColaman, AFAIK there isn't a local scope equivalent to the STRONGLINKTYPESStoffel

© 2022 - 2024 — McMap. All rights reserved.