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>
39 class SAL_NO_VTABLE IKeyPredicateLess
42 virtual bool isLess( css::uno::Any
const & _lhs
, css::uno::Any
const & _rhs
) const = 0;
43 virtual ~IKeyPredicateLess() {}
47 //= LessPredicateAdapter
49 struct LessPredicateAdapter
51 LessPredicateAdapter( const IKeyPredicateLess
& _predicate
)
52 :m_predicate( _predicate
)
56 bool operator()( css::uno::Any
const & _lhs
, css::uno::Any
const & _rhs
) const
58 return m_predicate
.isLess( _lhs
, _rhs
);
62 IKeyPredicateLess
const & m_predicate
;
66 //= ScalarPredicateLess
68 template< typename SCALAR
>
69 class ScalarPredicateLess final
: public IKeyPredicateLess
72 virtual bool isLess( css::uno::Any
const & _lhs
, css::uno::Any
const & _rhs
) const override
74 SCALAR
lhs(0), rhs(0);
75 if ( !( _lhs
>>= lhs
)
78 throw css::lang::IllegalArgumentException();
84 //= StringPredicateLess
86 class StringPredicateLess final
: public IKeyPredicateLess
89 virtual bool isLess( css::uno::Any
const & _lhs
, css::uno::Any
const & _rhs
) const override
92 if ( !( _lhs
>>= lhs
)
95 throw css::lang::IllegalArgumentException();
101 //= StringCollationPredicateLess
103 class StringCollationPredicateLess final
: public IKeyPredicateLess
106 StringCollationPredicateLess( css::uno::Reference
< css::i18n::XCollator
> const & i_collator
)
107 :m_collator( i_collator
)
111 virtual bool isLess( css::uno::Any
const & _lhs
, css::uno::Any
const & _rhs
) const override
114 if ( !( _lhs
>>= lhs
)
117 throw css::lang::IllegalArgumentException();
118 return m_collator
->compareString( lhs
, rhs
) < 0;
122 css::uno::Reference
< css::i18n::XCollator
> const m_collator
;
126 //= TypePredicateLess
128 class TypePredicateLess final
: public IKeyPredicateLess
131 virtual bool isLess( css::uno::Any
const & _lhs
, css::uno::Any
const & _rhs
) const override
133 css::uno::Type lhs
, rhs
;
134 if ( !( _lhs
>>= lhs
)
137 throw css::lang::IllegalArgumentException();
138 return lhs
.getTypeName() < rhs
.getTypeName();
143 //= EnumPredicateLess
145 class EnumPredicateLess final
: public IKeyPredicateLess
148 EnumPredicateLess( css::uno::Type
const & _enumType
)
149 :m_enumType( _enumType
)
153 virtual bool isLess( css::uno::Any
const & _lhs
, css::uno::Any
const & _rhs
) const override
155 sal_Int32
lhs(0), rhs(0);
156 if ( !::cppu::enum2int( lhs
, _lhs
)
157 || !::cppu::enum2int( rhs
, _rhs
)
158 || !_lhs
.getValueType().equals( m_enumType
)
159 || !_rhs
.getValueType().equals( m_enumType
)
161 throw css::lang::IllegalArgumentException();
166 css::uno::Type
const m_enumType
;
170 //= InterfacePredicateLess
172 class InterfacePredicateLess final
: public IKeyPredicateLess
175 virtual bool isLess( css::uno::Any
const & _lhs
, css::uno::Any
const & _rhs
) const override
177 if ( ( _lhs
.getValueTypeClass() != css::uno::TypeClass_INTERFACE
)
178 || ( _rhs
.getValueTypeClass() != css::uno::TypeClass_INTERFACE
)
180 throw css::lang::IllegalArgumentException();
182 css::uno::Reference
< css::uno::XInterface
> lhs( _lhs
, css::uno::UNO_QUERY
);
183 css::uno::Reference
< css::uno::XInterface
> rhs( _rhs
, css::uno::UNO_QUERY
);
184 return lhs
.get() < rhs
.get();
189 //= getStandardLessPredicate
191 /** creates a default IKeyPredicateLess instance for the given UNO type
193 the type for which a predicate instance should be created
195 specifies a collator instance to use, or <NULL/>. If <NULL/>, strings will be compared using the <code><</code>
196 operator, otherwise the collator will be used. The parameter is ignored if <arg>i_type</arg> does not specify
199 a default implementation of IKeyPredicateLess, which is able to compare values of the given type. If no
200 such default implementation is known for the given type, then <NULL/> is returned.
202 ::std::unique_ptr
< IKeyPredicateLess
> COMPHELPER_DLLPUBLIC
203 getStandardLessPredicate(
204 css::uno::Type
const & i_type
,
205 css::uno::Reference
< css::i18n::XCollator
> const & i_collator
211 bool COMPHELPER_DLLPUBLIC
anyLess( css::uno::Any
const & lhs
, css::uno::Any
const & rhs
);
213 } // namespace comphelper
216 #endif // INCLUDED_COMPHELPER_ANYCOMPARE_HXX
218 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */