Wip: more compact serialize.h 342/head
authorsvost <ya.nowa@yandex.ru>
Wed, 4 May 2016 10:59:24 +0000 (13:59 +0300)
committersvost <ya.nowa@yandex.ru>
Wed, 4 May 2016 10:59:24 +0000 (13:59 +0300)
src/serialize.cpp
src/serialize.h

index f18630b..9094787 100644 (file)
@@ -79,6 +79,80 @@ template uint64_t ReadCompactSize<CAutoFile>(CAutoFile&);
 template uint64_t ReadCompactSize<CDataStream>(CDataStream&);
 
 //
+//CDataStream
+//
+
+
+bool CDataStream::Rewind(size_type n)
+{
+    // Rewind by n characters if the buffer hasn't been compacted yet
+    if (n > nReadPos)
+        return false;
+    nReadPos -= (unsigned int)n;
+    return true;
+}
+
+void CDataStream::setstate(short bits, const char* psz)
+{
+    state |= bits;
+    if (state & exceptmask)
+        throw std::ios_base::failure(psz);
+}
+
+CDataStream& CDataStream::read(char* pch, int nSize)
+{
+    // Read from the beginning of the buffer
+    assert(nSize >= 0);
+    unsigned int nReadPosNext = nReadPos + nSize;
+    if (nReadPosNext >= vch.size())
+    {
+        if (nReadPosNext > vch.size())
+        {
+            setstate(std::ios::failbit, "CDataStream::read() : end of data");
+            memset(pch, 0, nSize);
+            nSize = (int)(vch.size() - nReadPos);
+        }
+        memcpy(pch, &vch[nReadPos], nSize);
+        nReadPos = 0;
+        vch.clear();
+        return (*this);
+    }
+    memcpy(pch, &vch[nReadPos], nSize);
+    nReadPos = nReadPosNext;
+    return (*this);
+}
+
+CDataStream& CDataStream::ignore(int nSize)
+{
+    // Ignore from the beginning of the buffer
+    assert(nSize >= 0);
+    unsigned int nReadPosNext = nReadPos + nSize;
+    if (nReadPosNext >= vch.size())
+    {
+        if (nReadPosNext > vch.size())
+            setstate(std::ios::failbit, "CDataStream::ignore() : end of data");
+        nReadPos = 0;
+        vch.clear();
+        return (*this);
+    }
+    nReadPos = nReadPosNext;
+    return (*this);
+}
+
+CDataStream& CDataStream::write(const char* pch, int nSize)
+{
+    // Write to the end of the buffer
+    assert(nSize >= 0);
+    vch.insert(vch.end(), pch, pch + nSize);
+    return (*this);
+}
+
+void CDataStream::GetAndClear(CSerializeData &data) {
+    vch.swap(data);
+    CSerializeData().swap(vch);
+}
+
+//
 //CAutoFile
 //
 
index 3bf0487..e54d1aa 100644 (file)
@@ -929,26 +929,12 @@ public:
         nReadPos = 0;
     }
 
-    bool Rewind(size_type n)
-    {
-        // Rewind by n characters if the buffer hasn't been compacted yet
-        if (n > nReadPos)
-            return false;
-        nReadPos -= (unsigned int)n;
-        return true;
-    }
-
+    bool Rewind(size_type n);
 
     //
     // Stream subset
     //
-    void setstate(short bits, const char* psz)
-    {
-        state |= bits;
-        if (state & exceptmask)
-            throw std::ios_base::failure(psz);
-    }
-
+    void setstate(short bits, const char* psz);
     bool eof() const             { return size() == 0; }
     bool fail() const            { return (state & (std::ios::badbit | std::ios::failbit)) != 0; }
     bool good() const            { return !eof() && (state == 0); }
@@ -965,53 +951,9 @@ public:
     void ReadVersion()           { *this >> nVersion; }
     void WriteVersion()          { *this << nVersion; }
 
-    CDataStream& read(char* pch, int nSize)
-    {
-        // Read from the beginning of the buffer
-        assert(nSize >= 0);
-        unsigned int nReadPosNext = nReadPos + nSize;
-        if (nReadPosNext >= vch.size())
-        {
-            if (nReadPosNext > vch.size())
-            {
-                setstate(std::ios::failbit, "CDataStream::read() : end of data");
-                memset(pch, 0, nSize);
-                nSize = (int)(vch.size() - nReadPos);
-            }
-            memcpy(pch, &vch[nReadPos], nSize);
-            nReadPos = 0;
-            vch.clear();
-            return (*this);
-        }
-        memcpy(pch, &vch[nReadPos], nSize);
-        nReadPos = nReadPosNext;
-        return (*this);
-    }
-
-    CDataStream& ignore(int nSize)
-    {
-        // Ignore from the beginning of the buffer
-        assert(nSize >= 0);
-        unsigned int nReadPosNext = nReadPos + nSize;
-        if (nReadPosNext >= vch.size())
-        {
-            if (nReadPosNext > vch.size())
-                setstate(std::ios::failbit, "CDataStream::ignore() : end of data");
-            nReadPos = 0;
-            vch.clear();
-            return (*this);
-        }
-        nReadPos = nReadPosNext;
-        return (*this);
-    }
-
-    CDataStream& write(const char* pch, int nSize)
-    {
-        // Write to the end of the buffer
-        assert(nSize >= 0);
-        vch.insert(vch.end(), pch, pch + nSize);
-        return (*this);
-    }
+    CDataStream& read(char* pch, int nSize);
+    CDataStream& ignore(int nSize);
+    CDataStream& write(const char* pch, int nSize);
 
     template<typename Stream>
     void Serialize(Stream& s, int nType, int nVersion) const
@@ -1044,10 +986,7 @@ public:
         return (*this);
     }
 
-    void GetAndClear(CSerializeData &data) {
-        vch.swap(data);
-        CSerializeData().swap(vch);
-    }
+    void GetAndClear(CSerializeData &data);
 };