CWS-TOOLING: integrate CWS os146
[LibreOffice.git] / comphelper / source / misc / types.cxx
blob31719eec881a8784be857c5627994f1b9d271284
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2000, 2010 Oracle and/or its affiliates.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * This file is part of OpenOffice.org.
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.
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).
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.
26 ************************************************************************/
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>
41 #include <memory.h>
44 //.........................................................................
45 namespace comphelper
47 //.........................................................................
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;
54 //-------------------------------------------------------------------------
55 sal_Bool operator ==(const DateTime& _rLeft, const DateTime& _rRight)
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) ;
66 //-------------------------------------------------------------------------
67 sal_Bool operator ==(const Date& _rLeft, const Date& _rRight)
69 return ( _rLeft.Day == _rRight.Day) &&
70 ( _rLeft.Month == _rRight.Month) &&
71 ( _rLeft.Year == _rRight.Year) ;
74 //-------------------------------------------------------------------------
75 sal_Bool operator ==(const Time& _rLeft, const Time& _rRight)
77 return ( _rLeft.HundredthSeconds == _rRight.HundredthSeconds) &&
78 ( _rLeft.Seconds == _rRight.Seconds) &&
79 ( _rLeft.Minutes == _rRight.Minutes) &&
80 ( _rLeft.Hours == _rRight.Hours) ;
83 //------------------------------------------------------------------------------
84 sal_Int32 getINT32(const Any& _rAny)
86 sal_Int32 nReturn = 0;
87 OSL_VERIFY( _rAny >>= nReturn );
88 return nReturn;
91 //------------------------------------------------------------------------------
92 sal_Int16 getINT16(const Any& _rAny)
94 sal_Int16 nReturn = 0;
95 OSL_VERIFY( _rAny >>= nReturn );
96 return nReturn;
99 //------------------------------------------------------------------------------
100 double getDouble(const Any& _rAny)
102 double nReturn = 0.0;
103 OSL_VERIFY( _rAny >>= nReturn );
104 return nReturn;
107 //------------------------------------------------------------------------------
108 float getFloat(const Any& _rAny)
110 float nReturn = 0.0;
111 OSL_VERIFY( _rAny >>= nReturn );
112 return nReturn;
115 //------------------------------------------------------------------------------
116 ::rtl::OUString getString(const Any& _rAny)
118 ::rtl::OUString nReturn;
119 OSL_VERIFY( _rAny >>= nReturn );
120 return nReturn;
123 //------------------------------------------------------------------------------
124 sal_Bool getBOOL(const Any& _rAny)
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;
134 //------------------------------------------------------------------------------
135 sal_Int32 getEnumAsINT32(const Any& _rAny) throw(IllegalArgumentException)
137 sal_Int32 nReturn = 0;
138 if (! ::cppu::enum2int(nReturn,_rAny) )
139 throw IllegalArgumentException();
140 return nReturn;
143 //------------------------------------------------------------------------------
144 FontDescriptor getDefaultFont()
146 FontDescriptor aReturn;
147 aReturn.Slant = FontSlant_DONTKNOW;
148 aReturn.Underline = FontUnderline::DONTKNOW;
149 aReturn.Strikeout = FontStrikeout::DONTKNOW;
150 return aReturn;
153 //------------------------------------------------------------------------------
154 sal_Bool isAssignableFrom(const Type& _rAssignable, const Type& _rFrom)
156 // getthe type lib descriptions
157 typelib_TypeDescription* pAssignable = NULL;
158 _rAssignable.getDescription(&pAssignable);
160 typelib_TypeDescription* pFrom = NULL;
161 _rFrom.getDescription(&pFrom);
163 // and ask the type lib
164 return typelib_typedescription_isAssignableFrom(pAssignable, pFrom);
167 //------------------------------------------------------------------
168 template<class TYPE>
169 sal_Bool tryCompare(const void* _pData, const Any& _rValue, sal_Bool& _bIdentical, TYPE& _rOut)
171 sal_Bool bSuccess = _rValue >>= _rOut;
172 _bIdentical = bSuccess && (_rOut == *reinterpret_cast<const TYPE*>(_pData));
173 return bSuccess;
176 //------------------------------------------------------------------
177 sal_Bool tryCompare(const void* _pData, const Any& _rValue, sal_Bool& _bIdentical, sal_Unicode& _rOut)
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;
186 //------------------------------------------------------------------
187 sal_Bool compare_impl(const Type& _rType, const void* pData, const Any& _rValue)
189 sal_Bool bRes = sal_True;
191 if (_rType.getTypeClass() == TypeClass_ANY)
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);
205 else if ( (_rType.getTypeClass() == TypeClass_VOID)
206 || (_rValue.getValueType().getTypeClass() == TypeClass_VOID)
209 bRes = _rType.getTypeClass() == _rValue.getValueType().getTypeClass();
211 else
213 sal_Bool bConversionSuccess = sal_False;
214 switch (_rType.getTypeClass())
216 case TypeClass_VOID:
217 bConversionSuccess = sal_True;
218 bRes = _rValue.getValueType().getTypeClass() == TypeClass_VOID;
219 break;
220 case TypeClass_BOOLEAN:
222 sal_Bool aDummy;
223 bConversionSuccess = tryCompare(pData, _rValue, bRes, aDummy);
224 break;
226 case TypeClass_CHAR:
228 sal_Unicode aDummy(0);
229 bConversionSuccess = tryCompare(pData, _rValue, bRes, aDummy);
230 break;
232 case TypeClass_STRING:
234 ::rtl::OUString aDummy;
235 bConversionSuccess = tryCompare(pData, _rValue, bRes, aDummy);
236 break;
238 case TypeClass_FLOAT:
240 float aDummy;
241 bConversionSuccess = tryCompare(pData, _rValue, bRes, aDummy);
242 break;
244 case TypeClass_DOUBLE:
246 double aDummy;
247 bConversionSuccess = tryCompare(pData, _rValue, bRes, aDummy);
248 break;
250 case TypeClass_BYTE:
252 sal_Int8 aDummy;
253 bConversionSuccess = tryCompare(pData, _rValue, bRes, aDummy);
254 break;
256 case TypeClass_SHORT:
258 sal_Int16 aDummy;
259 bConversionSuccess = tryCompare(pData, _rValue, bRes, aDummy);
260 break;
262 case TypeClass_ENUM:
264 sal_Int32 nAsInt32 = 0;
265 bConversionSuccess = ::cppu::enum2int(nAsInt32, _rValue);
266 bRes = bConversionSuccess && (nAsInt32== *reinterpret_cast<const sal_Int32*>(pData));
267 break;
269 case TypeClass_LONG:
271 sal_Int32 aDummy;
272 bConversionSuccess = tryCompare(pData, _rValue, bRes, aDummy);
273 break;
275 case TypeClass_UNSIGNED_SHORT:
277 sal_uInt16 aDummy;
278 bConversionSuccess = tryCompare(pData, _rValue, bRes, aDummy);
279 break;
281 case TypeClass_UNSIGNED_LONG:
283 sal_uInt32 aDummy;
284 bConversionSuccess = tryCompare(pData, _rValue, bRes, aDummy);
285 break;
287 case TypeClass_INTERFACE:
289 InterfaceRef aDummy;
290 bConversionSuccess = tryCompare(pData, _rValue, bRes, aDummy);
291 break;
293 case TypeClass_STRUCT:
294 if (isA(_rType, static_cast<FontDescriptor*>(NULL)))
296 FontDescriptor aTemp;
297 bConversionSuccess = _rValue >>= aTemp;
298 if (bConversionSuccess)
300 bRes = *(FontDescriptor*)pData == aTemp;
302 else
303 bRes = sal_False;
304 break;
306 if (isA(_rType, static_cast<Date*>(NULL)))
308 Date aDummy;
309 bConversionSuccess = tryCompare(pData, _rValue, bRes, aDummy);
310 break;
312 if (isA(_rType, static_cast<Time*>(NULL)))
314 Time aDummy;
315 bConversionSuccess = tryCompare(pData, _rValue, bRes, aDummy);
316 break;
318 if (isA(_rType, static_cast<DateTime*>(NULL)))
320 DateTime aDummy;
321 bConversionSuccess = tryCompare(pData, _rValue, bRes, aDummy);
322 break;
324 break;
325 case TypeClass_SEQUENCE:
326 if (isA(_rType, static_cast< Sequence<sal_Int8>* >(NULL)))
328 Sequence<sal_Int8> aTemp;
329 bConversionSuccess = _rValue >>= aTemp;
330 if (bConversionSuccess)
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;
338 else if (isA(_rType, static_cast< Sequence<sal_uInt8>* >(NULL)))
340 Sequence<sal_uInt8> aTemp;
341 bConversionSuccess = _rValue >>= aTemp;
342 if (bConversionSuccess)
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;
350 else if (isA(_rType, static_cast< Sequence<sal_Int16>* >(NULL)))
352 Sequence<sal_Int16> aTemp;
353 bConversionSuccess = _rValue >>= aTemp;
354 if (bConversionSuccess)
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;
362 else if (isA(_rType, static_cast< Sequence<sal_uInt16>* >(NULL)))
364 Sequence<sal_uInt16> aTemp;
365 bConversionSuccess = _rValue >>= aTemp;
366 if (bConversionSuccess)
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;
374 else if (isA(_rType, static_cast< Sequence<sal_Int32>* >(NULL)))
376 Sequence<sal_Int32> aTemp;
377 bConversionSuccess = _rValue >>= aTemp;
378 if (bConversionSuccess)
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;
386 else if (isA(_rType, static_cast< Sequence<sal_uInt32>* >(NULL)))
388 Sequence<sal_uInt32> aTemp;
389 bConversionSuccess = _rValue >>= aTemp;
390 if (bConversionSuccess)
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;
398 else if (isA(_rType, static_cast< Sequence< ::rtl::OUString >* >(NULL)))
400 Sequence< ::rtl::OUString > aTemp;
401 bConversionSuccess = _rValue >>= aTemp;
402 if (bConversionSuccess)
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++ )
410 const ::rtl::OUString& rS1 = rLeftSeq.getConstArray()[n];
411 const ::rtl::OUString& rS2 = rRightSeq.getConstArray()[n];
412 bRes = ( rS1 == rS2 );
416 break;
417 default:
418 bRes = sal_False;
421 bRes = bRes && bConversionSuccess;
423 return bRes;
426 //------------------------------------------------------------------------------
427 sal_Bool compare(const Any& rLeft, const Any& rRight)
429 return compare_impl(rLeft.getValueType(), rLeft.getValue(), rRight);
432 //-------------------------------------------------------------------------
433 sal_Bool operator ==(const FontDescriptor& _rLeft, const FontDescriptor& _rRight)
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 ) ;
453 //-------------------------------------------------------------------------
454 Type getSequenceElementType(const Type& _rSequenceType)
456 OSL_ENSURE(_rSequenceType.getTypeClass() == TypeClass_SEQUENCE,
457 "getSequenceElementType: must be called with a sequence type!");
459 if (!(_rSequenceType.getTypeClass() == TypeClass_SEQUENCE))
460 return Type();
462 TypeDescription aTD(_rSequenceType);
463 typelib_IndirectTypeDescription* pSequenceTD =
464 reinterpret_cast< typelib_IndirectTypeDescription* >(aTD.get());
466 OSL_ASSERT(pSequenceTD);
467 OSL_ASSERT(pSequenceTD->pType);
469 if (pSequenceTD && pSequenceTD->pType)
470 return Type(pSequenceTD->pType);
472 return Type();
474 //.........................................................................
475 } // namespace comphelper
476 //.........................................................................