Update ooo320-m1
[ooovba.git] / comphelper / source / misc / types.cxx
blobb3536d526d9d19423e77641f25c2c05bce4a2412
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: types.cxx,v $
10 * $Revision: 1.12 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_comphelper.hxx"
33 #include <comphelper/types.hxx>
34 #include <comphelper/extract.hxx>
35 #include <com/sun/star/util/Date.hpp>
36 #include <com/sun/star/util/Time.hpp>
37 #include <com/sun/star/util/DateTime.hpp>
38 #include <com/sun/star/awt/FontUnderline.hpp>
39 #include <com/sun/star/awt/FontStrikeout.hpp>
40 #include <com/sun/star/awt/FontDescriptor.hpp>
41 #include <osl/diagnose.h>
42 #include <typelib/typedescription.hxx>
44 #include <memory.h>
47 //.........................................................................
48 namespace comphelper
50 //.........................................................................
52 using namespace ::com::sun::star::uno;
53 using namespace ::com::sun::star::awt;
54 using namespace ::com::sun::star::util;
55 using namespace ::com::sun::star::lang;
57 //-------------------------------------------------------------------------
58 sal_Bool operator ==(const DateTime& _rLeft, const DateTime& _rRight)
60 return ( _rLeft.HundredthSeconds == _rRight.HundredthSeconds) &&
61 ( _rLeft.Seconds == _rRight.Seconds) &&
62 ( _rLeft.Minutes == _rRight.Minutes) &&
63 ( _rLeft.Hours == _rRight.Hours) &&
64 ( _rLeft.Day == _rRight.Day) &&
65 ( _rLeft.Month == _rRight.Month) &&
66 ( _rLeft.Year == _rRight.Year) ;
69 //-------------------------------------------------------------------------
70 sal_Bool operator ==(const Date& _rLeft, const Date& _rRight)
72 return ( _rLeft.Day == _rRight.Day) &&
73 ( _rLeft.Month == _rRight.Month) &&
74 ( _rLeft.Year == _rRight.Year) ;
77 //-------------------------------------------------------------------------
78 sal_Bool operator ==(const Time& _rLeft, const Time& _rRight)
80 return ( _rLeft.HundredthSeconds == _rRight.HundredthSeconds) &&
81 ( _rLeft.Seconds == _rRight.Seconds) &&
82 ( _rLeft.Minutes == _rRight.Minutes) &&
83 ( _rLeft.Hours == _rRight.Hours) ;
86 //------------------------------------------------------------------------------
87 sal_Int32 getINT32(const Any& _rAny)
89 sal_Int32 nReturn = 0;
90 OSL_VERIFY( _rAny >>= nReturn );
91 return nReturn;
94 //------------------------------------------------------------------------------
95 sal_Int16 getINT16(const Any& _rAny)
97 sal_Int16 nReturn = 0;
98 OSL_VERIFY( _rAny >>= nReturn );
99 return nReturn;
102 //------------------------------------------------------------------------------
103 double getDouble(const Any& _rAny)
105 double nReturn = 0.0;
106 OSL_VERIFY( _rAny >>= nReturn );
107 return nReturn;
110 //------------------------------------------------------------------------------
111 float getFloat(const Any& _rAny)
113 float nReturn = 0.0;
114 OSL_VERIFY( _rAny >>= nReturn );
115 return nReturn;
118 //------------------------------------------------------------------------------
119 ::rtl::OUString getString(const Any& _rAny)
121 ::rtl::OUString nReturn;
122 OSL_VERIFY( _rAny >>= nReturn );
123 return nReturn;
126 //------------------------------------------------------------------------------
127 sal_Bool getBOOL(const Any& _rAny)
129 sal_Bool nReturn = sal_False;
130 if (_rAny.getValueType() == ::getCppuBooleanType())
131 nReturn = *(sal_Bool*)_rAny.getValue();
132 else
133 OSL_ENSURE(sal_False, "comphelper::getBOOL : invalid argument !");
134 return nReturn;
137 //------------------------------------------------------------------------------
138 sal_Int32 getEnumAsINT32(const Any& _rAny) throw(IllegalArgumentException)
140 sal_Int32 nReturn = 0;
141 if (! ::cppu::enum2int(nReturn,_rAny) )
142 throw IllegalArgumentException();
143 return nReturn;
146 //------------------------------------------------------------------------------
147 FontDescriptor getDefaultFont()
149 FontDescriptor aReturn;
150 aReturn.Slant = FontSlant_DONTKNOW;
151 aReturn.Underline = FontUnderline::DONTKNOW;
152 aReturn.Strikeout = FontStrikeout::DONTKNOW;
153 return aReturn;
156 //------------------------------------------------------------------------------
157 sal_Bool isAssignableFrom(const Type& _rAssignable, const Type& _rFrom)
159 // getthe type lib descriptions
160 typelib_TypeDescription* pAssignable = NULL;
161 _rAssignable.getDescription(&pAssignable);
163 typelib_TypeDescription* pFrom = NULL;
164 _rFrom.getDescription(&pFrom);
166 // and ask the type lib
167 return typelib_typedescription_isAssignableFrom(pAssignable, pFrom);
170 //------------------------------------------------------------------
171 template<class TYPE>
172 sal_Bool tryCompare(const void* _pData, const Any& _rValue, sal_Bool& _bIdentical, TYPE& _rOut)
174 sal_Bool bSuccess = _rValue >>= _rOut;
175 _bIdentical = bSuccess && (_rOut == *reinterpret_cast<const TYPE*>(_pData));
176 return bSuccess;
179 //------------------------------------------------------------------
180 sal_Bool tryCompare(const void* _pData, const Any& _rValue, sal_Bool& _bIdentical, sal_Unicode& _rOut)
182 sal_Bool bSuccess = ( _rValue.getValueTypeClass() == TypeClass_CHAR );
183 if ( bSuccess )
184 _rOut = *static_cast< const sal_Unicode* >( _rValue.getValue() );
185 _bIdentical = bSuccess && ( _rOut == *static_cast< const sal_Unicode* >( _pData ) );
186 return bSuccess;
189 //------------------------------------------------------------------
190 sal_Bool compare_impl(const Type& _rType, const void* pData, const Any& _rValue)
192 sal_Bool bRes = sal_True;
194 if (_rType.getTypeClass() == TypeClass_ANY)
196 // beides AnyWerte
197 if (_rValue.getValueType().getTypeClass() == TypeClass_ANY)
198 bRes = compare_impl(
199 reinterpret_cast<const Any*>(pData)->getValueType(),
200 reinterpret_cast<const Any*>(pData)->getValue(),
201 *reinterpret_cast<const Any*>(_rValue.getValue()));
202 else
203 bRes = compare_impl(
204 reinterpret_cast<const Any*>(pData)->getValueType(),
205 reinterpret_cast<const Any*>(pData)->getValue(),
206 _rValue);
208 else if ( (_rType.getTypeClass() == TypeClass_VOID)
209 || (_rValue.getValueType().getTypeClass() == TypeClass_VOID)
212 bRes = _rType.getTypeClass() == _rValue.getValueType().getTypeClass();
214 else
216 sal_Bool bConversionSuccess = sal_False;
217 switch (_rType.getTypeClass())
219 case TypeClass_VOID:
220 bConversionSuccess = sal_True;
221 bRes = _rValue.getValueType().getTypeClass() == TypeClass_VOID;
222 break;
223 case TypeClass_BOOLEAN:
225 sal_Bool aDummy;
226 bConversionSuccess = tryCompare(pData, _rValue, bRes, aDummy);
227 break;
229 case TypeClass_CHAR:
231 sal_Unicode aDummy(0);
232 bConversionSuccess = tryCompare(pData, _rValue, bRes, aDummy);
233 break;
235 case TypeClass_STRING:
237 ::rtl::OUString aDummy;
238 bConversionSuccess = tryCompare(pData, _rValue, bRes, aDummy);
239 break;
241 case TypeClass_FLOAT:
243 float aDummy;
244 bConversionSuccess = tryCompare(pData, _rValue, bRes, aDummy);
245 break;
247 case TypeClass_DOUBLE:
249 double aDummy;
250 bConversionSuccess = tryCompare(pData, _rValue, bRes, aDummy);
251 break;
253 case TypeClass_BYTE:
255 sal_Int8 aDummy;
256 bConversionSuccess = tryCompare(pData, _rValue, bRes, aDummy);
257 break;
259 case TypeClass_SHORT:
261 sal_Int16 aDummy;
262 bConversionSuccess = tryCompare(pData, _rValue, bRes, aDummy);
263 break;
265 case TypeClass_ENUM:
267 sal_Int32 nAsInt32 = 0;
268 bConversionSuccess = ::cppu::enum2int(nAsInt32, _rValue);
269 bRes = bConversionSuccess && (nAsInt32== *reinterpret_cast<const sal_Int32*>(pData));
270 break;
272 case TypeClass_LONG:
274 sal_Int32 aDummy;
275 bConversionSuccess = tryCompare(pData, _rValue, bRes, aDummy);
276 break;
278 case TypeClass_UNSIGNED_SHORT:
280 sal_uInt16 aDummy;
281 bConversionSuccess = tryCompare(pData, _rValue, bRes, aDummy);
282 break;
284 case TypeClass_UNSIGNED_LONG:
286 sal_uInt32 aDummy;
287 bConversionSuccess = tryCompare(pData, _rValue, bRes, aDummy);
288 break;
290 case TypeClass_INTERFACE:
292 InterfaceRef aDummy;
293 bConversionSuccess = tryCompare(pData, _rValue, bRes, aDummy);
294 break;
296 case TypeClass_STRUCT:
297 if (isA(_rType, static_cast<FontDescriptor*>(NULL)))
299 FontDescriptor aTemp;
300 bConversionSuccess = _rValue >>= aTemp;
301 if (bConversionSuccess)
303 bRes = *(FontDescriptor*)pData == aTemp;
305 else
306 bRes = sal_False;
307 break;
309 if (isA(_rType, static_cast<Date*>(NULL)))
311 Date aDummy;
312 bConversionSuccess = tryCompare(pData, _rValue, bRes, aDummy);
313 break;
315 if (isA(_rType, static_cast<Time*>(NULL)))
317 Time aDummy;
318 bConversionSuccess = tryCompare(pData, _rValue, bRes, aDummy);
319 break;
321 if (isA(_rType, static_cast<DateTime*>(NULL)))
323 DateTime aDummy;
324 bConversionSuccess = tryCompare(pData, _rValue, bRes, aDummy);
325 break;
327 break;
328 case TypeClass_SEQUENCE:
329 if (isA(_rType, static_cast< Sequence<sal_Int8>* >(NULL)))
331 Sequence<sal_Int8> aTemp;
332 bConversionSuccess = _rValue >>= aTemp;
333 if (bConversionSuccess)
335 const Sequence<sal_Int8>& rLeftSeq = *reinterpret_cast<const Sequence<sal_Int8>*>(pData);
336 const Sequence<sal_Int8>& rRightSeq = aTemp;
337 bRes = rLeftSeq.getLength() == rRightSeq.getLength() &&
338 memcmp(rLeftSeq.getConstArray(), rRightSeq.getConstArray(), rLeftSeq.getLength()) == 0;
341 else if (isA(_rType, static_cast< Sequence<sal_uInt8>* >(NULL)))
343 Sequence<sal_uInt8> aTemp;
344 bConversionSuccess = _rValue >>= aTemp;
345 if (bConversionSuccess)
347 const Sequence<sal_uInt8>& rLeftSeq = *reinterpret_cast<const Sequence<sal_uInt8>*>(pData);
348 const Sequence<sal_uInt8>& rRightSeq = aTemp;
349 bRes = rLeftSeq.getLength() == rRightSeq.getLength() &&
350 memcmp(rLeftSeq.getConstArray(), rRightSeq.getConstArray(), rLeftSeq.getLength()) == 0;
353 else if (isA(_rType, static_cast< Sequence<sal_Int16>* >(NULL)))
355 Sequence<sal_Int16> aTemp;
356 bConversionSuccess = _rValue >>= aTemp;
357 if (bConversionSuccess)
359 const Sequence<sal_Int16>& rLeftSeq = *reinterpret_cast<const Sequence<sal_Int16>*>(pData);
360 const Sequence<sal_Int16>& rRightSeq = aTemp;
361 bRes = rLeftSeq.getLength() == rRightSeq.getLength() &&
362 memcmp(rLeftSeq.getConstArray(), rRightSeq.getConstArray(), rLeftSeq.getLength()*sizeof(sal_Int16)) == 0;
365 else if (isA(_rType, static_cast< Sequence<sal_uInt16>* >(NULL)))
367 Sequence<sal_uInt16> aTemp;
368 bConversionSuccess = _rValue >>= aTemp;
369 if (bConversionSuccess)
371 const Sequence<sal_uInt16>& rLeftSeq = *reinterpret_cast<const Sequence<sal_uInt16>*>(pData);
372 const Sequence<sal_uInt16>& rRightSeq = aTemp;
373 bRes = rLeftSeq.getLength() == rRightSeq.getLength() &&
374 memcmp(rLeftSeq.getConstArray(), rRightSeq.getConstArray(), rLeftSeq.getLength()*sizeof(sal_uInt16)) == 0;
377 else if (isA(_rType, static_cast< Sequence<sal_Int32>* >(NULL)))
379 Sequence<sal_Int32> aTemp;
380 bConversionSuccess = _rValue >>= aTemp;
381 if (bConversionSuccess)
383 const Sequence<sal_Int32>& rLeftSeq = *reinterpret_cast<const Sequence<sal_Int32>*>(pData);
384 const Sequence<sal_Int32>& rRightSeq = aTemp;
385 bRes = rLeftSeq.getLength() == rRightSeq.getLength() &&
386 memcmp(rLeftSeq.getConstArray(), rRightSeq.getConstArray(), rLeftSeq.getLength()*sizeof(sal_Int32)) == 0;
389 else if (isA(_rType, static_cast< Sequence<sal_uInt32>* >(NULL)))
391 Sequence<sal_uInt32> aTemp;
392 bConversionSuccess = _rValue >>= aTemp;
393 if (bConversionSuccess)
395 const Sequence<sal_uInt32>& rLeftSeq = *reinterpret_cast<const Sequence<sal_uInt32>*>(pData);
396 const Sequence<sal_uInt32>& rRightSeq = aTemp;
397 bRes = rLeftSeq.getLength() == rRightSeq.getLength() &&
398 memcmp(rLeftSeq.getConstArray(), rRightSeq.getConstArray(), rLeftSeq.getLength()*sizeof(sal_uInt32)) == 0;
401 else if (isA(_rType, static_cast< Sequence< ::rtl::OUString >* >(NULL)))
403 Sequence< ::rtl::OUString > aTemp;
404 bConversionSuccess = _rValue >>= aTemp;
405 if (bConversionSuccess)
407 const Sequence< ::rtl::OUString >& rLeftSeq = *reinterpret_cast<const Sequence< ::rtl::OUString>*>(pData);
408 const Sequence< ::rtl::OUString >& rRightSeq = aTemp;
409 sal_Int32 nSeqLen = rLeftSeq.getLength();
410 bRes = ( nSeqLen == rRightSeq.getLength() );
411 for ( sal_Int32 n = 0; bRes && ( n < nSeqLen ); n++ )
413 const ::rtl::OUString& rS1 = rLeftSeq.getConstArray()[n];
414 const ::rtl::OUString& rS2 = rRightSeq.getConstArray()[n];
415 bRes = ( rS1 == rS2 );
419 break;
420 default:
421 bRes = sal_False;
424 bRes = bRes && bConversionSuccess;
426 return bRes;
429 //------------------------------------------------------------------------------
430 sal_Bool compare(const Any& rLeft, const Any& rRight)
432 return compare_impl(rLeft.getValueType(), rLeft.getValue(), rRight);
435 //-------------------------------------------------------------------------
436 sal_Bool operator ==(const FontDescriptor& _rLeft, const FontDescriptor& _rRight)
438 return ( _rLeft.Name.equals( _rRight.Name ) ) &&
439 ( _rLeft.Height == _rRight.Height ) &&
440 ( _rLeft.Width == _rRight.Width ) &&
441 ( _rLeft.StyleName.equals( _rRight.StyleName ) ) &&
442 ( _rLeft.Family == _rRight.Family ) &&
443 ( _rLeft.CharSet == _rRight.CharSet ) &&
444 ( _rLeft.Pitch == _rRight.Pitch ) &&
445 ( _rLeft.CharacterWidth == _rRight.CharacterWidth ) &&
446 ( _rLeft.Weight == _rRight.Weight ) &&
447 ( _rLeft.Slant == _rRight.Slant ) &&
448 ( _rLeft.Underline == _rRight.Underline ) &&
449 ( _rLeft.Strikeout == _rRight.Strikeout ) &&
450 ( _rLeft.Orientation == _rRight.Orientation ) &&
451 ( _rLeft.Kerning == _rRight.Kerning ) &&
452 ( _rLeft.WordLineMode == _rRight.WordLineMode ) &&
453 ( _rLeft.Type == _rRight.Type ) ;
456 //-------------------------------------------------------------------------
457 Type getSequenceElementType(const Type& _rSequenceType)
459 OSL_ENSURE(_rSequenceType.getTypeClass() == TypeClass_SEQUENCE,
460 "getSequenceElementType: must be called with a sequence type!");
462 if (!(_rSequenceType.getTypeClass() == TypeClass_SEQUENCE))
463 return Type();
465 TypeDescription aTD(_rSequenceType);
466 typelib_IndirectTypeDescription* pSequenceTD =
467 reinterpret_cast< typelib_IndirectTypeDescription* >(aTD.get());
469 OSL_ASSERT(pSequenceTD);
470 OSL_ASSERT(pSequenceTD->pType);
472 if (pSequenceTD && pSequenceTD->pType)
473 return Type(pSequenceTD->pType);
475 return Type();
477 //.........................................................................
478 } // namespace comphelper
479 //.........................................................................