LibreOffice Module comphelper (master) 1
anycompare.hxx
Go to the documentation of this file.
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
24
25#include <com/sun/star/lang/IllegalArgumentException.hpp>
26#include <com/sun/star/i18n/XCollator.hpp>
27
29
30#include <memory>
31#include <utility>
32
33
34namespace comphelper
35{
36
37
38 //= IKeyPredicateLess
39
41 {
42 public:
43 virtual bool isLess( css::uno::Any const & _lhs, css::uno::Any const & _rhs ) const = 0;
44 virtual ~IKeyPredicateLess() {}
45 };
46
47
48 //= LessPredicateAdapter
49
51 {
53 :m_predicate( _predicate )
54 {
55 }
56
57 bool operator()( css::uno::Any const & _lhs, css::uno::Any const & _rhs ) const
58 {
59 return m_predicate.isLess( _lhs, _rhs );
60 }
61
62 private:
64 };
65
66
67 //= ScalarPredicateLess
68
69 template< typename SCALAR >
71 {
72 public:
73 virtual bool isLess( css::uno::Any const & _lhs, css::uno::Any const & _rhs ) const override
74 {
75 SCALAR lhs(0), rhs(0);
76 if ( !( _lhs >>= lhs )
77 || !( _rhs >>= rhs )
78 )
79 throw css::lang::IllegalArgumentException();
80 return lhs < rhs;
81 }
82 };
83
84
85 //= StringPredicateLess
86
88 {
89 public:
90 virtual bool isLess( css::uno::Any const & _lhs, css::uno::Any const & _rhs ) const override
91 {
92 OUString lhs, rhs;
93 if ( !( _lhs >>= lhs )
94 || !( _rhs >>= rhs )
95 )
96 throw css::lang::IllegalArgumentException();
97 return lhs < rhs;
98 }
99 };
100
101
102 //= StringCollationPredicateLess
103
105 {
106 public:
107 StringCollationPredicateLess( css::uno::Reference< css::i18n::XCollator > i_collator )
108 :m_collator(std::move( i_collator ))
109 {
110 }
111
112 virtual bool isLess( css::uno::Any const & _lhs, css::uno::Any const & _rhs ) const override
113 {
114 OUString lhs, rhs;
115 if ( !( _lhs >>= lhs )
116 || !( _rhs >>= rhs )
117 )
118 throw css::lang::IllegalArgumentException();
119 return m_collator->compareString( lhs, rhs ) < 0;
120 }
121
122 private:
123 css::uno::Reference< css::i18n::XCollator > const m_collator;
124 };
125
126
127 //= TypePredicateLess
128
130 {
131 public:
132 virtual bool isLess( css::uno::Any const & _lhs, css::uno::Any const & _rhs ) const override
133 {
134 css::uno::Type lhs, rhs;
135 if ( !( _lhs >>= lhs )
136 || !( _rhs >>= rhs )
137 )
138 throw css::lang::IllegalArgumentException();
139 return lhs.getTypeName() < rhs.getTypeName();
140 }
141 };
142
143
144 //= EnumPredicateLess
145
147 {
148 public:
149 EnumPredicateLess( css::uno::Type const & _enumType )
150 :m_enumType( _enumType )
151 {
152 }
153
154 virtual bool isLess( css::uno::Any const & _lhs, css::uno::Any const & _rhs ) const override
155 {
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 )
161 )
162 throw css::lang::IllegalArgumentException();
163 return lhs < rhs;
164 }
165
166 private:
167 css::uno::Type const m_enumType;
168 };
169
170
171 //= InterfacePredicateLess
172
174 {
175 public:
176 virtual bool isLess( css::uno::Any const & _lhs, css::uno::Any const & _rhs ) const override
177 {
178 if ( ( _lhs.getValueTypeClass() != css::uno::TypeClass_INTERFACE )
179 || ( _rhs.getValueTypeClass() != css::uno::TypeClass_INTERFACE )
180 )
181 throw css::lang::IllegalArgumentException();
182
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();
186 }
187 };
188
189
190 //= getStandardLessPredicate
191
203 ::std::unique_ptr< IKeyPredicateLess > COMPHELPER_DLLPUBLIC
205 css::uno::Type const & i_type,
206 css::uno::Reference< css::i18n::XCollator > const & i_collator
207 );
208
212 bool COMPHELPER_DLLPUBLIC anyLess( css::uno::Any const & lhs, css::uno::Any const & rhs);
213
214} // namespace comphelper
215
216
217#endif // INCLUDED_COMPHELPER_ANYCOMPARE_HXX
218
219/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
virtual bool isLess(css::uno::Any const &_lhs, css::uno::Any const &_rhs) const override
Definition: anycompare.hxx:154
css::uno::Type const m_enumType
Definition: anycompare.hxx:167
EnumPredicateLess(css::uno::Type const &_enumType)
Definition: anycompare.hxx:149
virtual bool isLess(css::uno::Any const &_lhs, css::uno::Any const &_rhs) const =0
virtual bool isLess(css::uno::Any const &_lhs, css::uno::Any const &_rhs) const override
Definition: anycompare.hxx:176
virtual bool isLess(css::uno::Any const &_lhs, css::uno::Any const &_rhs) const override
Definition: anycompare.hxx:73
StringCollationPredicateLess(css::uno::Reference< css::i18n::XCollator > i_collator)
Definition: anycompare.hxx:107
css::uno::Reference< css::i18n::XCollator > const m_collator
Definition: anycompare.hxx:123
virtual bool isLess(css::uno::Any const &_lhs, css::uno::Any const &_rhs) const override
Definition: anycompare.hxx:112
virtual bool isLess(css::uno::Any const &_lhs, css::uno::Any const &_rhs) const override
Definition: anycompare.hxx:90
virtual bool isLess(css::uno::Any const &_lhs, css::uno::Any const &_rhs) const override
Definition: anycompare.hxx:132
#define COMPHELPER_DLLPUBLIC
std::unique_ptr< IKeyPredicateLess > getStandardLessPredicate(Type const &i_type, Reference< XCollator > const &i_collator)
Definition: anycompare.cxx:378
bool anyLess(css::uno::Any const &lhs, css::uno::Any const &rhs)
Compare two Anys.
Definition: anycompare.cxx:445
bool enum2int(sal_Int32 &rnEnum, const css::uno::Any &rAny)
Sets int32 from enum or int in any.
Definition: extract.hxx:54
LessPredicateAdapter(const IKeyPredicateLess &_predicate)
Definition: anycompare.hxx:52
bool operator()(css::uno::Any const &_lhs, css::uno::Any const &_rhs) const
Definition: anycompare.hxx:57
IKeyPredicateLess const & m_predicate
Definition: anycompare.hxx:63
#define SAL_NO_VTABLE