enum's & KConfigBase

David Faure faure at kde.org
Mon Jan 16 18:50:41 GMT 2006

On Monday 16 January 2006 19:39, Thomas Braxton wrote:
> 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);
> useCompletion((KGlobalSettings::Completion)mode);
> or
> config->writeEntry(key, static_cast<int>(enumValue));
> to
> config->writeEntry(key, enumValue);

Yes the latter would be nicer, that's for sure.

I was hoping for a way without explicit registration of the enum type, though...
Maybe an overload for QFlags is enough?

On the other hand, one could argue that future code that reads/writes
enums might want to use enum<->QString conversions for more readable
config files... but well, we're all too lazy for that :)

David Faure, faure at kde.org, sponsored by Trolltech to work on KDE,
Konqueror (http://www.konqueror.org), and KOffice (http://www.koffice.org).

More information about the kde-core-devel mailing list