[Development] submitting a multi-dimensional container class for Qt: QNDArray

Stephen Kelly stephen.kelly at kdab.com
Mon Jan 21 09:36:29 CET 2013


On Saturday, January 19, 2013 22:26:23 Glen Mabey wrote:
> 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.

What do you not understand?

> 
> > 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.

Yes, macros.

> 
> 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;

Yes.

> 
> 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> >;

No, a user does not do that. Only the macro needs to be used in Qt, as it is 
for QHash etc. Then the user can just put the container into QVariants easily.

> 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?

Yes. But note that the user does not have to do anything special in order to 
put the container in a QVariant. 

> 
> And by adding Q_DECLARE_METATYPE_TEMPLATE_2ARG(QNDArray) I could then do




>   template < > struct QMetaTypeID< QNDArray<3,int> >;

No, you don't do this. Only the macro is needed in Qt.

For example, if you try this in Qt 4.8:

 QHash<int, QDate> container;
 QVariant::fromValue(container);

it will not compile, unless you use Q_DECLARE_METATYPE (with a typedef).

In Qt 5 it works just fine without using Q_DECLARE_METATYPE. The same 
can/should be true for your container.

> Somehow I like
> 
>   Q_DECLARE_METATYPE_2( QNDArray<3,int> )
> 
> better.  Fewer characters??  Personal preference??  String concat hackery?

Not needed at all.

Thanks,

-- 
Stephen Kelly <stephen.kelly at kdab.com> | Software Engineer
KDAB (Deutschland) GmbH & Co.KG, a KDAB Group Company
www.kdab.com || Germany +49-30-521325470 || Sweden (HQ) +46-563-540090
KDAB - Qt Experts - Platform-Independent Software Solutions
-------------- next part --------------
A non-text attachment was scrubbed...
Name: smime.p7s
Type: application/pkcs7-signature
Size: 3636 bytes
Desc: not available
URL: <http://lists.qt-project.org/pipermail/development/attachments/20130121/2684bcce/attachment.bin>


More information about the Development mailing list