Archived

This topic is now archived and is closed to further replies.

rugal

Outputting enumerations

Recommended Posts

Hi all I was wondering is there an easy way to output the name of an enumeration? Tried google without luck. Say I have: enum Colours {red, yellow, blue} Colour MyColour = yellow; I know you can do: if (MyColour == yellow) { glPrint("Your colour is Yellow") } But is there a simpler way without the if statement? Put simply can you cast an enum value to a string? Thanks in advance

Share this post


Link to post
Share on other sites
quote:
Original post by rugal
But is there a simpler way without the if statement? Put simply can you cast an enum value to a string?


There''s no easy way, but it can atleast be done automatically with a bit of preprocessor magic. Wouldn''t recommend it unless you have atleast a few dozen entries though.

in colors.h

color(red),
color(green),
color(blue)


in color.h

enum Color
{
#define color(id) id
#include "colors.h"
#undef color
};

extern const char *ColorNames[];


in color.c

const char *ColorNames[] =
{
#define color(id) #id
#include "colors.h"
#undef color
};

Share this post


Link to post
Share on other sites