LibreOffice Module comphelper (master)  1
anycompare.cxx
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 #include <memory>
22 
23 #include <com/sun/star/util/Date.hpp>
24 #include <com/sun/star/util/Time.hpp>
25 #include <com/sun/star/util/DateTime.hpp>
26 
27 namespace comphelper
28 {
29  using ::com::sun::star::uno::Reference;
30  using ::com::sun::star::uno::Type;
31  using ::com::sun::star::uno::TypeClass_CHAR;
32  using ::com::sun::star::uno::TypeClass_BOOLEAN;
33  using ::com::sun::star::uno::TypeClass_BYTE;
34  using ::com::sun::star::uno::TypeClass_SHORT;
35  using ::com::sun::star::uno::TypeClass_UNSIGNED_SHORT;
36  using ::com::sun::star::uno::TypeClass_LONG;
37  using ::com::sun::star::uno::TypeClass_UNSIGNED_LONG;
38  using ::com::sun::star::uno::TypeClass_HYPER;
39  using ::com::sun::star::uno::TypeClass_UNSIGNED_HYPER;
40  using ::com::sun::star::uno::TypeClass_FLOAT;
41  using ::com::sun::star::uno::TypeClass_DOUBLE;
42  using ::com::sun::star::uno::TypeClass_STRING;
43  using ::com::sun::star::uno::TypeClass_TYPE;
44  using ::com::sun::star::uno::TypeClass_ENUM;
45  using ::com::sun::star::uno::TypeClass_INTERFACE;
46  using ::com::sun::star::uno::TypeClass_STRUCT;
47  using ::com::sun::star::i18n::XCollator;
48  using ::com::sun::star::util::Date;
49  using ::com::sun::star::util::Time;
50  using ::com::sun::star::util::DateTime;
51 
53  {
54  public:
55  virtual bool isLess( css::uno::Any const & _lhs, css::uno::Any const & _rhs ) const override
56  {
57  Date lhs, rhs;
58  if ( !( _lhs >>= lhs )
59  || !( _rhs >>= rhs )
60  )
61  throw css::lang::IllegalArgumentException();
62  // FIXME Timezone?
63 
64  if ( lhs.Year < rhs.Year )
65  return true;
66  if ( lhs.Year > rhs.Year )
67  return false;
68 
69  if ( lhs.Month < rhs.Month )
70  return true;
71  if ( lhs.Month > rhs.Month )
72  return false;
73 
74  if ( lhs.Day < rhs.Day )
75  return true;
76  return false;
77  }
78  };
79 
81  {
82  public:
83  virtual bool isLess( css::uno::Any const & _lhs, css::uno::Any const & _rhs ) const override
84  {
85  Time lhs, rhs;
86  if ( !( _lhs >>= lhs )
87  || !( _rhs >>= rhs )
88  )
89  throw css::lang::IllegalArgumentException();
90  // FIXME Timezone?
91 
92  if ( lhs.Hours < rhs.Hours )
93  return true;
94  if ( lhs.Hours > rhs.Hours )
95  return false;
96 
97  if ( lhs.Minutes < rhs.Minutes )
98  return true;
99  if ( lhs.Minutes > rhs.Minutes )
100  return false;
101 
102  if ( lhs.Seconds < rhs.Seconds )
103  return true;
104  if ( lhs.Seconds > rhs.Seconds )
105  return false;
106 
107  if ( lhs.NanoSeconds < rhs.NanoSeconds )
108  return true;
109  return false;
110  }
111  };
112 
114  {
115  public:
116  virtual bool isLess( css::uno::Any const & _lhs, css::uno::Any const & _rhs ) const override
117  {
118  DateTime lhs, rhs;
119  if ( !( _lhs >>= lhs )
120  || !( _rhs >>= rhs )
121  )
122  throw css::lang::IllegalArgumentException();
123  // FIXME Timezone?
124 
125  if ( lhs.Year < rhs.Year )
126  return true;
127  if ( lhs.Year > rhs.Year )
128  return false;
129 
130  if ( lhs.Month < rhs.Month )
131  return true;
132  if ( lhs.Month > rhs.Month )
133  return false;
134 
135  if ( lhs.Day < rhs.Day )
136  return true;
137  if ( lhs.Day > rhs.Day )
138  return false;
139 
140  if ( lhs.Hours < rhs.Hours )
141  return true;
142  if ( lhs.Hours > rhs.Hours )
143  return false;
144 
145  if ( lhs.Minutes < rhs.Minutes )
146  return true;
147  if ( lhs.Minutes > rhs.Minutes )
148  return false;
149 
150  if ( lhs.Seconds < rhs.Seconds )
151  return true;
152  if ( lhs.Seconds > rhs.Seconds )
153  return false;
154 
155  if ( lhs.NanoSeconds < rhs.NanoSeconds )
156  return true;
157  return false;
158  }
159  };
160 
161 
162  std::unique_ptr< IKeyPredicateLess > getStandardLessPredicate( Type const & i_type, Reference< XCollator > const & i_collator )
163  {
164  std::unique_ptr< IKeyPredicateLess > pComparator;
165  switch ( i_type.getTypeClass() )
166  {
167  case TypeClass_CHAR:
168  pComparator.reset( new ScalarPredicateLess< sal_Unicode > );
169  break;
170  case TypeClass_BOOLEAN:
171  pComparator.reset( new ScalarPredicateLess< bool > );
172  break;
173  case TypeClass_BYTE:
174  pComparator.reset( new ScalarPredicateLess< sal_Int8 > );
175  break;
176  case TypeClass_SHORT:
177  pComparator.reset( new ScalarPredicateLess< sal_Int16 > );
178  break;
179  case TypeClass_UNSIGNED_SHORT:
180  pComparator.reset( new ScalarPredicateLess< sal_uInt16 > );
181  break;
182  case TypeClass_LONG:
183  pComparator.reset( new ScalarPredicateLess< sal_Int32 > );
184  break;
185  case TypeClass_UNSIGNED_LONG:
186  pComparator.reset( new ScalarPredicateLess< sal_uInt32 > );
187  break;
188  case TypeClass_HYPER:
189  pComparator.reset( new ScalarPredicateLess< sal_Int64 > );
190  break;
191  case TypeClass_UNSIGNED_HYPER:
192  pComparator.reset( new ScalarPredicateLess< sal_uInt64 > );
193  break;
194  case TypeClass_FLOAT:
195  pComparator.reset( new ScalarPredicateLess< float > );
196  break;
197  case TypeClass_DOUBLE:
198  pComparator.reset( new ScalarPredicateLess< double > );
199  break;
200  case TypeClass_STRING:
201  if ( i_collator.is() )
202  pComparator.reset( new StringCollationPredicateLess( i_collator ) );
203  else
204  pComparator.reset( new StringPredicateLess );
205  break;
206  case TypeClass_TYPE:
207  pComparator.reset( new TypePredicateLess );
208  break;
209  case TypeClass_ENUM:
210  pComparator.reset( new EnumPredicateLess( i_type ) );
211  break;
212  case TypeClass_INTERFACE:
213  pComparator.reset( new InterfacePredicateLess );
214  break;
215  case TypeClass_STRUCT:
216  if ( i_type.equals( ::cppu::UnoType< Date >::get() ) )
217  pComparator.reset( new DatePredicateLess );
218  else if ( i_type.equals( ::cppu::UnoType< Time >::get() ) )
219  pComparator.reset( new TimePredicateLess );
220  else if ( i_type.equals( ::cppu::UnoType< DateTime >::get() ) )
221  pComparator.reset( new DateTimePredicateLess );
222  break;
223  default:
224  break;
225  }
226  return pComparator;
227  }
228 
229  bool anyLess( css::uno::Any const & lhs, css::uno::Any const & rhs)
230  {
231  auto lhsTypeClass = lhs.getValueType().getTypeClass();
232  auto rhsTypeClass = rhs.getValueType().getTypeClass();
233  if (lhsTypeClass != rhsTypeClass)
234  return lhsTypeClass < rhsTypeClass;
235  std::unique_ptr< IKeyPredicateLess > pred = getStandardLessPredicate( lhs.getValueType(), Reference< XCollator >() );
236  if (!pred) // type==VOID
237  return false;
238  return pred->isLess(lhs, rhs);
239  }
240 
241 } // namespace comphelper
242 
243 
244 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
Type
virtual bool isLess(css::uno::Any const &_lhs, css::uno::Any const &_rhs) const override
Definition: anycompare.cxx:55
virtual bool isLess(css::uno::Any const &_lhs, css::uno::Any const &_rhs) const override
Definition: anycompare.cxx:116
bool anyLess(css::uno::Any const &lhs, css::uno::Any const &rhs)
Compare two Anys.
Definition: anycompare.cxx:229
virtual bool isLess(css::uno::Any const &_lhs, css::uno::Any const &_rhs) const override
Definition: anycompare.cxx:83
std::unique_ptr< IKeyPredicateLess > getStandardLessPredicate(Type const &i_type, Reference< XCollator > const &i_collator)
Definition: anycompare.cxx:162