1 /************************************************************** 2 * 3 * Licensed to the Apache Software Foundation (ASF) under one 4 * or more contributor license agreements. See the NOTICE file 5 * distributed with this work for additional information 6 * regarding copyright ownership. The ASF licenses this file 7 * to you under the Apache License, Version 2.0 (the 8 * "License"); you may not use this file except in compliance 9 * with the License. You may obtain a copy of the License at 10 * 11 * http://www.apache.org/licenses/LICENSE-2.0 12 * 13 * Unless required by applicable law or agreed to in writing, 14 * software distributed under the License is distributed on an 15 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 16 * KIND, either express or implied. See the License for the 17 * specific language governing permissions and limitations 18 * under the License. 19 * 20 *************************************************************/ 21 22 23 24 #ifndef COMPHELPER_ANYCOMPARE_HXX 25 #define COMPHELPER_ANYCOMPARE_HXX 26 27 #include "comphelper/comphelperdllapi.h" 28 29 /** === begin UNO includes === **/ 30 #include <com/sun/star/lang/IllegalArgumentException.hpp> 31 #include <com/sun/star/i18n/XCollator.hpp> 32 /** === end UNO includes === **/ 33 34 #include <comphelper/extract.hxx> 35 36 #include <functional> 37 #include <memory> 38 39 //...................................................................................................................... 40 namespace comphelper 41 { 42 //...................................................................................................................... 43 44 //================================================================================================================== 45 //= IKeyPredicateLess 46 //================================================================================================================== 47 class SAL_NO_VTABLE IKeyPredicateLess 48 { 49 public: 50 virtual bool isLess( ::com::sun::star::uno::Any const & _lhs, ::com::sun::star::uno::Any const & _rhs ) const = 0; ~IKeyPredicateLess()51 virtual ~IKeyPredicateLess() {} 52 }; 53 54 //================================================================================================================== 55 //= LessPredicateAdapter 56 //================================================================================================================== 57 struct LessPredicateAdapter 58 { 59 typedef ::com::sun::star::uno::Any first_argument_type; 60 typedef ::com::sun::star::uno::Any second_argument_type; 61 typedef bool result_type; LessPredicateAdaptercomphelper::LessPredicateAdapter62 LessPredicateAdapter( const IKeyPredicateLess& _predicate ) 63 :m_predicate( _predicate ) 64 { 65 } 66 operator ()comphelper::LessPredicateAdapter67 bool operator()( ::com::sun::star::uno::Any const & _lhs, ::com::sun::star::uno::Any const & _rhs ) const 68 { 69 return m_predicate.isLess( _lhs, _rhs ); 70 } 71 72 private: 73 IKeyPredicateLess const & m_predicate; 74 75 private: 76 LessPredicateAdapter(); // never implemented 77 }; 78 79 //================================================================================================================== 80 //= ScalarPredicateLess 81 //================================================================================================================== 82 template< typename SCALAR > 83 class ScalarPredicateLess : public IKeyPredicateLess 84 { 85 public: isLess(::com::sun::star::uno::Any const & _lhs,::com::sun::star::uno::Any const & _rhs) const86 virtual bool isLess( ::com::sun::star::uno::Any const & _lhs, ::com::sun::star::uno::Any const & _rhs ) const 87 { 88 SCALAR lhs(0), rhs(0); 89 if ( !( _lhs >>= lhs ) 90 || !( _rhs >>= rhs ) 91 ) 92 throw ::com::sun::star::lang::IllegalArgumentException(); 93 return lhs < rhs; 94 } 95 }; 96 97 //================================================================================================================== 98 //= StringPredicateLess 99 //================================================================================================================== 100 class StringPredicateLess : public IKeyPredicateLess 101 { 102 public: isLess(::com::sun::star::uno::Any const & _lhs,::com::sun::star::uno::Any const & _rhs) const103 virtual bool isLess( ::com::sun::star::uno::Any const & _lhs, ::com::sun::star::uno::Any const & _rhs ) const 104 { 105 ::rtl::OUString lhs, rhs; 106 if ( !( _lhs >>= lhs ) 107 || !( _rhs >>= rhs ) 108 ) 109 throw ::com::sun::star::lang::IllegalArgumentException(); 110 return lhs < rhs; 111 } 112 }; 113 114 //================================================================================================================== 115 //= StringCollationPredicateLess 116 //================================================================================================================== 117 class StringCollationPredicateLess : public IKeyPredicateLess 118 { 119 public: StringCollationPredicateLess(::com::sun::star::uno::Reference<::com::sun::star::i18n::XCollator> const & i_collator)120 StringCollationPredicateLess( ::com::sun::star::uno::Reference< ::com::sun::star::i18n::XCollator > const & i_collator ) 121 :m_collator( i_collator ) 122 { 123 } 124 isLess(::com::sun::star::uno::Any const & _lhs,::com::sun::star::uno::Any const & _rhs) const125 virtual bool isLess( ::com::sun::star::uno::Any const & _lhs, ::com::sun::star::uno::Any const & _rhs ) const 126 { 127 ::rtl::OUString lhs, rhs; 128 if ( !( _lhs >>= lhs ) 129 || !( _rhs >>= rhs ) 130 ) 131 throw ::com::sun::star::lang::IllegalArgumentException(); 132 return m_collator->compareString( lhs, rhs ) < 0; 133 } 134 135 private: 136 ::com::sun::star::uno::Reference< ::com::sun::star::i18n::XCollator > const m_collator; 137 }; 138 139 //================================================================================================================== 140 //= TypePredicateLess 141 //================================================================================================================== 142 class TypePredicateLess : public IKeyPredicateLess 143 { 144 public: isLess(::com::sun::star::uno::Any const & _lhs,::com::sun::star::uno::Any const & _rhs) const145 virtual bool isLess( ::com::sun::star::uno::Any const & _lhs, ::com::sun::star::uno::Any const & _rhs ) const 146 { 147 ::com::sun::star::uno::Type lhs, rhs; 148 if ( !( _lhs >>= lhs ) 149 || !( _rhs >>= rhs ) 150 ) 151 throw ::com::sun::star::lang::IllegalArgumentException(); 152 return lhs.getTypeName() < rhs.getTypeName(); 153 } 154 }; 155 156 //================================================================================================================== 157 //= EnumPredicateLess 158 //================================================================================================================== 159 class EnumPredicateLess : public IKeyPredicateLess 160 { 161 public: EnumPredicateLess(::com::sun::star::uno::Type const & _enumType)162 EnumPredicateLess( ::com::sun::star::uno::Type const & _enumType ) 163 :m_enumType( _enumType ) 164 { 165 } 166 isLess(::com::sun::star::uno::Any const & _lhs,::com::sun::star::uno::Any const & _rhs) const167 virtual bool isLess( ::com::sun::star::uno::Any const & _lhs, ::com::sun::star::uno::Any const & _rhs ) const 168 { 169 sal_Int32 lhs(0), rhs(0); 170 if ( !::cppu::enum2int( lhs, _lhs ) 171 || !::cppu::enum2int( rhs, _rhs ) 172 || !_lhs.getValueType().equals( m_enumType ) 173 || !_rhs.getValueType().equals( m_enumType ) 174 ) 175 throw ::com::sun::star::lang::IllegalArgumentException(); 176 return lhs < rhs; 177 } 178 179 private: 180 ::com::sun::star::uno::Type const m_enumType; 181 }; 182 183 //================================================================================================================== 184 //= InterfacePredicateLess 185 //================================================================================================================== 186 class InterfacePredicateLess : public IKeyPredicateLess 187 { 188 public: isLess(::com::sun::star::uno::Any const & _lhs,::com::sun::star::uno::Any const & _rhs) const189 virtual bool isLess( ::com::sun::star::uno::Any const & _lhs, ::com::sun::star::uno::Any const & _rhs ) const 190 { 191 if ( ( _lhs.getValueTypeClass() != ::com::sun::star::uno::TypeClass_INTERFACE ) 192 || ( _rhs.getValueTypeClass() != ::com::sun::star::uno::TypeClass_INTERFACE ) 193 ) 194 throw ::com::sun::star::lang::IllegalArgumentException(); 195 196 ::com::sun::star::uno::Reference< ::com::sun::star::uno::XInterface > lhs( _lhs, ::com::sun::star::uno::UNO_QUERY ); 197 ::com::sun::star::uno::Reference< ::com::sun::star::uno::XInterface > rhs( _rhs, ::com::sun::star::uno::UNO_QUERY ); 198 return lhs.get() < rhs.get(); 199 } 200 }; 201 202 //================================================================================================================== 203 //= getStandardLessPredicate 204 //================================================================================================================== 205 /** creates a default IKeyPredicateLess instance for the given UNO type 206 @param i_type 207 the type for which a predicate instance should be created 208 @param i_collator 209 specifies a collator instance to use, or <NULL/>. If <NULL/>, strings will be compared using the <code><</code> 210 operator, otherwise the collator will be used. The parameter is ignored if <arg>i_type</arg> does not specify 211 the string type. 212 @return 213 a default implementation of IKeyPredicateLess, which is able to compare values of the given type. If no 214 such default implementation is known for the given type, then <NULL/> is returned. 215 */ 216 ::std::unique_ptr< IKeyPredicateLess > COMPHELPER_DLLPUBLIC 217 getStandardLessPredicate( 218 ::com::sun::star::uno::Type const & i_type, 219 ::com::sun::star::uno::Reference< ::com::sun::star::i18n::XCollator > const & i_collator 220 ); 221 222 //...................................................................................................................... 223 } // namespace comphelper 224 //...................................................................................................................... 225 226 #endif // COMPHELPER_ANYCOMPARE_HXX 227