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