1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
20 #include <comphelper/types.hxx>
21 #include <comphelper/extract.hxx>
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 #include <com/sun/star/awt/FontUnderline.hpp>
26 #include <com/sun/star/awt/FontStrikeout.hpp>
27 #include <com/sun/star/awt/FontDescriptor.hpp>
28 #include <osl/diagnose.h>
29 #include <typelib/typedescription.hxx>
39 using namespace ::com::sun::star::uno
;
40 using namespace ::com::sun::star::awt
;
41 using namespace ::com::sun::star::util
;
42 using namespace ::com::sun::star::lang
;
45 bool operator ==(const DateTime
& _rLeft
, const DateTime
& _rRight
)
47 return ( _rLeft
.NanoSeconds
== _rRight
.NanoSeconds
) &&
48 ( _rLeft
.Seconds
== _rRight
.Seconds
) &&
49 ( _rLeft
.Minutes
== _rRight
.Minutes
) &&
50 ( _rLeft
.Hours
== _rRight
.Hours
) &&
51 ( _rLeft
.Day
== _rRight
.Day
) &&
52 ( _rLeft
.Month
== _rRight
.Month
) &&
53 ( _rLeft
.Year
== _rRight
.Year
) ;
57 bool operator ==(const Date
& _rLeft
, const Date
& _rRight
)
59 return ( _rLeft
.Day
== _rRight
.Day
) &&
60 ( _rLeft
.Month
== _rRight
.Month
) &&
61 ( _rLeft
.Year
== _rRight
.Year
) ;
65 bool operator ==(const Time
& _rLeft
, const Time
& _rRight
)
67 return ( _rLeft
.NanoSeconds
== _rRight
.NanoSeconds
) &&
68 ( _rLeft
.Seconds
== _rRight
.Seconds
) &&
69 ( _rLeft
.Minutes
== _rRight
.Minutes
) &&
70 ( _rLeft
.Hours
== _rRight
.Hours
) ;
74 sal_Int64
getINT64(const Any
& _rAny
)
76 sal_Int64 nReturn
= 0;
77 OSL_VERIFY( _rAny
>>= nReturn
);
82 sal_Int32
getINT32(const Any
& _rAny
)
84 sal_Int32 nReturn
= 0;
85 OSL_VERIFY( _rAny
>>= nReturn
);
90 sal_Int16
getINT16(const Any
& _rAny
)
92 sal_Int16 nReturn
= 0;
93 OSL_VERIFY( _rAny
>>= nReturn
);
98 double getDouble(const Any
& _rAny
)
100 double nReturn
= 0.0;
101 OSL_VERIFY( _rAny
>>= nReturn
);
106 float getFloat(const Any
& _rAny
)
109 OSL_VERIFY( _rAny
>>= nReturn
);
114 OUString
getString(const Any
& _rAny
)
117 OSL_VERIFY( _rAny
>>= nReturn
);
122 bool getBOOL(const Any
& _rAny
)
124 bool nReturn
= false;
125 if (_rAny
.getValueType() == cppu::UnoType
<bool>::get())
126 nReturn
= *static_cast<sal_Bool
const *>(_rAny
.getValue());
128 OSL_FAIL("comphelper::getBOOL : invalid argument !");
133 sal_Int32
getEnumAsINT32(const Any
& _rAny
) throw(IllegalArgumentException
)
135 sal_Int32 nReturn
= 0;
136 if (! ::cppu::enum2int(nReturn
,_rAny
) )
137 throw IllegalArgumentException();
142 FontDescriptor
getDefaultFont()
144 FontDescriptor aReturn
;
145 aReturn
.Slant
= FontSlant_DONTKNOW
;
146 aReturn
.Underline
= FontUnderline::DONTKNOW
;
147 aReturn
.Strikeout
= FontStrikeout::DONTKNOW
;
152 bool isAssignableFrom(const Type
& _rAssignable
, const Type
& _rFrom
)
154 // getthe type lib descriptions
155 typelib_TypeDescription
* pAssignable
= NULL
;
156 _rAssignable
.getDescription(&pAssignable
);
158 typelib_TypeDescription
* pFrom
= NULL
;
159 _rFrom
.getDescription(&pFrom
);
161 // and ask the type lib
162 return typelib_typedescription_isAssignableFrom(pAssignable
, pFrom
);
167 bool tryCompare(const void* _pData
, const Any
& _rValue
, bool& _bIdentical
, TYPE
& _rOut
)
169 bool bSuccess
= _rValue
>>= _rOut
;
170 _bIdentical
= bSuccess
&& (_rOut
== *static_cast<const TYPE
*>(_pData
));
175 bool tryCompare(const void* _pData
, const Any
& _rValue
, bool& _bIdentical
, sal_Unicode
& _rOut
)
177 bool bSuccess
= ( _rValue
.getValueTypeClass() == TypeClass_CHAR
);
179 _rOut
= *static_cast< const sal_Unicode
* >( _rValue
.getValue() );
180 _bIdentical
= bSuccess
&& ( _rOut
== *static_cast< const sal_Unicode
* >( _pData
) );
185 bool compare_impl(const Type
& _rType
, const void* pData
, const Any
& _rValue
)
189 if (_rType
.getTypeClass() == TypeClass_ANY
)
192 if (_rValue
.getValueType().getTypeClass() == TypeClass_ANY
)
194 static_cast<const Any
*>(pData
)->getValueType(),
195 static_cast<const Any
*>(pData
)->getValue(),
196 *static_cast<const Any
*>(_rValue
.getValue()));
199 static_cast<const Any
*>(pData
)->getValueType(),
200 static_cast<const Any
*>(pData
)->getValue(),
203 else if ( (_rType
.getTypeClass() == TypeClass_VOID
)
204 || (_rValue
.getValueType().getTypeClass() == TypeClass_VOID
)
207 bRes
= _rType
.getTypeClass() == _rValue
.getValueType().getTypeClass();
211 bool bConversionSuccess
= false;
212 switch (_rType
.getTypeClass())
215 bConversionSuccess
= true;
216 bRes
= _rValue
.getValueType().getTypeClass() == TypeClass_VOID
;
218 case TypeClass_BOOLEAN
:
221 bConversionSuccess
= tryCompare(pData
, _rValue
, bRes
, aDummy
);
226 sal_Unicode
aDummy(0);
227 bConversionSuccess
= tryCompare(pData
, _rValue
, bRes
, aDummy
);
230 case TypeClass_STRING
:
233 bConversionSuccess
= tryCompare(pData
, _rValue
, bRes
, aDummy
);
236 case TypeClass_FLOAT
:
239 bConversionSuccess
= tryCompare(pData
, _rValue
, bRes
, aDummy
);
242 case TypeClass_DOUBLE
:
245 bConversionSuccess
= tryCompare(pData
, _rValue
, bRes
, aDummy
);
251 bConversionSuccess
= tryCompare(pData
, _rValue
, bRes
, aDummy
);
254 case TypeClass_SHORT
:
257 bConversionSuccess
= tryCompare(pData
, _rValue
, bRes
, aDummy
);
262 sal_Int32 nAsInt32
= 0;
263 bConversionSuccess
= ::cppu::enum2int(nAsInt32
, _rValue
);
264 bRes
= bConversionSuccess
&& (nAsInt32
== *static_cast<const sal_Int32
*>(pData
));
270 bConversionSuccess
= tryCompare(pData
, _rValue
, bRes
, aDummy
);
273 case TypeClass_UNSIGNED_SHORT
:
276 bConversionSuccess
= tryCompare(pData
, _rValue
, bRes
, aDummy
);
279 case TypeClass_UNSIGNED_LONG
:
282 bConversionSuccess
= tryCompare(pData
, _rValue
, bRes
, aDummy
);
285 case TypeClass_INTERFACE
:
288 bConversionSuccess
= tryCompare(pData
, _rValue
, bRes
, aDummy
);
291 case TypeClass_STRUCT
:
292 if (isA(_rType
, static_cast<FontDescriptor
*>(NULL
)))
294 FontDescriptor aTemp
;
295 bConversionSuccess
= _rValue
>>= aTemp
;
296 if (bConversionSuccess
)
298 bRes
= *static_cast<FontDescriptor
const *>(pData
) == aTemp
;
304 if (isA(_rType
, static_cast<Date
*>(NULL
)))
307 bConversionSuccess
= tryCompare(pData
, _rValue
, bRes
, aDummy
);
310 if (isA(_rType
, static_cast<Time
*>(NULL
)))
313 bConversionSuccess
= tryCompare(pData
, _rValue
, bRes
, aDummy
);
316 if (isA(_rType
, static_cast<DateTime
*>(NULL
)))
319 bConversionSuccess
= tryCompare(pData
, _rValue
, bRes
, aDummy
);
323 case TypeClass_SEQUENCE
:
324 if (isA(_rType
, static_cast< Sequence
<sal_Int8
>* >(NULL
)))
326 Sequence
<sal_Int8
> aTemp
;
327 bConversionSuccess
= _rValue
>>= aTemp
;
328 if (bConversionSuccess
)
330 const Sequence
<sal_Int8
>& rLeftSeq
= *static_cast<const Sequence
<sal_Int8
>*>(pData
);
331 const Sequence
<sal_Int8
>& rRightSeq
= aTemp
;
332 bRes
= rLeftSeq
.getLength() == rRightSeq
.getLength() &&
333 memcmp(rLeftSeq
.getConstArray(), rRightSeq
.getConstArray(), rLeftSeq
.getLength()) == 0;
336 else if (isA(_rType
, static_cast< Sequence
<sal_uInt8
>* >(NULL
)))
338 Sequence
<sal_uInt8
> aTemp
;
339 bConversionSuccess
= _rValue
>>= aTemp
;
340 if (bConversionSuccess
)
342 const Sequence
<sal_uInt8
>& rLeftSeq
= *static_cast<const Sequence
<sal_uInt8
>*>(pData
);
343 const Sequence
<sal_uInt8
>& rRightSeq
= aTemp
;
344 bRes
= rLeftSeq
.getLength() == rRightSeq
.getLength() &&
345 memcmp(rLeftSeq
.getConstArray(), rRightSeq
.getConstArray(), rLeftSeq
.getLength()) == 0;
348 else if (isA(_rType
, static_cast< Sequence
<sal_Int16
>* >(NULL
)))
350 Sequence
<sal_Int16
> aTemp
;
351 bConversionSuccess
= _rValue
>>= aTemp
;
352 if (bConversionSuccess
)
354 const Sequence
<sal_Int16
>& rLeftSeq
= *static_cast<const Sequence
<sal_Int16
>*>(pData
);
355 const Sequence
<sal_Int16
>& rRightSeq
= aTemp
;
356 bRes
= rLeftSeq
.getLength() == rRightSeq
.getLength() &&
357 memcmp(rLeftSeq
.getConstArray(), rRightSeq
.getConstArray(), rLeftSeq
.getLength()*sizeof(sal_Int16
)) == 0;
360 else if (isA(_rType
, static_cast< Sequence
<sal_uInt16
>* >(NULL
)))
362 Sequence
<sal_uInt16
> aTemp
;
363 bConversionSuccess
= _rValue
>>= aTemp
;
364 if (bConversionSuccess
)
366 const Sequence
<sal_uInt16
>& rLeftSeq
= *static_cast<const Sequence
<sal_uInt16
>*>(pData
);
367 const Sequence
<sal_uInt16
>& rRightSeq
= aTemp
;
368 bRes
= rLeftSeq
.getLength() == rRightSeq
.getLength() &&
369 memcmp(rLeftSeq
.getConstArray(), rRightSeq
.getConstArray(), rLeftSeq
.getLength()*sizeof(sal_uInt16
)) == 0;
372 else if (isA(_rType
, static_cast< Sequence
<sal_Int32
>* >(NULL
)))
374 Sequence
<sal_Int32
> aTemp
;
375 bConversionSuccess
= _rValue
>>= aTemp
;
376 if (bConversionSuccess
)
378 const Sequence
<sal_Int32
>& rLeftSeq
= *static_cast<const Sequence
<sal_Int32
>*>(pData
);
379 const Sequence
<sal_Int32
>& rRightSeq
= aTemp
;
380 bRes
= rLeftSeq
.getLength() == rRightSeq
.getLength() &&
381 memcmp(rLeftSeq
.getConstArray(), rRightSeq
.getConstArray(), rLeftSeq
.getLength()*sizeof(sal_Int32
)) == 0;
384 else if (isA(_rType
, static_cast< Sequence
<sal_uInt32
>* >(NULL
)))
386 Sequence
<sal_uInt32
> aTemp
;
387 bConversionSuccess
= _rValue
>>= aTemp
;
388 if (bConversionSuccess
)
390 const Sequence
<sal_uInt32
>& rLeftSeq
= *static_cast<const Sequence
<sal_uInt32
>*>(pData
);
391 const Sequence
<sal_uInt32
>& rRightSeq
= aTemp
;
392 bRes
= rLeftSeq
.getLength() == rRightSeq
.getLength() &&
393 memcmp(rLeftSeq
.getConstArray(), rRightSeq
.getConstArray(), rLeftSeq
.getLength()*sizeof(sal_uInt32
)) == 0;
396 else if (isA(_rType
, static_cast< Sequence
< OUString
>* >(NULL
)))
398 Sequence
< OUString
> aTemp
;
399 bConversionSuccess
= _rValue
>>= aTemp
;
400 if (bConversionSuccess
)
402 const Sequence
< OUString
>& rLeftSeq
= *static_cast<const Sequence
< OUString
>*>(pData
);
403 const Sequence
< OUString
>& rRightSeq
= aTemp
;
404 sal_Int32 nSeqLen
= rLeftSeq
.getLength();
405 bRes
= ( nSeqLen
== rRightSeq
.getLength() );
406 for ( sal_Int32 n
= 0; bRes
&& ( n
< nSeqLen
); n
++ )
408 const OUString
& rS1
= rLeftSeq
.getConstArray()[n
];
409 const OUString
& rS2
= rRightSeq
.getConstArray()[n
];
410 bRes
= ( rS1
== rS2
);
419 bRes
= bRes
&& bConversionSuccess
;
425 bool compare(const Any
& rLeft
, const Any
& rRight
)
427 return compare_impl(rLeft
.getValueType(), rLeft
.getValue(), rRight
);
431 bool operator ==(const FontDescriptor
& _rLeft
, const FontDescriptor
& _rRight
)
433 return ( _rLeft
.Name
.equals( _rRight
.Name
) ) &&
434 ( _rLeft
.Height
== _rRight
.Height
) &&
435 ( _rLeft
.Width
== _rRight
.Width
) &&
436 ( _rLeft
.StyleName
.equals( _rRight
.StyleName
) ) &&
437 ( _rLeft
.Family
== _rRight
.Family
) &&
438 ( _rLeft
.CharSet
== _rRight
.CharSet
) &&
439 ( _rLeft
.Pitch
== _rRight
.Pitch
) &&
440 ( _rLeft
.CharacterWidth
== _rRight
.CharacterWidth
) &&
441 ( _rLeft
.Weight
== _rRight
.Weight
) &&
442 ( _rLeft
.Slant
== _rRight
.Slant
) &&
443 ( _rLeft
.Underline
== _rRight
.Underline
) &&
444 ( _rLeft
.Strikeout
== _rRight
.Strikeout
) &&
445 ( _rLeft
.Orientation
== _rRight
.Orientation
) &&
446 ( _rLeft
.Kerning
== _rRight
.Kerning
) &&
447 ( _rLeft
.WordLineMode
== _rRight
.WordLineMode
) &&
448 ( _rLeft
.Type
== _rRight
.Type
) ;
452 Type
getSequenceElementType(const Type
& _rSequenceType
)
454 OSL_ENSURE(_rSequenceType
.getTypeClass() == TypeClass_SEQUENCE
,
455 "getSequenceElementType: must be called with a sequence type!");
457 if (!(_rSequenceType
.getTypeClass() == TypeClass_SEQUENCE
))
460 TypeDescription
aTD(_rSequenceType
);
461 typelib_IndirectTypeDescription
* pSequenceTD
=
462 reinterpret_cast< typelib_IndirectTypeDescription
* >(aTD
.get());
464 OSL_ASSERT(pSequenceTD
);
465 OSL_ASSERT(pSequenceTD
->pType
);
467 if (pSequenceTD
&& pSequenceTD
->pType
)
468 return Type(pSequenceTD
->pType
);
473 } // namespace comphelper
476 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */