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