Provide a reasonably painless way to stream enums that aren't object

properties.
This commit is contained in:
Andrzej Kapolka 2014-06-05 15:32:55 -07:00
parent 8abcacd433
commit c44dba78cb
3 changed files with 38 additions and 6 deletions

View file

@ -1037,12 +1037,12 @@ public:
};
/// Macro for registering simple type streamers.
#define REGISTER_SIMPLE_TYPE_STREAMER(x) static int x##Streamer = \
Bitstream::registerTypeStreamer(qMetaTypeId<x>(), new SimpleTypeStreamer<x>());
#define REGISTER_SIMPLE_TYPE_STREAMER(X) static int X##Streamer = \
Bitstream::registerTypeStreamer(qMetaTypeId<X>(), new SimpleTypeStreamer<X>());
/// Macro for registering collection type streamers.
#define REGISTER_COLLECTION_TYPE_STREAMER(x) static int x##Streamer = \
Bitstream::registerTypeStreamer(qMetaTypeId<x>(), new CollectionTypeStreamer<x>());
#define REGISTER_COLLECTION_TYPE_STREAMER(X) static int x##Streamer = \
Bitstream::registerTypeStreamer(qMetaTypeId<X>(), new CollectionTypeStreamer<X>());
/// Declares the metatype and the streaming operators. The last lines
/// ensure that the generated file will be included in the link phase.
@ -1077,6 +1077,25 @@ public:
_Pragma(STRINGIFY(unused(_TypePtr##X)))
#endif
#define DECLARE_ENUM_METATYPE(S, N) Q_DECLARE_METATYPE(S::N) \
Bitstream& operator<<(Bitstream& out, const S::N& obj); \
Bitstream& operator>>(Bitstream& in, S::N& obj); \
template<> inline void Bitstream::writeRawDelta(const S::N& value, const S::N& reference) { *this << value; } \
template<> inline void Bitstream::readRawDelta(S::N& value, const S::N& reference) { *this >> value; }
#define IMPLEMENT_ENUM_METATYPE(S, N) \
static int S##N##MetaTypeId = registerEnumMetaType<S::N>(S::staticMetaObject.enumerator( \
S::staticMetaObject.indexOfEnumerator(#N))); \
Bitstream& operator<<(Bitstream& out, const S::N& obj) { \
static int bits = static_cast<const EnumTypeStreamer*>(Bitstream::getTypeStreamer(qMetaTypeId<S::N>()))->getBits(); \
return out.write(&obj, bits); \
} \
Bitstream& operator>>(Bitstream& in, S::N& obj) { \
static int bits = static_cast<const EnumTypeStreamer*>(Bitstream::getTypeStreamer(qMetaTypeId<S::N>()))->getBits(); \
obj = (S::N)0; \
return in.read(&obj, bits); \
}
/// Registers a simple type and its streamer.
template<class T> int registerSimpleMetaType() {
int type = qRegisterMetaType<T>();
@ -1084,6 +1103,13 @@ template<class T> int registerSimpleMetaType() {
return type;
}
/// Registers an enum type and its streamer.
template<class T> int registerEnumMetaType(const QMetaEnum& metaEnum) {
int type = qRegisterMetaType<T>();
Bitstream::registerTypeStreamer(type, new EnumTypeStreamer(metaEnum));
return type;
}
/// Registers a streamable type and its streamer.
template<class T> int registerStreamableMetaType() {
int type = qRegisterMetaType<T>();

View file

@ -20,6 +20,8 @@
REGISTER_META_OBJECT(TestSharedObjectA)
REGISTER_META_OBJECT(TestSharedObjectB)
IMPLEMENT_ENUM_METATYPE(TestSharedObjectA, TestEnum)
MetavoxelTests::MetavoxelTests(int& argc, char** argv) :
QCoreApplication(argc, argv) {
}
@ -80,6 +82,7 @@ static TestMessageC createRandomMessageC() {
message.bar = rand();
message.baz = randFloat();
message.bong.foo = createRandomBytes();
message.bong.baz = getRandomTestEnum();
return message;
}
@ -252,7 +255,7 @@ static QVariant createRandomMessage() {
return QVariant::fromValue(message);
}
case 1: {
TestMessageB message = { createRandomBytes(), createRandomSharedObject() };
TestMessageB message = { createRandomBytes(), createRandomSharedObject(), getRandomTestEnum() };
return QVariant::fromValue(message);
}
case 2:
@ -273,7 +276,7 @@ static bool messagesEqual(const QVariant& firstMessage, const QVariant& secondMe
} else if (type == TestMessageB::Type) {
TestMessageB first = firstMessage.value<TestMessageB>();
TestMessageB second = secondMessage.value<TestMessageB>();
return first.foo == second.foo && equals(first.bar, second.bar);
return first.foo == second.foo && equals(first.bar, second.bar) && first.baz == second.baz;
} else if (type == TestMessageC::Type) {
return firstMessage.value<TestMessageC>() == secondMessage.value<TestMessageC>();

View file

@ -106,6 +106,8 @@ private:
TestFlags _bong;
};
DECLARE_ENUM_METATYPE(TestSharedObjectA, TestEnum)
/// Another simple shared object.
class TestSharedObjectB : public SharedObject {
Q_OBJECT
@ -169,6 +171,7 @@ public:
STREAM QByteArray foo;
STREAM SharedObjectPointer bar;
STREAM TestSharedObjectA::TestEnum baz;
};
DECLARE_STREAMABLE_METATYPE(TestMessageB)