Thread: Free program I'm sharing: ConvertEnumToStrings

  1. #46
    ...and never returned. StainedBlue's Avatar
    Join Date
    Aug 2009
    Posts
    168
    I give up. Contact someone at boost and explain to them you need lexical cast to work with enums, or heck, call up my buddy Bjarne and tell him to work this into the next standard.

    Edit: Thank you for this program, C++ was totally lost without it.


    I joke. Nothing personal.
    goto( comeFrom() );

  2. #47
    Programming Ninja In-T...
    Join Date
    May 2009
    Posts
    827
    Quote Originally Posted by StainedBlue View Post
    I give up. Contact someone at boost and explain to them you need lexical cast to work with enums, or heck, call up my buddy Bjarne and tell him to work this into the next standard.
    That wont be necessary, because its a problem I already solved.
    hahaha.

  3. #48
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    I think you need to listen to the voice of reason here. We are not downing your program or your abilities but more we are a bit critical of the design philosophy behind it. What problem is this solving that you could not solve in a much better fashion or solve by using a better data structure? Enumerations were not designed to do what you want here.

    What you are essentially doing here is creating an associative array which isn't anything new or anything profound.

  4. #49
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Programmer_P
    That wont be necessary, because its a problem I already solved.
    Considering that you have to run a program rather than make use of a language feature or library, I would say that you have not solved this "problem" in an acceptable manner.

    At this point, I am moving this thread to the Projects and Job Recruitment forum.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #50
    ...and never returned. StainedBlue's Avatar
    Join Date
    Aug 2009
    Posts
    168
    Quote Originally Posted by Programmer_P View Post
    That wont be necessary, because its a problem I already solved.
    hahaha.
    Yes, and it was a big problem.
    goto( comeFrom() );

  6. #51
    Programming Ninja In-T...
    Join Date
    May 2009
    Posts
    827
    Quote Originally Posted by Bubba View Post
    I think you need to listen to the voice of reason here.
    To the best of my knowledge, it is not me who is not listening to the voice of reason here...
    I already explained multiple times why I found that I needed this program.
    Its not that I'm not willing to adjust my views if someone shows me a better way, but so far it seems everyone just wants to criticise my program.
    We are not downing your program or your abilities but more we are a bit critical of the design philosophy behind it. What problem is this solving that you could not solve in a much better fashion or solve by using a better data structure?
    Here we go again, talking in abstract concepts...
    Saying I can do things in a "much better fashion" without showing me an example of what you mean does not help me at all.
    And I already explained what problem its solving.
    Enumerations were not designed to do what you want here.
    Well, regardless of whether they were designed to or not, I'm using an enum because I think I need to (i.e. no one has really showed me any better way to solve the problem in my web coder program), I believe I need to have the string names of the enum values, and so I went and wrote a program to do that, and so now I think everything's just peachy.
    What you are essentially doing here is creating an associative array which isn't anything new or anything profound.
    I don't know what a "associative array" means, but I do know my program IS doing something new. And that is allowing easy access to the string names of enum values, without having to write up the names again.

  7. #52
    Programming Ninja In-T...
    Join Date
    May 2009
    Posts
    827
    Quote Originally Posted by laserlight View Post
    Considering that you have to run a program rather than make use of a language feature or library, I would say that you have not solved this "problem" in an acceptable manner.
    Hmm...sounds awfully like an opinion to me.
    At this point, I am moving this thread to the Projects and Job Recruitment forum.
    Ok, I don't really care what forum my thread is in, so long as its not deleted.

  8. #53
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Programmer_P
    Hmm...sounds awfully like an opinion to me.
    So? The fact that one has to run a program is cumbersome, especially considering that its use will be rare. I suspect that copy and paste will be faster, especially with an editor that supports stuff like column based editing. On the other hand, if you had a library that was able to generate the desired constructs for you, say via template metaprogramming or something, or even some ugly macros, then this would be less cumbersome and better for maintenance.

    EDIT:
    An example of this program versus feature thing is in SQLite, a relational database engine: until recently, SQLite did not enforce foreign key constraints. You could simulate it with triggers, and a program was written to generate these triggers. The program was even included in the SQLite source. But it was generally unpopular, because it required an extra step outside of the normal way of doing things. Now, SQLite does natively enforce foreign key constraints, if you enable the feature.
    Last edited by laserlight; 06-05-2010 at 01:07 PM.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  9. #54
    Programming Ninja In-T...
    Join Date
    May 2009
    Posts
    827
    Quote Originally Posted by laserlight View Post
    So? The fact that one has to run a program is cumbersome, especially considering that its use will be rare.
    I disagree on that point. Since its use will be rare, having to run it wont be so "cumbersome", after all, will it...
    I suspect that copy and paste will be faster, especially with an editor that supports stuff like column based editing.
    I doubt it.
    On the other hand, if you had a library that was able to generate the desired constructs for you, say via template metaprogramming or something, or even some ugly macros, then this would be less cumbersome and better for maintenance.
    Well, "the floor is open to you" to do that anytime.
    Until then, I'll use my method.

  10. #55
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Programmer_P
    I disagree on that point. Since its use will be rare, having to run it wont be so "cumbersome", after all, will it...
    It will, because it will not be at hand, e.g., it may be left out when setting up one's development environment. Contrast this with a compiler, which is often used and is typically immediately available in a development environment.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  11. #56
    Programming Ninja In-T...
    Join Date
    May 2009
    Posts
    827
    Quote Originally Posted by laserlight View Post
    It will, because it will not be at hand, e.g., it may be left out when setting up one's development environment. Contrast this with a compiler, which is often used and is typically immediately available in a development environment.
    Well, I would say that is the fault of whoever needs it and doesn't have it. lol

  12. #57
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    Posts
    1,853
    Quote Originally Posted by Programmer_P View Post
    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?
    Look at my second example, the dataStr[today]. You will get "Monday". How would you do that with a vector?? The programmer still has to do some work, so it is not a solution, just a way to do it.

    Quote Originally Posted by Programmer_P View Post
    When you get that working, let me know...
    Look here

    I can go as far as (for enums starting with 0)
    Code:
    #define DECL_ENUM(type) typedef enum type ## _{
    #define Enum(value) value
    #define END_ENUM(type) } type;
    
    #undef DECL_ENUM
    #undef ENUM
    #undef END_ENUM
    
    #define DECL_ENUM(type) char* type ##_names[] = {
    #define Enum(value) #value
    #define END_ENUM(type) ;} /
      char* type ## Str(type var) { return type ## _names[type]; }
    This is probably not complete, but I don't have the knowledge to make it work. I am sure there are ways. You can now
    Code:
    DECL_ENUM(Day)
     ENUM(Monday),
     ENUM(Tuesday),
     ...
    END_ENUM(Day)
    
    ...
    
    Day myDay = Monday;
    cout << DayStr(myDay) << endl;
    Maybe even better make a class with the result
    Code:
    Day myDay = Day::Monday;
    cout << myDay.str() << endl;

  13. #58
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    5,108
    The way you have it posted wouldn't work. You would need to duplicate the definition block after and between the macro definition and redefinition. You could use some trickery to simplify the use, but you could define a macro as the enumeration value pairs and have it done in one pass instead.

    I have not seen any method which can replicate [...] a way of retrieving all enum values [...] without having to write all those enum value names again
    I can think of a way to do this with iteration, a null terminated list, and one simple macro for an interface.

    The only thing you've done is generate a string/value association from a string/value association. You can do this a dozen different ways using macros with your choice of `std::map', free functions, or both.

    I'm not questioning the need for this. I'm not questioning the "reinvention of the wheel" for the sake of education. I don't doubt you have an immediate reason why you think you need this. That said, when a dozen skilled programmers are telling you the same thing, odds are that you should listen to what they are saying.

    Soma

  14. #59
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    Posts
    1,853
    Quote Originally Posted by phantomotap View Post
    The way you have it posted wouldn't work. You would need to duplicate the definition block after and between the macro definition and redefinition. You could use some trickery to simplify the use, but you could define a macro as the enumeration value pairs and have it done in one pass instead.

    I can think of a way to do this with iteration, a null terminated list, and one simple macro for an interface.

    The only thing you've done is generate a string/value association from a string/value association. You can do this a dozen different ways using macros with your choice of `std::map', free functions, or both.

    I'm not questioning the need for this. I'm not questioning the "reinvention of the wheel" for the sake of education. I don't doubt you have an immediate reason why you think you need this. That said, when a dozen skilled programmers are telling you the same thing, odds are that you should listen to what they are saying.

    Soma
    Can you give me the code so I can learn something different That is where my macro knowledge stops...

    It would be nice if there was a language support on this and maybe in the future there would be one, as there is in C# for example. But I don't think its that a hard work to do and you won't usually need to do this a lot in a program.

    So Programmer_P did a good attempt to solve the problem. The best attempt as mentioned is to do this without an external program.
    As for the program usefulness itself, it you had a huge number of enums, you might want to use it. If you had a couple, you should not be lazy and provide the strings yourself. If the enums are heavily used, you should provide a class in the first place.

  15. #60
    Programming Ninja In-T...
    Join Date
    May 2009
    Posts
    827
    Quote Originally Posted by phantomotap View Post
    I can think of a way to do this with iteration, a null terminated list, and one simple macro for an interface.
    Well, feel free to post that "iteration, a null terminated list, and one simple macro for an interface"...
    The only thing you've done is generate a string/value association from a string/value association. You can do this a dozen different ways using macros with your choice of `std::map', free functions, or both.
    Not exactly: that's not the only thing I did.
    I provided a means of getting a reference to a vector of all enum value names, without typing up all the enum values again.
    I'm not questioning the need for this. I'm not questioning the "reinvention of the wheel" for the sake of education. I don't doubt you have an immediate reason why you think you need this. That said, when a dozen skilled programmers are telling you the same thing, odds are that you should listen to what they are saying.

    Soma
    Oh, trust me...I listened and am still listening.
    But as yet, no one has yet posted an example of a truly better method of what my program does. And I mean the automation part.

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