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