Recent

Author Topic: Register Class - Runtime  (Read 3183 times)

J-23

  • Full Member
  • ***
  • Posts: 108
Register Class - Runtime
« on: June 07, 2020, 02:26:38 am »
Hello.
I need to register a class - I don't know these classes in advance, they are known at run time. Here is a problem because lazarus does not support RTTI - FindType. How else can you do that someone has an idea.

eljo

  • Sr. Member
  • ****
  • Posts: 468
Re: Register Class - Runtime
« Reply #1 on: June 07, 2020, 02:38:52 am »
Hello.
I need to register a class - I don't know these classes in advance,
That's not possible. Can you elaborate on your design a bit more?
they are known at run time. Here is a problem because lazarus does not support RTTI - FindType. How else can you do that someone has an idea.

The idea is to force the classes to register them selfs at run time after you set which function they suppose to use to register them self.

As I said it depends heavily on your design and specification.

J-23

  • Full Member
  • ***
  • Posts: 108
Re: Register Class - Runtime
« Reply #2 on: June 07, 2020, 03:57:30 am »
Hi
I create a component equivalent to the component palette in Lazarus. The component has its editor where when you drag it to the form you will be able to select the elements available for use

I want to use functions https://www.freepascal.org/docs-html/rtl/classes/registerclass.html , but there is no FindType in RTTI lazarus which prevents me from using this function so I am looking for an idea how it could be implemented differently

I wonder how the IDE editor does that it knows which classes are registered and which are not when loading the file.

If something else is not understood then ask, I will try to write back

Martin_fr

  • Administrator
  • Hero Member
  • *
  • Posts: 9869
  • Debugger - SynEdit - and more
    • wiki
Re: Register Class - Runtime
« Reply #3 on: June 07, 2020, 04:07:49 am »
Well the package itself knows what it contains. So the package itself can have a method that does not need to search, but rather has a hardcoded reference for each included class.
That method it the package, which knows it all, is called: Register.

Check out a package, in the package options you can specify if and which unit has the register method.

Each package also has an automatically created unit. That contains a "uses" for all classes.
And in that unit you will find
Code: Pascal  [Select][+][-]
  1. procedure Register;
  2. begin
  3.   RegisterUnit('SynEdit', @SynEdit.Register);
  4. end;
  5.  
  6. initialization
  7.   RegisterPackage('SynEdit', @Register);
  8.  

So the IDE knows:
- there is a package SynEdit
- One of its units is called SynEdit too.
  And that unit has a register method.

The IDE will call this method, and this method then calls RegisterClass or RegisterComponent.

The register procedure in the unit SynEdit is hand written. And it know (hardcoded) which classes to register.



Note: The IDE also has the configuration which classes are actually added to he IDE.

So if SynEdit was not added, but used by another package (the other package being added), then the IDE knows.
SynEdit would still do RegisterPackage. But the IDE would not look for the name "SynEdit" so the entry would be ignored.
The IDE would in that case not call the registered "Register" method.
« Last Edit: June 07, 2020, 04:11:17 am by Martin_fr »

eljo

  • Sr. Member
  • ****
  • Posts: 468
Re: Register Class - Runtime
« Reply #4 on: June 07, 2020, 09:34:43 am »
Hi
I create a component equivalent to the component palette in Lazarus. The component has its editor where when you drag it to the form you will be able to select the elements available for use

I want to use functions https://www.freepascal.org/docs-html/rtl/classes/registerclass.html , but there is no FindType in RTTI lazarus which prevents me from using this function so I am looking for an idea how it could be implemented differently

I wonder how the IDE editor does that it knows which classes are registered and which are not when loading the file.

If something else is not understood then ask, I will try to write back
Let me make sure I understand the problem. Your problem is that you can't search the existing class list? In that case use the FindClass or GetClass function.

The case of registering your own classes has already been address by @Martin_fr already.

In any other case please provide more info.

PascalDragon

  • Hero Member
  • *****
  • Posts: 5481
  • Compiler Developer
Re: Register Class - Runtime
« Reply #5 on: June 07, 2020, 01:06:15 pm »
I want to use functions https://www.freepascal.org/docs-html/rtl/classes/registerclass.html , but there is no FindType in RTTI lazarus which prevents me from using this function so I am looking for an idea how it could be implemented differently

Even if FPC had FindType that wouldn't help you, because if {$SmartLinkRTTI} is supplied than only classes which are used by code would be linked in. So that brings us to the next point:

Quote
I wonder how the IDE editor does that it knows which classes are registered and which are not when loading the file.

Because every packages that installs components has a Register procedure that is called by the IDE which registers the component using RegisterComponents. Alternatively you can use RegisterClass. And yes, for Delphi that is the same.

J-23

  • Full Member
  • ***
  • Posts: 108
Re: Register Class - Runtime
« Reply #6 on: June 07, 2020, 01:13:38 pm »
Hello.
I need to register a class - I don't know these classes in advance,
That's not possible. Can you elaborate on your design a bit more?
they are known at run time. Here is a problem because lazarus does not support RTTI - FindType. How else can you do that someone has an idea.

The idea is to force the classes to register them selfs at run time after you set which function they suppose to use to register them self.

As I said it depends heavily on your design and specification.

I wrote a component that is a "component palette" (similar to that in the IDE)

This component has the ability to customize which components will be visible for use (selected from those installed in Lazarus)

Now I have to build a mechanism for creating these components, it will be drag & drop (similar to lazarus)

The following code creates a control but requires that you specify a "control class" in advance


Code: Pascal  [Select][+][-]
  1.  type
  2.     TControlClass = class of TControl;
  3.  
  4.   function CreateControl(ControlClass: TControlClass; const ControlName: string;
  5.     X, Y, W, H: integer): TControl;
  6.   begin
  7.     Result := ControlClass.Create(FOwnerComponent);
  8.     with Result do
  9.     begin
  10.       Parent := (FOwnerComponent as TWinControl);
  11.       Name := ControlName;
  12.       SetBounds(X, Y, W, H);
  13.       Visible := True;
  14.     end;
  15.   end;                                                    
  16.  
I wonder how to create a factory here that could indicate the appropriate class based on the name

But for this class factory to work, I would need a list of classes for registered components in Lazarus (Is this achievable)

Maybe you have another idea how to solve it

Martin_fr

  • Administrator
  • Hero Member
  • *
  • Posts: 9869
  • Debugger - SynEdit - and more
    • wiki
Re: Register Class - Runtime
« Reply #7 on: June 07, 2020, 02:24:33 pm »
Is your code going to run in the IDE (Lazarus), or in its own?

Own its own, you probably need to look at installing a handler for getting RegisterComponent calls:
    RegisterComponentsProc:=@RegisterComponentsGlobalHandler;


In the IDE, use package IdeIntf
IDEComponentPalette.FindComponent

both of that works for anything registered with RegisterComponent.


For RegisterClass, the unit "Classes" provides FindClass/GetClass that searches the list.

J-23

  • Full Member
  • ***
  • Posts: 108
Re: Register Class - Runtime
« Reply #8 on: June 07, 2020, 03:22:12 pm »
Is your code going to run in the IDE (Lazarus), or in its own?

will work in my environment but will also be configured with the Lazarus IDE

Own its own, you probably need to look at installing a handler for getting RegisterComponent calls:
    RegisterComponentsProc:=@RegisterComponentsGlobalHandler;

I'll check this lead, thanks

In the IDE, use package IdeIntf
IDEComponentPalette.FindComponent

both of that works for anything registered with RegisterComponent.
yes I know it and use it.

For RegisterClass, the unit "Classes" provides FindClass/GetClass that searches the list.

yes i know it contains FindClass/GetClass and you can use it. The problem is where to get the list of available classes, but I will try with what you wrote above.

eljo

  • Sr. Member
  • ****
  • Posts: 468
Re: Register Class - Runtime
« Reply #9 on: June 07, 2020, 04:05:38 pm »
Is your code going to run in the IDE (Lazarus), or in its own?

will work in my environment but will also be configured with the Lazarus IDE
Do you support dynamic modules or everything is precompiled in a single monolithic exe?

PascalDragon

  • Hero Member
  • *****
  • Posts: 5481
  • Compiler Developer
Re: Register Class - Runtime
« Reply #10 on: June 07, 2020, 09:21:57 pm »
For RegisterClass, the unit "Classes" provides FindClass/GetClass that searches the list.

yes i know it contains FindClass/GetClass and you can use it. The problem is where to get the list of available classes, but I will try with what you wrote above.

For FindClass/GetClass to work you need to use RegisterClass. There is no auto-magic for that.

J-23

  • Full Member
  • ***
  • Posts: 108
Re: Register Class - Runtime
« Reply #11 on: June 08, 2020, 09:17:15 pm »
Is your code going to run in the IDE (Lazarus), or in its own?

Own its own, you probably need to look at installing a handler for getting RegisterComponent calls:
    RegisterComponentsProc:=@RegisterComponentsGlobalHandler;

RegisterComponentsProc is called when the Component is registered in the IDE

Could you elaborate on how I can use this for an IDE in Runtime?

J-23

  • Full Member
  • ***
  • Posts: 108
Re: Register Class - Runtime
« Reply #12 on: June 08, 2020, 09:24:49 pm »
Is your code going to run in the IDE (Lazarus), or in its own?

will work in my environment but will also be configured with the Lazarus IDE
Do you support dynamic modules or everything is precompiled in a single monolithic exe?

My goal is to create components that will allow you to build a form designer. These components will probably publish over time.

I have a plan to use it in my project.

eljo

  • Sr. Member
  • ****
  • Posts: 468
Re: Register Class - Runtime
« Reply #13 on: June 09, 2020, 09:52:52 am »
Is your code going to run in the IDE (Lazarus), or in its own?

will work in my environment but will also be configured with the Lazarus IDE
Do you support dynamic modules or everything is precompiled in a single monolithic exe?

My goal is to create components that will allow you to build a form designer. These components will probably publish over time.

I have a plan to use it in my project.
if I remember correctly, I haven't spend too much time on the subject, there are 2 packages that support runtime designers for lcl already. One is the jedi design package and the other one comes from the cindy controls, but that is not what I asked for.

What I essentially asked is if you have two or more class hierachies in your environment, knowing that fpc and lazarus do not support dynamic packages at the moment.
If you have multiple hierachies then for each dll you need to replace the registerclass funtion with the one that is in your exe before calling the registerclass from the dll code so all classes end up in a common list, instead one list for each dll.

Also since, is/as and inherits, are using pointer comparison you need to write your own versions of those functions to avoid false negatives. It might be easier and faster to convert your class hierarchy to a interface based design or wait for a couple of years for the next fpc release, hopping that pascaldragon will finish the dynamic package support in time for it.
« Last Edit: June 09, 2020, 09:54:49 am by eljo »

J-23

  • Full Member
  • ***
  • Posts: 108
Re: Register Class - Runtime
« Reply #14 on: June 09, 2020, 06:19:33 pm »
The components that will be available at the time of the exe compilation will be available

I now have exactly the same problem as described in this German forum:
https://www.delphipraxis.net/44581-wie-ermittelt-man-welche-komponenten-einem-package-sind.html

(So how to get a list of class from installed packages)
Unless RegisterComponentProc is allowed to connect to the component registration process, I still need to call RegisterCoponent and I don't know how many there are and how to get them to trigger the Registration process in the runtime.

Only that for Lazarus and I already want to register packages installed in Lazarus

 

TinyPortal © 2005-2018