[Development] [API review] QMetaType::TypeFlags

Jedrzej Nowacki jedrzej.nowacki at nokia.com
Thu Dec 22 13:12:32 CET 2011


Hi,

  We are about to introduce a new function and an enum in QMetaType class:

class QMetatype {
    ...
    enum TypeFlag {
        NeedsConstruction = 0x1,
        NeedsDestruction = 0x2,
        MovableType = 0x4
    };
    Q_DECLARE_FLAGS(TypeFlags, TypeFlag)
    ...
    static TypeFlags typeFlags(int type);
    ...
};

It would be nice to start API review for this change.

The goal of the new API is to allow access to an additional type information. 
For now we are not entirely sure what information we need, but we can suspect 
that it will be similar to <type_traits> + few Qt'ish extensions. Personally, 
I need to know if a type is movable or not, but in Gerrit there are already 
patches that want to extend it for example by adding information about QObject 
inheritance.

Up to know I haven't seen objections related to the function name, but values 
names of the enum caused a hot discussion :-)

Main options:

(1)
  HasTrivialDefaultConstructor (or IsTriviallyConstructible or both)
  HasTrivialCopyConstructor (or IsTriviallyCopyable or both)
  HasTrivialDestructor (or IsTriviallyDestructible or both)
  ...

Justification: We should align names with C++11 standard. Everyone have to know 
it, so it makes sens to not introduce more names.

(2)
 NeedsInitialization
 NeedsDestruction
 CanMoveWithMemcpy / IsMovableType / MovableType
 ...

Justification: It is more descriptive by showing what a feature can be used 
for. Nobody really cares if something is trivial or not, the most important 
thing is answer to a question "do I really have to call destructor?"


So we have a typical naming problem: "This is a chair" vs. "You can sit 
there".

We can map values from (1) to (2) but not necessary from (2) to (1), for 
example:
CanMoveWithMemcpy = HasTrivialCopyConstructor && HasTrivialDestructor


Code samples:
(1)
if (!(flag & QMetaType::HasTrivialDestructor))
    QMetaType::destruct(typeId, instance);
vs.
(2)
if (flag & QMetaType::NeedsDestruction)
    QMetaType::destruct(typeId, instance);


(1)
if (size <= maxSize && (flags & QMetaType::HasTrivialCopyConstructor) && (flags 
& QMetaType::HasTrivialDestructor))
    QMetaType::construct(typeId, where, copy);
vs.
(2)
if (size <= maxSize && (flags & QMetaType::MovableType))
    QMetaType::construct(typeId, where, copy);


Main concerns (by João):
 - Discrepancy with naming in QTypeInfo, though everyone agrees it is bad to 
start with (yes, even you, dear reader ;)
 - Oswald points out the asymmetry between "compliance requirements" of 
NeedsConstruction/Destruction and MovableType
 - Discrepancy with standard type traits naming (on a side note, @Thiago note 
how the standard has no way of expressing movability-with-memcpy orthogonally 
to trivial ctor/dtor. Think QString)


Cheers,
  Jędrek
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.qt-project.org/pipermail/development/attachments/20111222/45aadd99/attachment.html>


More information about the Development mailing list