Trolltech Home | Qt-interest Home | Recent Threads | All Threads | Author | Date
All threads index page 1

Qt-interest Archive, March 2008
QVariant design pattern


Message 1 in thread

I'm interesting in the implementation of QVariant. Actually, I want to
implement a similar type in a non-qt problem.  Is there a design
pattern behind the idea of QVariant.  Where can I begin to study it?
Thanks.

-- 
 [ signature omitted ] 

Message 2 in thread

Steven Woody wrote:
>I'm interesting in the implementation of QVariant. Actually, I want to
>implement a similar type in a non-qt problem.  Is there a design
>pattern behind the idea of QVariant.  Where can I begin to study it?

Only the source code itself. But if you study it, be sure to respect the 
licensing terms. (I am not a lawyer, this is not legal advice)

But what I can tell you of QVariant is:
 - it's got an integer that denotes a type ID
 - a union with the basic types (bool, int, double, qint64, etc.) is 
present, as well as a void* generic
 - when you Q_DECLARE_METATYPE, you declare a constructor/copy function 
and a destructor
 - template functions allow you to extract the type.

For instance (writing from memory):
template<typename T>
T qvariant_cast(const QVariant &v)
{
    if (v.userType() == qMetaTypeId<T>())
        return *reinterpret_cast<const T *>(v.constData());
    else
        return T();
}

The basic types will need specialisation.

-- 
 [ signature omitted ] 

Attachment: signature.asc
Description: This is a digitally signed message part.


Message 3 in thread

On Monday 03 March 2008, Steven Woody wrote:
> I'm interesting in the implementation of QVariant. Actually, I want to
> implement a similar type in a non-qt problem.  Is there a design
> pattern behind the idea of QVariant.  Where can I begin to study it?
> Thanks.

you may also look at boot::variant, it's not a replacement for QVariant but 
might give you some ideas. 

http://www.boost.org/doc/html/variant.html

It also has a more permissive license. 

-- 
 [ signature omitted ] 

Attachment: signature.asc
Description: This is a digitally signed message part.