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

Glen Mabey glen.mabey at swri.org
Tue Jan 22 12:25:02 CET 2013


On Jan 21, 2013, at 2:36 AM, Stephen Kelly wrote:

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

> Not needed at all.

*That* is what I didn't understand -- and I don't see any notion of that in the QMetaType docs, but that's okay; I'm just glad for the clarification.

My attempt to directly use Q_DECLARE_METATYPE_TEMPLATE_2ARG failed, and I'm pretty sure that's because the first template argument is not really a class but instead is a base data type, qint8.  

So, I copied it out and started modifying it (outside of any macro) and ended up with the following, which seems to work fine:

template< qint8 N, class DTYPE >
struct QMetaTypeId< QNDArray<N, DTYPE> >
{
   enum {
       Defined = QMetaTypeId2<DTYPE>::Defined
   };
   static int qt_metatype_id()
   {
       static QBasicAtomicInt metatype_id = Q_BASIC_ATOMIC_INITIALIZER(0);
       if (const int id = metatype_id.loadAcquire())
           return id;
       QVarLengthArray<char, 24> name;
       name.append("QNDArray", sizeof("QNDArray") - 1);
       name.append('<');
       QByteArray tName = QByteArray::number(N);
       name.append(tName.constData(), tName.size());
       name.append(',');
       const char *uName = QMetaType::typeName(qMetaTypeId<DTYPE>());
       Q_ASSERT(uName);
       name.append(uName, int(strlen(uName)));
       if (name.last() == '>')
           name.append(' ');
       name.append('>');
       const int newId = qRegisterNormalizedMetaType< QNDArray<N, DTYPE> >(
                       QByteArray(name.constData(), name.size()),
                       reinterpret_cast< QNDArray<N, DTYPE> *>(quintptr(-1)));
       metatype_id.storeRelease(newId);
       return newId;
   }
};

Where I'm not really happy with it is in the use of a QByteArray, as I think that the purpose for using QVarLengthArray in the original macro was to avoid heap allocations such as QByteArray will necessarily bring.

Is there something that can be done about that?

Thanks!
Glen




More information about the Development mailing list