[Libreoffice-commits] core.git: Branch 'private/tvajngerl/staging' - include/tools tools/qa tools/source vcl/source
Tomaž Vajngerl (via logerrit)
logerrit at kemper.freedesktop.org
Fri Apr 2 02:38:52 UTC 2021
include/tools/GenericTypeSerializer.hxx | 4 ++
include/tools/fract.hxx | 9 ++++--
tools/qa/cppunit/test_GenericTypeSerializer.cxx | 22 ++++++++++++++++
tools/source/generic/fract.cxx | 33 ------------------------
tools/source/stream/GenericTypeSerializer.cxx | 30 ++++++++++++++++++++-
vcl/source/gdi/TypeSerializer.cxx | 8 ++---
6 files changed, 64 insertions(+), 42 deletions(-)
New commits:
commit 327f1f18440beb2219b3167319e5d97c715198ce
Author: Tomaž Vajngerl <tomaz.vajngerl at collabora.co.uk>
AuthorDate: Fri Apr 2 11:37:42 2021 +0900
Commit: Tomaž Vajngerl <tomaz.vajngerl at collabora.co.uk>
CommitDate: Fri Apr 2 11:37:42 2021 +0900
vcl: move Fraction reading/writing to GenericTypeSerializer
Change-Id: Iccacaa7fd9cffe1d99f76def854c2150bb4d94f4
diff --git a/include/tools/GenericTypeSerializer.hxx b/include/tools/GenericTypeSerializer.hxx
index cb54e693c118..f67d92c845c5 100644
--- a/include/tools/GenericTypeSerializer.hxx
+++ b/include/tools/GenericTypeSerializer.hxx
@@ -23,6 +23,7 @@
#include <tools/color.hxx>
#include <tools/gen.hxx>
#include <tools/stream.hxx>
+#include <tools/fract.hxx>
namespace tools
{
@@ -47,6 +48,9 @@ public:
void readRectangle(Rectangle& rRectangle);
void writeRectangle(const Rectangle& rRectangle);
+
+ void readFraction(Fraction& rFraction);
+ void writeFraction(Fraction const& rFraction);
};
} // end namespace tools
diff --git a/include/tools/fract.hxx b/include/tools/fract.hxx
index c37859d7c17a..73d22fcaf430 100644
--- a/include/tools/fract.hxx
+++ b/include/tools/fract.hxx
@@ -33,7 +33,6 @@ class SAL_WARN_UNUSED TOOLS_DLLPUBLIC Fraction final
sal_Int32 mnNumerator = 0;
sal_Int32 mnDenominator = 1;
bool mbValid = true;
-
public:
Fraction() = default;
Fraction( const Fraction & rFrac ) = default;
@@ -85,8 +84,12 @@ public:
TOOLS_DLLPUBLIC friend bool operator<=( const Fraction& rVal1, const Fraction& rVal2 );
TOOLS_DLLPUBLIC friend bool operator>=( const Fraction& rVal1, const Fraction& rVal2 );
- TOOLS_DLLPUBLIC friend SvStream& ReadFraction( SvStream& rIStream, Fraction & rFract );
- TOOLS_DLLPUBLIC friend SvStream& WriteFraction( SvStream& rOStream, const Fraction& rFract );
+ static Fraction createInvalid()
+ {
+ Fraction aFraction;
+ aFraction.mbValid = false;
+ return aFraction;
+ }
};
TOOLS_DLLPUBLIC Fraction operator+( const Fraction& rVal1, const Fraction& rVal2 );
diff --git a/tools/qa/cppunit/test_GenericTypeSerializer.cxx b/tools/qa/cppunit/test_GenericTypeSerializer.cxx
index 24d1497f92d2..262f0d68671f 100644
--- a/tools/qa/cppunit/test_GenericTypeSerializer.cxx
+++ b/tools/qa/cppunit/test_GenericTypeSerializer.cxx
@@ -87,10 +87,32 @@ public:
}
}
+ void testRoundtripFraction()
+ {
+ {
+ Fraction aFraction(2, 5);
+ CPPUNIT_ASSERT(aFraction.IsValid());
+
+ SvMemoryStream aStream;
+ aStream.Seek(STREAM_SEEK_TO_BEGIN);
+ GenericTypeSerializer aSerializer(aStream);
+ aSerializer.writeFraction(aFraction);
+
+ aStream.Seek(STREAM_SEEK_TO_BEGIN);
+
+ Fraction aReadFraction(1, 2);
+ aSerializer.readFraction(aReadFraction);
+ CPPUNIT_ASSERT(aReadFraction.IsValid());
+ CPPUNIT_ASSERT_EQUAL(sal_Int32(2), aReadFraction.GetNumerator());
+ CPPUNIT_ASSERT_EQUAL(sal_Int32(5), aReadFraction.GetDenominator());
+ }
+ }
+
CPPUNIT_TEST_SUITE(GenericTypeSerializerTest);
CPPUNIT_TEST(testRoundtripPoint);
CPPUNIT_TEST(testRoundtripSize);
CPPUNIT_TEST(testRoundtripRectangle);
+ CPPUNIT_TEST(testRoundtripFraction);
CPPUNIT_TEST_SUITE_END();
};
diff --git a/tools/source/generic/fract.cxx b/tools/source/generic/fract.cxx
index 448a70c5ea33..b525d1de9896 100644
--- a/tools/source/generic/fract.cxx
+++ b/tools/source/generic/fract.cxx
@@ -370,39 +370,6 @@ bool operator > ( const Fraction& rVal1, const Fraction& rVal2 )
return toRational(rVal1.mnNumerator, rVal1.mnDenominator) > toRational(rVal2.mnNumerator, rVal2.mnDenominator);
}
-SvStream& ReadFraction( SvStream& rIStream, Fraction & rFract )
-{
- sal_Int32 num(0), den(0);
- rIStream.ReadInt32( num );
- rIStream.ReadInt32( den );
- if ( den <= 0 )
- {
- SAL_WARN( "tools.fraction", "'ReadFraction()' read an invalid fraction" );
- rFract.mbValid = false;
- }
- else
- {
- rFract.mnNumerator = num;
- rFract.mnDenominator = den;
- rFract.mbValid = true;
- }
- return rIStream;
-}
-
-SvStream& WriteFraction( SvStream& rOStream, const Fraction& rFract )
-{
- if ( !rFract.mbValid )
- {
- SAL_WARN( "tools.fraction", "'WriteFraction()' write an invalid fraction" );
- rOStream.WriteInt32( 0 );
- rOStream.WriteInt32( -1 );
- } else {
- rOStream.WriteInt32( rFract.mnNumerator );
- rOStream.WriteInt32( rFract.mnDenominator );
- }
- return rOStream;
-}
-
// If dVal > LONG_MAX or dVal < LONG_MIN, the rational throws a boost::bad_rational.
// Otherwise, dVal and denominator are multiplied by 8, until one of them
// is larger than (LONG_MAX / 8).
diff --git a/tools/source/stream/GenericTypeSerializer.cxx b/tools/source/stream/GenericTypeSerializer.cxx
index c099713d24ac..cfb5b26cf6be 100644
--- a/tools/source/stream/GenericTypeSerializer.cxx
+++ b/tools/source/stream/GenericTypeSerializer.cxx
@@ -17,9 +17,9 @@
* the License at http://www.apache.org/licenses/LICENSE-2.0 .
*/
-#include <sal/config.h>
-
#include <tools/GenericTypeSerializer.hxx>
+#include <sal/config.h>
+#include <sal/log.hxx>
#include <vector>
namespace tools
@@ -181,6 +181,32 @@ void GenericTypeSerializer::writeRectangle(const Rectangle& rRectangle)
}
}
+void GenericTypeSerializer::readFraction(Fraction& rFraction)
+{
+ sal_Int32 nNumerator(0);
+ sal_Int32 nDenominator(0);
+
+ mrStream.ReadInt32(nNumerator);
+ mrStream.ReadInt32(nDenominator);
+
+ rFraction = Fraction(nNumerator, nDenominator);
+}
+
+void GenericTypeSerializer::writeFraction(Fraction const& rFraction)
+{
+ if (!rFraction.IsValid())
+ {
+ SAL_WARN("tools.fraction", "'writeFraction()' write an invalid fraction");
+ mrStream.WriteInt32(0);
+ mrStream.WriteInt32(-1);
+ }
+ else
+ {
+ mrStream.WriteInt32(rFraction.GetNumerator());
+ mrStream.WriteInt32(rFraction.GetDenominator());
+ }
+}
+
} // end namespace tools
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
diff --git a/vcl/source/gdi/TypeSerializer.cxx b/vcl/source/gdi/TypeSerializer.cxx
index 7f0609e01826..8dc98c91da5d 100644
--- a/vcl/source/gdi/TypeSerializer.cxx
+++ b/vcl/source/gdi/TypeSerializer.cxx
@@ -428,8 +428,8 @@ void TypeSerializer::readMapMode(MapMode& rMapMode)
mrStream.ReadUInt16(nTmp16);
MapUnit eUnit = static_cast<MapUnit>(nTmp16);
readPoint(aOrigin);
- ReadFraction(mrStream, aScaleX);
- ReadFraction(mrStream, aScaleY);
+ readFraction(aScaleX);
+ readFraction(aScaleY);
mrStream.ReadCharAsBool(bSimple);
if (bSimple)
@@ -444,8 +444,8 @@ void TypeSerializer::writeMapMode(MapMode const& rMapMode)
mrStream.WriteUInt16(sal_uInt16(rMapMode.GetMapUnit()));
writePoint(rMapMode.GetOrigin());
- WriteFraction(mrStream, rMapMode.GetScaleX());
- WriteFraction(mrStream, rMapMode.GetScaleY());
+ writeFraction(rMapMode.GetScaleX());
+ writeFraction(rMapMode.GetScaleY());
mrStream.WriteBool(rMapMode.IsSimple());
}
More information about the Libreoffice-commits
mailing list