Storing arbitrary Serializables in QVariant objects
August 22, 2008 on 4:17 pm | In libs11n | 1 CommentAs part of the QBoard project i’ve been creating s11n bindings for Qt types which the app wants to serialize. The most interesting s11n work there has been the handling of Qt’s QVariant type. A QVariant object can hold a copy of various different types of data, e.g. QString, QPoint, and most of the other basic Qt data types. This is used heavily in the QObject Properties and QtScript APIs, and one can add support for their own types. One of the limitations is that QVariants always copy, instead of referencing, and this makes it not so useful for larger types or passing shared state around. Because it deeply copies, polymorphism goes out the window. There’s a way around that, though…
S11n supports polymorphic de/serialization if the Serializable type in question is set up for it (it’s not hard to do). When a Serializables is serialized, the data is not written directly to a stream, but is stored in intermediary objects called S11nNodes (basically a DOM tree of serialized data). S11nNodes are themselves copyable. Since they carry all the info they need for polymorphic deserialization (and can copy it freely), S11nNodes themselves need not be polymorphic. So…
All we had to do was write a small wrapper type to integrate the S11nNode type with QVariant’s conventions. We can use that wrapper to serialize any SerializableType into a QVariant object. That looks like:
#include "S11nQt.h"
...
QVariant var( s11n::qt::VariantS11n(mySerializable) );
To deserialize a SerializableType object from the QVariant, we convert the QVariant to a VariantS11n and then deserialize the data through that object:
if( var.canConvert<s11n::qt::VariantS11n>() ) {
s11n::qt::VariantS11n sv( myVariant.value<s11n::qt::VariantS11n>() );
MySerializable * my = sv.deserialize<MySerializable>();
...
}
Note that S11nNodes can contain arbitrarily large amounts of data, and are therefor inherently not efficient to copy. However, VariantS11n employs reference counting and copy-on-write to keep the copying to a minimum. This makes const copy operations O(1) and non-const operations O(N), where N is the size of the S11nNode’s data (i.e. the number and size of the node’s properties, values, and child nodes).
The S11nQt code is part of the QBoard project, and can be found here:
http://code.google.com/p/qboard/wiki/S11nQt
Happy hacking!
Qt 4.3/4.4 and s11n
August 14, 2008 on 7:31 pm | In General, libs11n | 1 CommentAs part of a new hobby project of mine (QBoard), i’ve been writing utility code for serializing Qt-based types. In an shameless attempt to show a good use for libs11n, i’ve extracted that code into its own distribution. It may be helpful to coders working on Qt-based projects.
Because i don’t have access to the development copy of the s11n web site at the moment, the code is distributed via the QBoard project:
http://code.google.com/p/qboard/wiki/S11nQt
See that page for information about what Qt types are supported and to download a copy.
Powered by WordPress with Pool theme design by Borja Fernandez.
Entries and comments feeds.
Valid XHTML and CSS. ^Top^