enum's & KConfigBase
Olivier Goffart
ogoffart at kde.org
Mon Jan 16 19:48:59 GMT 2006
Le Lundi 16 Janvier 2006 19:50, David Faure a écrit :
> 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.
On the other hand, saving from/to an enum is done in general maximum once or
twice in a program. I don't know if that macro is justified.
> 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 :)
There is QMetaEnum that does that job nicely
that looks like the attached (NOT TESTED)
the problem is that the enum must be declared in a QObject with a Q_ENUM , and
parsed by moc.
-------------- next part --------------
A non-text attachment was scrubbed...
Name: enum.cpp
Type: text/x-c++src
Size: 664 bytes
Desc: not available
URL: <http://mail.kde.org/pipermail/kde-core-devel/attachments/20060116/d8b27e3d/attachment.cpp>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 189 bytes
Desc: not available
URL: <http://mail.kde.org/pipermail/kde-core-devel/attachments/20060116/d8b27e3d/attachment.sig>
More information about the kde-core-devel
mailing list