Intelligent 'enum's in C++ Applications

Environment: C++
Keywords: Enumerations, strings, integers, automatic defaults.

On several occasions in the past I have found C++ enumerations lacking. Enums need an automatic default and Enums need string functionality along with the integer value, particularily in applications that interact with a user either on a GUI or in an error message.

I thus concieved the idea to represent Enums as C++ classes which provide all the standard functionality of an enum, by overiding the relevant operators. And then adding the extra functionality desired.

Rather than writing these classes, a utility which translates standard Enum declerations to such classes would be great. Along with the idea, I have supplied a utility EC (enum compiler) that will do the job on Microsoft platforms.

In other words if I supply the following:

  enum EState
  {
    Unknown = 0,
    Solid = 1,  // Solid Matter
    Liquid = 2, // "Liquid Matter "
    Gas,        /* Vapour */
  };
One could generate a class as follows:
  class EState
  {
  public:
   // EState::Unknown,Solid,Liquid,Gas,
   enum Enum
   {
     Unknown = 0,
     Solid   = 1,  // "Solid Matter"
     Liquid  = 2,  // "Liquid Matter "
     Gas,          // "Vapour"
   };

   Enum operator=(int i);
   operator int () const;

   Enum operator=(const char* sz);
   bool operator==(const char* sz);
   operator const char* () const;

   ...
  };

  EState::Map_t EState::m_Map[] =
  {
      {Unknown, "Unknown"},
      {Solid,   "Solid Matter"},
      {Liquid,  "Liquid Matter "},
      {Gas,     "Vapour"}
  };
which could then be used as an "intelligent" Enum.

These "intelligent" Enums can be used as follows:

  int X;
  ...
  EState eState;
  eState = X;
  if(eState == EState::Unknown) {...}
  ...
  printf("eState is '%s' [%d]", (const char*)eState, eState);
  ...
  eState = "Text String";
  if(eState == EState::Unknown) ...
  ...
making some parts of coding simpler and more understandable.

Downloads

Download EnumCompiler utility(Microsoft) and demo project - 16 Kb


Comments

Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • On-demand Event Event Date: December 18, 2014 The Internet of Things (IoT) incorporates physical devices into business processes using predictive analytics. While it relies heavily on existing Internet technologies, it differs by including physical devices, specialized protocols, physical analytics, and a unique partner network. To capture the real business value of IoT, the industry must move beyond customized projects to general patterns and platforms. Check out this webcast and join industry experts as …

  • On-demand Event Event Date: October 29, 2014 It's well understood how critical version control is for code. However, its importance to DevOps isn't always recognized. The 2014 DevOps Survey of Practice shows that one of the key predictors of DevOps success is putting all production environment artifacts into version control. In this webcast, Gene Kim discusses these survey findings and shares woeful tales of artifact management gone wrong! Gene also shares examples of how high-performing DevOps …

Most Popular Programming Stories

More for Developers

RSS Feeds