#PB_*_FirstCustomValue should work like MacroExpandedCount

Got an idea for enhancing PureBasic? New command(s) you'd like to see?
User avatar
Sicro
Enthusiast
Enthusiast
Posts: 538
Joined: Wed Jun 25, 2014 5:25 pm
Location: Germany
Contact:

#PB_*_FirstCustomValue should work like MacroExpandedCount

Post by Sicro »

Instead of the constants:
  • #PB_Event_FirstCustomValue
  • #PB_EventType_FirstCustomValue
commands that works like "MacroExpandedCount" would be very good:
  • GetFreeEventNumber
  • GetFreeEventTypeNumber
Edit: Fixed typos
Last edited by Sicro on Fri Jul 06, 2018 9:29 pm, edited 1 time in total.
Image
Why OpenSource should have a license :: PB-CodeArchiv-Rebirth :: Pleasant-Dark (syntax color scheme) :: RegEx-Engine (compiles RegExes to NFA/DFA)
Manjaro Xfce x64 (Main system) :: Windows 10 Home (VirtualBox) :: Newest PureBasic version
User avatar
NicTheQuick
Addict
Addict
Posts: 1224
Joined: Sun Jun 22, 2003 7:43 pm
Location: Germany, Saarbrücken
Contact:

Re: #PB_*_FirstCustomValue should work like MacroExpandedCou

Post by NicTheQuick »

+1
The english grammar is freeware, you can use it freely - But it's not Open Source, i.e. you can not change it or publish it in altered way.
User avatar
mk-soft
Always Here
Always Here
Posts: 5335
Joined: Fri May 12, 2006 6:51 pm
Location: Germany

Re: #PB_*_FirstCustomValue should work like MacroExpandedCou

Post by mk-soft »

+1
Small tip

Code: Select all

;-TOP
Macro MyEvent_NextCustomValue
  ($7FFFFFFF - MacroExpandedCount)
EndMacro

Macro MyEventType_NextCustomValue
  ($7FFFFFFF - MacroExpandedCount)
EndMacro

Debug MyEvent_NextCustomValue
Debug MyEvent_NextCustomValue
Debug MyEvent_NextCustomValue

Debug MyEventType_NextCustomValue
Debug MyEventType_NextCustomValue
Debug MYEventType_NextCustomValue
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
Little John
Addict
Addict
Posts: 4519
Joined: Thu Jun 07, 2007 3:25 pm
Location: Berlin, Germany

Re: #PB_*_FirstCustomValue should work like MacroExpandedCou

Post by Little John »

Sorry, I don't understand the reason for this feature request.
Can you please explain it?
User avatar
nco2k
Addict
Addict
Posts: 1344
Joined: Mon Sep 15, 2003 5:55 am

Re: #PB_*_FirstCustomValue should work like MacroExpandedCou

Post by nco2k »

nah, FirstCustomValue should stay as it is. i wouldnt mind a NextCustomValue though. but shouldnt it work like this @mk-soft?

Code: Select all

Macro PB_Event_NextCustomValue
  ($FFFF + MacroExpandedCount)
EndMacro

Macro PB_EventType_NextCustomValue
  ($3FFFF + MacroExpandedCount)
EndMacro
c ya,
nco2k
If OSVersion() = #PB_OS_Windows_ME : End : EndIf
User avatar
Sicro
Enthusiast
Enthusiast
Posts: 538
Joined: Wed Jun 25, 2014 5:25 pm
Location: Germany
Contact:

Re: #PB_*_FirstCustomValue should work like MacroExpandedCou

Post by Sicro »

@mk-soft, @nco2k:
Yes, there are many workarounds for it...
Little John wrote:Sorry, I don't understand the reason for this feature request.
Can you please explain it?
If you don't use third-party include codes, this is how you can solve the problem:

Code: Select all

; Content of include file "whatever1.pbi":
Enumeration CustomEvents #PB_Event_FirstCustomValue
  #myCustomEvent1
  #myCustomEvent2
  #myCustomEvent3
  #myCustomEvent4
EndEnumeration
; End of include file "whatever1.pbi"

; Content of include file "whatever2.pbi":
Enumeration CustomEvents
  #myCustomEvent5
  #myCustomEvent6
EndEnumeration
; End of include file "whatever2.pbi"

; Main code
Debug #myCustomEvent1 ; = #PB_Event_FirstCustomValue
Debug #myCustomEvent2 ; = #PB_Event_FirstCustomValue + 1
Debug #myCustomEvent3 ; = #PB_Event_FirstCustomValue + 2
Debug #myCustomEvent4 ; = #PB_Event_FirstCustomValue + 3
Debug #myCustomEvent5 ; = #PB_Event_FirstCustomValue + 4
Debug #myCustomEvent6 ; = #PB_Event_FirstCustomValue + 5
But if you use third-party include codes, it could look like this:

Code: Select all

; Content of include file "whatever1.pbi":
Enumeration #PB_Event_FirstCustomValue
  #myCustomEvent1
  #myCustomEvent2
  #myCustomEvent3
  #myCustomEvent4
EndEnumeration
; Followed by more code
; End of include file "whatever1.pbi"

; Content of include file "whatever2.pbi":
#Event_whatever = #PB_Event_FirstCustomValue
; Followed by more code
; End of include file "whatever2.pbi"

; Main code
Debug #myCustomEvent1 ; = #PB_Event_FirstCustomValue
Debug #myCustomEvent2 ; = #PB_Event_FirstCustomValue + 1
Debug #myCustomEvent3 ; = #PB_Event_FirstCustomValue + 2
Debug #myCustomEvent4 ; = #PB_Event_FirstCustomValue + 3
Debug #Event_whatever ; = #PB_Event_FirstCustomValue
The events "#myCustomEvent1" and "#Event_whatever" are in conflict in the above code.

If we had the commands wished for in this thread, the problem would not exist:

Code: Select all

; Content of include file "whatever1.pbi":
#myCustomEvent1 = GetFreeEventNumber
#myCustomEvent2 = GetFreeEventNumber
#myCustomEvent3 = GetFreeEventNumber
#myCustomEvent4 = GetFreeEventNumber
; Followed by more code
; End of include file "whatever1.pbi"

; Content of include file "whatever2.pbi":
#Event_whatever = GetFreeEventNumber
; Followed by more code
; End of include file "whatever2.pbi"

; Main code
Debug #myCustomEvent1 ; = #PB_Event_FirstCustomValue
Debug #myCustomEvent2 ; = #PB_Event_FirstCustomValue + 1
Debug #myCustomEvent3 ; = #PB_Event_FirstCustomValue + 2
Debug #myCustomEvent4 ; = #PB_Event_FirstCustomValue + 3
Debug #Event_whatever ; = #PB_Event_FirstCustomValue + 4
But I see now that we have a new problem:

Code: Select all

Enumeration GetFreeEventNumber
  #myCustomEvent1 ; = #PB_Event_FirstCustomValue
  #myCustomEvent2 ; = #PB_Event_FirstCustomValue + 1
  #myCustomEvent3 ; = #PB_Event_FirstCustomValue + 2
  #myCustomEvent4 ; = #PB_Event_FirstCustomValue + 3
EndEnumeration

Enumeration GetFreeEventNumber
  #myCustomEvent5 ; = #PB_Event_FirstCustomValue + 1
  #myCustomEvent6 ; = #PB_Event_FirstCustomValue + 2
  #myCustomEvent7 ; = #PB_Event_FirstCustomValue + 3
  #myCustomEvent8 ; = #PB_Event_FirstCustomValue + 4
EndEnumeration
The conflict exists again: The event constants "#myCustomEvent2" and "#myCustomEvent5" have the same value.
The compiler must call the enumeration parameter each line, but as it is currently the compiler doesn't do that.
Image
Why OpenSource should have a license :: PB-CodeArchiv-Rebirth :: Pleasant-Dark (syntax color scheme) :: RegEx-Engine (compiles RegExes to NFA/DFA)
Manjaro Xfce x64 (Main system) :: Windows 10 Home (VirtualBox) :: Newest PureBasic version
Little John
Addict
Addict
Posts: 4519
Joined: Thu Jun 07, 2007 3:25 pm
Location: Berlin, Germany

Re: #PB_*_FirstCustomValue should work like MacroExpandedCou

Post by Little John »

Sicro wrote:But if you use third-party include codes, it could look like this:
I see now. Thanks for your detailed explanation. I didn't think of third-party include codes.
Sicro wrote:But I see now that we have a new problem:
I think with a new function such as GetFreeEventNumber(), things will work reliably only if this function and nothing else will be used for generating event numbers. Enumerations shouldn't be used at all in this context:

Code: Select all

; WRONG:
Enumeration GetFreeEventNumber()
   #MyEvent1
   #MyEvent2
   #MyEvent3
EndEnumeration

; CORRECT:
#MyEvent1 = GetFreeEventNumber()
#MyEvent2 = GetFreeEventNumber()
#MyEvent3 = GetFreeEventNumber()
So some discipline is required, not only when writing the main code, but also with regard to the code located in the include files. Anyway, with such a feature it will be easier than now to write conflict free code. Thanks for the suggestion!

+1
User avatar
ts-soft
Always Here
Always Here
Posts: 5756
Joined: Thu Jun 24, 2004 2:44 pm
Location: Berlin - Germany

Re: #PB_*_FirstCustomValue should work like MacroExpandedCou

Post by ts-soft »

Most problems like this can you solve with my include "CommonConstants"

Code: Select all

CompilerIf Defined(CommonConstants, #PB_Module) = 0
  DeclareModule CommonConstants
    ; Form
    Enumeration FormWindow
    EndEnumeration
    Enumeration FormGadget
    EndEnumeration
    Enumeration FormMenu
    EndEnumeration
    Enumeration FormImage
    EndEnumeration
    Enumeration FormFont
    EndEnumeration
    ; Event
    Enumeration EventCustom #PB_Event_FirstCustomValue
    EndEnumeration
    Enumeration EventTypeCustom #PB_EventType_FirstCustomValue
    EndEnumeration
  EndDeclareModule
  
  Module CommonConstants
  EndModule
CompilerEndIf

UseModule CommonConstants
PureBasic 5.73 | SpiderBasic 2.30 | Windows 10 Pro (x64) | Linux Mint 20.1 (x64)
Old bugs good, new bugs bad! Updates are evil: might fix old bugs and introduce no new ones.
Image
User avatar
Sicro
Enthusiast
Enthusiast
Posts: 538
Joined: Wed Jun 25, 2014 5:25 pm
Location: Germany
Contact:

Re: #PB_*_FirstCustomValue should work like MacroExpandedCou

Post by Sicro »

@ts-soft: Yes, this is also a great workaround for this problem.

Here is my workaround:

Code: Select all

Macro GetFreeEventNumber
  ; MacroExpandedCount is used to reduce the unused value range
  (#PB_Event_FirstCustomValue + #PB_Compiler_Line - MacroExpandedCount)
EndMacro

Enumeration GetFreeEventNumber
  #CustomEvent1
  #CustomEvent2
  #CustomEvent3
EndEnumeration

Enumeration GetFreeEventNumber
  #CustomEvent4
  #CustomEvent5
  #CustomEvent6
EndEnumeration

Debug #CustomEvent1
Debug #CustomEvent2
Debug #CustomEvent3
Debug #CustomEvent4
Debug #CustomEvent5
Debug #CustomEvent6

Debug GetFreeEventNumber
But a nativly solution would be better. because it forces a standard code style.
The problem with workarounds is that some programmers use it and others don't.
Last edited by Sicro on Sun Jul 08, 2018 9:36 am, edited 1 time in total.
Image
Why OpenSource should have a license :: PB-CodeArchiv-Rebirth :: Pleasant-Dark (syntax color scheme) :: RegEx-Engine (compiles RegExes to NFA/DFA)
Manjaro Xfce x64 (Main system) :: Windows 10 Home (VirtualBox) :: Newest PureBasic version
Little John
Addict
Addict
Posts: 4519
Joined: Thu Jun 07, 2007 3:25 pm
Location: Berlin, Germany

Re: #PB_*_FirstCustomValue should work like MacroExpandedCou

Post by Little John »

@ts-soft:
Very good idea, thank you!
Post Reply