This section of the archives stores flipcode's complete Developer Toolbox collection,
featuring a variety of mini-articles and source code contributions from our readers.

Mapping Int Constants To Types & Class Members
Submitted by

Here's some nasty code that maps integer constants to types and class
members. It's similar to the loop unrolling code that's appeared previously
in flipcode. Although it's pretty disgusting to look at, I've used it once
or twice to keep seperate vertex component arrays in sync. (in this case,
TestFlag would represent a bit flag for the component, like a D3D FVF
constant, and the m_pData? arrays would be the seperate vertex component
arrays). If you're doing a lot of operations on arrays such as these, this
code makes it pretty easy to add new array members, add new functionality
that affects array members, or alter their underlying types.

class DiabolicalData
{
public :

//
// Enums
//
enum TestFlag
{
e1 = 0x1, // Bit flags representing the members and their types.
e2 = 0x2, // Here's the checklist to add a new member to this class:
e3 = 0x4, // 1) Add a value to this enum to represent the member
e4 = 0x8, // 2) Add the member's type to the TestTypes below
e5 = 0x10, // 3) Add the member to this class normally
// 4) Add access code for this member in the GetArrayPtr method.
// No constants after here...
eLast = e5, // Note : I've used bit flags here but normal incrementing enum values
// are possible too; just change the Unroll<TestFlag(i << 1)::???
// call in the Unroll static methods to Unroll<TestFlag(i + 1)::???
};

struct DiabolicalUnroller // Keep all the Unroll structs in here so they are publically accessible to
// each other (but we keep the mechanism as a whole private).
{
template <TestFlag i struct Unroll
{
//
// Typedefs
//
typedef TestType<i::VType UnrollType; // Type associated with the array matched to this flag