Touchless
enum to string
in C
No more enum mapping headaches

Arun Saha
Why

(Log) Message:
Error: Device 1234 failed.

What is device 1234,
btw, any name?
Nov 27 2013

Touchless enum to string

2
Why

For machines, numbers are natural,
but,
For humans, names are natural.

Nov 27 2013

Touchless enum to string

3
Why

(Log) Message:
Error: Device 1234 (cdrom)
failed.

Ooops…
aha! the cdrom
Nov 27 2013

Touchless enum to string

4
What

Number to Name
need
unique
bidirectional
(1:1)
mapping
Nov 27 2013

Touchless enum to string

5
What

Mapping Example
ColorCode (enum)
<->
ColorName (char const *)
Translation API
/// Map Number -> Name
ColorName colorName( ColorCode );
/// Map Name -> Number
ColorCode colorCode( ColorName );
Nov 27 2013

Touchless enum to string

6
How

Attempt #1: Array of strings
Numbers defined as enum
Names defined as array of char const *
In array of names, position/index of a string is same
as its code.
(A minor variation is to use a switch statement on the number (ColorCode))

Nov 27 2013

Touchless enum to string

7
How

Attempt #1: Array of strings
In the array, position/index of a string
is same as its code.
Issue 1: The sequence in the array is
dependent on the sequence in the
enum, any out of order element
messes up the mapping.
Nov 27 2013

Touchless enum to string

8
How

Attempt #1: Array of strings
In the array, position/index of a string
is same as its code.
Issue 2: For a (number, name) pair, the number
and the name are defined in two separate files;
out of sync file pair (e.g. maintainer added
enum in one file but missed to add name in the
other file) garbles the mapping.
Nov 27 2013

Touchless enum to string

9
How

Solution:
Define (number, name) pair in a
separate file
COLOR_DEF( ColorRed, “Red”)

Nov 27 2013

Touchless enum to string

10
How

Solution:
Define (number, name) pair in a
separate file as macro
#define COLOR_DEF( ColorRed, “Red”)

The author avoids macro as much as possible, but the current situation
is a good usage of macro, which is hard to obtain otherwise.
Nov 27 2013

Touchless enum to string

11
Solution Insight:
Different Macro Expansion

How

.h file:
• Expand macro to get number in enum
• #define COLOR_DEF(number, name) number,

.c file:
• Expand macro to get name in array
• #define COLOR_DEF(number, name) name,

Nov 27 2013

Touchless enum to string

12
How
.h file:
Expand macro to get number in enum

color.h
typedef enum ColorCode {
ColorFirst = 0,
#define COLOR_DEF( number, name ) number,
#include "color_defs.h"
#undef COLOR_DEF
ColorLast
} ColorCode;

color_defs.h
COLOR_DEF( ColorRed, "Red" )
COLOR_DEF( ColorYellow, "Yellow" )
COLOR_DEF( ColorGreen, "Green" )
color.c

.c file:
Expand macro to get name in array

static const ColorName colorNames[] = {
"First",
#define COLOR_DEF( number, name ) name,
#include "color_defs.h"
#undef COLOR_DEF
"Last"
};
How

Code available at github
https://github.com/arunksaha/enum_to_string

Nov 27 2013

Touchless enum to string

14
Thank you!
Comments are much appreciated
Arun Saha
http://www.linkedin.com/in/arunksaha

Nov 27 2013

Touchless enum to string

15

Touchless Enum to String in C

  • 1.
    Touchless enum to string inC No more enum mapping headaches Arun Saha
  • 2.
    Why (Log) Message: Error: Device1234 failed. What is device 1234, btw, any name? Nov 27 2013 Touchless enum to string 2
  • 3.
    Why For machines, numbersare natural, but, For humans, names are natural. Nov 27 2013 Touchless enum to string 3
  • 4.
    Why (Log) Message: Error: Device1234 (cdrom) failed. Ooops… aha! the cdrom Nov 27 2013 Touchless enum to string 4
  • 5.
  • 6.
    What Mapping Example ColorCode (enum) <-> ColorName(char const *) Translation API /// Map Number -> Name ColorName colorName( ColorCode ); /// Map Name -> Number ColorCode colorCode( ColorName ); Nov 27 2013 Touchless enum to string 6
  • 7.
    How Attempt #1: Arrayof strings Numbers defined as enum Names defined as array of char const * In array of names, position/index of a string is same as its code. (A minor variation is to use a switch statement on the number (ColorCode)) Nov 27 2013 Touchless enum to string 7
  • 8.
    How Attempt #1: Arrayof strings In the array, position/index of a string is same as its code. Issue 1: The sequence in the array is dependent on the sequence in the enum, any out of order element messes up the mapping. Nov 27 2013 Touchless enum to string 8
  • 9.
    How Attempt #1: Arrayof strings In the array, position/index of a string is same as its code. Issue 2: For a (number, name) pair, the number and the name are defined in two separate files; out of sync file pair (e.g. maintainer added enum in one file but missed to add name in the other file) garbles the mapping. Nov 27 2013 Touchless enum to string 9
  • 10.
    How Solution: Define (number, name)pair in a separate file COLOR_DEF( ColorRed, “Red”) Nov 27 2013 Touchless enum to string 10
  • 11.
    How Solution: Define (number, name)pair in a separate file as macro #define COLOR_DEF( ColorRed, “Red”) The author avoids macro as much as possible, but the current situation is a good usage of macro, which is hard to obtain otherwise. Nov 27 2013 Touchless enum to string 11
  • 12.
    Solution Insight: Different MacroExpansion How .h file: • Expand macro to get number in enum • #define COLOR_DEF(number, name) number, .c file: • Expand macro to get name in array • #define COLOR_DEF(number, name) name, Nov 27 2013 Touchless enum to string 12
  • 13.
    How .h file: Expand macroto get number in enum color.h typedef enum ColorCode { ColorFirst = 0, #define COLOR_DEF( number, name ) number, #include "color_defs.h" #undef COLOR_DEF ColorLast } ColorCode; color_defs.h COLOR_DEF( ColorRed, "Red" ) COLOR_DEF( ColorYellow, "Yellow" ) COLOR_DEF( ColorGreen, "Green" ) color.c .c file: Expand macro to get name in array static const ColorName colorNames[] = { "First", #define COLOR_DEF( number, name ) name, #include "color_defs.h" #undef COLOR_DEF "Last" };
  • 14.
    How Code available atgithub https://github.com/arunksaha/enum_to_string Nov 27 2013 Touchless enum to string 14
  • 15.
    Thank you! Comments aremuch appreciated Arun Saha http://www.linkedin.com/in/arunksaha Nov 27 2013 Touchless enum to string 15