How to implement an enum with 2 possible values

I’m trying to consolidate a couple of enums that are technically the same type, but have different values depending on the version of the file.

enum <ushort> CHUNKTYPE { 
        Any = 0x0,
        Mesh = 0x1000,
        Helper = 0x1001,
        ...
};

enum <uint> CHUNKTYPE {
        Any = 0x0,
        Mesh = 0xCCCC0000,
        Helper = 0xCCCC0001,
        ...
};

I’m running into namespace issues where I can’t have the same const values in a file. And the size difference between the enums (one is ushort, other is uint) also causes problems.

I’d like to be able to use the enum as a type, but have the correct version load up dependent on the file version from the header. Any thoughts on how to approach this?

Assuming that both styles of CHUNKTYPE could be in the same file, you would have to add either a prefix or postfix to the names so there won’t be a name collision. For example:

enum <ushort> CHUNKTYPE_US { 
        us_Any = 0x0,
        us_Mesh = 0x1000,
        us_Helper = 0x1001,
        ...
};
enum <uint> CHUNKTYPE_IS { 
        is_Any = 0x0,
        is_Mesh = 0x1000,
        is_Helper = 0x1001,
        ...
};

Then in your code you could use an if statement to check which one to use:

if( header.useUshortChunks )
     CHUNKTYPE_US chunk;
else
     CHUNKTYPE_UI chunk;

You could also put this code in a function if it is used a lot.

Graeme
SweetScape Software

1 Like