Access to 'main' stuff from a module

Got an idea for enhancing PureBasic? New command(s) you'd like to see?
infratec
Always Here
Always Here
Posts: 7618
Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany

Access to 'main' stuff from a module

Post by infratec »

Again I had to solve a problem with a module and an enumeration, which should extend an enumeration (event) from the main program.

I solved it at least with a variable which is set from the main program.
A nicer solution would be to add a main:: 'namespace' to allow access to all global stuff of the main code.

Maybe then it is possible to do something like this:

Code: Select all

DeclareModule XYZ

Enumeration main::event
  #PostEvent_FromModuleXYZ
Endenumeration
...
Last edited by infratec on Thu Nov 14, 2024 9:52 pm, edited 1 time in total.
Fred
Administrator
Administrator
Posts: 18220
Joined: Fri May 17, 2002 4:39 pm
Location: France
Contact:

Re: Access to 'main' stuff from a module

Post by Fred »

You should be able to do a 'Common' module to put all your common stuff between modules and main, and then use UseModule Common in your module which needs it and UseModule Common in the main file.

Code: Select all

DeclareModule Common
  #PB_CustomEvent = 5
EndDeclareModule

Module Common
EndModule

DeclareModule MyModule
EndDeclareModule

Module MyModule
  UseModule Common
  Debug #PB_CustomEvent
EndModule

UseModule Common

Debug #PB_CustomEvent
infratec
Always Here
Always Here
Posts: 7618
Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany

Re: Access to 'main' stuff from a module

Post by infratec »

I know this solution, but ...

I had to extend and older very large program.
I had to add a server plugin.

It is simply to much work to rewrite the old large program to use a common module, because of other pbi files which were done without module.

And I'm not sure if I can use named Enumerations with the common module.
I think it is not possible.
User avatar
ChrisR
Addict
Addict
Posts: 1466
Joined: Sun Jan 08, 2017 10:27 pm
Location: France

Re: Access to 'main' stuff from a module

Post by ChrisR »

You can use named Enumerations with the common module, as long as UseModule Common is done before Enumerations

Code: Select all

DeclareModule Common
  Enumeration Name
  EndEnumeration
  Debug "DeclareModule Common: " + #PB_Compiler_EnumerationValue
EndDeclareModule

Module Common
  Enumeration Name
    #NameCommon
  EndEnumeration
  Debug "Module Common: " + #NameCommon
EndModule

DeclareModule MyModule
  UseModule Common
  Enumeration Name
    #NameDeclareMyModule
  EndEnumeration
  Debug "DeclareModule MyModule: " + #NameDeclareMyModule
EndDeclareModule

Module MyModule
  Enumeration Name
    #NameMyModule
  EndEnumeration
  Debug "Module MyModule: " + #NameMyModule
EndModule

UseModule Common
Enumeration Name
  #NameMain
EndEnumeration
Debug "Main: " + #NameMain
infratec
Always Here
Always Here
Posts: 7618
Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany

Re: Access to 'main' stuff from a module

Post by infratec »

Ah ...

a modified clearing example:

Code: Select all

DeclareModule Common
  Enumeration Name
  EndEnumeration
EndDeclareModule

Module Common
  Enumeration Name
    #Test1
    #NameCommon
  EndEnumeration
  Debug "Module Common: " + #NameCommon
EndModule

DeclareModule MyModule
  UseModule Common
  Enumeration Name
  EndEnumeration
EndDeclareModule

Module MyModule
  Enumeration Name
    #NameMyModule
  EndEnumeration
  Debug "Module MyModule: " + #NameMyModule
EndModule

UseModule Common

Enumeration Name
  #Test2
  #NameMain
EndEnumeration
Debug "Main: " + #NameMai
You have to use an empty Enumeration in every Declare part of a module.
DarkDragon
Addict
Addict
Posts: 2345
Joined: Mon Jun 02, 2003 9:16 am
Location: Germany
Contact:

Re: Access to 'main' stuff from a module

Post by DarkDragon »

Is this propagated enumeration really necessary? If it's for Gadgets or PB objects use PB_Any and you're not depending on Enum propagation anymore. Or use a Hashmap to generate event numbers during runtime. In ERPs there's one instance managing number circles like bill numbers or similar.
bye,
Daniel
User avatar
ChrisR
Addict
Addict
Posts: 1466
Joined: Sun Jan 08, 2017 10:27 pm
Location: France

Re: Access to 'main' stuff from a module

Post by ChrisR »

infratec wrote: Fri Nov 15, 2024 7:47 am You have to use an empty Enumeration in every Declare part of a module.
The empty Enumeration seems only required in the common module declaration, to initialize the Enumeration name (Common::Name).
And in other modules declaration, "UseModule Common" seems to be enough.

Actually, I did as you do, with a variable which is set from the main program.
As I use constants and not PB_Any, I'll probably use it now that I'm re-reviewing it.

Code: Select all

DeclareModule Common
  Enumeration Name
  EndEnumeration
EndDeclareModule

Module Common
  Enumeration Name
    #Test1
    #NameCommon
  EndEnumeration
  Debug "Module Common: " + #NameCommon
EndModule

DeclareModule MyModule
  UseModule Common
EndDeclareModule

Module MyModule
  Enumeration Name
    #NameMyModule
  EndEnumeration
  Debug "Module MyModule: " + #NameMyModule
EndModule

UseModule Common
Enumeration Name
  #Test2
  #NameMain
EndEnumeration
Debug "Main: " + #NameMain
infratec
Always Here
Always Here
Posts: 7618
Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany

Re: Access to 'main' stuff from a module

Post by infratec »

DarkDragon wrote: Fri Nov 15, 2024 12:40 pm Is this propagated enumeration really necessary?

I add a module which sends a custom PostEvent().
There are already custom PostEvents() in the old program via

Code: Select all

Enumeration event #PB_Event_FirstCustomValue
So I want to continue the enumeration inside of the module.
User avatar
mk-soft
Always Here
Always Here
Posts: 6245
Joined: Fri May 12, 2006 6:51 pm
Location: Germany

Re: Access to 'main' stuff from a module

Post by mk-soft »

Default Common Module for Teams ... :!: :?:

Code: Select all

DeclareModule Common
  
  Enumeration CustomEvent #PB_Event_FirstCustomValue
    ;
  EndEnumeration
  
  Enumeration CustomEventType #PB_EventType_FirstCustomValue
    ;
  EndEnumeration
  
  Enumeration Windows
    ;
  EndEnumeration
  
  Enumeration Menus
    ;
  EndEnumeration
  
  Enumeration MenuItems
    ;
  EndEnumeration
  
  Enumeration StatusBars
    ;
  EndEnumeration
  
  Enumeration Gadgets
    ;
  EndEnumeration
  
  ; And More
  
EndDeclareModule

Module Common
  
EndModule
My Projects ThreadToGUI / OOP-BaseClass / EventDesigner V3
PB v3.30 / v5.75 - OS Mac Mini OSX 10.xx - VM Window Pro / Linux Ubuntu
Downloads on my Webspace / OneDrive
Post Reply