enum's & KConfigBase

Thomas Braxton brax108 at cox.net
Mon Jan 16 19:30:58 GMT 2006


On Monday 16 January 2006 12:50, David Faure wrote:
> 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 :)
If the app writer wants that they can write their own implementation of the 
functions readEntry/writeEntry(key, T) and not use the macro, it's not that 
difficult.

Thomas




More information about the kde-core-devel mailing list