[Development] submitting a multi-dimensional container class for Qt: QNDArray
Glen Mabey
gmabey at swri.org
Sun Jan 20 05:26:23 CET 2013
Hello --
On Friday, December 28, 2012 8:55 AM, Stephen Kelly wrote:
> On Friday, December 28, 2012 08:36:21 Glen Mabey wrote:
>> #define Q_DECLARE_METATYPE_2(h1,h2) \
>> QT_BEGIN_NAMESPACE \
>> template <> struct QMetaTypeId< h1,h2 > { \
>> enum { Defined = 1 }; \
>> static int qt_metatype_id() { \
>> static QBasicAtomicInt metatype_id =
>> Q_BASIC_ATOMIC_INITIALIZER(0); \ if (!metatype_id.load())
>> metatype_id.storeRelease(qRegisterMetaType< h1,h2 >( #h1 "," #h2,
>> reinterpret_cast< h1,h2 *>(quintptr(-1)))); \ return
>> metatype_id.loadAcquire(); \
>> } \
>> }; \
>> QT_END_NAMESPACE
>>
>> This seems like a hack to me, but it works great with all of the compilers
>> I've tried it with (32/64-bit g++ on linux and mac; mingw, MSVC 2008,
>> 2010).
>
>> Does anyone foresee a big objection to this approach?
>
> Yes. At least, QNDArray is not part of the typeName() of the metatypes.
I guess, I don't understand that.
> For testing, you can probably use this just after the QNDArray definition:
>
> Q_DECLARE_METATYPE_TEMPLATE_2ARG(QNDArray)
>
> but if this class is going to end up in Qt, then you would need to extend the
>
> QT_FOR_EACH_AUTOMATIC_TEMPLATE_2ARG(F)
>
> macro to deal with it.
Wow, I just tried following the interactions of Q_DECLARE_METATYPE_TEMPLATE_2ARG, Q_DECLARE_METATYPE_TEMPLATE_2ARG_ITER, and Q_DECLARE_METATYPE_TEMPLATE_2ARG and that is some kind of macroization.
So, if I understand things correctly, Q_DECLARE_METATYPE_TEMPLATE_2ARG_ITER is first declaring each of QMap, QHash, and QPair like this:
template <class T1, class T2> class QMap;
template <class T1, class T2> class QHash;
template <class T1, class T2> struct QPair;
and then after each one it is declaring a templated QMetaTypeId struct so that a user could easily do
template < > struct QMetaTypeId< QMap<int, QString> >;
for example (and at the same time handle something like this "template < > struct QMetaTypeId< QMap<int, QList<qint8> > >;") and that explicit instantiation would create the necessary QMetaTypeId so that QMap<int, QString> could be used as a QVariant, right?
And by adding Q_DECLARE_METATYPE_TEMPLATE_2ARG(QNDArray) I could then do
template < > struct QMetaTypeID< QNDArray<3,int> >;
Somehow I like
Q_DECLARE_METATYPE_2( QNDArray<3,int> )
better. Fewer characters?? Personal preference?? String concat hackery?
Glen
More information about the Development
mailing list