bump product version to 4.2.0.1
[LibreOffice.git] / include / comphelper / anycompare.hxx
blob7bff8d456f179261edd4c215b2e85b4df97dad84
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 <functional>
31 #include <memory>
33 //......................................................................................................................
34 namespace comphelper
36 //......................................................................................................................
38 //==================================================================================================================
39 //= IKeyPredicateLess
40 //==================================================================================================================
41 class SAL_NO_VTABLE IKeyPredicateLess
43 public:
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 );
63 private:
64 IKeyPredicateLess const & m_predicate;
66 private:
67 LessPredicateAdapter(); // never implemented
70 //==================================================================================================================
71 //= ScalarPredicateLess
72 //==================================================================================================================
73 template< typename SCALAR >
74 class ScalarPredicateLess : public IKeyPredicateLess
76 public:
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 )
81 || !( _rhs >>= rhs )
83 throw ::com::sun::star::lang::IllegalArgumentException();
84 return lhs < rhs;
88 //==================================================================================================================
89 //= StringPredicateLess
90 //==================================================================================================================
91 class StringPredicateLess : public IKeyPredicateLess
93 public:
94 virtual bool isLess( ::com::sun::star::uno::Any const & _lhs, ::com::sun::star::uno::Any const & _rhs ) const
96 OUString lhs, rhs;
97 if ( !( _lhs >>= lhs )
98 || !( _rhs >>= rhs )
100 throw ::com::sun::star::lang::IllegalArgumentException();
101 return lhs < rhs;
105 //==================================================================================================================
106 //= StringCollationPredicateLess
107 //==================================================================================================================
108 class StringCollationPredicateLess : public IKeyPredicateLess
110 public:
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
118 OUString lhs, rhs;
119 if ( !( _lhs >>= lhs )
120 || !( _rhs >>= rhs )
122 throw ::com::sun::star::lang::IllegalArgumentException();
123 return m_collator->compareString( lhs, rhs ) < 0;
126 private:
127 ::com::sun::star::uno::Reference< ::com::sun::star::i18n::XCollator > const m_collator;
130 //==================================================================================================================
131 //= TypePredicateLess
132 //==================================================================================================================
133 class TypePredicateLess : public IKeyPredicateLess
135 public:
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 )
140 || !( _rhs >>= rhs )
142 throw ::com::sun::star::lang::IllegalArgumentException();
143 return lhs.getTypeName() < rhs.getTypeName();
147 //==================================================================================================================
148 //= EnumPredicateLess
149 //==================================================================================================================
150 class EnumPredicateLess : public IKeyPredicateLess
152 public:
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();
167 return lhs < rhs;
170 private:
171 ::com::sun::star::uno::Type const m_enumType;
174 //==================================================================================================================
175 //= InterfacePredicateLess
176 //==================================================================================================================
177 class InterfacePredicateLess : public IKeyPredicateLess
179 public:
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
197 @param i_type
198 the type for which a predicate instance should be created
199 @param i_collator
200 specifies a collator instance to use, or <NULL/>. If <NULL/>, strings will be compared using the <code>&lt</code>
201 operator, otherwise the collator will be used. The parameter is ignored if <arg>i_type</arg> does not specify
202 the string type.
203 @return
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: */