From 66eedce71f10c30712f34732157e4dcdfcb49090 Mon Sep 17 00:00:00 2001 From: Tomaž Vajngerl Date: Thu, 26 Sep 2019 16:22:09 +0200 Subject: Move Rectangle,Point,Size serialization to GenericTypeSerializer MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Change-Id: Iae489fc31b13b836e1df5327ba2fa07e0325907a Reviewed-on: https://gerrit.libreoffice.org/79793 Tested-by: Jenkins Reviewed-by: Tomaž Vajngerl --- tools/CppunitTest_tools_test.mk | 1 + tools/qa/cppunit/test_GenericTypeSerializer.cxx | 101 ++++++++++++++++++++++++ tools/source/generic/gen.cxx | 44 ----------- tools/source/stream/GenericTypeSerializer.cxx | 81 +++++++++++++++++++ 4 files changed, 183 insertions(+), 44 deletions(-) create mode 100644 tools/qa/cppunit/test_GenericTypeSerializer.cxx (limited to 'tools') diff --git a/tools/CppunitTest_tools_test.mk b/tools/CppunitTest_tools_test.mk index a4cdf8626f3a..515c3fbd1e14 100644 --- a/tools/CppunitTest_tools_test.mk +++ b/tools/CppunitTest_tools_test.mk @@ -29,6 +29,7 @@ $(eval $(call gb_CppunitTest_add_exception_objects,tools_test, \ tools/qa/cppunit/test_100mm2twips \ tools/qa/cppunit/test_fround \ tools/qa/cppunit/test_xmlwalker \ + tools/qa/cppunit/test_GenericTypeSerializer \ )) $(eval $(call gb_CppunitTest_add_cxxobjects,tools_test,\ diff --git a/tools/qa/cppunit/test_GenericTypeSerializer.cxx b/tools/qa/cppunit/test_GenericTypeSerializer.cxx new file mode 100644 index 000000000000..24d1497f92d2 --- /dev/null +++ b/tools/qa/cppunit/test_GenericTypeSerializer.cxx @@ -0,0 +1,101 @@ +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ +/* + * This file is part of the LibreOffice project. + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + * + * This file incorporates work covered by the following license notice: + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed + * with this work for additional information regarding copyright + * ownership. The ASF licenses this file to you under the Apache + * License, Version 2.0 (the "License"); you may not use this file + * except in compliance with the License. You may obtain a copy of + * the License at http://www.apache.org/licenses/LICENSE-2.0 . + */ + +#include + +#include +#include + +#include +#include +#include + +namespace tools +{ +class GenericTypeSerializerTest : public CppUnit::TestFixture +{ +public: + void testRoundtripPoint() + { + Point aPoint(20, 50); + SvMemoryStream aStream; + GenericTypeSerializer aSerializer(aStream); + aSerializer.writePoint(aPoint); + aStream.Seek(STREAM_SEEK_TO_BEGIN); + Point aReadPoint; + aSerializer.readPoint(aReadPoint); + CPPUNIT_ASSERT_EQUAL(aPoint, aReadPoint); + } + + void testRoundtripSize() + { + Size aSize(40, 80); + SvMemoryStream aStream; + GenericTypeSerializer aSerializer(aStream); + aSerializer.writeSize(aSize); + aStream.Seek(STREAM_SEEK_TO_BEGIN); + Size aReadSize; + aSerializer.readSize(aReadSize); + CPPUNIT_ASSERT_EQUAL(aSize, aReadSize); + } + + void testRoundtripRectangle() + { + { + Rectangle aRectangle; + CPPUNIT_ASSERT(aRectangle.IsEmpty()); + SvMemoryStream aStream; + aStream.Seek(STREAM_SEEK_TO_BEGIN); + GenericTypeSerializer aSerializer(aStream); + aSerializer.writeRectangle(aRectangle); + aStream.Seek(STREAM_SEEK_TO_BEGIN); + // Need to set the rectangle to non-empty, so it will be set to empty later + Rectangle aReadRectangle(Point(20, 50), Size(10, 30)); + aSerializer.readRectangle(aReadRectangle); + CPPUNIT_ASSERT(aRectangle.IsEmpty()); + } + + { + Rectangle aRectangle(Point(20, 50), Size(10, 30)); + SvMemoryStream aStream; + aStream.Seek(STREAM_SEEK_TO_BEGIN); + GenericTypeSerializer aSerializer(aStream); + aSerializer.writeRectangle(aRectangle); + aStream.Seek(STREAM_SEEK_TO_BEGIN); + Rectangle aReadRectangle; + aSerializer.readRectangle(aReadRectangle); + CPPUNIT_ASSERT_EQUAL(aRectangle.Top(), aReadRectangle.Top()); + CPPUNIT_ASSERT_EQUAL(aRectangle.Left(), aReadRectangle.Left()); + CPPUNIT_ASSERT_EQUAL(aRectangle.Right(), aReadRectangle.Right()); + CPPUNIT_ASSERT_EQUAL(aRectangle.Bottom(), aReadRectangle.Bottom()); + } + } + + CPPUNIT_TEST_SUITE(GenericTypeSerializerTest); + CPPUNIT_TEST(testRoundtripPoint); + CPPUNIT_TEST(testRoundtripSize); + CPPUNIT_TEST(testRoundtripRectangle); + CPPUNIT_TEST_SUITE_END(); +}; + +CPPUNIT_TEST_SUITE_REGISTRATION(GenericTypeSerializerTest); + +} // namespace tools + +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/tools/source/generic/gen.cxx b/tools/source/generic/gen.cxx index 0a488a4e51b4..7c182818c460 100644 --- a/tools/source/generic/gen.cxx +++ b/tools/source/generic/gen.cxx @@ -27,23 +27,6 @@ #include #include -SvStream& ReadPair( SvStream& rIStream, Pair& rPair ) -{ - sal_Int32 nTmpA(0), nTmpB(0); - rIStream.ReadInt32( nTmpA ).ReadInt32( nTmpB ); - rPair.nA = nTmpA; - rPair.nB = nTmpB; - - return rIStream; -} - -SvStream& WritePair( SvStream& rOStream, const Pair& rPair ) -{ - rOStream.WriteInt32( rPair.nA ).WriteInt32( rPair.nB ); - - return rOStream; -} - OString Pair::toString() const { std::stringstream ss; @@ -200,33 +183,6 @@ bool tools::Rectangle::IsOver( const tools::Rectangle& rRect ) const return !GetIntersection( rRect ).IsEmpty(); } -namespace tools -{ -SvStream& ReadRectangle( SvStream& rIStream, tools::Rectangle& rRect ) -{ - sal_Int32 nTmpL(0), nTmpT(0), nTmpR(0), nTmpB(0); - - rIStream.ReadInt32( nTmpL ).ReadInt32( nTmpT ).ReadInt32( nTmpR ).ReadInt32( nTmpB ); - - rRect.nLeft = nTmpL; - rRect.nTop = nTmpT; - rRect.nRight = nTmpR; - rRect.nBottom = nTmpB; - - return rIStream; -} - -SvStream& WriteRectangle( SvStream& rOStream, const tools::Rectangle& rRect ) -{ - rOStream.WriteInt32( rRect.nLeft ) - .WriteInt32( rRect.nTop ) - .WriteInt32( rRect.nRight ) - .WriteInt32( rRect.nBottom ); - - return rOStream; -} -} - OString tools::Rectangle::toString() const { std::stringstream ss; diff --git a/tools/source/stream/GenericTypeSerializer.cxx b/tools/source/stream/GenericTypeSerializer.cxx index 7e261350ad50..c099713d24ac 100644 --- a/tools/source/stream/GenericTypeSerializer.cxx +++ b/tools/source/stream/GenericTypeSerializer.cxx @@ -26,6 +26,8 @@ namespace tools { constexpr sal_uInt16 COL_NAME_USER = 0x8000; +constexpr sal_Int32 RECT_EMPTY_VALUE_RIGHT_BOTTOM = -32767; + void GenericTypeSerializer::readColor(Color& rColor) { sal_uInt16 nColorNameID(0); @@ -100,6 +102,85 @@ void GenericTypeSerializer::writeColor(const Color& rColor) mrStream.WriteUInt16((nB << 8) + nB); } +void GenericTypeSerializer::readPoint(Point& rPoint) +{ + sal_Int32 nX(0); + sal_Int32 nY(0); + + mrStream.ReadInt32(nX); + mrStream.ReadInt32(nY); + + rPoint.setX(nX); + rPoint.setY(nY); +} + +void GenericTypeSerializer::writePoint(const Point& rPoint) +{ + mrStream.WriteInt32(rPoint.getX()); + mrStream.WriteInt32(rPoint.getY()); +} + +void GenericTypeSerializer::readSize(Size& rSize) +{ + sal_Int32 nWidth(0); + sal_Int32 nHeight(0); + + mrStream.ReadInt32(nWidth); + mrStream.ReadInt32(nHeight); + + rSize.setWidth(nWidth); + rSize.setHeight(nHeight); +} + +void GenericTypeSerializer::writeSize(const Size& rSize) +{ + mrStream.WriteInt32(rSize.getWidth()); + mrStream.WriteInt32(rSize.getHeight()); +} + +void GenericTypeSerializer::readRectangle(Rectangle& rRectangle) +{ + sal_Int32 nLeft(0); + sal_Int32 nTop(0); + sal_Int32 nRight(0); + sal_Int32 nBottom(0); + + mrStream.ReadInt32(nLeft); + mrStream.ReadInt32(nTop); + mrStream.ReadInt32(nRight); + mrStream.ReadInt32(nBottom); + + if (nRight == RECT_EMPTY_VALUE_RIGHT_BOTTOM || nBottom == RECT_EMPTY_VALUE_RIGHT_BOTTOM) + { + rRectangle.SetEmpty(); + } + else + { + rRectangle.SetLeft(nLeft); + rRectangle.SetTop(nTop); + rRectangle.SetRight(nRight); + rRectangle.SetBottom(nBottom); + } +} + +void GenericTypeSerializer::writeRectangle(const Rectangle& rRectangle) +{ + if (rRectangle.IsEmpty()) + { + mrStream.WriteInt32(0); + mrStream.WriteInt32(0); + mrStream.WriteInt32(RECT_EMPTY_VALUE_RIGHT_BOTTOM); + mrStream.WriteInt32(RECT_EMPTY_VALUE_RIGHT_BOTTOM); + } + else + { + mrStream.WriteInt32(rRectangle.Left()); + mrStream.WriteInt32(rRectangle.Top()); + mrStream.WriteInt32(rRectangle.Right()); + mrStream.WriteInt32(rRectangle.Bottom()); + } +} + } // end namespace tools /* vim:set shiftwidth=4 softtabstop=4 expandtab: */ -- cgit