1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
20 #ifndef INCLUDED_COMPHELPER_ANYCOMPARE_HXX
21 #define INCLUDED_COMPHELPER_ANYCOMPARE_HXX
23 #include <comphelper/comphelperdllapi.h>
25 #include <com/sun/star/lang/IllegalArgumentException.hpp>
26 #include <com/sun/star/i18n/XCollator.hpp>
28 #include <comphelper/extract.hxx>
33 //......................................................................................................................
36 //......................................................................................................................
38 //==================================================================================================================
40 //==================================================================================================================
41 class SAL_NO_VTABLE IKeyPredicateLess
44 virtual bool isLess( ::com::sun::star::uno::Any
const & _lhs
, ::com::sun::star::uno::Any
const & _rhs
) const = 0;
45 virtual ~IKeyPredicateLess() {}
48 //==================================================================================================================
49 //= LessPredicateAdapter
50 //==================================================================================================================
51 struct LessPredicateAdapter
: public ::std::binary_function
< ::com::sun::star::uno::Any
, ::com::sun::star::uno::Any
, bool >
53 LessPredicateAdapter( const IKeyPredicateLess
& _predicate
)
54 :m_predicate( _predicate
)
58 bool operator()( ::com::sun::star::uno::Any
const & _lhs
, ::com::sun::star::uno::Any
const & _rhs
) const
60 return m_predicate
.isLess( _lhs
, _rhs
);
64 IKeyPredicateLess
const & m_predicate
;
67 LessPredicateAdapter(); // never implemented
70 //==================================================================================================================
71 //= ScalarPredicateLess
72 //==================================================================================================================
73 template< typename SCALAR
>
74 class ScalarPredicateLess
: public IKeyPredicateLess
77 virtual bool isLess( ::com::sun::star::uno::Any
const & _lhs
, ::com::sun::star::uno::Any
const & _rhs
) const
79 SCALAR
lhs(0), rhs(0);
80 if ( !( _lhs
>>= lhs
)
83 throw ::com::sun::star::lang::IllegalArgumentException();
88 //==================================================================================================================
89 //= StringPredicateLess
90 //==================================================================================================================
91 class StringPredicateLess
: public IKeyPredicateLess
94 virtual bool isLess( ::com::sun::star::uno::Any
const & _lhs
, ::com::sun::star::uno::Any
const & _rhs
) const
97 if ( !( _lhs
>>= lhs
)
100 throw ::com::sun::star::lang::IllegalArgumentException();
105 //==================================================================================================================
106 //= StringCollationPredicateLess
107 //==================================================================================================================
108 class StringCollationPredicateLess
: public IKeyPredicateLess
111 StringCollationPredicateLess( ::com::sun::star::uno::Reference
< ::com::sun::star::i18n::XCollator
> const & i_collator
)
112 :m_collator( i_collator
)
116 virtual bool isLess( ::com::sun::star::uno::Any
const & _lhs
, ::com::sun::star::uno::Any
const & _rhs
) const
119 if ( !( _lhs
>>= lhs
)
122 throw ::com::sun::star::lang::IllegalArgumentException();
123 return m_collator
->compareString( lhs
, rhs
) < 0;
127 ::com::sun::star::uno::Reference
< ::com::sun::star::i18n::XCollator
> const m_collator
;
130 //==================================================================================================================
131 //= TypePredicateLess
132 //==================================================================================================================
133 class TypePredicateLess
: public IKeyPredicateLess
136 virtual bool isLess( ::com::sun::star::uno::Any
const & _lhs
, ::com::sun::star::uno::Any
const & _rhs
) const
138 ::com::sun::star::uno::Type lhs
, rhs
;
139 if ( !( _lhs
>>= lhs
)
142 throw ::com::sun::star::lang::IllegalArgumentException();
143 return lhs
.getTypeName() < rhs
.getTypeName();
147 //==================================================================================================================
148 //= EnumPredicateLess
149 //==================================================================================================================
150 class EnumPredicateLess
: public IKeyPredicateLess
153 EnumPredicateLess( ::com::sun::star::uno::Type
const & _enumType
)
154 :m_enumType( _enumType
)
158 virtual bool isLess( ::com::sun::star::uno::Any
const & _lhs
, ::com::sun::star::uno::Any
const & _rhs
) const
160 sal_Int32
lhs(0), rhs(0);
161 if ( !::cppu::enum2int( lhs
, _lhs
)
162 || !::cppu::enum2int( rhs
, _rhs
)
163 || !_lhs
.getValueType().equals( m_enumType
)
164 || !_rhs
.getValueType().equals( m_enumType
)
166 throw ::com::sun::star::lang::IllegalArgumentException();
171 ::com::sun::star::uno::Type
const m_enumType
;
174 //==================================================================================================================
175 //= InterfacePredicateLess
176 //==================================================================================================================
177 class InterfacePredicateLess
: public IKeyPredicateLess
180 virtual bool isLess( ::com::sun::star::uno::Any
const & _lhs
, ::com::sun::star::uno::Any
const & _rhs
) const
182 if ( ( _lhs
.getValueTypeClass() != ::com::sun::star::uno::TypeClass_INTERFACE
)
183 || ( _rhs
.getValueTypeClass() != ::com::sun::star::uno::TypeClass_INTERFACE
)
185 throw ::com::sun::star::lang::IllegalArgumentException();
187 ::com::sun::star::uno::Reference
< ::com::sun::star::uno::XInterface
> lhs( _lhs
, ::com::sun::star::uno::UNO_QUERY
);
188 ::com::sun::star::uno::Reference
< ::com::sun::star::uno::XInterface
> rhs( _rhs
, ::com::sun::star::uno::UNO_QUERY
);
189 return lhs
.get() < rhs
.get();
193 //==================================================================================================================
194 //= getStandardLessPredicate
195 //==================================================================================================================
196 /** creates a default IKeyPredicateLess instance for the given UNO type
198 the type for which a predicate instance should be created
200 specifies a collator instance to use, or <NULL/>. If <NULL/>, strings will be compared using the <code><</code>
201 operator, otherwise the collator will be used. The parameter is ignored if <arg>i_type</arg> does not specify
204 a default implementation of IKeyPredicateLess, which is able to compare values of the given type. If no
205 such default implementation is known for the given type, then <NULL/> is returned.
207 ::std::auto_ptr
< IKeyPredicateLess
> COMPHELPER_DLLPUBLIC
208 getStandardLessPredicate(
209 ::com::sun::star::uno::Type
const & i_type
,
210 ::com::sun::star::uno::Reference
< ::com::sun::star::i18n::XCollator
> const & i_collator
213 //......................................................................................................................
214 } // namespace comphelper
215 //......................................................................................................................
217 #endif // INCLUDED_COMPHELPER_ANYCOMPARE_HXX
219 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */