1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: types.cxx,v $
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>
47 //.........................................................................
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
);
94 //------------------------------------------------------------------------------
95 sal_Int16
getINT16(const Any
& _rAny
)
97 sal_Int16 nReturn
= 0;
98 OSL_VERIFY( _rAny
>>= nReturn
);
102 //------------------------------------------------------------------------------
103 double getDouble(const Any
& _rAny
)
105 double nReturn
= 0.0;
106 OSL_VERIFY( _rAny
>>= nReturn
);
110 //------------------------------------------------------------------------------
111 float getFloat(const Any
& _rAny
)
114 OSL_VERIFY( _rAny
>>= nReturn
);
118 //------------------------------------------------------------------------------
119 ::rtl::OUString
getString(const Any
& _rAny
)
121 ::rtl::OUString nReturn
;
122 OSL_VERIFY( _rAny
>>= 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();
133 OSL_ENSURE(sal_False
, "comphelper::getBOOL : invalid argument !");
137 //------------------------------------------------------------------------------
138 sal_Int32
getEnumAsINT32(const Any
& _rAny
) throw(IllegalArgumentException
)
140 sal_Int32 nReturn
= 0;
141 if (! ::cppu::enum2int(nReturn
,_rAny
) )
142 throw IllegalArgumentException();
146 //------------------------------------------------------------------------------
147 FontDescriptor
getDefaultFont()
149 FontDescriptor aReturn
;
150 aReturn
.Slant
= FontSlant_DONTKNOW
;
151 aReturn
.Underline
= FontUnderline::DONTKNOW
;
152 aReturn
.Strikeout
= FontStrikeout::DONTKNOW
;
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 //------------------------------------------------------------------
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
));
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
);
184 _rOut
= *static_cast< const sal_Unicode
* >( _rValue
.getValue() );
185 _bIdentical
= bSuccess
&& ( _rOut
== *static_cast< const sal_Unicode
* >( _pData
) );
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
)
197 if (_rValue
.getValueType().getTypeClass() == TypeClass_ANY
)
199 reinterpret_cast<const Any
*>(pData
)->getValueType(),
200 reinterpret_cast<const Any
*>(pData
)->getValue(),
201 *reinterpret_cast<const Any
*>(_rValue
.getValue()));
204 reinterpret_cast<const Any
*>(pData
)->getValueType(),
205 reinterpret_cast<const Any
*>(pData
)->getValue(),
208 else if ( (_rType
.getTypeClass() == TypeClass_VOID
)
209 || (_rValue
.getValueType().getTypeClass() == TypeClass_VOID
)
212 bRes
= _rType
.getTypeClass() == _rValue
.getValueType().getTypeClass();
216 sal_Bool bConversionSuccess
= sal_False
;
217 switch (_rType
.getTypeClass())
220 bConversionSuccess
= sal_True
;
221 bRes
= _rValue
.getValueType().getTypeClass() == TypeClass_VOID
;
223 case TypeClass_BOOLEAN
:
226 bConversionSuccess
= tryCompare(pData
, _rValue
, bRes
, aDummy
);
231 sal_Unicode
aDummy(0);
232 bConversionSuccess
= tryCompare(pData
, _rValue
, bRes
, aDummy
);
235 case TypeClass_STRING
:
237 ::rtl::OUString aDummy
;
238 bConversionSuccess
= tryCompare(pData
, _rValue
, bRes
, aDummy
);
241 case TypeClass_FLOAT
:
244 bConversionSuccess
= tryCompare(pData
, _rValue
, bRes
, aDummy
);
247 case TypeClass_DOUBLE
:
250 bConversionSuccess
= tryCompare(pData
, _rValue
, bRes
, aDummy
);
256 bConversionSuccess
= tryCompare(pData
, _rValue
, bRes
, aDummy
);
259 case TypeClass_SHORT
:
262 bConversionSuccess
= tryCompare(pData
, _rValue
, bRes
, aDummy
);
267 sal_Int32 nAsInt32
= 0;
268 bConversionSuccess
= ::cppu::enum2int(nAsInt32
, _rValue
);
269 bRes
= bConversionSuccess
&& (nAsInt32
== *reinterpret_cast<const sal_Int32
*>(pData
));
275 bConversionSuccess
= tryCompare(pData
, _rValue
, bRes
, aDummy
);
278 case TypeClass_UNSIGNED_SHORT
:
281 bConversionSuccess
= tryCompare(pData
, _rValue
, bRes
, aDummy
);
284 case TypeClass_UNSIGNED_LONG
:
287 bConversionSuccess
= tryCompare(pData
, _rValue
, bRes
, aDummy
);
290 case TypeClass_INTERFACE
:
293 bConversionSuccess
= tryCompare(pData
, _rValue
, bRes
, aDummy
);
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
;
309 if (isA(_rType
, static_cast<Date
*>(NULL
)))
312 bConversionSuccess
= tryCompare(pData
, _rValue
, bRes
, aDummy
);
315 if (isA(_rType
, static_cast<Time
*>(NULL
)))
318 bConversionSuccess
= tryCompare(pData
, _rValue
, bRes
, aDummy
);
321 if (isA(_rType
, static_cast<DateTime
*>(NULL
)))
324 bConversionSuccess
= tryCompare(pData
, _rValue
, bRes
, aDummy
);
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
);
424 bRes
= bRes
&& bConversionSuccess
;
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
))
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
);
477 //.........................................................................
478 } // namespace comphelper
479 //.........................................................................