|
||||||||||||||||||
Add Forum to Favorites | Send Topic To a Friend | View Forum FAQ | Track this topic |
Last Thread Next Thread ![]() |
| C++, outputing enum values from fprintf |
|
![]() sp_1d4r Banned Member since: 9/6/2005 From: tumwater, WA, United States |
||||
|
||||
| can i output the character label of an enum value into a file from c++ and fprintf? how would i do that? I have a realy long enum list for debugging/monitoring purposes, and it'd be helpful if i could just out put the character enum name from the enum value. |
||||
|
||||
![]() stylin Member since: 7/18/2005 From: Glendale, AZ, United States |
||||
|
|
||||
| No, but you can convert it to an integer and output that to the file. :stylin: "Make games, not war." "...if you're doing this to learn then just study a modern C++ compiler's implementation." -snk_kid |
||||
|
||||
![]() pinacolada Member since: 10/17/2003 From: San Francisco, CA, United States |
||||
|
|
||||
| No, but If you have a nice text editor that's good at making macros (coughVIcough), then it only takes a few seconds to write a macro that converts your enum into a function that returns that returns a string for each value. |
||||
|
||||
![]() sp_1d4r Banned Member since: 9/6/2005 From: tumwater, WA, United States |
||||
|
||||
| which would be faster, a function or a macro? this might be a stupid question, stupid, meaning it has little focus and is "dumb" |
||||
|
||||
![]() Anonymous Poster |
||||
|
||||
| 1. It's the same speed 2. It's irrelevant |
||||
|
||||
![]() JohnBolton Member since: 4/3/2002 From: Belmont, CA, United States |
||||
|
|
||||
Here is a fairly common (though hackish) way of accomplishing what you want to do: #define string_or_enum( x ) x // enum
enum MyEnums
{
#include "MyEnums.h"
};
#define string_or_enum( x ) #x // string
char const * const MyEnumNames[] =
{
#include "MyEnums.h"
}; MyEnums.h looks like this: string_or_enum( enum1 ),
string_or_enum( enum2 ),
string_or_enum( enum3 ),
string_or_enum( enum4 )
The result is this: enum MyEnums
{
enum1,
enum2,
enum3,
enum4
};
char const * const MyEnumNames[] =
{
"enum1",
"enum2",
"enum3",
"enum4"
}; The advantage is that you have a single list for both, so the two lists are always in sync.John Bolton Locomotive Games (THQ) Current Project: Destroy All Humans (Wii). IN STORES NOW! |
||||
|
||||
![]() ChaosEngine Member since: 6/9/2000 From: Christchurch, New Zealand |
||||
|
|
||||
Quote: The only problem with that is the enums must be a) zero-based and b) contiguous. One thing you can do is create a templated enum class. I've written one for a previous job. something like this
template <typename ENUM_TYPE>
class Enum
{
public:
typedef std::map<ENUM_TYPE, std::string> ValueMap;
const std::string& ToString() const
{
return TypeStrs()[type];
}
static ValueMap &TypeStrs()
{
static ValueMap vMap;
return vMap;
}
private:
ENUM_TYPE type;
};
// use as follows
enum WeekDays
{
Monday = 2,
Tuesday,
Wednesday
};
typedef Enum<WeekDays> WeekDayEnum;
WeekDayEnum::TypeStrs()[Monday] = "Monday";
WeekDayEnum::TypeStrs()[Tuesday] = "Tuesday";
obviously it's not perfect. you have to maintain 2 seperate lists, but that can be improved with some macro trickery (ordinarily macros are the tool of satan, but this is a situation where they can be useful, ask Herb Sutter) if you think programming is like sex, you probably haven't done much of either. -------------- - capn_midnight |
||||
|
||||
![]() DrEvil Member since: 7/13/2003 From: Los Angeles, CA, United States |
||||
|
|
||||
| Why would anyone prefer that template example over 2 static arrays? You have to maintain the enum and string list anyways, and in that example there's the added overhead of a std::map lookup, which is much slower than just indexing the string array with the enum. I usually go the route of 2 arrays, using the enum index to get the string, and using BOOST_STATIC_ASSERT for compile time checking that both arrays have the same number of elements. Just an added protection against adding/removing from one array and forgetting the other. Still has maintaining both lists, but its still about as fast as it can be. I wish there were some language specs for generating enum strings, even if they were an RTTI feature. edit: nm, u answered my question already heh. Apparently you needed non contiguous and non zero based. Something I've never had much need for yet. |
||||
|
||||
![]() ChaosEngine Member since: 6/9/2000 From: Christchurch, New Zealand |
||||
|
|
||||
Quote: ordinarily, I'd agree with you on the static arrays. However in this example I was working with enums defined in a library and asked to find a generic way to store them in xml as a string. Apparently you can do this quite nicely in .net. |
||||
|
||||
All times are ET (US)![]() |
Last Thread Next Thread ![]() |
|