Synopsis: Creating string lookup tables based on an enumerated type. January 97 From erwin@pip.dknet.dk Thu Mar 20 16:54:26 1997 To: MERC/Envy Mailing List Subject: Easy creation of table with string values and constants with corresponding numbers I recently added ability to refer to almost any field of any variable in mob progs using a dot notation (e.g. $i.level would be the level of the acting mobile). There were quite a few (over 80) of those fields: I would have to build a table of strings AND #define each position in the table to a unique value, so I could use a switch statement. Instead, I did the following: In a file called "dotfun.h" I added call to a macro, DOT_FUN, once for each field necessary: ==== DOT_FUN(LEVEL) DOT_FUN(ROOM) ==== Nothing else is in that file. Now, in the main file, I have: ==== #define DOT_FUN(field) #field , const char * dotfun_keywords[] = { #include "dotfun.h" "\n" }; ==== This declares a DOT_FUN macro, which will turn field into a string (e.g. DOT_FUN(level) will turn into "LEVEL" followed by a comma. After this , I have: === #undef DOT_FUN #define DOT_FUN(field) DOT_ ## field , typedef enum { #include "dotfun.h" } dotfun_t; === I start by removing the old definition for DOT_FUN and making a new one, which will be valid for the rest of this file. The new macro tells the preprocessor to concatenate the string DOT_ with whatever is given as parameter: thus, DOT_FUN(LEVEL) will turn into DOT_LEVEL , Afterwards, I declare an enumerated type: variables of that type can take on any one of the values specified, and the compiler assigns each of them value, starting with 0. Then I include dotfun.h again - this time, the DOT_FUN macro has a different effect. As a result, I have declared both the string table and the DOT_XXXX using only one file: changing the file by adding or deleting new values is the only action needed - all the values will be correctly updated. Also, when doing the switch statement, if I switch on a variable of type dotfun_t, the compiler will warn me if I have neglected to handle any of the values in the enumarated type. Now that all the values of the keyword array are in one place, it is possible to further optimize lookup by sorting the table and using binary search (though I haven't done that yet, it struck me just as I was writing this letter :) This method is not exactly new, but it was not employed in Newt's version of MOB Progs which I use. Note that it's probably only a good idea to use this on types that are only used internally or are saved as strings in area/player files: if you would e.g. replace ITEM_XXX definitions with this and delete a field or sort it, the items in areas would have their types mixed up as they are saved as a number. ============================================================================== Erwin Andreasen Viby J, Denmark Computer Science Student at Aarhus Business erwin@pip.dknet.dk College ==============================================================================