#PB_Compiler_Event_NextCustomValue; prevents event conflicts

Got an idea for enhancing PureBasic? New command(s) you'd like to see?
User avatar
BasicallyPure
Enthusiast
Enthusiast
Posts: 536
Joined: Thu Mar 24, 2011 12:40 am
Location: Iowa, USA

#PB_Compiler_Event_NextCustomValue; prevents event conflicts

Post by BasicallyPure »

This could prevent conflicts between custom events
Say your application uses #PB_Event_FirstCustomValue to assign to
one of your custom events.
What if you have an include file that also used #PB_Event_FirstCustomValue
for a different custom event.
They would end up with the same value for both custom events. :(

I propose the use of this: #PB_Compiler_Event_NextCustomValue
On first encounter the compiler would assign the value 65536 (or whatever) to the event,
the same as it would if you were using #PB_Event_FirstCustomValue.
On the next encounter with #PB_Compiler_Event_NextCustomValue the compiler
would assign the next available unused custom value.

It might look like this in practice.

Code: Select all

; first encounter with compiler give same value as #PB_Event_FirstCustomValue
Enumeration #PB_Compiler_Event_NextCustomValue
   #AA ; 65536, same as #PB_Event_FirstCustomValue
   #AB ; 65537
   #AC ; 65538
EndEnumeration

Enumeration
   #BA ; 0
   #BB ; 1
   #BC ; 2
EndEnumeration

Enumeration #PB_Compiler_Event_NextCustomValue
   #CA ; 65539
   #CB ; 65540
   #CC ; 65541
EndEnumeration
We could also do the same thing with custom event types by using:
#PB_Compiler_EventType_NextCustomValue

What do you think?

PB
BasicallyPure
Until you know everything you know nothing, all you have is what you believe.
User avatar
ts-soft
Always Here
Always Here
Posts: 5756
Joined: Thu Jun 24, 2004 2:44 pm
Location: Berlin - Germany

Re: #PB_Compiler_Event_NextCustomValue; prevents event confl

Post by ts-soft »

You can add a module like this:

Code: Select all

DeclareModule Common
  Enumeration CustomEvent #PB_Event_FirstCustomValue 
  EndEnumeration
EndDeclareModule

Module Common
EndModule

UseModule Common

Enumeration CustomEvent
  #MyEvent
EndEnumeration

Debug #MyEvent
You can use it at all, you have only to insert the module and use it with name.

Or you insert this on top:

Code: Select all

Enumeration CustomEvent #PB_Event_FirstCustomValue 
EndEnumeration

Enumeration CustomEvent
  #MyEvent1
  #MyEvent2
EndEnumeration

Debug #MyEvent1
Debug #MyEvent2
Last edited by ts-soft on Sun Oct 19, 2014 8:33 pm, edited 1 time in total.
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
davido
Addict
Addict
Posts: 1890
Joined: Fri Nov 09, 2012 11:04 pm
Location: Uttoxeter, UK

Re: #PB_Compiler_Event_NextCustomValue; prevents event confl

Post by davido »

+1
DE AA EB
uwekel
Enthusiast
Enthusiast
Posts: 740
Joined: Sat Dec 03, 2011 5:54 pm
Location: Oldenburg (Germany)

Re: #PB_Compiler_Event_NextCustomValue; prevents event confl

Post by uwekel »

+1

I had some trouble with this problem, too. Alternatively, custom events could also start with 0 and PB adds the required offset 65536 at compilation time.
PB 5.70 LTS (x64) - Debian Testing, Gnome 3.30.2
User avatar
BasicallyPure
Enthusiast
Enthusiast
Posts: 536
Joined: Thu Mar 24, 2011 12:40 am
Location: Iowa, USA

Re: #PB_Compiler_Event_NextCustomValue; prevents event confl

Post by BasicallyPure »

@ts-soft
That looks like a good working solution.
A small problem is all of your includes would have to be coded with
the same variable, CustomEvent in your example.

I think there would be some advantage to use the one compiler
constant as I describe then you don't have to check through
code that may have been written by someone else to ensure the
custom events are set up properly.

I would even go further and suggest that #PB_Event_FirstCustomValue
and #PB_EventType_FirstCustomValue be eliminated from PureBasic
and only use #PB_Compiler_Event_NextCustomValue and
#PB_Compiler_EventType_NextCustomValue.
(yes I know, this would break old code.)

I'm not sure but it seems to me it would be very easy to make this change.

BP
BasicallyPure
Until you know everything you know nothing, all you have is what you believe.
User avatar
helpy
Enthusiast
Enthusiast
Posts: 552
Joined: Sat Jun 28, 2003 12:01 am

Re: #PB_Compiler_Event_NextCustomValue; prevents event confl

Post by helpy »

A macro to create event constants:

Code: Select all

Macro NewCustomEvent_Constant(__ConstantName__)
  CompilerIf Not Defined(Enumeration_CustomEvent_Values, #PB_Enumeration)
    Enumeration Enumeration_CustomEvent_Values #PB_Event_FirstCustomValue
      #__ConstantName__
    EndEnumeration
  CompilerElse
    Enumeration Enumeration_CustomEvent_Values
      #__ConstantName__
    EndEnumeration
  CompilerEndIf
EndMacro

NewCustomEvent_Constant( Event1 )
NewCustomEvent_Constant( Event2 )
NewCustomEvent_Constant( Event3 )
NewCustomEvent_Constant( Event4 )


Debug #Event1
Debug #Event2
Debug #Event3
Debug #Event4
Windows 10 / Windows 7
PB Last Final / Last Beta Testing
User avatar
helpy
Enthusiast
Enthusiast
Posts: 552
Joined: Sat Jun 28, 2003 12:01 am

Re: #PB_Compiler_Event_NextCustomValue; prevents event confl

Post by helpy »

A module to reserve values for custom events:

Code: Select all

DeclareModule Custom
  Global NewMap CustomEvents.i()
  Declare Event(EventName.s)
EndDeclareModule

Module Custom
  Procedure Event(EventName.s)
    Static NextCustomEventValue = #PB_Event_FirstCustomValue
    If EventName <> ""
      If Not FindMapElement(CustomEvents(), EventName)
        CustomEvents(EventName) = NextCustomEventValue
        NextCustomEventValue + 1
      EndIf
      ProcedureReturn CustomEvents(EventName)
    Else
      ProcedureReturn #False
    EndIf
  EndProcedure
EndModule


Debug Custom::Event("Event1")
Debug Custom::Event("Event2")
Debug Custom::Event("Event3")
Debug Custom::Event("Event4")
Debug "-"
Debug Custom::Event("Event1")
Debug Custom::Event("Event2")
Debug Custom::Event("Event3")
Debug Custom::Event("Event4")
Windows 10 / Windows 7
PB Last Final / Last Beta Testing
User avatar
BasicallyPure
Enthusiast
Enthusiast
Posts: 536
Joined: Thu Mar 24, 2011 12:40 am
Location: Iowa, USA

Re: #PB_Compiler_Event_NextCustomValue; prevents event confl

Post by BasicallyPure »

Thanks helpy,
Those are very clever solutions.
I especially like your macro.

BP
BasicallyPure
Until you know everything you know nothing, all you have is what you believe.
Post Reply