Page 1 of 1
#PB_Compiler_Event_NextCustomValue; prevents event conflicts
Posted: Sun Oct 19, 2014 7:59 pm
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
Re: #PB_Compiler_Event_NextCustomValue; prevents event confl
Posted: Sun Oct 19, 2014 8:26 pm
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
Re: #PB_Compiler_Event_NextCustomValue; prevents event confl
Posted: Sun Oct 19, 2014 8:33 pm
by davido
+1
Re: #PB_Compiler_Event_NextCustomValue; prevents event confl
Posted: Sun Oct 19, 2014 9:43 pm
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.
Re: #PB_Compiler_Event_NextCustomValue; prevents event confl
Posted: Sun Oct 19, 2014 10:05 pm
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
Re: #PB_Compiler_Event_NextCustomValue; prevents event confl
Posted: Mon Oct 20, 2014 1:55 pm
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
Re: #PB_Compiler_Event_NextCustomValue; prevents event confl
Posted: Mon Oct 20, 2014 2:04 pm
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")
Re: #PB_Compiler_Event_NextCustomValue; prevents event confl
Posted: Mon Oct 20, 2014 4:44 pm
by BasicallyPure
Thanks helpy,
Those are very clever solutions.
I especially like your macro.
BP