WinROTT Port and the !^@%$# dirtype enum

by Codewiz51 July 16, 2008 23:21

The dirtype enum has been driving me crazy in the process of porting WinROTT C code to C++. The C compiler allows variables of type dirtype to be used interchangably with int types.  The C style enum is defined as:

typedef enum {
       east,
       northeast,
       north,
       northwest,
       west,
       southwest,
       south,
       southeast,
       nodir
} dirtype;

// So with C, you are allowed to perform the following:
dirtype x;
int y = north;
x = y >> 1; // (result is northeast)

// In C++, you cannot assign or cast an integer type to  a dirtype.
// I could have defined operator overloads for dirtype, but it was
// easier to work out a brute force translation until I get all the files
// moved over to C++.
// The fix is something like this:
            switch (y >> 1)
            {
            case 0:
                x = east;
                break;
            case 1:
                x = northeast;
                break;
            case 2:
                x = north;
                break;
            case 3:
                x = northest;
                break;
            case 4:
                x = west;
                break;
            case 5:
                x = southwest;
                break;
            case 6:
                x = south;
                break;
            case 7:
                x = southeast;
                break;
            default:
                x = east;
                break;
            }

Because of the mixed environment, it is difficult to modify enum so that it can interchange with an integer. What works for C doesn't work correctly for C++.  I ended up redefining the direction enum to use #define. It proved to be the simplest fix. No complex macros, no worrying about c++ decoration.

#define    east 0
#define    northeast 1
#define    north 2
#define    northwest 3
#define    west 4
#define    southwest 5
#define    south 6
#define    southeast 7
#define    nodir -99
typedef short dirtype;

This allows the same tokens to be used in both the C++ modules and the C modules. It also allows arithmetic on dirtype variables.

The code is compiling, but I have 86 unresolved symbol errors. These are caused by C++ decoration of names in most cases and should be easy to fix. Hopefully, I'll have something to release soon. 

Tags: , ,

WinROTT

Comments are closed

Powered by BlogEngine.NET 1.6.0.0
Theme by Mads Kristensen | Modified by Mooglegiant

Disclaimer

This blog represents my personal hobby, observations and views. It does not represent the views of my employer, clients, especially my wife, children, in-laws, clergy, the dog, the cats or my daughter's horse. In fact, I am not even sure it represents my views when I take the time to reread postings.

© Copyright 2008-2011