Line data Source code
1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /*
3 : * This file is part of the LibreOffice project.
4 : *
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/.
8 : *
9 : * This file incorporates work covered by the following license notice:
10 : *
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 .
18 : */
19 :
20 : #ifndef INCLUDED_COMPHELPER_ANYCOMPARE_HXX
21 : #define INCLUDED_COMPHELPER_ANYCOMPARE_HXX
22 :
23 : #include <comphelper/comphelperdllapi.h>
24 :
25 : #include <com/sun/star/lang/IllegalArgumentException.hpp>
26 : #include <com/sun/star/i18n/XCollator.hpp>
27 :
28 : #include <comphelper/extract.hxx>
29 :
30 : #include <functional>
31 : #include <memory>
32 :
33 :
34 : namespace comphelper
35 : {
36 :
37 :
38 :
39 : //= IKeyPredicateLess
40 :
41 80 : class SAL_NO_VTABLE IKeyPredicateLess
42 : {
43 : public:
44 : virtual bool isLess( ::com::sun::star::uno::Any const & _lhs, ::com::sun::star::uno::Any const & _rhs ) const = 0;
45 80 : virtual ~IKeyPredicateLess() {}
46 : };
47 :
48 :
49 : //= LessPredicateAdapter
50 :
51 : struct LessPredicateAdapter : public ::std::binary_function< ::com::sun::star::uno::Any, ::com::sun::star::uno::Any, bool >
52 : {
53 68 : LessPredicateAdapter( const IKeyPredicateLess& _predicate )
54 68 : :m_predicate( _predicate )
55 : {
56 68 : }
57 :
58 2804 : bool operator()( ::com::sun::star::uno::Any const & _lhs, ::com::sun::star::uno::Any const & _rhs ) const
59 : {
60 2804 : return m_predicate.isLess( _lhs, _rhs );
61 : }
62 :
63 : private:
64 : IKeyPredicateLess const & m_predicate;
65 :
66 : private:
67 : LessPredicateAdapter(); // never implemented
68 : };
69 :
70 :
71 : //= ScalarPredicateLess
72 :
73 : template< typename SCALAR >
74 186 : class ScalarPredicateLess : public IKeyPredicateLess
75 : {
76 : public:
77 2200 : virtual bool isLess( ::com::sun::star::uno::Any const & _lhs, ::com::sun::star::uno::Any const & _rhs ) const SAL_OVERRIDE
78 : {
79 2200 : SCALAR lhs(0), rhs(0);
80 4400 : if ( !( _lhs >>= lhs )
81 2200 : || !( _rhs >>= rhs )
82 : )
83 0 : throw ::com::sun::star::lang::IllegalArgumentException();
84 2200 : return lhs < rhs;
85 : }
86 : };
87 :
88 :
89 : //= StringPredicateLess
90 :
91 18 : class StringPredicateLess : public IKeyPredicateLess
92 : {
93 : public:
94 422 : virtual bool isLess( ::com::sun::star::uno::Any const & _lhs, ::com::sun::star::uno::Any const & _rhs ) const SAL_OVERRIDE
95 : {
96 844 : OUString lhs, rhs;
97 844 : if ( !( _lhs >>= lhs )
98 422 : || !( _rhs >>= rhs )
99 : )
100 0 : throw ::com::sun::star::lang::IllegalArgumentException();
101 844 : return lhs < rhs;
102 : }
103 : };
104 :
105 :
106 : //= StringCollationPredicateLess
107 :
108 0 : class StringCollationPredicateLess : public IKeyPredicateLess
109 : {
110 : public:
111 0 : StringCollationPredicateLess( ::com::sun::star::uno::Reference< ::com::sun::star::i18n::XCollator > const & i_collator )
112 0 : :m_collator( i_collator )
113 : {
114 0 : }
115 :
116 0 : virtual bool isLess( ::com::sun::star::uno::Any const & _lhs, ::com::sun::star::uno::Any const & _rhs ) const SAL_OVERRIDE
117 : {
118 0 : OUString lhs, rhs;
119 0 : if ( !( _lhs >>= lhs )
120 0 : || !( _rhs >>= rhs )
121 : )
122 0 : throw ::com::sun::star::lang::IllegalArgumentException();
123 0 : return m_collator->compareString( lhs, rhs ) < 0;
124 : }
125 :
126 : private:
127 : ::com::sun::star::uno::Reference< ::com::sun::star::i18n::XCollator > const m_collator;
128 : };
129 :
130 :
131 : //= TypePredicateLess
132 :
133 12 : class TypePredicateLess : public IKeyPredicateLess
134 : {
135 : public:
136 148 : virtual bool isLess( ::com::sun::star::uno::Any const & _lhs, ::com::sun::star::uno::Any const & _rhs ) const SAL_OVERRIDE
137 : {
138 296 : ::com::sun::star::uno::Type lhs, rhs;
139 296 : if ( !( _lhs >>= lhs )
140 148 : || !( _rhs >>= rhs )
141 : )
142 0 : throw ::com::sun::star::lang::IllegalArgumentException();
143 296 : return lhs.getTypeName() < rhs.getTypeName();
144 : }
145 : };
146 :
147 :
148 : //= EnumPredicateLess
149 :
150 8 : class EnumPredicateLess : public IKeyPredicateLess
151 : {
152 : public:
153 4 : EnumPredicateLess( ::com::sun::star::uno::Type const & _enumType )
154 4 : :m_enumType( _enumType )
155 : {
156 4 : }
157 :
158 142 : virtual bool isLess( ::com::sun::star::uno::Any const & _lhs, ::com::sun::star::uno::Any const & _rhs ) const SAL_OVERRIDE
159 : {
160 142 : sal_Int32 lhs(0), rhs(0);
161 284 : if ( !::cppu::enum2int( lhs, _lhs )
162 142 : || !::cppu::enum2int( rhs, _rhs )
163 142 : || !_lhs.getValueType().equals( m_enumType )
164 284 : || !_rhs.getValueType().equals( m_enumType )
165 : )
166 0 : throw ::com::sun::star::lang::IllegalArgumentException();
167 142 : return lhs < rhs;
168 : }
169 :
170 : private:
171 : ::com::sun::star::uno::Type const m_enumType;
172 : };
173 :
174 :
175 : //= InterfacePredicateLess
176 :
177 12 : class InterfacePredicateLess : public IKeyPredicateLess
178 : {
179 : public:
180 304 : virtual bool isLess( ::com::sun::star::uno::Any const & _lhs, ::com::sun::star::uno::Any const & _rhs ) const SAL_OVERRIDE
181 : {
182 608 : if ( ( _lhs.getValueTypeClass() != ::com::sun::star::uno::TypeClass_INTERFACE )
183 304 : || ( _rhs.getValueTypeClass() != ::com::sun::star::uno::TypeClass_INTERFACE )
184 : )
185 0 : throw ::com::sun::star::lang::IllegalArgumentException();
186 :
187 304 : ::com::sun::star::uno::Reference< ::com::sun::star::uno::XInterface > lhs( _lhs, ::com::sun::star::uno::UNO_QUERY );
188 608 : ::com::sun::star::uno::Reference< ::com::sun::star::uno::XInterface > rhs( _rhs, ::com::sun::star::uno::UNO_QUERY );
189 608 : return lhs.get() < rhs.get();
190 : }
191 : };
192 :
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><</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.
206 : */
207 : ::std::unique_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
211 : );
212 :
213 :
214 : } // namespace comphelper
215 :
216 :
217 : #endif // INCLUDED_COMPHELPER_ANYCOMPARE_HXX
218 :
219 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|