Page 1 of 1

[Implemented] Enumeration Binary

Posted: Mon Feb 16, 2015 1:07 am
by netmaestro
Normal enumerations go up by one or you can set a step, which has to be a constant. A binary enumeration would go up by bit value, allowing for each member of the enumeration to have a bit all to itself.

Code: Select all

Enumeration Binary
  #This     ; 0
  #That     ; 1
  #TheOther ; 2
  #AndOn    ; 4
  #LikeSo   ; 8
EndEnumeration

Re: Enumeration Binary

Posted: Mon Feb 16, 2015 1:11 am
by luis
If you mean this -> http://www.purebasic.fr/english/viewtop ... =3&t=52244

... has been requested some times.

Re: Enumeration Binary

Posted: Mon Feb 16, 2015 1:29 am
by oreopa
Macro is a decent workaround, but +1 tho. I often needed it.

Re: Enumeration Binary

Posted: Mon Feb 16, 2015 9:07 am
by User_Russian

Re: Enumeration Binary

Posted: Mon Feb 16, 2015 12:43 pm
by Justin
+1

Re: Enumeration Binary

Posted: Mon Feb 16, 2015 1:54 pm
by luis
At least seven threads now starting from 2006. :lol:

Re: Enumeration Binary

Posted: Mon Feb 16, 2015 2:56 pm
by Dude
Here's my alternative. Kinda messy I admit, but a lot easier and far less typing than the other methods to specify the constants.

Code: Select all

Macro BinEnum(var)
  Enumeration (#PB_Compiler_EnumerationValue)*2
  EndEnumeration
  #var=(#PB_Compiler_EnumerationValue)/4
EndMacro

Enumeration 1
EndEnumeration

BinEnum(This)
BinEnum(That)
BinEnum(TheOther)
BinEnum(AndOn)
BinEnum(LikeSo)

Debug #This     ; 0
Debug #That     ; 1
Debug #TheOther ; 2
Debug #AndOn    ; 4
Debug #LikeSo   ; 8

Enumeration 1
EndEnumeration

BinEnum(Second)
BinEnum(Bunch)
BinEnum(Now)
BinEnum(Shown)
BinEnum(Here)

Debug #Second ; 0
Debug #Bunch  ; 1
Debug #Now    ; 2
Debug #Shown  ; 4
Debug #Here   ; 8

Re: Enumeration Binary

Posted: Mon Feb 16, 2015 3:49 pm
by Tenaja
Thanks for sharing, Dude!

Re: Enumeration Binary

Posted: Mon Feb 16, 2015 6:07 pm
by skywalk
I use this for autogenerated flags in both directions.

Code: Select all

Macro HASH
  #
EndMacro
Macro ENUM_RESET(n=0)
  Enumeration n
  EndEnumeration
EndMacro
Macro ENUM_x2(n, Name, negative=0)
  ; Create enumeration constant for later use as an index within array()'s
  ; Or as a bit flag to compare error fields.
  ; Positive (0,1,2,4,...) or Negative (0,-1,-2,-4,...) series.
  ; Enumerations support Long, Not Quad.
  CompilerIf #PB_Compiler_EnumerationValue = 0
    Enumeration #PB_Compiler_EnumerationValue
      HASH#Name#n
  CompilerElseIf #PB_Compiler_EnumerationValue = 1
    CompilerIf negative
      Enumeration #PB_Compiler_EnumerationValue Step -1
        HASH#Name#n = -#PB_Compiler_EnumerationValue
    CompilerElse
      Enumeration #PB_Compiler_EnumerationValue
        HASH#Name#n
    CompilerEndIf
  CompilerElse    ; #PB_Compiler_EnumerationValue >= 2
    CompilerIf negative
      Enumeration #PB_Compiler_EnumerationValue Step -1
        HASH#Name#n = (#PB_Compiler_EnumerationValue + 1) * 2
    CompilerElse
        Enumeration #PB_Compiler_EnumerationValue Step 1
          HASH#Name#n = (#PB_Compiler_EnumerationValue - 1) * 2
    CompilerEndIf
  CompilerEndIf
  EndEnumeration
EndMacro

ENUM_RESET(0)
Debug "-- Up --"
ENUM_x2(0, UP, 0): Debug #UP0
ENUM_x2(1, UP, 0): Debug #UP1
ENUM_x2(2, UP, 0): Debug #UP2
ENUM_x2(4, UP, 0): Debug #UP4
ENUM_RESET(0)
Debug "-- Down --"
ENUM_x2(0, DN, 1): Debug #DN0
ENUM_x2(1, DN, 1): Debug #DN1
ENUM_x2(2, DN, 1): Debug #DN2
ENUM_x2(4, DN, 1): Debug #DN4