[Libreoffice-commits] core.git: include/tools

Stephan Bergmann sbergman at redhat.com
Wed Nov 9 11:20:06 UTC 2016


 include/tools/gen.hxx |   55 +++++++++++++++++++++++++++++++++++++++++---------
 1 file changed, 46 insertions(+), 9 deletions(-)

New commits:
commit cd61f3eb3df492113883d53e75233afcd6a19fab
Author: Stephan Bergmann <sbergman at redhat.com>
Date:   Wed Nov 9 12:17:04 2016 +0100

    Make sure not to compare different subclasses of Pair
    
    (A related option would be to make those subclasses derive privately from Pair,
    but there are a few places that generically operate on any Pair instances, like
    Pair::Read/WritePair or SvxShape::ForceMetricToItemPoolMetric/100th_mm.)
    
    Change-Id: I6c638fe65ee5684593fdeab29b144f547e173f4e

diff --git a/include/tools/gen.hxx b/include/tools/gen.hxx
index 987dd43..142f82c 100644
--- a/include/tools/gen.hxx
+++ b/include/tools/gen.hxx
@@ -48,9 +48,6 @@ public:
     long&               A() { return nA; }
     long&               B() { return nB; }
 
-    bool                operator == ( const Pair& rPair ) const;
-    bool                operator != ( const Pair& rPair ) const;
-
     TOOLS_DLLPUBLIC friend SvStream&    ReadPair( SvStream& rIStream, Pair& rPair );
     TOOLS_DLLPUBLIC friend SvStream&    WritePair( SvStream& rOStream, const Pair& rPair );
 
@@ -59,16 +56,16 @@ protected:
     long                nB;
 };
 
-inline bool Pair::operator == ( const Pair& rPair ) const
-{
-    return ((nA == rPair.nA) && (nB == rPair.nB));
-}
+namespace tools { namespace detail {
 
-inline bool Pair::operator != ( const Pair& rPair ) const
+// Used to implement operator == for subclasses of Pair:
+inline bool equal(Pair const & p1, Pair const & p2)
 {
-    return ((nA != rPair.nA) || (nB != rPair.nB));
+    return p1.A() == p2.A() && p1.B() == p2.B();
 }
 
+} }
+
 // Point
 
 class SAL_WARN_UNUSED SAL_DLLPUBLIC_EXPORT Point : public Pair
@@ -158,6 +155,16 @@ inline Point operator/( const Point &rVal1, const long nVal2 )
     return Point( rVal1.nA/nVal2, rVal1.nB/nVal2 );
 }
 
+inline bool operator ==(Point const & p1, Point const & p2)
+{
+    return tools::detail::equal(p1, p2);
+}
+
+inline bool operator !=(Point const & p1, Point const & p2)
+{
+    return !(p1 == p2);
+}
+
 template< typename charT, typename traits >
 inline std::basic_ostream<charT, traits> & operator <<(
     std::basic_ostream<charT, traits> & stream, const Point& point )
@@ -185,6 +192,16 @@ public:
     void            setHeight(long nHeight)  { Height() = nHeight; }
 };
 
+inline bool operator ==(Size const & s1, Size const & s2)
+{
+    return tools::detail::equal(s1, s2);
+}
+
+inline bool operator !=(Size const & s1, Size const & s2)
+{
+    return !(s1 == s2);
+}
+
 template< typename charT, typename traits >
 inline std::basic_ostream<charT, traits> & operator <<(
     std::basic_ostream<charT, traits> & stream, const Size& size )
@@ -229,6 +246,16 @@ inline void Range::Justify()
     }
 }
 
+inline bool operator ==(Range const & r1, Range const & r2)
+{
+    return tools::detail::equal(r1, r2);
+}
+
+inline bool operator !=(Range const & r1, Range const & r2)
+{
+    return !(r1 == r2);
+}
+
 template< typename charT, typename traits >
 inline std::basic_ostream<charT, traits> & operator <<(
     std::basic_ostream<charT, traits> & stream, const Range& range )
@@ -281,6 +308,16 @@ inline void Selection::Justify()
     }
 }
 
+inline bool operator ==(Selection const & s1, Selection const & s2)
+{
+    return tools::detail::equal(s1, s2);
+}
+
+inline bool operator !=(Selection const & s1, Selection const & s2)
+{
+    return !(s1 == s2);
+}
+
 template< typename charT, typename traits >
 inline std::basic_ostream<charT, traits> & operator <<(
     std::basic_ostream<charT, traits> & stream, const Selection& selection )


More information about the Libreoffice-commits mailing list