Thread: Free program I'm sharing: ConvertEnumToStrings

  1. #31
    ...and never returned. StainedBlue's Avatar
    Join Date
    Aug 2009
    Posts
    168
    Quote Originally Posted by Programmer_P View Post
    What my program does that makes it more valuable than that is it provides a function which returns a reference to a vector of enum value strings (automatically generated by my program)..

    Hmmmm. So, I create an enum in a source or header file, parse it as text to return as a vector of strings representing the enum values?

    Admittedly I didn't follow your explanation of the purpose of all this.

    I can understand the following transformations:

    text file -> c++

    c++ -> text file

    I can't wrap my brain around:

    c++ -> text file -> c++

    Is that what you're trying to do here?
    goto( comeFrom() );

  2. #32
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Another potential use for this kind of thing is with long enums from libraries used, eg, as error codes. When you get an error code like that back, it can be a pain to have to figure out what the number means every time.

    I deal with it this way:
    Code:
    #!/usr/bin/perl -w
    use strict;
    
    die "Input file required" if (!defined($ARGV[0]));
    
    open(IN,"<".$ARGV[0]) || die "Couldn't open ".$ARGV[0]."$!";
    
    print "switch (VAR) {\n";
    
    while (<IN>) {
            (my $enum = $_) =~ s/[\s|\t]//g;
            $enum =~ s/,.*$//;
            print "\tcase ($enum):\n"
                    ."\t\tstrcpy(STR_HERE,\"$enum\");\n"
                    ."\t\tbreak;\n";
    }
    
    print "\tdefault:\n}\n";
    
    close(IN);
    to create a short function which returns the enum value as a c-string.

    So your presumably your program will be good for that too.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  3. #33
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    Posts
    1,853
    Definetely better using enum than int, was just not thinking.
    make_pair can be found here

    But yes, map doesn't replace your program. Should you use map in your program? It would have saved you probably time.

    I think people are mentioning map because it offers a way for the programmer to do quite easily what your program in the first place. Obviously, it is better to offer yourself a way to do this than use any other external program. So somebody could do
    Code:
    enum day{
        Monday,
        Tuesday,
        ...
    };
    ...
    map<string,int> dayStr;
    dayStr.insert(par<string,int>(Monday, "Monday");
    dayStr.insert(par<string,int>(Tuesday, "Tuesday");
    ...
    day today = Monday;
    cout << dayStr[Monday] << endl;
    cout << dayStr[today] << endl;
    So he would be able to easily get the string.

    You could make a library with macros. But I cannot get my head of how to make this elegant for the users! But I am sure there is a way so the user can just type
    Code:
    DECL_ENUM(day)
      ENUM(Monday)
      ...
    END_ENUM
    and there will be an enum generated as well as an array of strings and finally a dayStr() function to get the appropriate string value.

  4. #34
    Programming Ninja In-T...
    Join Date
    May 2009
    Posts
    827
    Quote Originally Posted by StainedBlue View Post
    Hmmmm. So, I create an enum in a source or header file, parse it as text to return as a vector of strings representing the enum values?
    If you compile and run the program, passing to to it the --help argument, you will be informed how it works.
    Admittedly I didn't follow your explanation of the purpose of all this.

    I can understand the following transformations:

    text file -> c++

    c++ -> text file

    I can't wrap my brain around:

    c++ -> text file -> c++

    Is that what you're trying to do here?
    Not exactly...
    Its more like:

    enum in header or source file -> C++ string in program

    C++ string in program -> header or source file

    Essentially, what I do is I get the enum (various elements of it) of a source or header file passed to the program, then I write a class definition containing the enum values with a few functions to an output file (.h or cpp, .h by default). You can then use the functions generated by the program to get the enum value names and other elements of the enum.
    Last edited by Programmer_P; 06-05-2010 at 11:59 AM.

  5. #35
    Programming Ninja In-T...
    Join Date
    May 2009
    Posts
    827
    It can also handle multiple enums in one file.
    It'll generate a class and functions in a single output file for each enum found.

  6. #36
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    And alas after all this we realize that the class or object used to implement said feature is actually more complicated and involved than just using enums how the language intended us to use them. When someone tries to offer something more than the language offers but actually ends up being more complicated than just using the language the way it was intended...IE: enums.....I shy away from them.

    There is no problem being solved here and no feature set offered that cannot be done more efficiently another way. In other words C++ and the STL both offer solutions to this issue in a much more elegant fashion. Nice attempt at a util but not something I would use.

  7. #37
    Programming Ninja In-T...
    Join Date
    May 2009
    Posts
    827
    Quote Originally Posted by C_ntua View Post
    I think people are mentioning map because it offers a way for the programmer to do quite easily what your program in the first place. Obviously, it is better to offer yourself a way to do this than use any other external program. So somebody could do
    Code:
    enum day{
        Monday,
        Tuesday,
        ...
    };
    ...
    map<string,int> dayStr;
    dayStr.insert(par<string,int>(Monday, "Monday");
    dayStr.insert(par<string,int>(Tuesday, "Tuesday");
    ...
    day today = Monday;
    cout << dayStr[Monday] << endl;
    cout << dayStr[today] << endl;
    So he would be able to easily get the string.
    Yes, but as always, you still have to pass the string name of the enum value to get the string name back, which at its core, does NOT make any sense at all. You're getting the string names of enum values by passing those same names? Why not just create a vector of strings manually assigned the literal string names of the enum values, and don't bother with the map at all?
    The beauty of my program, like already mentioned, is it automatically generates the vector of string names of enum values you need for you (you don't have to do the hard work yourself). Plus, you still have the option of calling the const string& CnameOfEnumToStrings::getEnumValue(nameOfEnum enumValue) to get a specific enum value, if you want to do it that way, as well calling const int& CnameOfEnumToStrings::getNumOfEnumValues() to get the number of enum values in the enum.
    You could make a library with macros. But I cannot get my head of how to make this elegant for the users! But I am sure there is a way so the user can just type
    Code:
    DECL_ENUM(day)
      ENUM(Monday)
      ...
    END_ENUM
    and there will be an enum generated as well as an array of strings and finally a dayStr() function to get the appropriate string value.
    When you get that working, let me know...

  8. #38
    Programming Ninja In-T...
    Join Date
    May 2009
    Posts
    827
    Quote Originally Posted by Bubba View Post
    And alas after all this we realize that the class or object used to implement said feature is actually more complicated and involved than just using enums how the language intended us to use them. When someone tries to offer something more than the language offers but actually ends up being more complicated than just using the language the way it was intended...IE: enums.....I shy away from them.

    There is no problem being solved here and no feature set offered that cannot be done more efficiently another way. In other words C++ and the STL both offer solutions to this issue in a much more elegant fashion. Nice attempt at a util but not something I would use.
    Actually, there is a problem getting solved here: And that is being able to easily retrieve the enum value names of an enum, without hard-coding those string names yourself.
    But I'm done arguing this point with you...

    And for the record, my program's not that complicated or that difficult to use.

  9. #39
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    You are attempting to treat enumerations as strings and although they appear to be strings in the code they are not. It would be nice if the language allowed us to convert an enumeration value to it's C++ string equivalent but it does not and probably for good reason. This entire thread, IMO, comes from a pure misunderstanding of what enumerations are and an improper use of them. C++ offers many more solutions to this problem without attempting to redefine what an enumeration is.

    without hard-coding those string names yourself.
    Eh? Who is hard-coding anything? Calling an enumeration hard-coded is a bit of a misnomer. If you use an enumeration and make it available to those using your interface how is that hard-coded? If you want a generic feature in your interface then you would not use an enumeration. There are many many more ways to do this. One could load an XML file that would then map certain values to strings. If you wanted to add a feature then you simply change the file and when your code loads it, it works. You could then also provide a way for someone to add a callback and when the new 'feature' is found in the xml file you would then just callback to the user who added the feature and they would then know how to deal with it. I just don't understand the point here at all.

    Enumerations, by their very nature, are somewhat 'hard-coded' values but they are better than placing the raw value everywhere the enumerated value would be in the code. Bit wise comparisons and operations can also be performed on enumerations and are done quite often in a lot of code.

    Take for instance the Direct3D enumeration for memory pools:
    typedef enum D3DPOOL
    {
    D3DPOOL_DEFAULT = 0,
    D3DPOOL_MANAGED = 1,
    D3DPOOL_SYSTEMMEM = 2,
    D3DPOOL_SCRATCH = 3,
    D3DPOOL_FORCE_DWORD = 0x7fffffff,
    } D3DPOOL, *LPD3DPOOL;
    Are these hard-coded? From your point of view, certainly, but not from mine. These are the pools the library supports. It is far better for me to specify the pool I want as one of these enumerated types than it is to just pass in an integer. Can I check to see which pool a resource is allocated in via this enumeration? Certainly. If memory pools were something the designers thought would be ever-changing and ever-expanding then an enumeration would be a poor choice to store this information in since it would require you to recompile every time you change something.

    For instance a game event system would NOT be a good place for an enumeration. Every time someone needed to add or remove an event the enumeration would have to change and everyone would have to recompile. This would be a nightmare with many people working on the project. So the solution isn't to create some weird enumeration util but it is to use a different data structure for the task at hand. There is an article about this exact thing in Game Code Complete 2nd. ed. where the author states that their event system used an enumeration early on but later was changed due to the exact problems I just described. Lesson learned = use the correct data structure for the job.
    Last edited by VirtualAce; 06-05-2010 at 12:29 PM.

  10. #40
    ...and never returned. StainedBlue's Avatar
    Join Date
    Aug 2009
    Posts
    168
    Programmer_P said...

    If you compile and run the program, passing to to it the --help argument, you will be informed how it works.
    Yes I did that. I even looked through the source. *How* to use your program is not what confuses me; *why* I should use it, does.

    MK27 has pointed out one potential use, although there are many other ways to implement error checking and unit testing, and this route wouldn't be my first choice.
    goto( comeFrom() );

  11. #41
    Programming Ninja In-T...
    Join Date
    May 2009
    Posts
    827
    Quote Originally Posted by Bubba View Post
    You are attempting to treat enumerations as strings and although they appear to be strings in the code they are not.
    Actually, I wasn't attempting anything. I've already done it.
    And so what if I'm treating enumerations as strings? This can be useful at times, and it certainly is useful in a web coder program I'm writing right now.
    It would be nice if the language allowed us to convert an enumeration value to it's C++ string equivalent but it does not and probably for good reason. This entire thread, IMO, comes from a pure misunderstanding of what enumerations are and an improper use of them. C++ offers many more solutions to this problem without attempting to redefine what an enumeration is.
    Maybe so, but so far I haven't seen any examples of all those wonderful "solutions" to the problem which I already talked about in a different thread which I started.
    So it seems, you are wrong...

  12. #42
    ...and never returned. StainedBlue's Avatar
    Join Date
    Aug 2009
    Posts
    168
    Quote Originally Posted by Programmer_P View Post
    Actually, I wasn't attempting anything. I've already done it.
    And so what if I'm treating enumerations as strings? This can be useful at times, and it certainly is useful in a web coder program I'm writing right now.

    Maybe so, but so far I haven't seen any examples of all those wonderful "solutions" to the problem which I already talked about in a different thread which I started.
    So it seems, you are wrong...
    Ok, but this is the perspective others have....

    "Hi, I have a program that helps you add two numbers together. It's called AddTwoNumbers. This is how it works...

    Code:
    int add(int a, int b)
    {
        return a + b;
    }
    
    int main()
    {
        int a = 1, b = 1;
        int c = add(a, b);
    }
    ...Isn't that so much more convenient than using that confounded + operator? Enjoy!"
    goto( comeFrom() );

  13. #43
    Programming Ninja In-T...
    Join Date
    May 2009
    Posts
    827
    Quote Originally Posted by StainedBlue View Post
    Ok, but this is the perspective others have....

    "Hi, I have a program that helps you add two numbers together. It's called AddTwoNumbers. This is how it works...

    Code:
    int add(int a, int b)
    {
        return a + b;
    }
    
    int main()
    {
        int a = 1, b = 1;
        int c = add(a, b);
    }
    ...Isn't that so much more convenient than using that confounded + operator? Enjoy!"
    Those are actually two very different things.
    What my program does is something that nothing else that I'm aware of can do (at least, automatically). You guys have posted many different "solutions" for converting enum values to strings (no disrespect intended, of course), but so far, I have not seen any method which can replicate what I did (and that is, to provide a way of retrieving all enum values, or a specific enum value, without having to write all those enum value names again).

  14. #44
    ...and never returned. StainedBlue's Avatar
    Join Date
    Aug 2009
    Posts
    168
    Quote Originally Posted by Programmer_P View Post
    Those are actually two very different things.
    What my program does is something that nothing else that I'm aware of can do (at least, automatically). You guys have posted many different "solutions" for converting enum values to strings, but so far, I have not seen any method which can replicate what I did (and that is, to provide a way of retrieving all enum values, or a specific enum value, without having to write all those enum value names again).

    Arrrgh!

    But this is exactly why in 99.9% of designs needing something like this would use a MAP.

    Either an int mapped to a string, or vice versa, the MAP will accomplish this!



    ARRRRGGGGH!
    goto( comeFrom() );

  15. #45
    Programming Ninja In-T...
    Join Date
    May 2009
    Posts
    827
    Quote Originally Posted by StainedBlue View Post
    Arrrgh!

    But this is exactly why in 99.9% of designs needing something like this would use a MAP.

    Either an int mapped to a string, or vice versa, the MAP will accomplish this!



    ARRRRGGGGH!
    Even using maps, you still have to write the enum value names again, like was already demonstrated.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. im a noob at c++, do you think so?
    By belRasho in forum C++ Programming
    Replies: 6
    Last Post: 04-25-2010, 11:02 PM
  2. Client-server system with input from separate program
    By robot-ic in forum Networking/Device Communication
    Replies: 3
    Last Post: 01-16-2009, 03:30 PM
  3. help sharing a program with friends...
    By fingerlickin in forum C# Programming
    Replies: 1
    Last Post: 11-05-2005, 09:54 AM
  4. Replies: 3
    Last Post: 03-04-2005, 02:46 PM
  5. Making a program to free up Ram memory
    By cfrost in forum Windows Programming
    Replies: 1
    Last Post: 10-03-2004, 06:52 AM