Commit c873db0b authored by Philip Trettner's avatar Philip Trettner

Added serialization interface

parent c871e3f6
......@@ -152,30 +152,34 @@ class QIODevice;
} \
else \
value.clear(); \
} \
void serialize##typeName##Vector(std::vector<type> &value, std::string const &field) \
{ \
if (isReadMode()) \
read##typeName##Vector(value, field); \
else \
write##typeName##Vector(value, field); \
}
#define NETMESSAGE_VALUE_TYPE(type, typeName) \
void write##typeName(const type &value, std::string _field) \
{ \
writeTypeInfo(#type, _field); \
appendData<type>(value); \
} \
size_t writeDummy##typeName(std::string _field) \
{ \
writeTypeInfo(#type, _field); \
type value; \
memset(&value, 0xFF, sizeof(type)); \
size_t pos = mData.size(); \
appendData<type>(value); \
return pos; \
} \
void setDummy##typeName(const type &value, size_t pos) { *((type *)(mData.data() + pos)) = value; } \
type read##typeName(const std::string &_field) \
{ \
verifyTypeInfo(#type, _field); \
type value = (type)0; \
readData((char *) & value, sizeof(type)); \
return value; \
#define NETMESSAGE_VALUE_TYPE(type, typeName) \
void write##typeName(const type &value, std::string _field) \
{ \
writeTypeInfo(#type, _field); \
appendData<type>(value); \
} \
type read##typeName(const std::string &_field) \
{ \
verifyTypeInfo(#type, _field); \
type value = (type)0; \
readData((char *) & value, sizeof(type)); \
return value; \
} \
void serialize##typeName(type &value, std::string const &field) \
{ \
if (isReadMode()) \
value = read##typeName(field); \
else \
write##typeName(value, field); \
}
#define NETMESSAGE_VALUE_TYPE_FULL(type, typeName) \
......@@ -222,11 +226,12 @@ private:
/// has the compress function already been called?
bool mIsCompressed;
/// Mode of this message (read/write)
NetMessageMode mMode = NetMessageMode::Write;
private:
NetMessage(const NetMessage &);
NetMessage &operator=(const NetMessage &);
/// Appends one byte
void appendByte(char _c);
......@@ -266,6 +271,10 @@ public:
void decompress();
void setMode(NetMessageMode mode) { mMode = mode; }
NetMessageMode getMode() const { return mMode; }
bool isReadMode() const { return mMode == NetMessageMode::Read; }
bool isWriteMode() const { return mMode == NetMessageMode::Write; }
/// Creates an _unowned_ copy of this message
NetMessage *clone() const;
......@@ -326,7 +335,6 @@ public:
/// Const reference to the internal data
const std::vector<char> &getData() const { return mData; }
// Supported types:
// value types
......@@ -345,6 +353,8 @@ public:
NETMESSAGE_VALUE_TYPE_FULL(uint32_t, UInt32)
NETMESSAGE_VALUE_TYPE_FULL(uint64_t, UInt64)
NETMESSAGE_VALUE_TYPE_FULL(size_t, SizeT)
NETMESSAGE_VALUE_TYPE_FULL(float, Float)
NETMESSAGE_VALUE_TYPE_FULL(double, Double)
......@@ -363,10 +373,24 @@ public:
void writeIntPacked(int64_t value, const std::string &field);
int64_t readIntPacked(const std::string &field);
void serializeIntPacked(int64_t &value, std::string const &field)
{
if (isReadMode())
value = readIntPacked(field);
else
writeIntPacked(value, field);
}
void writeString(const char *value, const std::string &field) { writeString(std::string(value), field); }
void writeString(const std::string &value, const std::string &field);
std::string readString(const std::string &field);
void serializeString(std::string &value, std::string const &field)
{
if (isReadMode())
value = readString(field);
else
writeString(value, field);
}
template <typename T>
void writeStruct(T &value, const std::string &field)
......@@ -383,6 +407,14 @@ public:
readData((char *)&value, sizeof(T));
return value;
}
template <typename T>
void serializeStruct(T &value, std::string const &field)
{
if (isReadMode())
readStruct<T>(value, field);
else
writeStruct<T>(value, field);
}
template <typename T>
void readStruct(T &value, const std::string &field)
......@@ -393,4 +425,3 @@ public:
friend class NetMessageReader;
};
......@@ -11,3 +11,9 @@ const Type compressed = 1 << 1;
const Type RESERVED = 1 << 7;
const Type UNINITIALIZED = 0xFF;
}
enum class NetMessageMode
{
Write,
Read
};
......@@ -24,6 +24,7 @@ void NetMessageReader::pushByte(uint8_t _c)
mSizePos = 5;
mCurrMessage = new NetMessage(mOptionsByte, (NetMessage::TypeInformation)mTypeInfo);
mCurrMessage->setMode(NetMessageMode::Read); // defaults to reading after being read
if (mOptionsByte & NetMessageOptions::compressed)
mCurrMessage->mIsCompressed = true;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment