SlideShare a Scribd company logo
1 of 15
Download to read offline
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

More Related Content

What's hot

Tree and binary tree
Tree and binary treeTree and binary tree
Tree and binary treeZaid Shabbir
 
Chapter3 Search
Chapter3 SearchChapter3 Search
Chapter3 SearchKhiem Ho
 
Tries - Tree Based Structures for Strings
Tries - Tree Based Structures for StringsTries - Tree Based Structures for Strings
Tries - Tree Based Structures for StringsAmrinder Arora
 
Breadth First Search Algorithm In 10 Minutes | Artificial Intelligence Tutori...
Breadth First Search Algorithm In 10 Minutes | Artificial Intelligence Tutori...Breadth First Search Algorithm In 10 Minutes | Artificial Intelligence Tutori...
Breadth First Search Algorithm In 10 Minutes | Artificial Intelligence Tutori...Edureka!
 
Regex Presentation
Regex PresentationRegex Presentation
Regex Presentationarnolambert
 
Dfs presentation
Dfs presentationDfs presentation
Dfs presentationAlizay Khan
 
Procedures And Functions in Matlab
Procedures And Functions in MatlabProcedures And Functions in Matlab
Procedures And Functions in MatlabDataminingTools Inc
 
Data structures final lecture 1
Data structures final  lecture 1Data structures final  lecture 1
Data structures final lecture 1Nazir Ahmed
 
Binary tree and Binary search tree
Binary tree and Binary search treeBinary tree and Binary search tree
Binary tree and Binary search treeMayeesha Samiha
 
Python advanced 2. regular expression in python
Python advanced 2. regular expression in pythonPython advanced 2. regular expression in python
Python advanced 2. regular expression in pythonJohn(Qiang) Zhang
 
lecture 17
lecture 17lecture 17
lecture 17sajinsc
 
Strings in Python
Strings in PythonStrings in Python
Strings in Pythonnitamhaske
 
Data Structure and Algorithms Binary Tree
Data Structure and Algorithms Binary TreeData Structure and Algorithms Binary Tree
Data Structure and Algorithms Binary TreeManishPrajapati78
 
Data structure and algorithm All in One
Data structure and algorithm All in OneData structure and algorithm All in One
Data structure and algorithm All in Onejehan1987
 
Regular Expressions in PHP
Regular Expressions in PHPRegular Expressions in PHP
Regular Expressions in PHPAndrew Kandels
 

What's hot (20)

Tree and binary tree
Tree and binary treeTree and binary tree
Tree and binary tree
 
Chapter3 Search
Chapter3 SearchChapter3 Search
Chapter3 Search
 
Tries - Tree Based Structures for Strings
Tries - Tree Based Structures for StringsTries - Tree Based Structures for Strings
Tries - Tree Based Structures for Strings
 
Breadth First Search Algorithm In 10 Minutes | Artificial Intelligence Tutori...
Breadth First Search Algorithm In 10 Minutes | Artificial Intelligence Tutori...Breadth First Search Algorithm In 10 Minutes | Artificial Intelligence Tutori...
Breadth First Search Algorithm In 10 Minutes | Artificial Intelligence Tutori...
 
Regular Expressions
Regular ExpressionsRegular Expressions
Regular Expressions
 
Regex Presentation
Regex PresentationRegex Presentation
Regex Presentation
 
Dfs presentation
Dfs presentationDfs presentation
Dfs presentation
 
Procedures And Functions in Matlab
Procedures And Functions in MatlabProcedures And Functions in Matlab
Procedures And Functions in Matlab
 
Data structures final lecture 1
Data structures final  lecture 1Data structures final  lecture 1
Data structures final lecture 1
 
Binary tree and Binary search tree
Binary tree and Binary search treeBinary tree and Binary search tree
Binary tree and Binary search tree
 
Python advanced 2. regular expression in python
Python advanced 2. regular expression in pythonPython advanced 2. regular expression in python
Python advanced 2. regular expression in python
 
PHP Regular Expressions
PHP Regular ExpressionsPHP Regular Expressions
PHP Regular Expressions
 
lecture 17
lecture 17lecture 17
lecture 17
 
binary search tree
binary search treebinary search tree
binary search tree
 
Strings in Python
Strings in PythonStrings in Python
Strings in Python
 
Data Structure and Algorithms Binary Tree
Data Structure and Algorithms Binary TreeData Structure and Algorithms Binary Tree
Data Structure and Algorithms Binary Tree
 
Data structure and algorithm All in One
Data structure and algorithm All in OneData structure and algorithm All in One
Data structure and algorithm All in One
 
Regular Expressions in PHP
Regular Expressions in PHPRegular Expressions in PHP
Regular Expressions in PHP
 
Bfs dfs
Bfs dfsBfs dfs
Bfs dfs
 
DFS and BFS
DFS and BFSDFS and BFS
DFS and BFS
 

Similar to Touchless Enum to String in C

Hello, I need help writing a code in C. It wants to do things with f.pdf
Hello, I need help writing a code in C. It wants to do things with f.pdfHello, I need help writing a code in C. It wants to do things with f.pdf
Hello, I need help writing a code in C. It wants to do things with f.pdfmohamednihalshahru
 
Data structure.pptx
Data structure.pptxData structure.pptx
Data structure.pptxSajalFayyaz
 
Unitii classnotes
Unitii classnotesUnitii classnotes
Unitii classnotesSowri Rajan
 
Odoo ORM Methods | Object Relational Mapping in Odoo15
Odoo ORM Methods | Object Relational Mapping in Odoo15 Odoo ORM Methods | Object Relational Mapping in Odoo15
Odoo ORM Methods | Object Relational Mapping in Odoo15 Celine George
 
Java Extension Methods
Java Extension MethodsJava Extension Methods
Java Extension MethodsAndreas Enbohm
 

Similar to Touchless Enum to String in C (8)

Hello, I need help writing a code in C. It wants to do things with f.pdf
Hello, I need help writing a code in C. It wants to do things with f.pdfHello, I need help writing a code in C. It wants to do things with f.pdf
Hello, I need help writing a code in C. It wants to do things with f.pdf
 
Perl_Part4
Perl_Part4Perl_Part4
Perl_Part4
 
2 Prolog L 1 V2.ppt
2 Prolog L 1 V2.ppt2 Prolog L 1 V2.ppt
2 Prolog L 1 V2.ppt
 
Shishirppt
ShishirpptShishirppt
Shishirppt
 
Data structure.pptx
Data structure.pptxData structure.pptx
Data structure.pptx
 
Unitii classnotes
Unitii classnotesUnitii classnotes
Unitii classnotes
 
Odoo ORM Methods | Object Relational Mapping in Odoo15
Odoo ORM Methods | Object Relational Mapping in Odoo15 Odoo ORM Methods | Object Relational Mapping in Odoo15
Odoo ORM Methods | Object Relational Mapping in Odoo15
 
Java Extension Methods
Java Extension MethodsJava Extension Methods
Java Extension Methods
 

Recently uploaded

Scanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL CertsScanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL CertsRizwan Syed
 
Unraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdfUnraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdfAlex Barbosa Coqueiro
 
Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?Mattias Andersson
 
Science&tech:THE INFORMATION AGE STS.pdf
Science&tech:THE INFORMATION AGE STS.pdfScience&tech:THE INFORMATION AGE STS.pdf
Science&tech:THE INFORMATION AGE STS.pdfjimielynbastida
 
"Federated learning: out of reach no matter how close",Oleksandr Lapshyn
"Federated learning: out of reach no matter how close",Oleksandr Lapshyn"Federated learning: out of reach no matter how close",Oleksandr Lapshyn
"Federated learning: out of reach no matter how close",Oleksandr LapshynFwdays
 
Understanding the Laravel MVC Architecture
Understanding the Laravel MVC ArchitectureUnderstanding the Laravel MVC Architecture
Understanding the Laravel MVC ArchitecturePixlogix Infotech
 
Beyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry InnovationBeyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry InnovationSafe Software
 
Streamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project SetupStreamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project SetupFlorian Wilhelm
 
WordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your BrandWordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your Brandgvaughan
 
Connect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck PresentationConnect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck PresentationSlibray Presentation
 
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmaticsKotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmaticscarlostorres15106
 
Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024Scott Keck-Warren
 
Gen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdfGen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdfAddepto
 
Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024Enterprise Knowledge
 
Dev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio WebDev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio WebUiPathCommunity
 
Unleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding ClubUnleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding ClubKalema Edgar
 
CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):comworks
 
SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024Scott Keck-Warren
 
"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii Soldatenko"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii SoldatenkoFwdays
 
Human Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsHuman Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsMark Billinghurst
 

Recently uploaded (20)

Scanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL CertsScanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL Certs
 
Unraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdfUnraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdf
 
Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?
 
Science&tech:THE INFORMATION AGE STS.pdf
Science&tech:THE INFORMATION AGE STS.pdfScience&tech:THE INFORMATION AGE STS.pdf
Science&tech:THE INFORMATION AGE STS.pdf
 
"Federated learning: out of reach no matter how close",Oleksandr Lapshyn
"Federated learning: out of reach no matter how close",Oleksandr Lapshyn"Federated learning: out of reach no matter how close",Oleksandr Lapshyn
"Federated learning: out of reach no matter how close",Oleksandr Lapshyn
 
Understanding the Laravel MVC Architecture
Understanding the Laravel MVC ArchitectureUnderstanding the Laravel MVC Architecture
Understanding the Laravel MVC Architecture
 
Beyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry InnovationBeyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
 
Streamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project SetupStreamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project Setup
 
WordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your BrandWordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your Brand
 
Connect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck PresentationConnect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck Presentation
 
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmaticsKotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
 
Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024
 
Gen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdfGen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdf
 
Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024
 
Dev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio WebDev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio Web
 
Unleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding ClubUnleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding Club
 
CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):
 
SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024
 
"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii Soldatenko"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii Soldatenko
 
Human Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsHuman Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR Systems
 

Touchless Enum to String in C

  • 1. Touchless enum to string in C No more enum mapping headaches Arun Saha
  • 2. Why (Log) Message: Error: Device 1234 failed. What is device 1234, btw, any name? Nov 27 2013 Touchless enum to string 2
  • 3. Why For machines, numbers are natural, but, For humans, names are natural. Nov 27 2013 Touchless enum to string 3
  • 4. Why (Log) Message: Error: Device 1234 (cdrom) failed. Ooops… aha! the cdrom Nov 27 2013 Touchless enum to string 4
  • 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: 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
  • 8. 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
  • 9. 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
  • 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 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
  • 13. 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" };
  • 14. How Code available at github https://github.com/arunksaha/enum_to_string Nov 27 2013 Touchless enum to string 14
  • 15. Thank you! Comments are much appreciated Arun Saha http://www.linkedin.com/in/arunksaha Nov 27 2013 Touchless enum to string 15