[prev in list] [next in list] [prev in thread] [next in thread] 

List:       kde-core-devel
Subject:    Re: enum's & KConfigBase
From:       Olivier Goffart <ogoffart () kde ! org>
Date:       2006-01-16 19:48:59
Message-ID: 200601162049.06689.ogoffart () kde ! org
[Download RAW message or body]

[Attachment #2 (multipart/mixed)]


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.


["enum.cpp" (text/x-c++src)]

#define KCONFIG_DECLARE_ENUM( OBJECT , ENUM ) \
template <> inline OBJECT::ENUM KConfigBase::readEntry( const char* pKey, const \
OBJECT::ENUM& value ) const \ { \
	QMetaEnum metaEnum = \
OBJECT::staticMetaObject.enumerator(OBJECT::staticMetaObject.indexOfEnumerator(#ENUM)); \
\  return static_cast<OBJECT::ENUM>( metaEnum.keyToValue( \
readEntry(pKey,metaEnum.valueToKey(value)) ) ); \ } \
template <> inline void KConfigBase::writeEntry( const char* pKey, const \
OBJECT::ENUM& value, WriteConfigFlags flags ) \ { \
	writeEntry( pKey, KGlobalSettings::staticMetaObject.enumerator( \
		OBJECT::staticMetaObject.indexOfEnumerator(#ENUM)).valueToKey(value) , flags ); \
}


[Attachment #6 (application/pgp-signature)]

[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic