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

  • Live Event Date: October 29, 2014 @ 11:00 a.m. ET / 8:00 a.m. PT Are you interested in building a cognitive application using the power of IBM Watson? Need a platform that provides speed and ease for rapidly deploying this application? Join Chris Madison, Watson Solution Architect, as he walks through the process of building a Watson powered application on IBM Bluemix. Chris will talk about the new Watson Services just released on IBM bluemix, but more importantly he will do a step by step cognitive …

  • A modern mobile IT strategy is no longer an option, it is an absolute business necessity. Today's most productive employees are not tied to a desk, an office, or a location. They are mobile. And your company's IT strategy has to be ready to support them with easy, reliable, 24/7 access to the business information they need, from anywhere in the world, across a broad range of communication devices. Here's how some of the nation's most progressive corporations are meeting the many needs of their mobile workers …

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds