From 7ad4e757c3a37889e77ecc60081ef12c27cb8815 Mon Sep 17 00:00:00 2001 From: "Frank Schoenheit [fs]" Date: Tue, 18 Jan 2011 22:11:21 +0100 Subject: gridsort: getStandardLessPredicate: optional collator instance for string comparison --- comphelper/inc/comphelper/anycompare.hxx | 57 +++++++++++++++++++++++---- comphelper/source/container/enumerablemap.cxx | 2 +- comphelper/source/misc/anycompare.cxx | 8 +++- 3 files changed, 56 insertions(+), 11 deletions(-) (limited to 'comphelper') diff --git a/comphelper/inc/comphelper/anycompare.hxx b/comphelper/inc/comphelper/anycompare.hxx index 94a97e4d78fc..03f41e108ba9 100755 --- a/comphelper/inc/comphelper/anycompare.hxx +++ b/comphelper/inc/comphelper/anycompare.hxx @@ -31,6 +31,7 @@ /** === begin UNO includes === **/ #include +#include /** === end UNO includes === **/ #include @@ -88,7 +89,7 @@ namespace comphelper if ( !( _lhs >>= lhs ) || !( _rhs >>= rhs ) ) - throw ::com::sun::star::lang::IllegalArgumentException( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "Unsupported key type." ) ), NULL, 1 ); + throw ::com::sun::star::lang::IllegalArgumentException(); return lhs < rhs; } }; @@ -105,11 +106,36 @@ namespace comphelper if ( !( _lhs >>= lhs ) || !( _rhs >>= rhs ) ) - throw ::com::sun::star::lang::IllegalArgumentException( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "Unsupported key type." ) ), NULL, 1 ); + throw ::com::sun::star::lang::IllegalArgumentException(); return lhs < rhs; } }; + //================================================================================================================== + //= StringCollationPredicateLess + //================================================================================================================== + class StringCollationPredicateLess : public IKeyPredicateLess + { + public: + StringCollationPredicateLess( ::com::sun::star::uno::Reference< ::com::sun::star::i18n::XCollator > const & i_collator ) + :m_collator( i_collator ) + { + } + + virtual bool isLess( ::com::sun::star::uno::Any const & _lhs, ::com::sun::star::uno::Any const & _rhs ) const + { + ::rtl::OUString lhs, rhs; + if ( !( _lhs >>= lhs ) + || !( _rhs >>= rhs ) + ) + throw ::com::sun::star::lang::IllegalArgumentException(); + return m_collator->compareString( lhs, rhs ) < 0; + } + + private: + ::com::sun::star::uno::Reference< ::com::sun::star::i18n::XCollator > const m_collator; + }; + //================================================================================================================== //= TypePredicateLess //================================================================================================================== @@ -122,7 +148,7 @@ namespace comphelper if ( !( _lhs >>= lhs ) || !( _rhs >>= rhs ) ) - throw ::com::sun::star::lang::IllegalArgumentException( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "Unsupported key type." ) ), NULL, 1 ); + throw ::com::sun::star::lang::IllegalArgumentException(); return lhs.getTypeName() < rhs.getTypeName(); } }; @@ -146,7 +172,7 @@ namespace comphelper || !_lhs.getValueType().equals( m_enumType ) || !_rhs.getValueType().equals( m_enumType ) ) - throw ::com::sun::star::lang::IllegalArgumentException( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "Unsupported key type." ) ), NULL, 1 ); + throw ::com::sun::star::lang::IllegalArgumentException(); return lhs < rhs; } @@ -165,7 +191,7 @@ namespace comphelper if ( ( _lhs.getValueTypeClass() != ::com::sun::star::uno::TypeClass_INTERFACE ) || ( _rhs.getValueTypeClass() != ::com::sun::star::uno::TypeClass_INTERFACE ) ) - throw ::com::sun::star::lang::IllegalArgumentException( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "Unsupported key type." ) ), NULL, 1 ); + throw ::com::sun::star::lang::IllegalArgumentException(); ::com::sun::star::uno::Reference< ::com::sun::star::uno::XInterface > lhs( _lhs, ::com::sun::star::uno::UNO_QUERY ); ::com::sun::star::uno::Reference< ::com::sun::star::uno::XInterface > rhs( _rhs, ::com::sun::star::uno::UNO_QUERY ); @@ -174,9 +200,24 @@ namespace comphelper }; //================================================================================================================== - //= InterfacePredicateLess - //================================================================================================================== - ::std::auto_ptr< IKeyPredicateLess > COMPHELPER_DLLPUBLIC getStandardLessPredicate( ::com::sun::star::uno::Type const & i_type ); + //= getStandardLessPredicate + //================================================================================================================== + /** creates a default IKeyPredicateLess instance for the given UNO type + @param i_type + the type for which a predicate instance should be created + @param i_collator + specifies a collator instance to use, or . If , strings will be compared using the < + operator, otherwise the collator will be used. The parameter is ignored if i_type does not specify + the string type. + @return + a default implementation of IKeyPredicateLess, which is able to compare values of the given type. If no + such default implementation is known for the given type, then is returned. + */ + ::std::auto_ptr< IKeyPredicateLess > COMPHELPER_DLLPUBLIC + getStandardLessPredicate( + ::com::sun::star::uno::Type const & i_type, + ::com::sun::star::uno::Reference< ::com::sun::star::i18n::XCollator > const & i_collator + ); //...................................................................................................................... } // namespace comphelper diff --git a/comphelper/source/container/enumerablemap.cxx b/comphelper/source/container/enumerablemap.cxx index ecc9494056ee..a73983517751 100644 --- a/comphelper/source/container/enumerablemap.cxx +++ b/comphelper/source/container/enumerablemap.cxx @@ -405,7 +405,7 @@ namespace comphelper throw IllegalTypeException( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "Unsupported value type." ) ), *this ); // create the comparator for the KeyType, and throw if the type is not supported - ::std::auto_ptr< IKeyPredicateLess > pComparator( getStandardLessPredicate( aKeyType ) ); + ::std::auto_ptr< IKeyPredicateLess > pComparator( getStandardLessPredicate( aKeyType, NULL ) ); if ( !pComparator.get() ) throw IllegalTypeException( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "Unsupported key type." ) ), *this ); diff --git a/comphelper/source/misc/anycompare.cxx b/comphelper/source/misc/anycompare.cxx index 2a6b7abda265..a86174daf08e 100755 --- a/comphelper/source/misc/anycompare.cxx +++ b/comphelper/source/misc/anycompare.cxx @@ -63,10 +63,11 @@ namespace comphelper using ::com::sun::star::uno::TypeClass_TYPE; using ::com::sun::star::uno::TypeClass_ENUM; using ::com::sun::star::uno::TypeClass_INTERFACE; + using ::com::sun::star::i18n::XCollator; /** === end UNO using === **/ //------------------------------------------------------------------------------------------------------------------ - ::std::auto_ptr< IKeyPredicateLess > getStandardLessPredicate( ::com::sun::star::uno::Type const & i_type ) + ::std::auto_ptr< IKeyPredicateLess > getStandardLessPredicate( Type const & i_type, Reference< XCollator > const & i_collator ) { ::std::auto_ptr< IKeyPredicateLess > pComparator; switch ( i_type.getTypeClass() ) @@ -105,7 +106,10 @@ namespace comphelper pComparator.reset( new ScalarPredicateLess< double >() ); break; case TypeClass_STRING: - pComparator.reset( new StringPredicateLess() ); + if ( i_collator.is() ) + pComparator.reset( new StringCollationPredicateLess( i_collator ) ); + else + pComparator.reset( new StringPredicateLess() ); break; case TypeClass_TYPE: pComparator.reset( new TypePredicateLess() ); -- cgit