Click here to Skip to main content
Click here to Skip to main content
 
Add your own
alternative version

EnumBinder - Bind C++ enums to strings, combo-boxes, arbitrary data structures

, 15 Aug 2005 CPOL
An easy way to bind C++ enums to strings, combo-boxes, list-boxes, arbitrary data structures.
enumbinder_exe.zip
EnumBinder.exe
enumbinder_sln.zip
EnumBinder
res
EnumBinder.ico
EnumBinder.manifest
weekday.ico
weekend.ico
enumbinder_src.zip
// EnumBinderDlg.h : header file
//
#pragma once
#include "afxwin.h"
#include "EnumBinder.h"

////////////////////////////////////////////////////////////////////////////
// Example 1 - standard binding of enum to two strings 
//             (one string for files/registry, one for users to see)
////////////////////////////////////////////////////////////////////////////

enum eShapes { // standard C++ enum
	eSquare,
	eCircle,
	eRectangle,
	eTriangle,
	eOctagon,
	ePentacontagon,
};

BIND_ENUM(eShapes, EnumShapes)
{
	{ eSquare       , _T("Square")       , _T("Square (4 equal length edges)") },
	{ eCircle       , _T("Circle")       , _T("Circle (1 continuous edge)")    },
	{ eRectangle    , _T("Rectangle")    , _T("Rectangle (4 edges)")           },
	{ eTriangle     , _T("Triangle")     , _T("Triangle (3 edges)")            },
	{ eOctagon      , _T("Octagon")      , _T("Octagon (8 edges)")             },
	{ ePentacontagon, _T("Pentacontagon"), _T("Pentacontagon (tons of edges)") }, // actually 50 edges
};

////////////////////////////////////////////////////////////////////////////
// Example 2 - unit test failure
//             (ensures 1-1 correspondence between enums and code strings)
////////////////////////////////////////////////////////////////////////////

enum eFruit {
	eApple,
	eOrange,
	ePear,
	ePlum,
	eBanana,
};

////////////////////////////////////////////////////////////////////////////////////////
// *** NOTE ***
// Un-comment any of the follow 3 comment lines in the 
// following BIND_ENUM structure to see a unit test failure
////////////////////////////////////////////////////////////////////////////////////////
BIND_ENUM(eFruit, EnumFruit)
{
	{ eApple , _T("apple") , _T("Apples")  },
	{ eOrange, _T("orange"), _T("Oranges") },
	{ ePear  , _T("pear")  , _T("Pears")   },	
	//{ ePlum  , _T("orange"), _T("Plums")   }, // error 1: forgot to change "orange" to "plum"
	//{ eApple , _T("banana"), _T("Banana")  }, // error 2: forgot to change "eApple" to "eBanana"
	//{ eOrange, _T("orange"), _T("Oranges") }, // error 3: total duplicate of another line
};

////////////////////////////////////////////////////////////////////////////
// Example 3 - custom data
////////////////////////////////////////////////////////////////////////////
enum eWeekdays
{
	eSunday,
	eMonday,
	eTuesday,
	eWednesday,
	eThursday,
	eFriday,
	eSaturday,
};

BIND_ENUM(eWeekdays, EnumWeekdays)
{
	{ eSunday   , _T("sun")  , _T("Sunday")    },
	{ eMonday   , _T("mon")  , _T("Monday")    },
	{ eTuesday  , _T("tues") , _T("Tuesday")   },
	{ eWednesday, _T("Wed")  , _T("Wednesday") },
	{ eThursday , _T("thurs"), _T("Thursday")  },
	{ eFriday   , _T("fri")  , _T("Friday")    },
	{ eSaturday , _T("sat")  , _T("Saturday")  },
};

class CWeekdaysData
{
public:
	INSERT_ENUM_BINDER_ITEMS(eWeekdays);
	UINT m_iconID;      // add a resource ID
	int m_offsetX;      // add an integer
	// or anything else ...
};


BIND_ENUM_CUSTOM(eWeekdays, EnumWeekdaysCustom, CWeekdaysData)
{
	{ eSunday   , _T("sun")  , _T("Sunday")    , IDI_WEEKEND,  15 },
	{ eMonday   , _T("mon")  , _T("Monday")    , IDI_WEEKDAY,  30 },
	{ eTuesday  , _T("tues") , _T("Tuesday")   , IDI_WEEKDAY,  45 },
	{ eWednesday, _T("Wed")  , _T("Wednesday") , IDI_WEEKDAY,  60 },
	{ eThursday , _T("thurs"), _T("Thursday")  , IDI_WEEKDAY,  75 },
	{ eFriday   , _T("fri")  , _T("Friday")    , IDI_WEEKDAY,  90 },
	{ eSaturday , _T("sat")  , _T("Saturday")  , IDI_WEEKEND, 105 },
};


////////////////////////////////////////////////////////////////////////////
// Example 4 - CListBox
////////////////////////////////////////////////////////////////////////////
enum eCanadianProvinces 
{
	eCanadianProvinceAB,
	eCanadianProvinceBC,
	eCanadianProvinceMB,
	eCanadianProvinceNB,
	eCanadianProvinceNL,
	eCanadianProvinceNT,
	eCanadianProvinceNS,
	eCanadianProvinceNU,
	eCanadianProvinceON,
	eCanadianProvincePE,
	eCanadianProvinceQC,
	eCanadianProvinceSK,
	eCanadianProvinceYT,
};

BIND_ENUM(eCanadianProvinces, EnumCanada)
{
	{ eCanadianProvinceAB, _T("AB"), _T("Alberta")                   },
	{ eCanadianProvinceBC, _T("BC"), _T("British Columbia")          },
	{ eCanadianProvinceMB, _T("MB"), _T("Manitoba")                  },
	{ eCanadianProvinceNB, _T("NB"), _T("New Brunswick")             },
	{ eCanadianProvinceNL, _T("NL"), _T("Newfoundland and Labrador") },
	{ eCanadianProvinceNT, _T("NT"), _T("Northwest Territories")     },
	{ eCanadianProvinceNS, _T("NS"), _T("Nova Scotia")               },
	{ eCanadianProvinceNU, _T("NU"), _T("Nunavut")                   },
	{ eCanadianProvinceON, _T("ON"), _T("Ontario")                   },
	{ eCanadianProvincePE, _T("PE"), _T("Prince Edward Island")      },
	{ eCanadianProvinceQC, _T("QC"), _T("Qu�bec")                    },
	{ eCanadianProvinceSK, _T("SK"), _T("Saskatchewan")              },
	{ eCanadianProvinceYT, _T("YT"), _T("Yukon")                     },
};



// CEnumBinderDlg dialog
class CEnumBinderDlg : public CDialog
{
// Construction
public:
	CEnumBinderDlg(CWnd* pParent = NULL);	// standard constructor

// Dialog Data
	enum { IDD = IDD_ENUMBINDER_DIALOG };

	protected:
	virtual void DoDataExchange(CDataExchange* pDX);	// DDX/DDV support

	void EnumBinderExamples();

// Implementation
protected:	
	eWeekdays m_weekday;         // <-- ordinary enum variable
	eFruit m_fruit;              // <-- ordinary enum variable
	eCanadianProvinces m_canada; // <-- ordinary enum variable
	eShapes m_shape;             // <-- ordinary enum variable
	CComboBox m_comboShapes;     // <-- ordinary combobox (or derived class)
	CListBox m_listCanada;       // <-- ordinary listbox  (or derived class)
	CComboBox m_comboFruit;      // <-- ordinary combobox (or derived class)
	CComboBox m_comboWeekdays;   // <-- ordinary combobox (or derived class)
	CStatic m_weekdayImage;

	HICON m_hIcon;

	void RegistryExchange(bool readFromRegistry);

	// Generated message map functions
	virtual BOOL OnInitDialog();
	afx_msg void OnSysCommand(UINT nID, LPARAM lParam);
	afx_msg void OnPaint();
	afx_msg HCURSOR OnQueryDragIcon();
	DECLARE_MESSAGE_MAP()
public:
	afx_msg void OnBnClickedButtonCheckValues();
	afx_msg void OnLbnSelchangeListCanada();
	afx_msg void OnBnClickedOk();
	afx_msg void OnBnClickedButtonUnitTest();
	afx_msg void OnBnClickedCancel();	
	afx_msg void OnCbnSelchangeComboWeekdays();
};

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Warren Stevens
Software Developer (Senior)
Canada Canada
www.IconsReview.com[^]
Huge list of stock icon collections (both free and commercial)
 
The picture is from September 2006, after picking up a rental car at the airport in Denver, Colorado. I'm smiling in the picture, because I have yet to come to the realization that I just wasted 400 bucks ( because you don't really need a car in downtown Denver - you can just walk everywhere).

| Advertise | Privacy | Mobile
Web04 | 2.8.141022.1 | Last Updated 15 Aug 2005
Article Copyright 2005 by Warren Stevens
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid