[Implemented] Enumeration enhancements

Got an idea for enhancing PureBasic? New command(s) you'd like to see?
User avatar
Michael Vogel
Addict
Addict
Posts: 2797
Joined: Thu Feb 09, 2006 11:27 pm
Contact:

[Implemented] Enumeration enhancements

Post by Michael Vogel »

I'm quite sure, most of these things have already been posted, but must be already a long time ago (or I just used the wrong keywords when seeking for these entries :lol: )...

I like the possibilities to do Enumerations, but sometimes I'd like to have some more possibilities, like the following: I want to split long constant lists into more smaller areas for structuring, what about a command which continues enumeration beginning with the highest enumerated constant so far?

Code: Select all

Enumeration
#A; 0
#B; 1
#C; 2
EndEnumeration

Enumeration Continue
#D; 3
#E; 4
#F; 5
EndEnumeration
Or what about a binary enumeration for flags?

Code: Select all

Enumeration Flag 1
#X;  =1
#Y;  =2
#Z;  =4
EndEnumeration
[/code]
gnozal
PureBasic Expert
PureBasic Expert
Posts: 4229
Joined: Sat Apr 26, 2003 8:27 am
Location: Strasbourg / France
Contact:

Post by gnozal »

Michael Vogel wrote:... I want to split long constant lists into more smaller areas for structuring, what about a command which continues enumeration beginning with the highest enumerated constant so far?
It's possible since PB3.9x or am I missing something ?

Code: Select all

  Enumeration
    #GadgetInfo ; Will be 0
    #GadgetText ; Will be 1
    #GadgetOK   ; Will be 2
  EndEnumeration
  
  Enumeration #PB_Compiler_EnumerationValue
    #GadgetCancel ; Will be 3
    #GadgetImage  ; Will be 4
    #GadgetSound  ; Will be 5
  EndEnumeration
For free libraries and tools, visit my web site (also home of jaPBe V3 and PureFORM).
User avatar
Michael Vogel
Addict
Addict
Posts: 2797
Joined: Thu Feb 09, 2006 11:27 pm
Contact:

Post by Michael Vogel »

gnozal wrote:It's already possible since PB3.9x or am I missing something ?

Code: Select all

  Enumeration
    #GadgetInfo ; Will be 0
    #GadgetText ; Will be 1
    #GadgetOK   ; Will be 2
  EndEnumeration
  
  Enumeration #PB_Compiler_EnumerationValue
    #GadgetCancel ; Will be 3
    #GadgetImage  ; Will be 4
    #GadgetSound  ; Will be 5
  EndEnumeration
You are right, even I wanted to get the highest value ever assigned to an enumerated constant so far - but when using a clear order of the enumeration blocks, your example will show exactly what I have asked for :roll:

So maybe the following example just shows my untypical way of coding - it should demonstrate it better now, what I have ment before :?

Code: Select all

; Option Window
Enumeration
	#OptPanelDisplay;	0
	#OptPanelInfos;		1
	#OptPanelWaypoints;	2
	#OptPanelAltitude;	3
	#OptPanelExtras;		4
	:
	#OptCancel;			87
EndEnumeration

; Option Colors
Enumeration
	#OptBlack;			0
	#OptWhite;			1
	#OptGreen;			2
	:
EndEnumeration

; Report Window
Enumeration Continue
	#RepSkipDots;		88
	#RepSkipInfo;			89
	#RepLineColor;		90
	#RepInfoType;		91
	:
	#ReportCancel;		117
EndEnumeration

; Report Values
Enumeration
	#RepYearly;			0
	#RepMonthly;			1
	#RepWeekly;			2
	:
EndEnumeration

; Statistic Window
Enumeration Continue
	#StaGraph;			118
	#StaTable;			119
	:
EndEnumeration
gnozal
PureBasic Expert
PureBasic Expert
Posts: 4229
Joined: Sat Apr 26, 2003 8:27 am
Location: Strasbourg / France
Contact:

Post by gnozal »

Ok, I understand now.
You could do something like this :

Code: Select all

; Windows
Enumeration
  #Win0
  #Win1
  #Win2
  #Win3
  #Win4
EndEnumeration
#CurrentWindowEnum1 = #PB_Compiler_EnumerationValue

; Gadgets
Enumeration
  #Gad0
  #Gad1
EndEnumeration
#CurrentGadgetEnum1 = #PB_Compiler_EnumerationValue

; Windows
Enumeration #CurrentWindowEnum1
  #Win5
  #Win6
  #Win7
EndEnumeration
#CurrentWindowEnum2 = #PB_Compiler_EnumerationValue

; Gadgets
Enumeration #CurrentGadgetEnum1
  #Gad2
  #Gad3
EndEnumeration
#CurrentGadgetEnum2 = #PB_Compiler_EnumerationValue

; Windows
Enumeration #CurrentWindowEnum2
  #Win8
  #Win9
  #Win10
EndEnumeration
#CurrentWindowEnum3 = #PB_Compiler_EnumerationValue

Debug #Gad3
Debug #Win5
Debug #Win10
It would be easier if we could reassign #PB_Compiler_EnumerationValue to the same constant, i.e. #CurrentGadgetEnum = #PB_Compiler_EnumerationValue several times instead of using #CurrentGadgetEnum1, #CurrentGadgetEnum2, etc...
For free libraries and tools, visit my web site (also home of jaPBe V3 and PureFORM).
freak
PureBasic Team
PureBasic Team
Posts: 5940
Joined: Fri Apr 25, 2003 5:21 pm
Location: Germany

Post by freak »

quidquid Latine dictum sit altum videtur
User avatar
Michael Vogel
Addict
Addict
Posts: 2797
Joined: Thu Feb 09, 2006 11:27 pm
Contact:

Post by Michael Vogel »

freak wrote:A solution with macros:
http://www.purebasic.fr/english/viewtop ... 996#133996
This is a really great solution!

Thanks,
Michael

PS these macros should be integrated into the system (to get rid of rewriting the name also in endenumeration line), or it should be done into an include file... -- oi, next idea: maybe a certain file (PureBasic.pbi) should be included by default :wink:
User avatar
Randy
User
User
Posts: 29
Joined: Sat Sep 30, 2006 11:36 pm
Location: Atlanta, Georgia

Enumeration Final Maybe

Post by Randy »

Here is something that I've often wished for:

Code: Select all

Enumeration 1
#COLOR_RED
#COLOR_YELLOW
#COLOR_GREEN
#COLOR_BLUE
#COLOR_BROWN
#COLOR_BLACK
#COLOR_WHITE
#COLOR_ORANGE
#COLOR_PURPLE
EndEnumeration #TOTAL_COLORS

Global Dim ColorSet(#TOTAL_COLORS)

I've often had large enumeration lists and the only way that I've been able to make this work would be to do something like Global Dim ColorSet(#COLOR_PURPLE) because that would be the last one. But it gets messy: For x = 1 to #COLOR_PURPLE... Yes, I could use ArraySize(...) but having a constant is faster and easier to understand in many cases.

Randy
Maybe it's just a bunch of stuff that happens -- Homer Simpson
User avatar
Arctic Fox
Enthusiast
Enthusiast
Posts: 609
Joined: Sun Dec 21, 2008 5:02 pm
Location: Aarhus, Denmark

Post by Arctic Fox »

#PB_Compiler_EnumerationValue :?:
gnasen
Enthusiast
Enthusiast
Posts: 282
Joined: Wed Sep 24, 2008 12:21 am

Post by gnasen »

I think it has been wished more then one time but an enumeration like this would be fine:

Code: Select all

#const01 = %00001
#const02 = %00010
#const03 = %00100
#const04 = %01000
#const05 = %10000
maybe like this:

Code: Select all

enumeration #PB_enumeration_flags
pb 5.11
User avatar
Demivec
Addict
Addict
Posts: 4260
Joined: Mon Jul 25, 2005 3:51 pm
Location: Utah, USA

Post by Demivec »

Here's another macro solution for enumerating flags:

Code: Select all

Macro debugBin(var)
  Debug "%" + RSet(Bin(var), 8, "0")
EndMacro

Macro nextFlag
  (#PB_Compiler_EnumerationValue - 1) * 2
EndMacro

;flags
Enumeration
  #bigFlag     = 1
  #greenFlag   = nextFlag
  #newFlag     = nextFlag 
  #fastFlag    = nextFlag 
  #strongFlag  = nextFlag 
  #workingFlag = nextFlag
EndEnumeration

debugBin(#bigFlag)
debugBin(#greenFlag)
debugBin(#newFlag)
debugBin(#fastFlag)
debugBin(#strongFlag)
debugBin(#workingFlag)
horst
Enthusiast
Enthusiast
Posts: 197
Joined: Wed May 28, 2003 6:57 am
Location: Munich
Contact:

Post by horst »

gnozal wrote:It would be easier if we could reassign #PB_Compiler_EnumerationValue to the same constant, i.e. #CurrentGadgetEnum = #PB_Compiler_EnumerationValue several times instead of using #CurrentGadgetEnum1, #CurrentGadgetEnum2, etc...
A constant that can be changed is not a constant.

It is time to think about the concept of compiler variables (in contrast to runtime variables). For example, #PB_Compiler_EnumerationValue looks like a constant, but it is not a constant. It is a compiler variable. Only at the point of compilation it is used like a constant.

I think PureBasic should introduce the concept of compiler variables (instead of offering "not really constant constants")...
Horst.
User avatar
Randy
User
User
Posts: 29
Joined: Sat Sep 30, 2006 11:36 pm
Location: Atlanta, Georgia

Post by Randy »

Arctic Fox wrote:#PB_Compiler_EnumerationValue :?:
True, that would work. I had forgotten about that little internal value. I could then, hypothetically, use #TotalCount = #PB_Compiler_EnumerationValue. I'll give that a shot.

[Edit] It actually returns the next one so if the last value is 4, #PB_Compiler_EnumerationValue will actually equal 5.

Still, I could deduct one from it during assignment. At least it it frees me from having to make sure that a value is always at the bottom of the list.
[End of Edit]

Thanks for that tip,

Randy
Maybe it's just a bunch of stuff that happens -- Homer Simpson
User avatar
Michael Vogel
Addict
Addict
Posts: 2797
Joined: Thu Feb 09, 2006 11:27 pm
Contact:

Re: Enumeration enhancements

Post by Michael Vogel »

I'm sure, somewhere in the forum is a fine solution for doing binary enumerations, but I couldn't find it...

Code: Select all

Macro BinaryEnumeration
	((#PB_Compiler_EnumerationValue-(#PB_Compiler_EnumerationValue>1))<<(#PB_Compiler_EnumerationValue>1))
EndMacro

Enumeration
	#FlagA	=BinaryEnumeration
	#FlagB	=BinaryEnumeration
	#FlagC =BinaryEnumeration
	#FlagD =BinaryEnumeration
	#FlagE =BinaryEnumeration
EndEnumeration

Debug #FlagA
Debug #FlagB
Debug #FlagC
Debug #FlagD
Debug #FlagE
...that's why I am using the code above for a while. Is there a simple way to update it for PB5.10?
User avatar
Demivec
Addict
Addict
Posts: 4260
Joined: Mon Jul 25, 2005 3:51 pm
Location: Utah, USA

Re: Enumeration enhancements

Post by Demivec »

Michael Vogel wrote:I'm sure, somewhere in the forum is a fine solution for doing binary enumerations, but I couldn't find it...
My solution is three posts up. :)
Michael Vogel wrote:...that's why I am using the code above for a while. Is there a simple way to update it for PB5.10?
What's wrong with the code?
User avatar
Michael Vogel
Addict
Addict
Posts: 2797
Joined: Thu Feb 09, 2006 11:27 pm
Contact:

Re: Enumeration enhancements

Post by Michael Vogel »

Demivec wrote:My solution is three posts up. :)
:oops:
Demivec wrote:What's wrong with the code?
Nothing up to PB 5.0, and I can use the macro for the first constant as well. But when using 5.1, I'll get an error, that boolean expressions are not allowed without the new Bool function, but Bool's results are non-integer values and bring up another error message...

I don't like the following code, but it works at least:

Code: Select all

	Macro BinaryEnumeration
		((#PB_Compiler_EnumerationValue-1)<<1+((#PB_Compiler_EnumerationValue-2)&$8000000+(~-#PB_Compiler_EnumerationValue)&$8000000)>>27)
	EndMacro
Post Reply