Don't search for old (<= 2.0) .NET SDK anymore
[LibreOffice.git] / include / comphelper / anycompare.hxx
blobdd92d2734ca2e0b332b1417cec736b1a459c43b9
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
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>
30 #include <memory>
33 namespace comphelper
37 //= IKeyPredicateLess
39 class SAL_NO_VTABLE IKeyPredicateLess
41 public:
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 );
61 private:
62 IKeyPredicateLess const & m_predicate;
66 //= ScalarPredicateLess
68 template< typename SCALAR >
69 class ScalarPredicateLess : public IKeyPredicateLess
71 public:
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 )
76 || !( _rhs >>= rhs )
78 throw css::lang::IllegalArgumentException();
79 return lhs < rhs;
84 //= StringPredicateLess
86 class StringPredicateLess : public IKeyPredicateLess
88 public:
89 virtual bool isLess( css::uno::Any const & _lhs, css::uno::Any const & _rhs ) const override
91 OUString lhs, rhs;
92 if ( !( _lhs >>= lhs )
93 || !( _rhs >>= rhs )
95 throw css::lang::IllegalArgumentException();
96 return lhs < rhs;
101 //= StringCollationPredicateLess
103 class StringCollationPredicateLess : public IKeyPredicateLess
105 public:
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
113 OUString lhs, rhs;
114 if ( !( _lhs >>= lhs )
115 || !( _rhs >>= rhs )
117 throw css::lang::IllegalArgumentException();
118 return m_collator->compareString( lhs, rhs ) < 0;
121 private:
122 css::uno::Reference< css::i18n::XCollator > const m_collator;
126 //= TypePredicateLess
128 class TypePredicateLess : public IKeyPredicateLess
130 public:
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 )
135 || !( _rhs >>= rhs )
137 throw css::lang::IllegalArgumentException();
138 return lhs.getTypeName() < rhs.getTypeName();
143 //= EnumPredicateLess
145 class EnumPredicateLess : public IKeyPredicateLess
147 public:
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();
162 return lhs < rhs;
165 private:
166 css::uno::Type const m_enumType;
170 //= InterfacePredicateLess
172 class InterfacePredicateLess : public IKeyPredicateLess
174 public:
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
192 @param i_type
193 the type for which a predicate instance should be created
194 @param i_collator
195 specifies a collator instance to use, or <NULL/>. If <NULL/>, strings will be compared using the <code>&lt</code>
196 operator, otherwise the collator will be used. The parameter is ignored if <arg>i_type</arg> does not specify
197 the string type.
198 @return
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
209 } // namespace comphelper
212 #endif // INCLUDED_COMPHELPER_ANYCOMPARE_HXX
214 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */