Home » Community » Forums » General Programming » C++, outputing enum values from fprintf
  Intel sponsors gamedev.net search:   
[Control Panel] [Register] [Bookmarks] [Who's Online] [Active Topics] [Stats] [FAQ] [Search]

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
Post New Topic  Post Reply 
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.

 User Rating: 852   |  Rate This User  Send Private MessageView Profile Report this Post to a Moderator | Link

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


 User Rating: 1439   |  Rate This User  Send Private MessageView Profile Report this Post to a Moderator | Link

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.

 User Rating: 1494   |  Rate This User  Send Private MessageView ProfileView Journal Report this Post to a Moderator | Link

which would be faster, a function or a macro?

this might be a stupid question,

stupid, meaning it has little focus and is "dumb"

 User Rating: 852   |  Rate This User  Send Private MessageView Profile Report this Post to a Moderator | Link

1. It's the same speed 2. It's irrelevant

 User Rating: 1015    Report this Post to a Moderator | Link

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!


 User Rating: 1839   |  Rate This User  Send Private MessageView Profile Report this Post to a Moderator | Link

Quote:
Original post by JohnBolton
    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.


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


 User Rating: 1220   |  Rate This User  Send Private MessageView Profile Report this Post to a Moderator | Link

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.

 User Rating: 1464   |  Rate This User  Send Private MessageView Profile Report this Post to a Moderator | Link

Quote:
Original post by DrEvil
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.


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.



 User Rating: 1220   |  Rate This User  Send Private MessageView Profile Report this Post to a Moderator | Link

All times are ET (US)

Post Reply
 Last Thread Next Thread 
Forum Rules:
You may not post new threads
You may post replies
You may not edit your posts
You may not use HTML in your posts
Jump To:
Administrative Options: