[fpc-devel] reference/pointer to class function/constructor ?

Sven Barth pascaldragon at googlemail.com
Fri Feb 21 17:44:11 CET 2020


Harald Houppermans via fpc-devel <fpc-devel at lists.freepascal.org> schrieb
am Fr., 21. Feb. 2020, 12:37:

> *** Shorter version / less explanation ***:
> (Alternative shorter version):
>
> I want/have the need to do the following, I want one unit as follows:
>
> UnitVersion:
>
> type
>   TVersion =
>   (
>     v1,
>     v2,
>     v3
>   )
>
> I want to create some kind of pluggeable/plugin array to c;ass functions
> like so:
>
> (class functions can be called without creating an object like so
> TSomeObject.ClassFunction;)
>
>   TInitializer = class function;
>
> // could also be a constructor maybe
>   TInitialize = class constructor;
>
>   TInitializerArray = array[TVersion] of TIntializer;
>
> var
>   InitializerArray : TInitializerArray;
>
> Then the user can add certain units to the project yes or no... certain
> versions.
> For example version v1 and v2, but not v3.
>
> Version1 and Version2 should "plug themselfes into this array" like so:
>
> unit version 1
>
> type
>   Tversion1 = class
>     class function Initialize;
>   end;
>
> initialization
>
> UnitVersion.InitializerArray[v1] := TVersion1.Initializer;
>
> unit version 2
>
> type
>   Tversion2 = class
>     class function Initialize;
>   end;
>
> initialization
>
> UnitVersion.InitializerArray[v2] := TVersion2.Initializer;
>
>
> The idea is then that user code can use
>
> UnitVersion.pas and include only version 1 and version 2.
>
> And other code can then automate the initialization without breaking code.
>
> If version 3 were added it would break code, cause version3.pas might not
> be
> present or included in project.
>
> So automation code should work even if a certain unit is not present.
>
> Currently initialization is done with class functions.
>
> Not sure if it's possible to make pointers to class functions.
>
> With method pointers this would be easy/doable.
>
> But now I am not sure if this is possible.
>

If you don't need the initializers to be virtual or have access to the
class type using Self you can declare them as static (class procedure
Initialize; static;), then you can assign them to normal procedure
variables (TInitializer = procedure;).

Regards,
Sven

>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.freepascal.org/pipermail/fpc-devel/attachments/20200221/9a6531d1/attachment.html>


More information about the fpc-devel mailing list