Add StructuredSerialiser which allows serialising to SDObjects directly

This commit is contained in:
baldurk
2018-10-11 11:43:44 +01:00
parent 9ff71eb175
commit 6744802a53
4 changed files with 42 additions and 5 deletions
+5 -1
View File
@@ -40,12 +40,16 @@ int64_t Chunk::m_TotalMem = 0;
// Read Serialiser functions
template <>
Serialiser<SerialiserMode::Reading>::Serialiser(StreamReader *reader, Ownership own)
Serialiser<SerialiserMode::Reading>::Serialiser(StreamReader *reader, Ownership own,
SDObject *rootStructuredObj)
{
m_Read = reader;
m_Write = NULL;
m_Ownership = own;
if(rootStructuredObj)
m_StructureStack.push_back(rootStructuredObj);
}
template <>
+13 -3
View File
@@ -166,7 +166,7 @@ public:
return StringFormat::Fmt("<No Chunk Lookup: %u>", m_ChunkMetadata.chunkID);
}
ChunkLookup GetChunkLookup() { return m_ChunkLookup; }
/////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////
@@ -1520,7 +1520,7 @@ public:
// constructors only available by the derived classes for each serialiser type
protected:
Serialiser(StreamWriter *writer, Ownership own);
Serialiser(StreamReader *reader, Ownership own);
Serialiser(StreamReader *reader, Ownership own, SDObject *rootStructuredObj);
private:
static const uint64_t ChunkAlignment = 64;
@@ -1632,7 +1632,7 @@ public:
class ReadSerialiser : public Serialiser<SerialiserMode::Reading>
{
public:
ReadSerialiser(StreamReader *reader, Ownership own) : Serialiser(reader, own) {}
ReadSerialiser(StreamReader *reader, Ownership own) : Serialiser(reader, own, NULL) {}
template <typename ChunkType>
ChunkType ReadChunk()
{
@@ -1640,6 +1640,16 @@ public:
return (ChunkType)BeginChunk(0, 0);
}
};
class StructuredSerialiser : public Serialiser<SerialiserMode::Reading>
{
public:
StructuredSerialiser(SDObject *obj, ChunkLookup lookup)
: Serialiser(new StreamReader(StreamReader::DummyStream), Ownership::Stream, obj)
{
ConfigureStructuredExport(lookup, false);
}
};
#endif
#define BASIC_TYPE_SERIALISE(typeName, member, type, byteSize) \
+12
View File
@@ -73,6 +73,18 @@ StreamReader::StreamReader(StreamInvalidType)
m_HasError = true;
}
StreamReader::StreamReader(StreamDummyType)
{
m_InputSize = 0;
m_BufferSize = 0;
m_BufferHead = m_BufferBase = NULL;
m_Ownership = Ownership::Nothing;
m_Dummy = true;
}
StreamReader::StreamReader(Network::Socket *sock, Ownership own)
{
m_Sock = sock;
+12 -1
View File
@@ -73,8 +73,13 @@ public:
{
InvalidStream
};
enum StreamDummyType
{
DummyStream
};
StreamReader(StreamInvalidType);
StreamReader(StreamDummyType);
StreamReader(const byte *buffer, uint64_t bufferSize);
StreamReader(const std::vector<byte> &buffer);
@@ -93,6 +98,8 @@ public:
inline uint64_t GetSize() { return m_InputSize; }
inline bool AtEnd()
{
if(m_Dummy)
return false;
if(m_Sock)
return Available() == 0;
return GetOffset() >= GetSize();
@@ -113,7 +120,7 @@ public:
bool Read(void *data, uint64_t numBytes)
{
if(numBytes == 0)
if(numBytes == 0 || m_Dummy)
return true;
if(!m_BufferBase)
@@ -229,6 +236,10 @@ private:
// flag indicating if an error has been encountered and the stream is now invalid
bool m_HasError = false;
// flag indicating this reader is a dummy and doesn't read anything or clear inputs. Used with a
// structured serialiser to 'read' pre-existing data.
bool m_Dummy = false;
// do we own the file/compressor? are we responsible for
// cleaning it up?
Ownership m_Ownership;