Thread: Free program I'm sharing: ConvertEnumToStrings

  1. #1
    Programming Ninja In-T...
    Join Date
    May 2009
    Posts
    827

    Lightbulb Free program I'm sharing: ConvertEnumToStrings

    Well, I recently wrote a program, and thought I'd share it with the community here.
    Its called ConvertEnumToStrings, and the purpose is similiar to the name: to convert enums to strings.
    More specifically, the purpose is to be able to retrieve the names of enum values as strings.
    Yes, there are other methods around to accomplish this, but none that I know of other than mine can operate on just any enum. You'd have to write a method for each enum you write, in order to retrieve their string names. The beauty of my program is, you can get the enum value string names of ANY enum, provided it is formatted something like this:

    Code:
    enum NAMEOFENUM {
    
      enumValue1,
      enumValue2,
      enumValue3
    
    };
    or:

    Code:
    enum NAMEOFENUM {
    
      enumValue1 = 0,
      enumValue2 = 1,
      enumValue3 = 2, //note, if this were the last enum value, no comma should be there
      //etc...
    
    };
    It is especially important that each enum value is prefixed by at least one space. Otherwise it wont work (or it wont work WELL anyway...).

    Since I'm providing the source code, and not just the binary(ies), you are welcome to edit it as much as you like. But I provide no guarantees that the program will work correctly after you do this.

    Instructions:

    Download the .zip file containing the source and header files for ConvertEnumToStrings from this link.
    Extract everything from the .zip to somewhere on your hard drive, being sure to retain the directory structures.
    Add the sources and headers found in the "ConvertEnumToStrings" directory and all sub-directories to a new project file created in whatever IDE you use (I use Code::Blocks).
    Make sure your IDE is set to link.
    Build the project. It should compile without any errors.
    You should now have an executable you can run from whatever target directory you compiled it in.
    Now open up a Terminal or Command Prompt (depending on whether you're in Linux or Windows), or whatever name the command-line program is in your OS.
    Through the command-line, navigate to the directory (using the "cd" command or similiar) the "ConvertEnumToStrings" executable is in.
    Once there, run this if in Windows:
    Code:
    ConvertEnumToStrings.exe --help
    to get a description of how to use the program.

    Or this if in Linux:

    Code:
    ./ConvertEnumToStrings --help
    to get a description of how to use the program.
    Read what it says and respond accordingly.
    You should now be able to pass into the program an existing file with an enum in it, and have the program generate a header or source file (header by default) for you providing the method(s) to get different enum elements automatically, including the enum name, a vector of all the enum value names, or the number of enum values.

    Constructive criticisms/comments are welcome and encouraged.
    Last edited by Programmer_P; 06-03-2010 at 09:21 PM.

  2. #2
    ...and never returned. StainedBlue's Avatar
    Join Date
    Aug 2009
    Posts
    168
    Didn't compile for me. Not in Code::Blocks, or using my own automake routine. Same error using both methods though:

    Code:
    ~\ConvertEnumToStrings\ConvertEnumToStrings\module\CconvertEnumToStrings\CconvertEnumToStrings.cpp|3|error: 'CconvertEnumToStrings' has not been declared|
    
    ~\ConvertEnumToStrings\ConvertEnumToStrings\module\CconvertEnumToStrings\CconvertEnumToStrings.cpp|3|error: 'CconvertEnumToStrings' has not been declared|
    
    ~\ConvertEnumToStrings\ConvertEnumToStrings\module\CconvertEnumToStrings\CconvertEnumToStrings.cpp|3|error: expected constructor, destructor, or type conversion before '(' token
    goto( comeFrom() );

  3. #3
    Programming Ninja In-T...
    Join Date
    May 2009
    Posts
    827
    Quote Originally Posted by StainedBlue View Post
    Didn't compile for me. Not in Code::Blocks, or using my own automake routine. Same error using both methods though:

    Code:
    ~\ConvertEnumToStrings\ConvertEnumToStrings\module\CconvertEnumToStrings\CconvertEnumToStrings.cpp|3|error: 'CconvertEnumToStrings' has not been declared|
    
    ~\ConvertEnumToStrings\ConvertEnumToStrings\module\CconvertEnumToStrings\CconvertEnumToStrings.cpp|3|error: 'CconvertEnumToStrings' has not been declared|
    
    ~\ConvertEnumToStrings\ConvertEnumToStrings\module\CconvertEnumToStrings\CconvertEnumToStrings.cpp|3|error: expected constructor, destructor, or type conversion before '(' token
    Sorry, I didn't mean to include CconvertEnumToStrings.cpp. Delete that file from that project then try again. I'll re-upload the zip in a minute.

  4. #4
    Programming Ninja In-T...
    Join Date
    May 2009
    Posts
    827
    There, changed the download link to point to the corrected .zip.
    It should work now.

    Let me know if it doesn't.

  5. #5
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    Ok so this is essentially a std::map<unsigned int,std::string> ??

  6. #6
    Programming Ninja In-T...
    Join Date
    May 2009
    Posts
    827
    Quote Originally Posted by Bubba View Post
    Ok so this is essentially a std::map<unsigned int,std::string> ??
    Yeah, I suppose so...
    But I didn't know much about std::maps when I wrote it, so I uses classes instead.

  7. #7
    ...and never returned. StainedBlue's Avatar
    Join Date
    Aug 2009
    Posts
    168
    The initial explanation is way too long. As far as the program, for doing such a specific task, you should provide a little more leeway for how users may enter their enum

    Example
    Code:
    enum NAMEOFENUM {
    
      enumValue1 = 0,
      enumValue2 = 1,
      enumValue3 = 2, //note, if this were the last enum value, no comma should be there
      //etc...
    
    };
    is really kinda silly, a more realistic use would be:

    Code:
    enum foo{
    
    a = 2, b, c
    };

    So just check for everything between 'enum' and the next encountered '};' characters.
    goto( comeFrom() );

  8. #8
    Programming Ninja In-T...
    Join Date
    May 2009
    Posts
    827
    Quote Originally Posted by StainedBlue View Post
    The initial explanation is way too long.
    You are welcome to shorten it if you want...
    I think the explanation and what it states is necessary, even if its a little long.
    I might be able to change the wording a little bit to make it shorter, but I can't change what it basically says.
    As far as the program, for doing such a specific task, you should provide a little more leeway for how users may enter their enum

    Example
    Code:
    enum NAMEOFENUM {
    
      enumValue1 = 0,
      enumValue2 = 1,
      enumValue3 = 2, //note, if this were the last enum value, no comma should be there
      //etc...
    
    };
    is really kinda silly, a more realistic use would be:

    Code:
    enum foo{
    
    a = 2, b, c
    };

    So just check for everything between 'enum' and the next encountered '};' characters.
    Hmm...I may do that.

  9. #9
    Programming Ninja In-T...
    Join Date
    May 2009
    Posts
    827
    @StainedBlue: Did you test it on a file which contains an enum?

  10. #10
    ...and never returned. StainedBlue's Avatar
    Join Date
    Aug 2009
    Posts
    168
    All I'm saying is enums are really useful for defining a related set of values, and it's not convenient for your expected format:

    Code:
    enum Divs{
        wrapper = 960,
        content = 2*wrapper/3,
        sidebar = wrapper/3
    };
    may define the div widths of a web page.

    or

    Code:
    enum Notes{
        whole = 4,
        half = whole/2,
        quarter = half/2
    };
    might define the length of musical notes in 4/4 time
    goto( comeFrom() );

  11. #11
    Programming Ninja In-T...
    Join Date
    May 2009
    Posts
    827
    Quote Originally Posted by StainedBlue View Post
    All I'm saying is enums are really useful for defining a related set of values, and it's not convenient for your expected format:

    Code:
    enum Divs{
        wrapper = 960,
        content = 2*wrapper/3,
        sidebar = wrapper/3
    };
    may define the div widths of a web page.

    or

    Code:
    enum Notes{
        whole = 4,
        half = whole/2,
        quarter = half/2
    };
    might define the length of musical notes in 4/4 time
    Tested it with both, and it works with both...

  12. #12
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    Well reasons I would use std::map over this:

    • map is standardized
    • map has been used for many many many years and is well tested
    • map has O(n log n) lookup time
    • map uses a balanced red/black tree
    • most <algorithm> functions can be used on map
    • map is much more generic
    • since map does all of this I see no need to reinvent the wheel

  13. #13
    Programming Ninja In-T...
    Join Date
    May 2009
    Posts
    827
    Quote Originally Posted by Bubba View Post
    Well reasons I would use std::map over this:

    • map is standardized
    • map has been used for many many many years and is well tested
    • map has O(n log n) lookup time
    • map uses a balanced red/black tree
    • most <algorithm> functions can be used on map
    • map is much more generic
    • since map does all of this I see no need to reinvent the wheel
    And how exactly do you use an std::map to get an enum value's name?
    I'd like to see some example code for this...

  14. #14
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    Posts
    1,853
    Without looking exactly what your code does, you could use a map like
    Code:
    enum name{
        Mike = 0,
        John,
    };
    ...
    map<string,int> enumName;
    map.insert(par<string,int>(0, "Mike");
    map.insert(par<string,int>(1, "John");
    then make a class that contains the enum and the map. You will make sure it is read-only if you want and static. You could use it like
    Code:
    Name x = Name::Mike;
    cout << x.str() << endl;
    where x.str() could return the string. To do so it would do something like
    Code:
    string& str()
    {
        return this.innerMap[this.value];
    }
    and of course, you would overload the = operator so it assigns the value given to the "this.value". Where value would be an "enum name" and innerMap the map you use to map the string with the int value of the enum.

    So you could create an "Advanced Enum" in order to have the additional functionality you want. The program will replace an enum with the advanced class.

  15. #15
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    map.insert(par<string,int>(0, "Mike");
    map.insert(par<string,int>(1, "John");
    I'm assuming you meant pair not par.

    But yes you could do that or this:

    Code:
    class Foo
    {
       public:
          ...     
          typedef std::map<unsigned int,std::string> intToStringMap;
          typedef intToStringMap::iterator intToStringMapIter;
    
          bool AddMapping(unsigned int ID,const std::string &text);
          
       private:
          intToStringMap m_intToStringMap;
    };
    ...
    ...
    bool Foo::AddMapping(unsigned int ID,const std::string &text)
    {
        std::pair<intToStringMapIter,bool> pairResult = m_intToStringMap.insert(inToStringMap::value_type(ID,text));
        return pairResult.second;
    }
    A map is a data structure that maps one value to a unique key. Mapping an int to a string and vice versa is quite trivial. A multi-map can map a unique key to several values.
    Last edited by VirtualAce; 06-05-2010 at 12:43 AM.

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