LCOV - code coverage report
Current view: top level - comphelper/source/misc - types.cxx (source / functions) Hit Total Coverage
Test: commit 0e63ca4fde4e446f346e35849c756a30ca294aab Lines: 141 236 59.7 %
Date: 2014-04-11 Functions: 21 30 70.0 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
       2             : /*
       3             :  * This file is part of the LibreOffice project.
       4             :  *
       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/.
       8             :  *
       9             :  * This file incorporates work covered by the following license notice:
      10             :  *
      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 .
      18             :  */
      19             : 
      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>
      30             : 
      31             : #include <memory.h>
      32             : 
      33             : 
      34             : 
      35             : namespace comphelper
      36             : {
      37             : 
      38             : 
      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;
      43             : 
      44             : 
      45           0 : bool operator ==(const DateTime& _rLeft, const DateTime& _rRight)
      46             : {
      47           0 :     return ( _rLeft.NanoSeconds == _rRight.NanoSeconds) &&
      48           0 :     ( _rLeft.Seconds == _rRight.Seconds) &&
      49           0 :     ( _rLeft.Minutes == _rRight.Minutes) &&
      50           0 :     ( _rLeft.Hours == _rRight.Hours) &&
      51           0 :     ( _rLeft.Day == _rRight.Day) &&
      52           0 :     ( _rLeft.Month == _rRight.Month) &&
      53           0 :     ( _rLeft.Year == _rRight.Year) ;
      54             : }
      55             : 
      56             : 
      57         270 : bool operator ==(const Date& _rLeft, const Date& _rRight)
      58             : {
      59         489 :     return ( _rLeft.Day == _rRight.Day) &&
      60         487 :     ( _rLeft.Month == _rRight.Month) &&
      61         487 :     ( _rLeft.Year == _rRight.Year) ;
      62             : }
      63             : 
      64             : 
      65         229 : bool operator ==(const Time& _rLeft, const Time& _rRight)
      66             : {
      67         456 :     return ( _rLeft.NanoSeconds == _rRight.NanoSeconds) &&
      68         452 :     ( _rLeft.Seconds == _rRight.Seconds) &&
      69         667 :     ( _rLeft.Minutes == _rRight.Minutes) &&
      70         442 :     ( _rLeft.Hours == _rRight.Hours) ;
      71             : }
      72             : 
      73             : 
      74           0 : sal_Int64 getINT64(const Any& _rAny)
      75             : {
      76           0 :     sal_Int64 nReturn = 0;
      77           0 :     OSL_VERIFY( _rAny >>= nReturn );
      78           0 :     return nReturn;
      79             : }
      80             : 
      81             : 
      82        8643 : sal_Int32 getINT32(const Any& _rAny)
      83             : {
      84        8643 :     sal_Int32 nReturn = 0;
      85        8643 :     OSL_VERIFY( _rAny >>= nReturn );
      86        8643 :     return nReturn;
      87             : }
      88             : 
      89             : 
      90        8386 : sal_Int16 getINT16(const Any& _rAny)
      91             : {
      92        8386 :     sal_Int16 nReturn = 0;
      93        8386 :     OSL_VERIFY( _rAny >>= nReturn );
      94        8386 :     return nReturn;
      95             : }
      96             : 
      97             : 
      98           0 : double getDouble(const Any& _rAny)
      99             : {
     100           0 :     double nReturn = 0.0;
     101           0 :     OSL_VERIFY( _rAny >>= nReturn );
     102           0 :     return nReturn;
     103             : }
     104             : 
     105             : 
     106           0 : float getFloat(const Any& _rAny)
     107             : {
     108           0 :     float nReturn = 0.0;
     109           0 :     OSL_VERIFY( _rAny >>= nReturn );
     110           0 :     return nReturn;
     111             : }
     112             : 
     113             : 
     114       45598 : OUString getString(const Any& _rAny)
     115             : {
     116       45598 :     OUString nReturn;
     117       45598 :     OSL_VERIFY( _rAny >>= nReturn );
     118       45598 :     return nReturn;
     119             : }
     120             : 
     121             : 
     122        6487 : bool getBOOL(const Any& _rAny)
     123             : {
     124        6487 :     bool nReturn = false;
     125        6487 :     if (_rAny.getValueType() == ::getCppuBooleanType())
     126        6487 :         nReturn = *(sal_Bool*)_rAny.getValue();
     127             :     else
     128             :         OSL_FAIL("comphelper::getBOOL : invalid argument !");
     129        6487 :     return nReturn;
     130             : }
     131             : 
     132             : 
     133       24457 : sal_Int32 getEnumAsINT32(const Any& _rAny) throw(IllegalArgumentException)
     134             : {
     135       24457 :     sal_Int32 nReturn = 0;
     136       24457 :     if (! ::cppu::enum2int(nReturn,_rAny) )
     137           0 :         throw IllegalArgumentException();
     138       24457 :     return nReturn;
     139             : }
     140             : 
     141             : 
     142         138 : FontDescriptor  getDefaultFont()
     143             : {
     144         138 :     FontDescriptor aReturn;
     145         138 :     aReturn.Slant = FontSlant_DONTKNOW;
     146         138 :     aReturn.Underline = FontUnderline::DONTKNOW;
     147         138 :     aReturn.Strikeout = FontStrikeout::DONTKNOW;
     148         138 :     return aReturn;
     149             : }
     150             : 
     151             : 
     152        5380 : bool isAssignableFrom(const Type& _rAssignable, const Type& _rFrom)
     153             : {
     154             :     // getthe type lib descriptions
     155        5380 :     typelib_TypeDescription* pAssignable = NULL;
     156        5380 :     _rAssignable.getDescription(&pAssignable);
     157             : 
     158        5380 :     typelib_TypeDescription* pFrom = NULL;
     159        5380 :     _rFrom.getDescription(&pFrom);
     160             : 
     161             :     // and ask the type lib
     162        5380 :     return typelib_typedescription_isAssignableFrom(pAssignable, pFrom);
     163             : }
     164             : 
     165             : 
     166             : template<class TYPE>
     167       29764 : bool tryCompare(const void* _pData, const Any& _rValue, bool& _bIdentical, TYPE& _rOut)
     168             : {
     169       29764 :     bool bSuccess = _rValue >>= _rOut;
     170       29764 :     _bIdentical = bSuccess && (_rOut == *reinterpret_cast<const TYPE*>(_pData));
     171       29764 :     return bSuccess;
     172             : }
     173             : 
     174             : 
     175           0 : bool tryCompare(const void* _pData, const Any& _rValue, bool& _bIdentical, sal_Unicode& _rOut)
     176             : {
     177           0 :     bool bSuccess = ( _rValue.getValueTypeClass() == TypeClass_CHAR );
     178           0 :     if ( bSuccess )
     179           0 :         _rOut = *static_cast< const sal_Unicode* >( _rValue.getValue() );
     180           0 :     _bIdentical = bSuccess && ( _rOut == *static_cast< const sal_Unicode* >( _pData ) );
     181           0 :     return bSuccess;
     182             : }
     183             : 
     184             : 
     185       33423 : bool compare_impl(const Type& _rType, const void* pData, const Any& _rValue)
     186             : {
     187       33423 :     bool bRes = true;
     188             : 
     189       33423 :     if (_rType.getTypeClass() == TypeClass_ANY)
     190             :     {
     191             :         // beides AnyWerte
     192           0 :         if (_rValue.getValueType().getTypeClass() == TypeClass_ANY)
     193             :             bRes = compare_impl(
     194           0 :                 reinterpret_cast<const Any*>(pData)->getValueType(),
     195             :                 reinterpret_cast<const Any*>(pData)->getValue(),
     196           0 :                 *reinterpret_cast<const Any*>(_rValue.getValue()));
     197             :         else
     198             :             bRes = compare_impl(
     199           0 :                 reinterpret_cast<const Any*>(pData)->getValueType(),
     200             :                 reinterpret_cast<const Any*>(pData)->getValue(),
     201           0 :                 _rValue);
     202             :     }
     203       66846 :     else if (   (_rType.getTypeClass() == TypeClass_VOID)
     204       33423 :             ||  (_rValue.getValueType().getTypeClass() == TypeClass_VOID)
     205             :             )
     206             :     {
     207         555 :         bRes = _rType.getTypeClass() == _rValue.getValueType().getTypeClass();
     208             :     }
     209             :     else
     210             :     {
     211       32868 :         bool bConversionSuccess = false;
     212       32868 :         switch (_rType.getTypeClass())
     213             :         {
     214             :             case TypeClass_VOID:
     215           0 :                 bConversionSuccess = true;
     216           0 :                 bRes = _rValue.getValueType().getTypeClass() == TypeClass_VOID;
     217           0 :                 break;
     218             :             case TypeClass_BOOLEAN:
     219             :             {
     220             :                 bool aDummy;
     221       10831 :                 bConversionSuccess = tryCompare(pData, _rValue, bRes, aDummy);
     222       10831 :                 break;
     223             :             }
     224             :             case TypeClass_CHAR:
     225             :             {
     226           0 :                 sal_Unicode aDummy(0);
     227           0 :                 bConversionSuccess = tryCompare(pData, _rValue, bRes, aDummy);
     228           0 :                 break;
     229             :             }
     230             :             case TypeClass_STRING:
     231             :             {
     232        7085 :                 OUString aDummy;
     233        7085 :                 bConversionSuccess = tryCompare(pData, _rValue, bRes, aDummy);
     234        7085 :                 break;
     235             :             }
     236             :             case TypeClass_FLOAT:
     237             :             {
     238             :                 float aDummy;
     239           0 :                 bConversionSuccess = tryCompare(pData, _rValue, bRes, aDummy);
     240           0 :                 break;
     241             :             }
     242             :             case TypeClass_DOUBLE:
     243             :             {
     244             :                 double aDummy;
     245         863 :                 bConversionSuccess = tryCompare(pData, _rValue, bRes, aDummy);
     246         863 :                 break;
     247             :             }
     248             :             case TypeClass_BYTE:
     249             :             {
     250             :                 sal_Int8 aDummy;
     251           0 :                 bConversionSuccess = tryCompare(pData, _rValue, bRes, aDummy);
     252           0 :                 break;
     253             :             }
     254             :             case TypeClass_SHORT:
     255             :             {
     256             :                 sal_Int16 aDummy;
     257        8578 :                 bConversionSuccess = tryCompare(pData, _rValue, bRes, aDummy);
     258        8578 :                 break;
     259             :             }
     260             :             case TypeClass_ENUM:
     261             :             {
     262           5 :                 sal_Int32 nAsInt32 = 0;
     263           5 :                 bConversionSuccess = ::cppu::enum2int(nAsInt32, _rValue);
     264           5 :                 bRes = bConversionSuccess && (nAsInt32== *reinterpret_cast<const sal_Int32*>(pData));
     265           5 :                 break;
     266             :             }
     267             :             case TypeClass_LONG:
     268             :             {
     269             :                 sal_Int32 aDummy;
     270        1456 :                 bConversionSuccess = tryCompare(pData, _rValue, bRes, aDummy);
     271        1456 :                 break;
     272             :             }
     273             :             case TypeClass_UNSIGNED_SHORT:
     274             :             {
     275             :                 sal_uInt16 aDummy;
     276           0 :                 bConversionSuccess = tryCompare(pData, _rValue, bRes, aDummy);
     277           0 :                 break;
     278             :             }
     279             :             case TypeClass_UNSIGNED_LONG:
     280             :             {
     281             :                 sal_uInt32 aDummy;
     282           0 :                 bConversionSuccess = tryCompare(pData, _rValue, bRes, aDummy);
     283           0 :                 break;
     284             :             }
     285             :             case TypeClass_INTERFACE:
     286             :             {
     287         452 :                 InterfaceRef aDummy;
     288         452 :                 bConversionSuccess = tryCompare(pData, _rValue, bRes, aDummy);
     289         452 :                 break;
     290             :             }
     291             :             case TypeClass_STRUCT:
     292        3221 :                 if (isA(_rType, static_cast<FontDescriptor*>(NULL)))
     293             :                 {
     294        2722 :                     FontDescriptor aTemp;
     295        2722 :                     bConversionSuccess = _rValue >>= aTemp;
     296        2722 :                     if (bConversionSuccess)
     297             :                     {
     298        2722 :                         bRes = *(FontDescriptor*)pData == aTemp;
     299             :                     }
     300             :                     else
     301           0 :                         bRes = false;
     302        2722 :                     break;
     303             :                 }
     304         499 :                 if (isA(_rType, static_cast<Date*>(NULL)))
     305             :                 {
     306         270 :                     Date aDummy;
     307         270 :                     bConversionSuccess = tryCompare(pData, _rValue, bRes, aDummy);
     308         270 :                     break;
     309             :                 }
     310         229 :                 if (isA(_rType, static_cast<Time*>(NULL)))
     311             :                 {
     312         229 :                     Time aDummy;
     313         229 :                     bConversionSuccess = tryCompare(pData, _rValue, bRes, aDummy);
     314         229 :                     break;
     315             :                 }
     316           0 :                 if (isA(_rType, static_cast<DateTime*>(NULL)))
     317             :                 {
     318           0 :                     DateTime aDummy;
     319           0 :                     bConversionSuccess = tryCompare(pData, _rValue, bRes, aDummy);
     320           0 :                     break;
     321             :                 }
     322           0 :                 break;
     323             :             case TypeClass_SEQUENCE:
     324         377 :                 if (isA(_rType, static_cast< Sequence<sal_Int8>* >(NULL)))
     325             :                 {
     326           0 :                     Sequence<sal_Int8> aTemp;
     327           0 :                     bConversionSuccess = _rValue >>= aTemp;
     328           0 :                     if (bConversionSuccess)
     329             :                     {
     330           0 :                         const Sequence<sal_Int8>& rLeftSeq = *reinterpret_cast<const Sequence<sal_Int8>*>(pData);
     331           0 :                         const Sequence<sal_Int8>& rRightSeq = aTemp;
     332           0 :                         bRes = rLeftSeq.getLength() == rRightSeq.getLength() &&
     333           0 :                             memcmp(rLeftSeq.getConstArray(), rRightSeq.getConstArray(), rLeftSeq.getLength()) == 0;
     334           0 :                     }
     335             :                 }
     336         377 :                 else if (isA(_rType, static_cast< Sequence<sal_uInt8>* >(NULL)))
     337             :                 {
     338           0 :                     Sequence<sal_uInt8> aTemp;
     339           0 :                     bConversionSuccess = _rValue >>= aTemp;
     340           0 :                     if (bConversionSuccess)
     341             :                     {
     342           0 :                         const Sequence<sal_uInt8>& rLeftSeq = *reinterpret_cast<const Sequence<sal_uInt8>*>(pData);
     343           0 :                         const Sequence<sal_uInt8>& rRightSeq = aTemp;
     344           0 :                         bRes = rLeftSeq.getLength() == rRightSeq.getLength() &&
     345           0 :                             memcmp(rLeftSeq.getConstArray(), rRightSeq.getConstArray(), rLeftSeq.getLength()) == 0;
     346           0 :                     }
     347             :                 }
     348         377 :                 else if (isA(_rType, static_cast< Sequence<sal_Int16>* >(NULL)))
     349             :                 {
     350         245 :                     Sequence<sal_Int16> aTemp;
     351         245 :                     bConversionSuccess = _rValue >>= aTemp;
     352         245 :                     if (bConversionSuccess)
     353             :                     {
     354         245 :                         const Sequence<sal_Int16>& rLeftSeq = *reinterpret_cast<const Sequence<sal_Int16>*>(pData);
     355         245 :                         const Sequence<sal_Int16>& rRightSeq = aTemp;
     356         430 :                         bRes = rLeftSeq.getLength() == rRightSeq.getLength() &&
     357         430 :                             memcmp(rLeftSeq.getConstArray(), rRightSeq.getConstArray(), rLeftSeq.getLength()*sizeof(sal_Int16)) == 0;
     358         245 :                     }
     359             :                 }
     360         132 :                 else if (isA(_rType, static_cast< Sequence<sal_uInt16>* >(NULL)))
     361             :                 {
     362           0 :                     Sequence<sal_uInt16> aTemp;
     363           0 :                     bConversionSuccess = _rValue >>= aTemp;
     364           0 :                     if (bConversionSuccess)
     365             :                     {
     366           0 :                         const Sequence<sal_uInt16>& rLeftSeq = *reinterpret_cast<const Sequence<sal_uInt16>*>(pData);
     367           0 :                         const Sequence<sal_uInt16>& rRightSeq = aTemp;
     368           0 :                         bRes = rLeftSeq.getLength() == rRightSeq.getLength() &&
     369           0 :                             memcmp(rLeftSeq.getConstArray(), rRightSeq.getConstArray(), rLeftSeq.getLength()*sizeof(sal_uInt16)) == 0;
     370           0 :                     }
     371             :                 }
     372         132 :                 else if (isA(_rType, static_cast< Sequence<sal_Int32>* >(NULL)))
     373             :                 {
     374           0 :                     Sequence<sal_Int32> aTemp;
     375           0 :                     bConversionSuccess = _rValue >>= aTemp;
     376           0 :                     if (bConversionSuccess)
     377             :                     {
     378           0 :                         const Sequence<sal_Int32>& rLeftSeq = *reinterpret_cast<const Sequence<sal_Int32>*>(pData);
     379           0 :                         const Sequence<sal_Int32>& rRightSeq = aTemp;
     380           0 :                         bRes = rLeftSeq.getLength() == rRightSeq.getLength() &&
     381           0 :                             memcmp(rLeftSeq.getConstArray(), rRightSeq.getConstArray(), rLeftSeq.getLength()*sizeof(sal_Int32)) == 0;
     382           0 :                     }
     383             :                 }
     384         132 :                 else if (isA(_rType, static_cast< Sequence<sal_uInt32>* >(NULL)))
     385             :                 {
     386           0 :                     Sequence<sal_uInt32> aTemp;
     387           0 :                     bConversionSuccess = _rValue >>= aTemp;
     388           0 :                     if (bConversionSuccess)
     389             :                     {
     390           0 :                         const Sequence<sal_uInt32>& rLeftSeq = *reinterpret_cast<const Sequence<sal_uInt32>*>(pData);
     391           0 :                         const Sequence<sal_uInt32>& rRightSeq = aTemp;
     392           0 :                         bRes = rLeftSeq.getLength() == rRightSeq.getLength() &&
     393           0 :                             memcmp(rLeftSeq.getConstArray(), rRightSeq.getConstArray(), rLeftSeq.getLength()*sizeof(sal_uInt32)) == 0;
     394           0 :                     }
     395             :                 }
     396         132 :                 else if (isA(_rType, static_cast< Sequence< OUString >* >(NULL)))
     397             :                 {
     398         132 :                     Sequence< OUString > aTemp;
     399         132 :                     bConversionSuccess = _rValue >>= aTemp;
     400         132 :                     if (bConversionSuccess)
     401             :                     {
     402         132 :                         const Sequence< OUString >& rLeftSeq = *reinterpret_cast<const Sequence< OUString>*>(pData);
     403         132 :                         const Sequence< OUString >& rRightSeq = aTemp;
     404         132 :                         sal_Int32 nSeqLen = rLeftSeq.getLength();
     405         132 :                         bRes = ( nSeqLen == rRightSeq.getLength() );
     406         273 :                         for ( sal_Int32 n = 0; bRes && ( n < nSeqLen ); n++ )
     407             :                         {
     408         141 :                             const OUString& rS1 = rLeftSeq.getConstArray()[n];
     409         141 :                             const OUString& rS2 = rRightSeq.getConstArray()[n];
     410         141 :                             bRes = ( rS1 == rS2 );
     411             :                         }
     412         132 :                     }
     413             :                 }
     414         377 :                 break;
     415             :             default:
     416           0 :                 bRes = false;
     417             :         }
     418             : 
     419       32868 :         bRes = bRes && bConversionSuccess;
     420             :     }
     421       33423 :     return bRes;
     422             : }
     423             : 
     424             : 
     425       33423 : bool compare(const Any& rLeft, const Any& rRight)
     426             : {
     427       33423 :     return compare_impl(rLeft.getValueType(), rLeft.getValue(), rRight);
     428             : }
     429             : 
     430             : 
     431        2768 : bool    operator ==(const FontDescriptor& _rLeft, const FontDescriptor& _rRight)
     432             : {
     433        5259 :     return ( _rLeft.Name.equals( _rRight.Name ) ) &&
     434        4913 :     ( _rLeft.Height == _rRight.Height ) &&
     435        4797 :     ( _rLeft.Width == _rRight.Width ) &&
     436        4703 :     ( _rLeft.StyleName.equals( _rRight.StyleName ) ) &&
     437        4609 :     ( _rLeft.Family == _rRight.Family ) &&
     438        4513 :     ( _rLeft.CharSet == _rRight.CharSet ) &&
     439        4417 :     ( _rLeft.Pitch == _rRight.Pitch ) &&
     440        4323 :     ( _rLeft.CharacterWidth == _rRight.CharacterWidth ) &&
     441        4227 :     ( _rLeft.Weight == _rRight.Weight ) &&
     442        3086 :     ( _rLeft.Slant == _rRight.Slant ) &&
     443        1961 :     ( _rLeft.Underline == _rRight.Underline ) &&
     444        1894 :     ( _rLeft.Strikeout == _rRight.Strikeout ) &&
     445        1828 :     ( _rLeft.Orientation == _rRight.Orientation ) &&
     446        1763 :     ( _rLeft.Kerning == _rRight.Kerning ) &&
     447        4466 :     ( _rLeft.WordLineMode == _rRight.WordLineMode ) &&
     448        3601 :     ( _rLeft.Type == _rRight.Type ) ;
     449             : }
     450             : 
     451             : 
     452           2 : Type getSequenceElementType(const Type& _rSequenceType)
     453             : {
     454             :     OSL_ENSURE(_rSequenceType.getTypeClass() == TypeClass_SEQUENCE,
     455             :                 "getSequenceElementType: must be called with a  sequence type!");
     456             : 
     457           2 :     if (!(_rSequenceType.getTypeClass() == TypeClass_SEQUENCE))
     458           0 :         return Type();
     459             : 
     460           2 :     TypeDescription aTD(_rSequenceType);
     461             :     typelib_IndirectTypeDescription* pSequenceTD =
     462           2 :         reinterpret_cast< typelib_IndirectTypeDescription* >(aTD.get());
     463             : 
     464             :     OSL_ASSERT(pSequenceTD);
     465             :     OSL_ASSERT(pSequenceTD->pType);
     466             : 
     467           2 :     if (pSequenceTD && pSequenceTD->pType)
     468           2 :         return Type(pSequenceTD->pType);
     469             : 
     470           0 :     return Type();
     471             : }
     472             : 
     473             : }   // namespace comphelper
     474             : 
     475             : 
     476             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10