Sign in to follow this  
n4gix

typedef enum

Recommended Posts

I need to understand how to create a "matrix" table of constants that may be selected based on an enum variable.I have a list of "factors" that need to be applied from an index value ranging from 0 to 20.something like this:typedef double zoomvar{ 0.082290, 0.164582, 0.246873, 0.329164, 0.576037, etc. 648200 926000 }zoomvar;Now the question is, how do I cast the "zoomvar" value from the table to my variable "zoomfactor"?

Share this post


Link to post
Share on other sites
Help AVSIM continue to serve you!
Please donate today!

Hey Bill.Still not sure what you're trying to get at here but is that typdef list meant to be an array or the enum typedef as per the subject title of your post? What structure is your 'matrix' table and what datatypes does it contain? Is it a single-dimension array or multi-dimensional? Or is it a struct?Can I make a guess that your trying to set up a construct that allows you to apply a scale transformation to a 2D display in discreet, predefined step values? Or do I completely have the wrong end of the stick in my hands here - Darren

Share this post


Link to post
Share on other sites

> What structure is your>'matrix' table and what datatypes does it contain? Is it a>single-dimension array or multi-dimensional? Or is it a>struct?>>Can I make a guess that your trying to set up a construct that>allows you to apply a scale transformation to a 2D display in>discreet, predefined step values? Or do I completely have the>wrong end of the stick in my hands here OK, I can see where what I wish to accomplish might be a bit vague, but you've got the gist of it in any case.Actually, I have around 10 to maybe 40 variables which need to be "preset" to selected values, based on an "index variable." Think of a step selector switch that sets an index variable in 20 steps, 0 to 19.I would have an array with something like this:0 = 0,0,0,0,0,1,1,1,1,1,0x00,00,24.5,1,1....1 = 0,0,0,0,1,1,1,1,1,1,0x05,01,29.0,1,1....2 = 0,0,0,0,1,1,1,1,1,1,0x07,02,29.0,1,1....3 = 0,0,0,1,1,1,1,1,1,1,0x1F,03,29.0,1,1....4 = 0,0,1,1,1,1,1,1,1,1,0x1F,05,38.1,1,1....5 = 0,1,1,1,1,1,1,1,1,1,0x4F,10,55.8,1,1....6 = 1,1,1,1,1,1,1,1,1,1,0x4F,25,65.3,1,1....etc. Note that the contents some of the columns in the matrix might be bool, and some of the others might be float, one might be hex...So, in summary, if I set the index to 14 I would be able to set all variables in that row to their matrix values... ;)I'm trying to avoid a massive mess of "if's" in the code...

Share this post


Link to post
Share on other sites

I think I get what you mean. You want to initialise or set those 10 or 40 variables you mention to the different values (and types) as defined in the matrix array, which I assume are constants, depending on the row index?Presumably, if each of those different combinations of numbers in the array rows instigates a specific 'command' or 'action' once they initialise those 10 or 40 variables, you can use the 'typedef enum' you mentioned in the title of your first post to give the index number a more descriptive name instead of being a vague number?My first reaction is you need an array of structs for that matrix table. I assume (and hope) that for each matrix entry in the array the order of data types is set and does not change? If so you would create a basic structure containing the data types in the order required then initialise the constant values. You then simply create a normal 20 element array of those structs to iterate through.The sticking point is deciding how those 10 or 40 variables you need to initialise are stored and accessed. If they too are stored in another structure with the same data type order then I'm sure as you iterate through the matrix array you could simply copy the matrix array structure values to this 'variable structure' using something as simple as assigning one structure to equal another statement. Basically like a kind of binary copy of one variable's memory location to another. But the source and destination memory layout has to obviously be identical. This is, I believe, the basis of the solution, though there may be issues with the 'constness' of the matrix structure to get around.Of course things are a little trickier if those 10 or 40 variables are discreet entities, and also whether you need to set them all at once or in a different order at different times depending on your needs. I think I know a way around that though. To come up with difinitive suggestions it's all a question of knowing how you store your initialisation data (same data type order, just different values) and how you store the variables into which you want to stuff that data. It's an interesting problem though :)I'll have a think and see if I can come up with example code pointing out some possible scenarios. Sure beats the #### out of flexing my new C/C++ parts of my brain by following dull, dry samples from books.- Darren

Share this post


Link to post
Share on other sites

Each "row" of the matrix will define the values to be used for each variable in the row, based on the index value. All variables in the table are pre-defined data types that will never change. The only thing changing are the contents of the pointer variables.This would be equivalent to a series of if statement of the type (in pseudo-code):if (index=13) (var1=0, var2=1, var3=0x4F, var4=28.4, var5=0, etc.)if (index=14) (var1=1, var2=0, var3=0x2F, var4=58.7, var5=1, etc.)I simply need to enable/disable certain items, or change their values based on the index value selected.

Share this post


Link to post
Share on other sites

Well, in the meantime I've consulted my available references. It would appear that I may be S.O.L. since I don't see how to create a multi-dimensional array with different data types in each "column."For example,int SomeArray[20][16] = { etc. } ;will simply create an array 20 rows long by 16 columns wide, but all are of the type int...I will need an array where the columns are varying types, such as:bool, bool, bool, FLOAT64, SINT32, SINT32, FLOAT64, bool, etc.I suppose I could simply make 'em all FLOAT64, and then cast them to whatever type when required?

Share this post


Link to post
Share on other sites

I decided to make my life simple. This works just fine:FLOAT64 zoomfactor[20] = { 0.082290,0.164582,0.246873,0.329164,0.576037,1.0,1.5,2.0,3.5,5,10,15,20,35,50,100,150,200,350,500 } ;case PANEL_SERVICE_PRE_UPDATE:Zoom = (zoomfactor[zoomvar]);break;I can use the same schema for all the other "indexed" variables I need to set... ;)

Share this post


Link to post
Share on other sites

Blast this hot weather.I just had my CPU fry itself to a fiery death and had to get a replacement and new motherboard. #### that CPU fan, I think I'll invest in one of those drive bay displays that shows temperature and fan speeds so I can see trouble coming :)Your solution works but I would guess you waste some memory having everything a FLOAT64 but, depending on the size of your table, that may not be too much of a resource hit. You do lose type safety and take on the responsibility of casting everything correctly though. Me, I like to make the compiler do all that nitty gritty hard work by making sure I create 'type safe' data structures and variables from the get go and trying my best to avoid casting whenever possible :)- Darren

Share this post


Link to post
Share on other sites

>I just had my CPU fry itself to a fiery death and had to get a>replacement and new motherboard. #### that CPU fan, I think>I'll invest in one of those drive bay displays that shows>temperature and fan speeds so I can see trouble coming :)Yikes! Even though my home has central airconditioning, one of my flightsim computers evidently has circulation problems, because if I don't have FS9 loaded (hence forcing the CPU fan to "high speed," the X700 Pro video card will quickly reach a temperature threshold that causes the primary & secondard displays to flash on and off, as the protection circuit kicks in... I've added extra case fans and that has helped, but not solved the problem... So, I just keep FS9 loaded all the time! ;)>Your solution works but I would guess you waste some memory>having everything a FLOAT64 but, depending on the size of your>table, that may not be too much of a resource hit.Well, as it happens most of the variables are SINT32, so that mitigates the memory hit quite a bit, but I do understand your point.I've actually used this same schema to adjust the "refresh rate" based on the same, indexed value, and another array of char type to display a list of string data so it is quite handy... ;)

Share this post


Link to post
Share on other sites

I've invested in some round IDE cables rather than the ribbon ones used to connect drives and CD-Roms inside my PC. The reduced surface area of the cables lets air flow better. I've also got a better CPU fan and replaced my case fans with quieter, more powerful (and alas more expensive) ball bearing types. I friend of mine once put case fans in the wrong way around. Those at the front AND the back were sucking air in! Those at the back should blow out and those in front should suck in. Something to check on your 'Hot & Bothered' PC ;)The issue with memory waste using FLOAT64 for all those values sounds fine if your data set in the array is generally close to that size anyway. If however you find you need a data model containing widely differing data types in the future, or you need to save on memory, I'm sending you sample code via your email for you to check over. It uses an array of structures that directly models the data types in use, which keeps things type safe and also devoid of the need to cast. I hope it proves useful.To sum up though, if you need a container to hold data of differing types then the structure is always your friend :)- Darren

Share this post


Link to post
Share on other sites

>I've invested in some round IDE cables rather than the ribbon>ones used to connect drives and CD-Roms inside my PC. The>reduced surface area of the cables lets air flow better. My "hot and bothered" PC already is new enough to use the round IDE and SATA cabling, so airflow restrictions isn't much of an issue. I suspect that where I have the PC located may be more of an issue, since it is on the floor and next to a filing cabinet (the CPU exhausts to the left side), so there is only a few inches of space between the CPU case and the cabinet. I'll likely have to redesign my flightstation's physical arrangement. ;)As for the data types, the database I'm accessing has the data stored as FLOAT64, UINT32, etc., so I'm matching the same data types with my internal variables.Thanks for the sample code in the email. I'm sure it will prove instructional and useful! ;)

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this