xref: /aoo41x/main/comphelper/source/misc/types.cxx (revision cdf0e10c)
1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26  ************************************************************************/
27 
28 // MARKER(update_precomp.py): autogen include statement, do not remove
29 #include "precompiled_comphelper.hxx"
30 #include <comphelper/types.hxx>
31 #include <comphelper/extract.hxx>
32 #include <com/sun/star/util/Date.hpp>
33 #include <com/sun/star/util/Time.hpp>
34 #include <com/sun/star/util/DateTime.hpp>
35 #include <com/sun/star/awt/FontUnderline.hpp>
36 #include <com/sun/star/awt/FontStrikeout.hpp>
37 #include <com/sun/star/awt/FontDescriptor.hpp>
38 #include <osl/diagnose.h>
39 #include <typelib/typedescription.hxx>
40 
41 #include <memory.h>
42 
43 
44 //.........................................................................
45 namespace comphelper
46 {
47 //.........................................................................
48 
49 using namespace ::com::sun::star::uno;
50 using namespace ::com::sun::star::awt;
51 using namespace ::com::sun::star::util;
52 using namespace ::com::sun::star::lang;
53 
54 //-------------------------------------------------------------------------
55 sal_Bool operator ==(const DateTime& _rLeft, const DateTime& _rRight)
56 {
57 	return ( _rLeft.HundredthSeconds == _rRight.HundredthSeconds) &&
58 	( _rLeft.Seconds == _rRight.Seconds) &&
59 	( _rLeft.Minutes == _rRight.Minutes) &&
60 	( _rLeft.Hours == _rRight.Hours) &&
61 	( _rLeft.Day == _rRight.Day) &&
62 	( _rLeft.Month == _rRight.Month) &&
63 	( _rLeft.Year == _rRight.Year) ;
64 }
65 
66 //-------------------------------------------------------------------------
67 sal_Bool operator ==(const Date& _rLeft, const Date& _rRight)
68 {
69 	return ( _rLeft.Day == _rRight.Day) &&
70 	( _rLeft.Month == _rRight.Month) &&
71 	( _rLeft.Year == _rRight.Year) ;
72 }
73 
74 //-------------------------------------------------------------------------
75 sal_Bool operator ==(const Time& _rLeft, const Time& _rRight)
76 {
77 	return ( _rLeft.HundredthSeconds == _rRight.HundredthSeconds) &&
78 	( _rLeft.Seconds == _rRight.Seconds) &&
79 	( _rLeft.Minutes == _rRight.Minutes) &&
80 	( _rLeft.Hours == _rRight.Hours) ;
81 }
82 
83 //------------------------------------------------------------------------------
84 sal_Int32 getINT32(const Any& _rAny)
85 {
86 	sal_Int32 nReturn = 0;
87 	OSL_VERIFY( _rAny >>= nReturn );
88 	return nReturn;
89 }
90 
91 //------------------------------------------------------------------------------
92 sal_Int16 getINT16(const Any& _rAny)
93 {
94 	sal_Int16 nReturn = 0;
95 	OSL_VERIFY( _rAny >>= nReturn );
96 	return nReturn;
97 }
98 
99 //------------------------------------------------------------------------------
100 double getDouble(const Any& _rAny)
101 {
102 	double nReturn = 0.0;
103 	OSL_VERIFY( _rAny >>= nReturn );
104 	return nReturn;
105 }
106 
107 //------------------------------------------------------------------------------
108 float getFloat(const Any& _rAny)
109 {
110 	float nReturn = 0.0;
111 	OSL_VERIFY( _rAny >>= nReturn );
112 	return nReturn;
113 }
114 
115 //------------------------------------------------------------------------------
116 ::rtl::OUString	getString(const Any& _rAny)
117 {
118 	::rtl::OUString nReturn;
119 	OSL_VERIFY( _rAny >>= nReturn );
120 	return nReturn;
121 }
122 
123 //------------------------------------------------------------------------------
124 sal_Bool getBOOL(const Any& _rAny)
125 {
126 	sal_Bool nReturn = sal_False;
127 	if (_rAny.getValueType() == ::getCppuBooleanType())
128 		nReturn = *(sal_Bool*)_rAny.getValue();
129 	else
130 		OSL_ENSURE(sal_False, "comphelper::getBOOL : invalid argument !");
131 	return nReturn;
132 }
133 
134 //------------------------------------------------------------------------------
135 sal_Int32 getEnumAsINT32(const Any& _rAny) throw(IllegalArgumentException)
136 {
137 	sal_Int32 nReturn = 0;
138 	if (! ::cppu::enum2int(nReturn,_rAny) )
139 		throw IllegalArgumentException();
140 	return nReturn;
141 }
142 
143 //------------------------------------------------------------------------------
144 FontDescriptor	getDefaultFont()
145 {
146 	FontDescriptor aReturn;
147 	aReturn.Slant = FontSlant_DONTKNOW;
148 	aReturn.Underline = FontUnderline::DONTKNOW;
149 	aReturn.Strikeout = FontStrikeout::DONTKNOW;
150 	return aReturn;
151 }
152 
153 //------------------------------------------------------------------------------
154 sal_Bool isAssignableFrom(const Type& _rAssignable, const Type& _rFrom)
155 {
156 	// getthe type lib descriptions
157 	typelib_TypeDescription* pAssignable = NULL;
158 	_rAssignable.getDescription(&pAssignable);
159 
160 	typelib_TypeDescription* pFrom = NULL;
161 	_rFrom.getDescription(&pFrom);
162 
163 	// and ask the type lib
164 	return typelib_typedescription_isAssignableFrom(pAssignable, pFrom);
165 }
166 
167 //------------------------------------------------------------------
168 template<class TYPE>
169 sal_Bool tryCompare(const void* _pData, const Any& _rValue, sal_Bool& _bIdentical, TYPE& _rOut)
170 {
171 	sal_Bool bSuccess = _rValue >>= _rOut;
172 	_bIdentical = bSuccess && (_rOut == *reinterpret_cast<const TYPE*>(_pData));
173 	return bSuccess;
174 }
175 
176 //------------------------------------------------------------------
177 sal_Bool tryCompare(const void* _pData, const Any& _rValue, sal_Bool& _bIdentical, sal_Unicode& _rOut)
178 {
179     sal_Bool bSuccess = ( _rValue.getValueTypeClass() == TypeClass_CHAR );
180     if ( bSuccess )
181         _rOut = *static_cast< const sal_Unicode* >( _rValue.getValue() );
182     _bIdentical = bSuccess && ( _rOut == *static_cast< const sal_Unicode* >( _pData ) );
183 	return bSuccess;
184 }
185 
186 //------------------------------------------------------------------
187 sal_Bool compare_impl(const Type& _rType, const void* pData, const Any& _rValue)
188 {
189 	sal_Bool bRes = sal_True;
190 
191 	if (_rType.getTypeClass() == TypeClass_ANY)
192 	{
193 		// beides AnyWerte
194 		if (_rValue.getValueType().getTypeClass() == TypeClass_ANY)
195 			bRes = compare_impl(
196 				reinterpret_cast<const Any*>(pData)->getValueType(),
197 				reinterpret_cast<const Any*>(pData)->getValue(),
198 				*reinterpret_cast<const Any*>(_rValue.getValue()));
199 		else
200 			bRes = compare_impl(
201 				reinterpret_cast<const Any*>(pData)->getValueType(),
202 				reinterpret_cast<const Any*>(pData)->getValue(),
203 				_rValue);
204 	}
205 	else if	(	(_rType.getTypeClass() == TypeClass_VOID)
206 			||	(_rValue.getValueType().getTypeClass() == TypeClass_VOID)
207 			)
208 	{
209 		bRes = _rType.getTypeClass() == _rValue.getValueType().getTypeClass();
210 	}
211 	else
212 	{
213 		sal_Bool bConversionSuccess = sal_False;
214 		switch (_rType.getTypeClass())
215 		{
216 			case TypeClass_VOID:
217 				bConversionSuccess = sal_True;
218 				bRes = _rValue.getValueType().getTypeClass() == TypeClass_VOID;
219 				break;
220 			case TypeClass_BOOLEAN:
221 			{
222 				sal_Bool aDummy;
223 				bConversionSuccess = tryCompare(pData, _rValue, bRes, aDummy);
224 				break;
225 			}
226 			case TypeClass_CHAR:
227 			{
228 				sal_Unicode aDummy(0);
229 				bConversionSuccess = tryCompare(pData, _rValue, bRes, aDummy);
230 				break;
231 			}
232 			case TypeClass_STRING:
233 			{
234 				::rtl::OUString aDummy;
235 				bConversionSuccess = tryCompare(pData, _rValue, bRes, aDummy);
236 				break;
237 			}
238 			case TypeClass_FLOAT:
239 			{
240 				float aDummy;
241 				bConversionSuccess = tryCompare(pData, _rValue, bRes, aDummy);
242 				break;
243 			}
244 			case TypeClass_DOUBLE:
245 			{
246 				double aDummy;
247 				bConversionSuccess = tryCompare(pData, _rValue, bRes, aDummy);
248 				break;
249 			}
250 			case TypeClass_BYTE:
251 			{
252 				sal_Int8 aDummy;
253 				bConversionSuccess = tryCompare(pData, _rValue, bRes, aDummy);
254 				break;
255 			}
256 			case TypeClass_SHORT:
257 			{
258 				sal_Int16 aDummy;
259 				bConversionSuccess = tryCompare(pData, _rValue, bRes, aDummy);
260 				break;
261 			}
262 			case TypeClass_ENUM:
263 			{
264 				sal_Int32 nAsInt32 = 0;
265 				bConversionSuccess = ::cppu::enum2int(nAsInt32, _rValue);
266 				bRes = bConversionSuccess && (nAsInt32== *reinterpret_cast<const sal_Int32*>(pData));
267 				break;
268 			}
269 			case TypeClass_LONG:
270 			{
271 				sal_Int32 aDummy;
272 				bConversionSuccess = tryCompare(pData, _rValue, bRes, aDummy);
273 				break;
274 			}
275 			case TypeClass_UNSIGNED_SHORT:
276 			{
277 				sal_uInt16 aDummy;
278 				bConversionSuccess = tryCompare(pData, _rValue, bRes, aDummy);
279 				break;
280 			}
281 			case TypeClass_UNSIGNED_LONG:
282 			{
283 				sal_uInt32 aDummy;
284 				bConversionSuccess = tryCompare(pData, _rValue, bRes, aDummy);
285 				break;
286 			}
287 			case TypeClass_INTERFACE:
288 			{
289 				InterfaceRef aDummy;
290 				bConversionSuccess = tryCompare(pData, _rValue, bRes, aDummy);
291 				break;
292 			}
293 			case TypeClass_STRUCT:
294 				if (isA(_rType, static_cast<FontDescriptor*>(NULL)))
295 				{
296 					FontDescriptor aTemp;
297 					bConversionSuccess = _rValue >>= aTemp;
298 					if (bConversionSuccess)
299 					{
300 						bRes = *(FontDescriptor*)pData == aTemp;
301 					}
302 					else
303 						bRes = sal_False;
304 					break;
305 				}
306 				if (isA(_rType, static_cast<Date*>(NULL)))
307 				{
308 					Date aDummy;
309 					bConversionSuccess = tryCompare(pData, _rValue, bRes, aDummy);
310 					break;
311 				}
312 				if (isA(_rType, static_cast<Time*>(NULL)))
313 				{
314 					Time aDummy;
315 					bConversionSuccess = tryCompare(pData, _rValue, bRes, aDummy);
316 					break;
317 				}
318 				if (isA(_rType, static_cast<DateTime*>(NULL)))
319 				{
320 					DateTime aDummy;
321 					bConversionSuccess = tryCompare(pData, _rValue, bRes, aDummy);
322 					break;
323 				}
324 				break;
325 			case TypeClass_SEQUENCE:
326 				if (isA(_rType, static_cast< Sequence<sal_Int8>* >(NULL)))
327 				{
328 					Sequence<sal_Int8> aTemp;
329 					bConversionSuccess = _rValue >>= aTemp;
330 					if (bConversionSuccess)
331 					{
332 						const Sequence<sal_Int8>& rLeftSeq = *reinterpret_cast<const Sequence<sal_Int8>*>(pData);
333 						const Sequence<sal_Int8>& rRightSeq = aTemp;
334 						bRes = rLeftSeq.getLength() == rRightSeq.getLength() &&
335 							memcmp(rLeftSeq.getConstArray(), rRightSeq.getConstArray(), rLeftSeq.getLength()) == 0;
336 					}
337 				}
338 				else if (isA(_rType, static_cast< Sequence<sal_uInt8>* >(NULL)))
339 				{
340 					Sequence<sal_uInt8> aTemp;
341 					bConversionSuccess = _rValue >>= aTemp;
342 					if (bConversionSuccess)
343 					{
344 						const Sequence<sal_uInt8>& rLeftSeq = *reinterpret_cast<const Sequence<sal_uInt8>*>(pData);
345 						const Sequence<sal_uInt8>& rRightSeq = aTemp;
346 						bRes = rLeftSeq.getLength() == rRightSeq.getLength() &&
347 							memcmp(rLeftSeq.getConstArray(), rRightSeq.getConstArray(), rLeftSeq.getLength()) == 0;
348 					}
349 				}
350 				else if (isA(_rType, static_cast< Sequence<sal_Int16>* >(NULL)))
351 				{
352 					Sequence<sal_Int16> aTemp;
353 					bConversionSuccess = _rValue >>= aTemp;
354 					if (bConversionSuccess)
355 					{
356 						const Sequence<sal_Int16>& rLeftSeq = *reinterpret_cast<const Sequence<sal_Int16>*>(pData);
357 						const Sequence<sal_Int16>& rRightSeq = aTemp;
358 						bRes = rLeftSeq.getLength() == rRightSeq.getLength() &&
359 							memcmp(rLeftSeq.getConstArray(), rRightSeq.getConstArray(), rLeftSeq.getLength()*sizeof(sal_Int16)) == 0;
360 					}
361 				}
362 				else if (isA(_rType, static_cast< Sequence<sal_uInt16>* >(NULL)))
363 				{
364 					Sequence<sal_uInt16> aTemp;
365 					bConversionSuccess = _rValue >>= aTemp;
366 					if (bConversionSuccess)
367 					{
368 						const Sequence<sal_uInt16>& rLeftSeq = *reinterpret_cast<const Sequence<sal_uInt16>*>(pData);
369 						const Sequence<sal_uInt16>& rRightSeq = aTemp;
370 						bRes = rLeftSeq.getLength() == rRightSeq.getLength() &&
371 							memcmp(rLeftSeq.getConstArray(), rRightSeq.getConstArray(), rLeftSeq.getLength()*sizeof(sal_uInt16)) == 0;
372 					}
373 				}
374 				else if (isA(_rType, static_cast< Sequence<sal_Int32>* >(NULL)))
375 				{
376 					Sequence<sal_Int32> aTemp;
377 					bConversionSuccess = _rValue >>= aTemp;
378 					if (bConversionSuccess)
379 					{
380 						const Sequence<sal_Int32>& rLeftSeq = *reinterpret_cast<const Sequence<sal_Int32>*>(pData);
381 						const Sequence<sal_Int32>& rRightSeq = aTemp;
382 						bRes = rLeftSeq.getLength() == rRightSeq.getLength() &&
383 							memcmp(rLeftSeq.getConstArray(), rRightSeq.getConstArray(), rLeftSeq.getLength()*sizeof(sal_Int32)) == 0;
384 					}
385 				}
386 				else if (isA(_rType, static_cast< Sequence<sal_uInt32>* >(NULL)))
387 				{
388 					Sequence<sal_uInt32> aTemp;
389 					bConversionSuccess = _rValue >>= aTemp;
390 					if (bConversionSuccess)
391 					{
392 						const Sequence<sal_uInt32>& rLeftSeq = *reinterpret_cast<const Sequence<sal_uInt32>*>(pData);
393 						const Sequence<sal_uInt32>& rRightSeq = aTemp;
394 						bRes = rLeftSeq.getLength() == rRightSeq.getLength() &&
395 							memcmp(rLeftSeq.getConstArray(), rRightSeq.getConstArray(), rLeftSeq.getLength()*sizeof(sal_uInt32)) == 0;
396 					}
397 				}
398 				else if (isA(_rType, static_cast< Sequence< ::rtl::OUString >* >(NULL)))
399 				{
400 					Sequence< ::rtl::OUString > aTemp;
401 					bConversionSuccess = _rValue >>= aTemp;
402 					if (bConversionSuccess)
403 					{
404 						const Sequence< ::rtl::OUString >& rLeftSeq = *reinterpret_cast<const Sequence< ::rtl::OUString>*>(pData);
405 						const Sequence< ::rtl::OUString >& rRightSeq = aTemp;
406 						sal_Int32 nSeqLen = rLeftSeq.getLength();
407 						bRes = ( nSeqLen == rRightSeq.getLength() );
408 						for ( sal_Int32 n = 0; bRes && ( n < nSeqLen ); n++ )
409 						{
410 							const ::rtl::OUString& rS1 = rLeftSeq.getConstArray()[n];
411 							const ::rtl::OUString& rS2 = rRightSeq.getConstArray()[n];
412 							bRes = ( rS1 == rS2 );
413 						}
414 					}
415 				}
416 				break;
417 			default:
418 				bRes = sal_False;
419 		}
420 
421 		bRes = bRes && bConversionSuccess;
422 	}
423 	return bRes;
424 }
425 
426 //------------------------------------------------------------------------------
427 sal_Bool compare(const Any& rLeft, const Any& rRight)
428 {
429 	return compare_impl(rLeft.getValueType(), rLeft.getValue(), rRight);
430 }
431 
432 //-------------------------------------------------------------------------
433 sal_Bool	operator ==(const FontDescriptor& _rLeft, const FontDescriptor& _rRight)
434 {
435 	return ( _rLeft.Name.equals( _rRight.Name ) ) &&
436 	( _rLeft.Height == _rRight.Height ) &&
437 	( _rLeft.Width == _rRight.Width ) &&
438 	( _rLeft.StyleName.equals( _rRight.StyleName ) ) &&
439 	( _rLeft.Family == _rRight.Family ) &&
440 	( _rLeft.CharSet == _rRight.CharSet ) &&
441 	( _rLeft.Pitch == _rRight.Pitch ) &&
442 	( _rLeft.CharacterWidth == _rRight.CharacterWidth ) &&
443 	( _rLeft.Weight == _rRight.Weight ) &&
444 	( _rLeft.Slant == _rRight.Slant ) &&
445 	( _rLeft.Underline == _rRight.Underline ) &&
446 	( _rLeft.Strikeout == _rRight.Strikeout ) &&
447 	( _rLeft.Orientation == _rRight.Orientation ) &&
448 	( _rLeft.Kerning == _rRight.Kerning ) &&
449 	( _rLeft.WordLineMode == _rRight.WordLineMode ) &&
450 	( _rLeft.Type == _rRight.Type ) ;
451 }
452 
453 //-------------------------------------------------------------------------
454 Type getSequenceElementType(const Type& _rSequenceType)
455 {
456 	OSL_ENSURE(_rSequenceType.getTypeClass() == TypeClass_SEQUENCE,
457 				"getSequenceElementType: must be called with a  sequence type!");
458 
459 	if (!(_rSequenceType.getTypeClass() == TypeClass_SEQUENCE))
460 		return Type();
461 
462 	TypeDescription aTD(_rSequenceType);
463 	typelib_IndirectTypeDescription* pSequenceTD =
464 		reinterpret_cast< typelib_IndirectTypeDescription* >(aTD.get());
465 
466 	OSL_ASSERT(pSequenceTD);
467 	OSL_ASSERT(pSequenceTD->pType);
468 
469 	if (pSequenceTD && pSequenceTD->pType)
470 		return Type(pSequenceTD->pType);
471 
472 	return Type();
473 }
474 //.........................................................................
475 }	// namespace comphelper
476 //.........................................................................
477 
478