473,396 Members | 2,002 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Enum with name string, best implementation

Hi Everyone,

I've been browsing around for solutions to how to have an enum that gives me a name string. Some rather complicated ones can be found on the gamedev forum and Code Project.

I am thinking about simply writing a script that takes a list of names and generates a class like so.
Expand|Select|Wrap|Line Numbers
  1. class GangstaEnum
  2. {
  3. public:
  4.    int VanillaIce = 0;
  5.    int MrT = 1;
  6.    int MrRogers = 2;
  7.  
  8.    char* get_str(int idx){return gangstas[idx];}
  9.  
  10. private:
  11.    char[3] gangstas = {"VanillaIce", "MrT"};
  12.  
and then just using the class like an enum. The downside is that I would have to rerun my script every time the list changed. The upside is that its extremely simple.

Does anyone know why I should favor more complicated methods using macros or STL maps? God didn't give me enough patience for Macros.
Jan 11 '11 #1
11 3453
weaknessforcats
9,208 Expert Mod 8TB
I would use an STL vector and put all of my strings in there. Then I can use the find algorithm to locate a string. The algorithm will return an iterator to your string. The iterator is a pointer to your string.
Jan 12 '11 #2
I don't like that as much. People will be using my code in a library and I like them to be able to see all the possible members somehow.

Thanks for the reply though
Jan 13 '11 #3
weaknessforcats
9,208 Expert Mod 8TB
An enum is just a named integer value. It's not an actual variable. That means using an enum is a compile-time activity.

The fact you developed a class leads me to believe you have a run-time requirement rather than a compile time requirement.

If you need to use your own class at run time, then you mught consider Singleton objects with a registry.
Jan 14 '11 #4
In a simple eample, the user code will be calling a method send:
Expand|Select|Wrap|Line Numbers
  1. Library::send(MsgType, MsgData);
There are about 400+ possible message types. Inside the library method I need to convert the MsgType argument into the corresponding string.

So perfect scenario, the user (in the code, not during run-time) passes a custom type that displays all options (in vs with intellisense) and is restricted to those options, and then that custom type can be converted to string within library.

An Enum will restrict values and display all types, but not convert to string. My class will display all values and convert to string, but not restrict values. I believe a macro would do all three but be a pain-in-the-you-know-what and could confuse future coders as well as myself.
Jan 14 '11 #5
weaknessforcats
9,208 Expert Mod 8TB
Try this:

Expand|Select|Wrap|Line Numbers
  1. class Message
  2. {
  3.    virtual void ReadMessage();
  4.  
  5. };
  6. class LunchTime : public Message
  7. {
  8.      virtual void ReadMessage();
  9.  
  10. };
  11.  
  12. class Temperature : public Message
  13. {
  14.      virtual void ReadMessage();
  15.  
  16. };
  17.  
  18.  
  19. Then in main() you:
  20.  
  21. Message* data = new LunchMessage("Louie's as 12:00");
  22.  
  23. data->ReadMessage();  //calls LunchMessage::ReadMessage
  24.  
  25. delete data;
  26.  
  27. data = new Temperature(45);
  28.  
  29. data->ReadMessage(); calls Temperature::ReadMessage
By setting up a polymorphism your 400 message types become derived classes and each derived class knows how to format it's own message. Then in the user code you create a derived object but use it as a base class pointer.

That means the classes know how to format their messages. And that means you don't need to do that yourself. Your enum approach looks something you might use in C where no object-oriented features exist.

The trick is substituting a derived class pointer as the argument to a function expecting a base class pointer. The virtual keyword insure the derived class method is called.

What do you think?
Jan 16 '11 #6
Thanks Mr. cats, the replies helped me process my options. I think the final code will have an enum wrapped in a class with a string array, and I'll force the user to access the enum through the class and use the class like a virtual singleton.

Your suggestion is perfect, but the example I gave was over-simplified; in this case the message with 400+ types is already inheriting from a message base class and a type in its own right. So I've actually already implemented your suggestion in one layer.
Jan 17 '11 #7
weaknessforcats
9,208 Expert Mod 8TB
Your problem will be that to add a message you will have to change your class by adding a new enum and a new string and a new format converter. Should you have your code installed on user computers you will need to upgrade every one of them.

By using polymorphism, you just add a new class and recompile. None of the older code needs to be changed. This is the beauty of polymorphism. Code written in 1990 when no one had every heard of MESSAGE_FROM_WEANESSFORCATS can be used to process those messages. You just create a derived object and send it's address into your old code as a base class address and your old code will call your new object. Think about it
Jan 17 '11 #8
Wait, I might be a little confused, but it seems to me that in both cases you have to recompile and redistribute the library. In one case I add an enum and an element to the string array, and in the other you add a class. These message subtypes need no different formatting methods or members different from the other subtypes and I can't see how the user code would be impacted by adding a couple of elements to the class.

If these message types did, on the other hand, require unique members and methods from each other, then another layer of abstraction would definitely be in order.
Jan 17 '11 #9
weaknessforcats
9,208 Expert Mod 8TB
Keep in mind that your library would contain functions whose arguments were pointers to base class objects. Therefore, the library never needs recompiling. You only recompile the program thet uses the library.
Jan 17 '11 #10
Only if the user is defining the child classes, right? In my case the child classes will be defined within the library.
Jan 17 '11 #11
weaknessforcats
9,208 Expert Mod 8TB
Then you are back to using a vector or perhaps a map if you need am integer value to identify a string:

Expand|Select|Wrap|Line Numbers
  1. map<enum value, string> MyMessages;
You could just call a function at the beginning of
main() to load the map from a text file that contains the enum value and the associated string.

Or use the singleton class you are thinking about anyway. You would load the map in the singleton constructor by calling the same function you would have called at the beginning of main().

If you use a map then you add messages by modifying a data file and not your code. The preferred approach is to not modify already written and debugged code.
Jan 17 '11 #12

Sign in to post your reply or Sign up for a free account.

Similar topics

8
by: Kamil Grymuza | last post by:
Hi Is it possible to have a name of enumerated value in string somehow? enum { ID1 = 1, ID2=2, } And can I somehow make something like cout<<ID1<<endl; and heve printed "ID1" not 1?
1
by: A.M | last post by:
Hi, enum LogType :string{}; I underestand that I can't have enumeration of strings. Is there any turn around for this problem? Thanks, Ali
1
by: Stelios Skiathitis | last post by:
In ASP.NET I want to create a form for authenticating a user to enter in a local intranet site. I want the user to have the option of selecting two different authentication modes. The first using...
2
by: Cc | last post by:
is there a way to create enum that contain string?
13
by: Don | last post by:
How do I get an Enum's type using only the Enum name? e.g. Dim enumType as System.Type Dim enumName as String = "MyEnum" enumType = ???(enumName)
5
by: Barry | last post by:
Hello, In VS2003, I tried using an enum and setting it into a field in a datarow. It seems as if the datatype of the field in the row determined what went into the field. If the datatype was...
8
by: tony | last post by:
Hello! I have below a for loop and a switch in the for loop. I have also a enum called colBlowStep with some values. I have also an array called m_columnBlowStep with some strings. All items in...
0
by: dejawoo | last post by:
Hi, I vebeen digging couple of days internet to find some solid information. Unfortunately no luck. I have seen similar question I just want to append it here I am trying to get a reference...
5
by: surapong | last post by:
Is there any easy way to extract the string from enum variable?? e.g. I have enum TheEnum {ONE, TWO, THREE}; I would like to generate the array of string containing {"ONE", "TWO", "THREE"}
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.