mirror of
https://github.com/overte-org/overte.git
synced 2025-07-26 05:59:57 +02:00
Provide a reasonably painless way to stream enums that aren't object
properties.
This commit is contained in:
parent
8abcacd433
commit
c44dba78cb
3 changed files with 38 additions and 6 deletions
|
@ -1037,12 +1037,12 @@ public:
|
||||||
};
|
};
|
||||||
|
|
||||||
/// Macro for registering simple type streamers.
|
/// Macro for registering simple type streamers.
|
||||||
#define REGISTER_SIMPLE_TYPE_STREAMER(x) static int x##Streamer = \
|
#define REGISTER_SIMPLE_TYPE_STREAMER(X) static int X##Streamer = \
|
||||||
Bitstream::registerTypeStreamer(qMetaTypeId<x>(), new SimpleTypeStreamer<x>());
|
Bitstream::registerTypeStreamer(qMetaTypeId<X>(), new SimpleTypeStreamer<X>());
|
||||||
|
|
||||||
/// Macro for registering collection type streamers.
|
/// Macro for registering collection type streamers.
|
||||||
#define REGISTER_COLLECTION_TYPE_STREAMER(x) static int x##Streamer = \
|
#define REGISTER_COLLECTION_TYPE_STREAMER(X) static int x##Streamer = \
|
||||||
Bitstream::registerTypeStreamer(qMetaTypeId<x>(), new CollectionTypeStreamer<x>());
|
Bitstream::registerTypeStreamer(qMetaTypeId<X>(), new CollectionTypeStreamer<X>());
|
||||||
|
|
||||||
/// Declares the metatype and the streaming operators. The last lines
|
/// Declares the metatype and the streaming operators. The last lines
|
||||||
/// ensure that the generated file will be included in the link phase.
|
/// ensure that the generated file will be included in the link phase.
|
||||||
|
@ -1077,6 +1077,25 @@ public:
|
||||||
_Pragma(STRINGIFY(unused(_TypePtr##X)))
|
_Pragma(STRINGIFY(unused(_TypePtr##X)))
|
||||||
#endif
|
#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.
|
/// Registers a simple type and its streamer.
|
||||||
template<class T> int registerSimpleMetaType() {
|
template<class T> int registerSimpleMetaType() {
|
||||||
int type = qRegisterMetaType<T>();
|
int type = qRegisterMetaType<T>();
|
||||||
|
@ -1084,6 +1103,13 @@ template<class T> int registerSimpleMetaType() {
|
||||||
return type;
|
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.
|
/// Registers a streamable type and its streamer.
|
||||||
template<class T> int registerStreamableMetaType() {
|
template<class T> int registerStreamableMetaType() {
|
||||||
int type = qRegisterMetaType<T>();
|
int type = qRegisterMetaType<T>();
|
||||||
|
|
|
@ -20,6 +20,8 @@
|
||||||
REGISTER_META_OBJECT(TestSharedObjectA)
|
REGISTER_META_OBJECT(TestSharedObjectA)
|
||||||
REGISTER_META_OBJECT(TestSharedObjectB)
|
REGISTER_META_OBJECT(TestSharedObjectB)
|
||||||
|
|
||||||
|
IMPLEMENT_ENUM_METATYPE(TestSharedObjectA, TestEnum)
|
||||||
|
|
||||||
MetavoxelTests::MetavoxelTests(int& argc, char** argv) :
|
MetavoxelTests::MetavoxelTests(int& argc, char** argv) :
|
||||||
QCoreApplication(argc, argv) {
|
QCoreApplication(argc, argv) {
|
||||||
}
|
}
|
||||||
|
@ -80,6 +82,7 @@ static TestMessageC createRandomMessageC() {
|
||||||
message.bar = rand();
|
message.bar = rand();
|
||||||
message.baz = randFloat();
|
message.baz = randFloat();
|
||||||
message.bong.foo = createRandomBytes();
|
message.bong.foo = createRandomBytes();
|
||||||
|
message.bong.baz = getRandomTestEnum();
|
||||||
return message;
|
return message;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -252,7 +255,7 @@ static QVariant createRandomMessage() {
|
||||||
return QVariant::fromValue(message);
|
return QVariant::fromValue(message);
|
||||||
}
|
}
|
||||||
case 1: {
|
case 1: {
|
||||||
TestMessageB message = { createRandomBytes(), createRandomSharedObject() };
|
TestMessageB message = { createRandomBytes(), createRandomSharedObject(), getRandomTestEnum() };
|
||||||
return QVariant::fromValue(message);
|
return QVariant::fromValue(message);
|
||||||
}
|
}
|
||||||
case 2:
|
case 2:
|
||||||
|
@ -273,7 +276,7 @@ static bool messagesEqual(const QVariant& firstMessage, const QVariant& secondMe
|
||||||
} else if (type == TestMessageB::Type) {
|
} else if (type == TestMessageB::Type) {
|
||||||
TestMessageB first = firstMessage.value<TestMessageB>();
|
TestMessageB first = firstMessage.value<TestMessageB>();
|
||||||
TestMessageB second = secondMessage.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) {
|
} else if (type == TestMessageC::Type) {
|
||||||
return firstMessage.value<TestMessageC>() == secondMessage.value<TestMessageC>();
|
return firstMessage.value<TestMessageC>() == secondMessage.value<TestMessageC>();
|
||||||
|
|
|
@ -106,6 +106,8 @@ private:
|
||||||
TestFlags _bong;
|
TestFlags _bong;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
DECLARE_ENUM_METATYPE(TestSharedObjectA, TestEnum)
|
||||||
|
|
||||||
/// Another simple shared object.
|
/// Another simple shared object.
|
||||||
class TestSharedObjectB : public SharedObject {
|
class TestSharedObjectB : public SharedObject {
|
||||||
Q_OBJECT
|
Q_OBJECT
|
||||||
|
@ -169,6 +171,7 @@ public:
|
||||||
|
|
||||||
STREAM QByteArray foo;
|
STREAM QByteArray foo;
|
||||||
STREAM SharedObjectPointer bar;
|
STREAM SharedObjectPointer bar;
|
||||||
|
STREAM TestSharedObjectA::TestEnum baz;
|
||||||
};
|
};
|
||||||
|
|
||||||
DECLARE_STREAMABLE_METATYPE(TestMessageB)
|
DECLARE_STREAMABLE_METATYPE(TestMessageB)
|
||||||
|
|
Loading…
Reference in a new issue