Preventing PB_Event_FirstCustomValue Clashes Between Independent Libraries

Got an idea for enhancing PureBasic? New command(s) you'd like to see?
Webarion
User
User
Posts: 32
Joined: Tue Sep 14, 2021 8:50 pm

Preventing PB_Event_FirstCustomValue Clashes Between Independent Libraries

Post by Webarion »

Hello! Is there any solution to eliminate conflicts between custom events in different independent libraries?

The authors of various libraries cannot know which libraries the final application developer will use in their product. Therefore, using #PB_Event_FirstCustomValue in a library's Enumeration can eventually lead to conflicts and bugs.

It's possible to create a unified library for dynamic event registration, but ultimately, many developers won't use it. Thus, this problem needs to be solved at the compiler level.

At the compiler level, there should be a general rule for dynamically adding events. I suggest implementing a rule something like this: if the code contains an assignment of the constant Enumeration #PB_Event_FirstCustomValue or something like #LibEvent = #PB_Event_FirstCustomValue, then automatically increment #PB_Event_FirstCustomValue by 1.

Code: Select all

; Independent library number 1 from one author
Enumeration #PB_Event_FirstCustomValue ; the first time this combination is called #PB_Event_FirstCustomValue = 65536
  #Library1_Event1
  #Library1_Event2
  #Library1_Event3
EndEnumeration
; The compiler, when encountering the combination Enumeration #PB_Event_FirstCustomValue, here in EndEnumeration, the compiler must set #PB_Event_FirstCustomValue = #PB_Compiler_EnumerationValue

; ...

; Independent library number 2 from the second author
Enumeration Library2_Event #PB_Event_FirstCustomValue
  #Library2_Event1
  #Library2_Event2
  #Library2_Event3
EndEnumeration

; ...

; Independent library number 3 from the third author
Enumeration #PB_Event_FirstCustomValue ; This is the third challenge of this combination and here #PB_Event_FIRSTCURTOMVALUE should be 65542
  #Library3_Event1
  #Library3_Event2
  #Library3_Event3
EndEnumeration

; ...

; Independent library number 4 from another author
#Library4_Event1 = #PB_Event_FirstCustomValue + 1
; The compiler, encountering the equation #MyConst = #PB_Event_FirstCustomValue, must set #PB_Event_FirstCustomValue + 1
Or, perhaps it would be better to add a #PB_Event_NextCustomValue constant to the compiler, which would have an incrementing value, similar to how #PB_Compiler_EnumerationValue holds a different value after EndEnumeration. Although, this will reduce compatibility with older code. It is better, of course, to increase the value of #PB_Event_FirstCustomValue.

When creating a library and using events, we could set a random number for our custom events:

Code: Select all

Enumeration Event #PB_Event_FirstCustomValue + 300000
  #MyLib_Event1
  #MyLib_Event2
  #MyLib_Event3
EndEnumeration
However, this is not the correct approach. While this method reduces the probability of conflicts between libraries, it does not guarantee their absence. It's still possible that a library from another author might use the same event numbers.
User avatar
idle
Always Here
Always Here
Posts: 5974
Joined: Fri Sep 21, 2007 5:52 am
Location: New Zealand

Re: Preventing PB_Event_FirstCustomValue Clashes Between Independent Libraries

Post by idle »

there is #PB_Compiler_EnumerationValue so that could be used like this, assuming your compiling the libs or modules

Code: Select all

Enumeration #PB_Event_FirstCustomValue ; the first time this combination is called #PB_Event_FirstCustomValue = 65536
  #Library1_Event1
  #Library1_Event2
  #Library1_Event3
EndEnumeration

Enumeration #PB_Event_FirstCustomValue + (#PB_Compiler_EnumerationValue-#PB_Event_FirstCustomValue) ; the first time this combination is called #PB_Event_FirstCustomValue = 65536
  #Library2_Event1
  #Library2_Event2
  #Library2_Event3 
EndEnumeration

Debug #Library1_Event3 
Debug #Library2_Event1
Webarion
User
User
Posts: 32
Joined: Tue Sep 14, 2021 8:50 pm

Re: Preventing PB_Event_FirstCustomValue Clashes Between Independent Libraries

Post by Webarion »

idle wrote: Wed Oct 08, 2025 9:26 pm there is #PB_Compiler_EnumerationValue so that could be used like this, assuming your compiling the libs or modules

Code: Select all

Enumeration #PB_Event_FirstCustomValue ; the first time this combination is called #PB_Event_FirstCustomValue = 65536
  #Library1_Event1
  #Library1_Event2
  #Library1_Event3
EndEnumeration

Enumeration #PB_Event_FirstCustomValue + (#PB_Compiler_EnumerationValue-#PB_Event_FirstCustomValue) ; the first time this combination is called #PB_Event_FirstCustomValue = 65536
  #Library2_Event1
  #Library2_Event2
  #Library2_Event3 
EndEnumeration

Debug #Library1_Event3 
Debug #Library2_Event1
When I create and use my own libraries, there's no problem. However, this is about libraries from different authors that shouldn't conflict with each other.

Your example - it's better not to do it this way, as it could cause event collisions from different third-party libraries, because libraries might have multiple enumerations. A good solution that's compatible with legacy code is to increment #PB_Event_FirstCustomValue at the compiler level, similar to how it's done with #PB_Compiler_EnumerationValue.

Code: Select all

; File "Lib1.pbi" - library from another author
Enumeration #PB_Event_FirstCustomValue
#Library1_Event1
#Library1_Event2
#Library1_Event3
EndEnumeration

Enumeration Lib1_Const
#Lib1_Const1
#Lib1_Const2
EndEnumeration

; ... ... ...

; File "Lib2.pbi" - library from another author
; After #Lib1_Const2 from the first library Lib1.pbi, the following will create event number collisions:
Enumeration #PB_Event_FirstCustomValue + (#PB_Compiler_EnumerationValue-#PB_Event_FirstCustomValue)
#Library2_Event1
#Library2_Event2
#Library2_Event3
EndEnumeration

Debug #Library1_Event3
Debug #Library2_Event1

; ... ... ...

; final product developer's program
XIncludeFile "Lib1.pbi"
XIncludeFile "Lib2.pbi"
XIncludeFile "Lib3.pbi"
XIncludeFile "Lib4.pbi"
XIncludeFile "Lib5.pbi"
Post Reply