summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTomaž Vajngerl <tomaz.vajngerl@collabora.co.uk>2021-04-02 11:37:42 +0900
committerTomaž Vajngerl <quikee@gmail.com>2021-04-02 15:23:26 +0200
commit711ec7b6c71410b850cdf6d1411d054ca34ec5a0 (patch)
tree3d65f3db6a414c04f741ebf513146c82c7aa6880
parentd7d487875246ba00d5be7e4fb0fd82b78fea5205 (diff)
vcl: move Fraction reading/writing to GenericTypeSerializer
Change-Id: Iccacaa7fd9cffe1d99f76def854c2150bb4d94f4 Reviewed-on: https://gerrit.libreoffice.org/c/core/+/113499 Tested-by: Jenkins Reviewed-by: Tomaž Vajngerl <quikee@gmail.com>
-rw-r--r--include/tools/GenericTypeSerializer.hxx4
-rw-r--r--include/tools/fract.hxx9
-rw-r--r--tools/qa/cppunit/test_GenericTypeSerializer.cxx43
-rw-r--r--tools/source/generic/fract.cxx33
-rw-r--r--tools/source/stream/GenericTypeSerializer.cxx30
-rw-r--r--vcl/source/gdi/TypeSerializer.cxx8
6 files changed, 85 insertions, 42 deletions
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..d378dd7c304d 100644
--- a/tools/qa/cppunit/test_GenericTypeSerializer.cxx
+++ b/tools/qa/cppunit/test_GenericTypeSerializer.cxx
@@ -87,10 +87,53 @@ public:
}
}
+ void testRoundtripFraction()
+ {
+ {
+ Fraction aFraction(2, 5);
+ CPPUNIT_ASSERT_EQUAL(true, aFraction.IsValid());
+ CPPUNIT_ASSERT_EQUAL(sal_Int32(2), aFraction.GetNumerator());
+ CPPUNIT_ASSERT_EQUAL(sal_Int32(5), aFraction.GetDenominator());
+
+ 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_EQUAL(true, aReadFraction.IsValid());
+ CPPUNIT_ASSERT_EQUAL(sal_Int32(2), aReadFraction.GetNumerator());
+ CPPUNIT_ASSERT_EQUAL(sal_Int32(5), aReadFraction.GetDenominator());
+ }
+ {
+ Fraction aFraction(1, 0);
+ CPPUNIT_ASSERT_EQUAL(false, aFraction.IsValid());
+ CPPUNIT_ASSERT_EQUAL(sal_Int32(0), aFraction.GetNumerator());
+ CPPUNIT_ASSERT_EQUAL(sal_Int32(-1), aFraction.GetDenominator());
+
+ 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_EQUAL(false, aReadFraction.IsValid());
+ CPPUNIT_ASSERT_EQUAL(sal_Int32(0), aReadFraction.GetNumerator());
+ CPPUNIT_ASSERT_EQUAL(sal_Int32(-1), 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..7bba06d97aba 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(0);
+ }
+ 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());
}