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>
40 class SAL_NO_VTABLE IKeyPredicateLess
43 virtual bool isLess( css::uno::Any
const & _lhs
, css::uno::Any
const & _rhs
) const = 0;
44 virtual ~IKeyPredicateLess() {}
48 //= LessPredicateAdapter
50 struct LessPredicateAdapter
52 LessPredicateAdapter( const IKeyPredicateLess
& _predicate
)
53 :m_predicate( _predicate
)
57 bool operator()( css::uno::Any
const & _lhs
, css::uno::Any
const & _rhs
) const
59 return m_predicate
.isLess( _lhs
, _rhs
);
63 IKeyPredicateLess
const & m_predicate
;
67 //= ScalarPredicateLess
69 template< typename SCALAR
>
70 class ScalarPredicateLess final
: public IKeyPredicateLess
73 virtual bool isLess( css::uno::Any
const & _lhs
, css::uno::Any
const & _rhs
) const override
75 SCALAR
lhs(0), rhs(0);
76 if ( !( _lhs
>>= lhs
)
79 throw css::lang::IllegalArgumentException();
85 //= StringPredicateLess
87 class StringPredicateLess final
: public IKeyPredicateLess
90 virtual bool isLess( css::uno::Any
const & _lhs
, css::uno::Any
const & _rhs
) const override
93 if ( !( _lhs
>>= lhs
)
96 throw css::lang::IllegalArgumentException();
102 //= StringCollationPredicateLess
104 class StringCollationPredicateLess final
: public IKeyPredicateLess
107 StringCollationPredicateLess( css::uno::Reference
< css::i18n::XCollator
> i_collator
)
108 :m_collator(std::move( i_collator
))
112 virtual bool isLess( css::uno::Any
const & _lhs
, css::uno::Any
const & _rhs
) const override
115 if ( !( _lhs
>>= lhs
)
118 throw css::lang::IllegalArgumentException();
119 return m_collator
->compareString( lhs
, rhs
) < 0;
123 css::uno::Reference
< css::i18n::XCollator
> const m_collator
;
127 //= TypePredicateLess
129 class TypePredicateLess final
: public IKeyPredicateLess
132 virtual bool isLess( css::uno::Any
const & _lhs
, css::uno::Any
const & _rhs
) const override
134 css::uno::Type lhs
, rhs
;
135 if ( !( _lhs
>>= lhs
)
138 throw css::lang::IllegalArgumentException();
139 return lhs
.getTypeName() < rhs
.getTypeName();
144 //= EnumPredicateLess
146 class EnumPredicateLess final
: public IKeyPredicateLess
149 EnumPredicateLess( css::uno::Type
const & _enumType
)
150 :m_enumType( _enumType
)
154 virtual bool isLess( css::uno::Any
const & _lhs
, css::uno::Any
const & _rhs
) const override
156 sal_Int32
lhs(0), rhs(0);
157 if ( !::cppu::enum2int( lhs
, _lhs
)
158 || !::cppu::enum2int( rhs
, _rhs
)
159 || !_lhs
.getValueType().equals( m_enumType
)
160 || !_rhs
.getValueType().equals( m_enumType
)
162 throw css::lang::IllegalArgumentException();
167 css::uno::Type
const m_enumType
;
171 //= InterfacePredicateLess
173 class InterfacePredicateLess final
: public IKeyPredicateLess
176 virtual bool isLess( css::uno::Any
const & _lhs
, css::uno::Any
const & _rhs
) const override
178 if ( ( _lhs
.getValueTypeClass() != css::uno::TypeClass_INTERFACE
)
179 || ( _rhs
.getValueTypeClass() != css::uno::TypeClass_INTERFACE
)
181 throw css::lang::IllegalArgumentException();
183 css::uno::Reference
< css::uno::XInterface
> lhs( _lhs
, css::uno::UNO_QUERY
);
184 css::uno::Reference
< css::uno::XInterface
> rhs( _rhs
, css::uno::UNO_QUERY
);
185 return lhs
.get() < rhs
.get();
190 //= getStandardLessPredicate
192 /** creates a default IKeyPredicateLess instance for the given UNO type
194 the type for which a predicate instance should be created
196 specifies a collator instance to use, or <NULL/>. If <NULL/>, strings will be compared using the <code><</code>
197 operator, otherwise the collator will be used. The parameter is ignored if <arg>i_type</arg> does not specify
200 a default implementation of IKeyPredicateLess, which is able to compare values of the given type. If no
201 such default implementation is known for the given type, then <NULL/> is returned.
203 ::std::unique_ptr
< IKeyPredicateLess
> COMPHELPER_DLLPUBLIC
204 getStandardLessPredicate(
205 css::uno::Type
const & i_type
,
206 css::uno::Reference
< css::i18n::XCollator
> const & i_collator
212 bool COMPHELPER_DLLPUBLIC
anyLess( css::uno::Any
const & lhs
, css::uno::Any
const & rhs
);
214 } // namespace comphelper
217 #endif // INCLUDED_COMPHELPER_ANYCOMPARE_HXX
219 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */