Accessing the highest ENUM value

General FreeBASIC programming questions.
Julcar
Posts: 82
Joined: Oct 19, 2010 18:52
Contact:

Accessing the highest ENUM value

Postby Julcar » Jan 21, 2021 8:06

Hi all,

Let's suppose I have this enum

Code: Select all

ENUM my_enum
  value0 = 0
  value1
  value2
  value3
  value4
END ENUM


If I do DIM v AS my_enum it will have the lowest value, printing it just says "0"

I want an alternative to know the highest value in the enum regardless the number of values inside.

Just like in arrays I can use UBOUND

The only option at my head right now is to declare an always-last value, and reference it every time I want to traverse the enum from the lowest to the highest value, but I still want to read other opinions.

Thanks in advance
SARG
Posts: 1230
Joined: May 27, 2005 7:15
Location: FRANCE

Re: Accessing the highest ENUM value

Postby SARG » Jan 21, 2021 10:07

The value "0" is not the lowest enum value but only the default initialized value.
Try this code and you will get also "0".

Code: Select all

ENUM my_enum
  value0 = 1
  value1
  value2
  value3
  value4
  my_enum_up
end enum

DIM v AS my_enum
print v
sleep


I would also add an upper symbol used as ubound so you get the value of the real last symbol just by substracting 1. Keeping always my_enum_up as last symbol you have not to worry when adding new symbols.

Code: Select all

enum my_enum
  value0
  value1
  value2
  value3
  value4
  my_enum_up
end enum

DIM v AS my_enum=my_enum_up-1
print v
sleep
fxm
Posts: 10211
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: Accessing the highest ENUM value

Postby fxm » Jan 21, 2021 11:55

Subtraction can be done directly in the ENUM:

Code: Select all

enum my_enum
  value0
  value1
  value2
  value3
  value4
  __, my_enum_up = __ - 1
end enum
badidea
Posts: 2227
Joined: May 24, 2007 22:10
Location: The Netherlands

Re: Accessing the highest ENUM value

Postby badidea » Jan 21, 2021 14:14

I always found the enum datatype very limited.

You cannot ask the lbound, ubound or number of 'elements'.

'dim v as my_enum' does not seem to do anything extra compared to 'dim v as integer', v can still be any integer value.

Code: Select all

ENUM my_enum
   value0 = 10
   value1
   teminator
END ENUM

ENUM my_other_enum
   value0 = 14
   value1
   teminator
END ENUM

DIM v AS my_enum
print v 'prints 0

v = my_enum.teminator '12
print v

v = my_other_enum.teminator '16
print v

v = teminator 'does not understand that


And they are in global namespace unless you create a conflict?

Code: Select all

ENUM my_enum
   value0 = 10
   value1
   teminator
END ENUM

print teminator

dim as integer teminator = 7

print teminator
Julcar
Posts: 82
Joined: Oct 19, 2010 18:52
Contact:

Re: Accessing the highest ENUM value

Postby Julcar » Jan 21, 2021 21:41

Code: Select all

'Valid Options, just keep "Count" always at last position

ENUM My_Options
  OPTION_0 = 0
  OPTION_1
  OPTION_2
  OPTION_3
  OPTION_4
  Count
END ENUM

DIM ArrayOptions(0 TO My_Options.Count - 1) AS STRING = {"value1", "value2", "value3", "value4", "value5"}

I guess this is a valid case for ENUM use.
badidea
Posts: 2227
Joined: May 24, 2007 22:10
Location: The Netherlands

Re: Accessing the highest ENUM value

Postby badidea » Jan 21, 2021 23:19

Yes, but things will go 'wrong' if you do this:

Code: Select all

'Valid Options, just keep "Count" always at last position

ENUM My_Options
  OPTION_0 = 0
  OPTION_1
  OPTION_2 = 4
  OPTION_3
  OPTION_4
  Count
END ENUM

DIM ArrayOptions(0 TO My_Options.Count - 1) AS STRING = {"value1", "value2", "value3", "value4", "value5"}

print ArrayOptions(OPTION_0)
print ArrayOptions(OPTION_1)
print ArrayOptions(OPTION_2)
print ArrayOptions(OPTION_3)
print ArrayOptions(OPTION_4)

for i as integer = 0 to My_Options.Count - 1
   print i, ArrayOptions(i)
next
Julcar
Posts: 82
Joined: Oct 19, 2010 18:52
Contact:

Re: Accessing the highest ENUM value

Postby Julcar » Jan 22, 2021 5:52

badidea wrote:Yes, but things will go 'wrong' if you do this:

Code: Select all

'Valid Options, just keep "Count" always at last position

ENUM My_Options
  OPTION_0 = 0
  OPTION_1
  OPTION_2 = 4
  OPTION_3
  OPTION_4
  Count
END ENUM

DIM ArrayOptions(0 TO My_Options.Count - 1) AS STRING = {"value1", "value2", "value3", "value4", "value5"}

print ArrayOptions(OPTION_0)
print ArrayOptions(OPTION_1)
print ArrayOptions(OPTION_2)
print ArrayOptions(OPTION_3)
print ArrayOptions(OPTION_4)

for i as integer = 0 to My_Options.Count - 1
   print i, ArrayOptions(i)
next


AFAIK, unless specified, next option in the enum will have +1 value, so in that case the upper bound will be more than intended
dodicat
Posts: 6800
Joined: Jan 10, 2006 20:30
Location: Scotland

Re: Accessing the highest ENUM value

Postby dodicat » Jan 22, 2021 12:00

Enum play around

Code: Select all

Randomize
#define mk(a,b) a or b shl 16
#define Irange(f,l) Int(Rnd*((l+1)-(f)))+(f)
#define dx(d) cshort(loword(d))
#define dy(d) cshort(hiword(d))
Enum direction
      north
      south
      east
      west
End Enum

Enum
      n=mk(0,-1)
      s=mk(0,1)
      e=mk(1,0)
      w=mk(-1,0)
End Enum

Dim As direction enum_table(north To west)={n,s,e,w}

Sub GetPos(d As direction Ptr,Byref i As Long)
      Var x=dx(i)
      Var y=dy(i)
      Select Case Irange(north,west)
      Case north
            y+=dy(d[north])
      Case south
            y+=dy(d[south])
      Case east
            x+=dx(d[east])
      Case west
            x+=dx(d[west])
      End Select
      If x>1024 Then x=Irange(0,1024)
      If x<0 Then x=Irange(0,1024)
      If y<0 Then y=Irange(0,768)
      If y>768 Then y=Irange(0,768)
      i=mk(x,y)
End Sub

Screen 20,32,,64
Dim As Long i=mk(500,400),c
var start=@enum_table(north)
Do
      c+=1
      GetPos(start,i)
      If c Mod 1000=0 Then Line(0,0)-(1024,768),Rgba(0,0,0,1),bf:c=0
      Pset(dx(i),dy(i)),Rgb(Rnd*255,Rnd*225,0)
      If Inkey=Chr(27) Then Exit Do
Loop

 
 
badidea
Posts: 2227
Joined: May 24, 2007 22:10
Location: The Netherlands

Re: Accessing the highest ENUM value

Postby badidea » Jan 22, 2021 12:53

dodicat wrote:Enum play around
...

That looks very complicate for what is does.
And a small improvement suggestion: add ": sleep 1" to line "If c Mod 1000=0" to give the CPU an occasional break.
dodicat
Posts: 6800
Joined: Jan 10, 2006 20:30
Location: Scotland

Re: Accessing the highest ENUM value

Postby dodicat » Jan 22, 2021 17:35

Thanks for testing badidea.
Probably a good idea to sleep 1 on that line, although it works without it in Win 10.
I note that const is 4 bytes in 32 bits and 8 bytes in 64 bits.
Surely this is not a good idea?
MrSwiss
Posts: 3725
Joined: Jun 02, 2013 9:27
Location: Switzerland

Re: Accessing the highest ENUM value

Postby MrSwiss » Jan 22, 2021 17:41

dodicat wrote:I note that const is 4 bytes in 32 bits and 8 bytes in 64 bits. Surely this is not a good idea?

ENUM is, by FB's definition: INTEGER ... AFAIK, this cannot be changed.
(there might be a workaround however)
fxm
Posts: 10211
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: Accessing the highest ENUM value

Postby fxm » Jan 22, 2021 20:07

- In many languages, none integer variable is implicitly convertible to a ENUM instance (and reciprocally), this one being strongly typed (declaration equivalent to the ENUM qualified as EXPLICIT with FreeBASIC), and an explicit CAST is required for such a conversion.
- But FreeBASIC accepts it (even if the numeric value does not match any ENUM symbol defined). This is just one of the many lacks of FreeBASIC on the normal ENUM features.

Therefore, there is no many interest to declare a true ENUM instance (which is sized as an INTEGER), but otherwise any pre-built integer variable ([U]BYTE, [U]SHORT, [U]LONG...) can be declared instead:

Code: Select all

ENUM My_Options Explicit
  OPTION_0
  OPTION_1
  OPTION_2
  OPTION_3
  OPTION_4
  Count
END ENUM

Dim As My_Options opt_a      ' Enum instance
opt_a = My_Options.OPTION_3
Print opt_a, Sizeof(opt_a)

Dim As Byte opt_b            ' Byte instance
opt_b = My_Options.OPTION_3
Print opt_b, Sizeof(opt_b)

Sleep
Note:
If we don't use the EXPLICIT qualifier (namespace prefix not imposed), I don't see much point in using an ENUM structure compared to a simple discrete list of constants (except the auto-increment of values).
Julcar
Posts: 82
Joined: Oct 19, 2010 18:52
Contact:

Re: Accessing the highest ENUM value

Postby Julcar » Jan 23, 2021 7:45

fxm wrote:... I don't see much point in using an ENUM structure compared to a simple discrete list of constants (except the auto-increment of values).


In fact I was needing that functionality.
http://chiselapp.com/user/julcar/reposi ... Fadmin.bas
The ENUM definition is at line 7, and at line 241 is visible the utility for the incremental values
fxm
Posts: 10211
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: Accessing the highest ENUM value

Postby fxm » Jan 23, 2021 9:36


Otherwise, I'm guessing your code isn't finished debugging yet, because I'm seeing some weird stuff.
Julcar
Posts: 82
Joined: Oct 19, 2010 18:52
Contact:

Re: Accessing the highest ENUM value

Postby Julcar » Jan 23, 2021 9:59

fxm wrote:Otherwise, I'm guessing your code isn't finished debugging yet, because I'm seeing some weird stuff.


Of course, it is even pre-beta stage, and a lot of bugs around there

Return to “General”

Who is online

Users browsing this forum: No registered users and 9 guests