enum's & KConfigBase

Thomas Braxton brax108 at cox.net
Mon Jan 16 18:39:34 GMT 2006

What do you guys think of this macro?

#define KCONFIG_DECLARE_ENUM( Enum ) \
template <> inline \
Enum KConfigBase::readEntry( const char* pKey, const Enum& value ) const \
{ return static_cast<Enum>(readEntry(pKey, static_cast<int>(value))); } \
template <> inline \
void KConfigBase::writeEntry( const char* pKey, const Enum& value, \
                                               WriteConfigFlags flags ) \
{ writeEntry(pKey, static_cast<int>(value), flags); }

It allows enum's to be treated as other types, instead of having to explicitly 
cast to/from int. IMHO, app programmers shouldn't have to worry about what 
type they are trying to read/write with KConfig, they should just read/write 
it. With this macro we can clean up code that looks like:
int mode = config->readEntry(key, 0);
config->writeEntry(key, static_cast<int>(enumValue));
config->writeEntry(key, enumValue);

I think it looks nicer, especially since some enum names are pretty long, and 
all the casting to/from enum's is pretty ugly.


More information about the kde-core-devel mailing list