[Libreoffice] [PATCH] Add support for bool for the operator << and >> of SvStream

Norbert Thiebaud nthiebaud at gmail.com
Sat Oct 2 19:18:35 PDT 2010


[PATCH] Add support for bool for the operator << and >> of SvStream

In order to use the type bool when appropriate
-- instead of an unsigned char to mimic it --
SvStream need to be extended to support the serialization of
bool elements.

 tools/inc/tools/stream.hxx     |    2 ++
 tools/source/stream/stream.cxx |   34 ++++++++++++++++++++++++++++++++++
 2 files changed, 36 insertions(+), 0 deletions(-)

diff --git a/tools/inc/tools/stream.hxx b/tools/inc/tools/stream.hxx
index 0ce15bc..ad1d27a 100644
--- a/tools/inc/tools/stream.hxx
+++ b/tools/inc/tools/stream.hxx
@@ -361,6 +361,7 @@ public:
     SvStream&		operator>>( signed char& rChar );
     SvStream&		operator>>( char& rChar );
     SvStream&		operator>>( unsigned char& rChar );
+    SvStream&		operator>>( bool& rBool );
     SvStream&		operator>>( float& rFloat );
     SvStream&		operator>>( double& rDouble );
 #ifdef ENABLE_BYTESTRING_STREAM_OPERATORS
@@ -379,6 +380,7 @@ public:
     SvStream&		operator<<( signed char nChar );
     SvStream&		operator<<( char nChar );
     SvStream&		operator<<( unsigned char nChar );
+    SvStream&		operator<<( bool nBool );
     SvStream&		operator<<( float nFloat );
     SvStream&		operator<<( const double& rDouble );
     SvStream&		operator<<( const char* pBuf );
diff --git a/tools/source/stream/stream.cxx b/tools/source/stream/stream.cxx
index e4395cf..5ff0d8e 100644
--- a/tools/source/stream/stream.cxx
+++ b/tools/source/stream/stream.cxx
@@ -1271,6 +1271,21 @@ SvStream& SvStream::operator>>( unsigned char& r )
     return *this;
 }

+SvStream& SvStream::operator>>( bool& r )
+{
+    if( (eIOMode == STREAM_IO_READ || !bIsConsistent) &&
+        sizeof(char) <= nBufFree )
+    {
+        r = *pBufPos ? true : false;
+        nBufActualPos += sizeof(bool);
+        pBufPos += sizeof(bool);
+        nBufFree -= sizeof(bool);
+    }
+    else
+        Read( (char*)&r, sizeof(bool) );
+    return *this;
+}
+
 SvStream& SvStream::operator>>( float& r )
 {
     // Read( (char*)&r, sizeof(float) );
@@ -1422,6 +1437,25 @@ SvStream& SvStream::operator<<  ( unsigned char v )
     return *this;
 }

+SvStream& SvStream::operator<<  ( bool v )
+{
+    int tmp = eIOMode;
+
+    if(tmp == STREAM_IO_WRITE && sizeof(char) <= nBufFree )
+    {
+        *(unsigned char*)pBufPos = (unsigned char)v;
+        pBufPos++; // = sizeof(bool);
+        nBufActualPos++; // = sizeof(bool);
+        if( nBufActualPos > nBufActualLen )  // Append ?
+            nBufActualLen = nBufActualPos;
+        nBufFree--;
+        bIsDirty = TRUE;
+    }
+    else
+        Write( (char*)&v, sizeof(bool) );
+    return *this;
+}
+
 SvStream& SvStream::operator<< ( float v )
 {
 #ifdef UNX
-- 
1.7.1


More information about the LibreOffice mailing list