RFC: bool operators for some classes

Malte Starostik malte at kde.org
Fri Jan 3 01:04:02 GMT 2003


Hi,

Please consider code like this:

KFileMetaInfo metaInfo( info->url().path() );
if ( metaInfo.isValid() )
{
    KFileMetaInfoItem artist = metaInfo.item( KFileMimeTypeInfo::Author );
    KFileMetaInfoItem title = metaInfo.item( KFileMimeTypeInfo::Name );
    KFileMetaInfoItem album = metaInfo.item( "Album" );
    KFileMetaInfoItem length = metaInfo.item( "Length" );

    if ( length.isValid() ) fileInfo[ Tag::length ] = length.string();
    if ( artist.isValid() ) fileInfo[ Tag::artist ] = artist.string();
    if ( title.isValid() ) fileInfo[ Tag::title ] = title.string();
    if ( album.isValid() ) fileInfo[ Tag::album ] = album.string();
}

If those classes provided a boolean conversion, the above could be written 
more tersely:

if ( KFileMetaInfo metaInfo = KFileMetaInfo( info->url().path() ) )
{
    if ( KFileMetaInfoItem artist = metaInfo.item( KFileMimeTypeInfo::Author ) 
)
        fileInfo[ Tag::artist ] = artist.string();
    if ( KFileMetaInfoItem title = metaInfo.item( KFileMimeTypeInfo::Name ) )
        fileInfo[ Tag::title ] = title.string();
    if ( KFileMetaInfoItem album = metaInfo.item( "Album" ) )
        fileInfo[ Tag::album ] = album.string();
    if ( KFileMetaInfoItem length = metaInfo.item( "Length" ) )
        fileInfo[ Tag::length ] = length.string();
}

which ain't only shorter but also scopes those variables more appropriately. 
So I'd like to suggest the addition of

operator const void* () const { return isValid() ? this : 0; }

to KFileMetaInfo et al. and maybe some other classes in kdelibs that are 
likely to be used value-based. Since operator bool () has some problems, 
const void* seems a good (and usual) candidate.
Would there be any problems created by this?

Some candidates apart from KMFI would be:
KSaveFile (status() != 0)
KTempFile (status() != 0)
KURL (isValid())
KURL::List (!isEmpty())
KColor (isValid())
KArchive (isOpened())
DCOPRef (!isNull())
KABC::(anything that has an isEmpty() member) (!isEmpty())
maybe some others

Would anyone beat me for adding it to those?

-Malte





More information about the kde-core-devel mailing list