[Qt-interest] typedef on the same underlying type and Q_REGISTER_METATYPE
MARTIN Pierre
hickscorp at gmail.com
Tue Sep 13 21:06:30 CEST 2011
> You need to define the operators, yes. But remember you must have concrete
> classes, not typedefs to template instantiations.
Here you go, here is what i have done so far, it seems to be working. i probably have forgot most of the "functional" operator overloading, but the idea works. Sorry for formatting, the customer i'm working for provides a VM for development and it's visual studio =/
If you see problems with this code, i'd be gratefull for you to tell me. What important CopyCtors / Assignement OPs could be missing?
#ifndef DECLARE_TYPE_WRAPPER
#define DECLARE_TYPE_WRAPPER(Cls, typ) \
class Cls : public RE::TypeWrapper<typ> { \
public: \
Cls () : TypeWrapper() {} \
Cls (double const &t) : TypeWrapper(t) {} \
Cls (Cls const &c) : TypeWrapper(c) {} \
};
#endif
template<typename T>
class TypeWrapper {
public:
// Constructors.
TypeWrapper () : _t() {}
TypeWrapper (T const &t) : _t(t) {}
TypeWrapper (TypeWrapper<T> const &w) : _t(w._t) {}
// Cast operators.
operator T () const { return _t; }
// Assignment operators.
TypeWrapper<T>& operator= (T const &t) { _t = t; return *this; }
// QDataStream friend serialization stuff.
friend QDataStream& operator<< (QDataStream& s, TypeWrapper<T> const &w) { return s << w._t; }
friend QDataStream& operator>> (QDataStream& s, TypeWrapper<T> &w) { return s >> w._t; }
private:
T _t;
};
Then to declare my types:
namespace T {
DECLARE_TYPE_WRAPPER (Ratio, double);
DECLARE_TYPE_WRAPPER (PositiveRatio, double);
DECLARE_TYPE_WRAPPER (NegativeRatio, double);
}
Q_DECLARE_TYPEINFO(RE::T::Ratio, Q_MOVABLE_TYPE);
Q_DECLARE_METATYPE(RE::T::Ratio);
Q_DECLARE_TYPEINFO(RE::T::PositiveRatio, Q_MOVABLE_TYPE);
Q_DECLARE_METATYPE(RE::T::PositiveRatio);
Q_DECLARE_METATYPE(RE::T::NegativeRatio);
Q_DECLARE_TYPEINFO(RE::T::NegativeRatio, Q_MOVABLE_TYPE);
i didn't test too much, but at least it compiles, so feel free to reuse this.
> The MOVABLE is to tell QVector and QList that they can memcpy this type with
> impunity.
Thanks ;)
Pierre.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.qt-project.org/pipermail/qt-interest-old/attachments/20110913/0694342c/attachment.html
More information about the Qt-interest-old
mailing list