diff options
author | Tomaž Vajngerl <tomaz.vajngerl@collabora.co.uk> | 2021-04-02 11:37:42 +0900 |
---|---|---|
committer | Tomaž Vajngerl <quikee@gmail.com> | 2021-04-02 15:23:26 +0200 |
commit | 711ec7b6c71410b850cdf6d1411d054ca34ec5a0 (patch) | |
tree | 3d65f3db6a414c04f741ebf513146c82c7aa6880 /tools | |
parent | d7d487875246ba00d5be7e4fb0fd82b78fea5205 (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>
Diffstat (limited to 'tools')
-rw-r--r-- | tools/qa/cppunit/test_GenericTypeSerializer.cxx | 43 | ||||
-rw-r--r-- | tools/source/generic/fract.cxx | 33 | ||||
-rw-r--r-- | tools/source/stream/GenericTypeSerializer.cxx | 30 |
3 files changed, 71 insertions, 35 deletions
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: */ |