Wip: more compact serialize.h 334/head
authorsvost <ya.nowa@yandex.ru>
Fri, 29 Apr 2016 05:28:32 +0000 (08:28 +0300)
committersvost <ya.nowa@yandex.ru>
Fri, 29 Apr 2016 05:28:32 +0000 (08:28 +0300)
src/serialize.cpp
src/serialize.h

index 4b8ae44..f18630b 100644 (file)
@@ -79,6 +79,56 @@ template uint64_t ReadCompactSize<CAutoFile>(CAutoFile&);
 template uint64_t ReadCompactSize<CDataStream>(CDataStream&);
 
 //
+//CAutoFile
+//
+
+CAutoFile::CAutoFile(FILE* filenew, int nTypeIn, int nVersionIn)
+{
+    file = filenew;
+    nType = nTypeIn;
+    nVersion = nVersionIn;
+    state = 0;
+    exceptmask = std::ios::badbit | std::ios::failbit;
+}
+
+CAutoFile::~CAutoFile()
+{
+    fclose();
+}
+
+void CAutoFile::fclose()
+{
+    if (file != NULL && file != stdin && file != stdout && file != stderr)
+        ::fclose(file);
+    file = NULL;
+}
+
+void CAutoFile::setstate(short bits, const char* psz)
+{
+    state |= bits;
+    if (state & exceptmask)
+        throw std::ios_base::failure(psz);
+}
+
+CAutoFile& CAutoFile::read(char* pch, size_t nSize)
+{
+    if (!file)
+        throw std::ios_base::failure("CAutoFile::read : file handle is NULL");
+    if (fread(pch, 1, nSize, file) != nSize)
+        setstate(std::ios::failbit, feof(file) ? "CAutoFile::read : end of file" : "CAutoFile::read : fread failed");
+    return (*this);
+}
+
+CAutoFile& CAutoFile::write(const char* pch, size_t nSize)
+{
+    if (!file)
+        throw std::ios_base::failure("CAutoFile::write : file handle is NULL");
+    if (fwrite(pch, 1, nSize, file) != nSize)
+        setstate(std::ios::failbit, "CAutoFile::write : write failed");
+    return (*this);
+}
+
+//
 //CBufferedFile
 //
 
index be10832..3bf0487 100644 (file)
@@ -1051,20 +1051,11 @@ public:
 };
 
 
-
-
-
-
-
-
-
-
-/** RAII wrapper for FILE*.
- *
- * Will automatically close the file when it goes out of scope if not null.
- * If you're returning the file pointer, return file.release().
- * If you need to close the file early, use file.fclose() instead of fclose(file).
- */
+// RAII wrapper for FILE*.
+//
+// Will automatically close the file when it goes out of scope if not null.
+// If you're returning the file pointer, return file.release().
+// If you need to close the file early, use file.fclose() instead of fclose(file).
 class CAutoFile
 {
 protected:
@@ -1075,26 +1066,9 @@ public:
     int nType;
     int nVersion;
 
-    CAutoFile(FILE* filenew, int nTypeIn, int nVersionIn)
-    {
-        file = filenew;
-        nType = nTypeIn;
-        nVersion = nVersionIn;
-        state = 0;
-        exceptmask = std::ios::badbit | std::ios::failbit;
-    }
-
-    ~CAutoFile()
-    {
-        fclose();
-    }
-
-    void fclose()
-    {
-        if (file != NULL && file != stdin && file != stdout && file != stderr)
-            ::fclose(file);
-        file = NULL;
-    }
+    CAutoFile(FILE* filenew, int nTypeIn, int nVersionIn);
+    ~CAutoFile();
+    void fclose();
 
     FILE* release()             { FILE* ret = file; file = NULL; return ret; }
     operator FILE*()            { return file; }
@@ -1108,13 +1082,7 @@ public:
     //
     // 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 fail() const            { return (state & (std::ios::badbit | std::ios::failbit)) != 0; }
     bool good() const            { return state == 0; }
     void clear(short n = 0)      { state = n; }
@@ -1128,23 +1096,8 @@ public:
     void ReadVersion()           { *this >> nVersion; }
     void WriteVersion()          { *this << nVersion; }
 
-    CAutoFile& read(char* pch, size_t nSize)
-    {
-        if (!file)
-            throw std::ios_base::failure("CAutoFile::read : file handle is NULL");
-        if (fread(pch, 1, nSize, file) != nSize)
-            setstate(std::ios::failbit, feof(file) ? "CAutoFile::read : end of file" : "CAutoFile::read : fread failed");
-        return (*this);
-    }
-
-    CAutoFile& write(const char* pch, size_t nSize)
-    {
-        if (!file)
-            throw std::ios_base::failure("CAutoFile::write : file handle is NULL");
-        if (fwrite(pch, 1, nSize, file) != nSize)
-            setstate(std::ios::failbit, "CAutoFile::write : write failed");
-        return (*this);
-    }
+    CAutoFile& read(char* pch, size_t nSize);
+    CAutoFile& write(const char* pch, size_t nSize);
 
     template<typename T>
     unsigned int GetSerializeSize(const T& obj)