LCOV - code coverage report
Current view: top level - solver/unxlngi6.pro/inc/svl - ondemand.hxx (source / functions) Hit Total Coverage
Test: libreoffice_filtered.info Lines: 96 111 86.5 %
Date: 2012-08-25 Functions: 20 25 80.0 %
Legend: Lines: hit not hit | Branches: + taken - not taken # not executed Branches: 40 67 59.7 %

           Branch data     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                 :            : #ifndef INCLUDED_SVTOOLS_ONDEMAND_HXX
      21                 :            : #define INCLUDED_SVTOOLS_ONDEMAND_HXX
      22                 :            : 
      23                 :            : #include <unotools/syslocale.hxx>
      24                 :            : #include <i18npool/lang.h>
      25                 :            : #include <unotools/localedatawrapper.hxx>
      26                 :            : #include <unotools/calendarwrapper.hxx>
      27                 :            : #include <unotools/collatorwrapper.hxx>
      28                 :            : #include <com/sun/star/i18n/CollatorOptions.hpp>
      29                 :            : #include <unotools/transliterationwrapper.hxx>
      30                 :            : #include <com/sun/star/i18n/TransliterationModules.hpp>
      31                 :            : #include <unotools/nativenumberwrapper.hxx>
      32                 :            : #include <com/sun/star/uno/Reference.hxx>
      33                 :            : #include <com/sun/star/lang/XMultiServiceFactory.hpp>
      34                 :            : 
      35                 :            : /*
      36                 :            :     On demand instanciation and initialization of several i18n wrappers,
      37                 :            :     helping the number formatter to not perform worse than it already does.
      38                 :            :  */
      39                 :            : 
      40                 :            : /** @short
      41                 :            :     Switch between LANGUAGE_SYSTEM and LANGUAGE_ENGLISH_US and any other
      42                 :            :     LocaleDataWrapper.
      43                 :            :     SvNumberformatter uses it upon switching locales.
      44                 :            : 
      45                 :            :     @descr
      46                 :            :     Avoids reloading and analysing of locale data again and again.
      47                 :            : 
      48                 :            :     @ATTENTION
      49                 :            :     If the default ctor is used the init() method MUST be called before
      50                 :            :     accessing any locale data. The passed parameters Locale and LanguageType
      51                 :            :     must match each other.
      52                 :            :  */
      53                 :            : 
      54                 :            : class OnDemandLocaleDataWrapper
      55                 :            : {
      56                 :            :         ::com::sun::star::uno::Reference< ::com::sun::star::lang::XMultiServiceFactory > xSMgr;
      57                 :            :             SvtSysLocale        aSysLocale;
      58                 :            :             LanguageType        eCurrentLanguage;
      59                 :            :             LanguageType        eLastAnyLanguage;
      60                 :            :     const   LocaleDataWrapper*  pSystem;
      61                 :            :     const   LocaleDataWrapper*  pEnglish;
      62                 :            :             LocaleDataWrapper*  pAny;
      63                 :            :     const   LocaleDataWrapper*  pCurrent;
      64                 :            :             bool                bInitialized;
      65                 :            : 
      66                 :            : public:
      67                 :      32827 :                                 OnDemandLocaleDataWrapper()
      68                 :            :                                     : eLastAnyLanguage( LANGUAGE_DONTKNOW )
      69                 :            :                                     , pEnglish(0)
      70                 :            :                                     , pAny(0)
      71         [ +  - ]:      32827 :                                     , bInitialized(false)
      72                 :            :                                     {
      73         [ +  - ]:      32827 :                                         pCurrent = pSystem = aSysLocale.GetLocaleDataPtr();
      74                 :      32827 :                                         eCurrentLanguage = LANGUAGE_SYSTEM;
      75                 :      32827 :                                     }
      76                 :            :                                 OnDemandLocaleDataWrapper(
      77                 :            :                                     const ::com::sun::star::uno::Reference< ::com::sun::star::lang::XMultiServiceFactory >& rxSMgr,
      78                 :            :                                     ::com::sun::star::lang::Locale& rLocale,
      79                 :            :                                     LanguageType eLang
      80                 :            :                                     )
      81                 :            :                                     : pEnglish(0)
      82                 :            :                                     , pAny(0)
      83                 :            :                                     , pCurrent(0)
      84                 :            :                                     , bInitialized(false)
      85                 :            :                                     {
      86                 :            :                                         pSystem = aSysLocale.GetLocaleDataPtr();
      87                 :            :                                         init( rxSMgr, rLocale, eLang );
      88                 :            :                                     }
      89                 :      32378 :                                 ~OnDemandLocaleDataWrapper()
      90         [ +  - ]:      32378 :                                     {
      91 [ +  + ][ +  - ]:      32378 :                                         delete pEnglish;
      92 [ +  + ][ +  - ]:      32378 :                                         delete pAny;
      93                 :      32378 :                                     }
      94                 :            : 
      95                 :          0 :             bool                isInitialized() const   { return bInitialized; }
      96                 :            : 
      97                 :            :             bool                is() const      { return pCurrent != NULL; }
      98                 :            : 
      99                 :       2191 :             void                init(
     100                 :            :                                     const ::com::sun::star::uno::Reference< ::com::sun::star::lang::XMultiServiceFactory >& rxSMgr,
     101                 :            :                                     ::com::sun::star::lang::Locale& rLocale,
     102                 :            :                                     LanguageType eLang
     103                 :            :                                     )
     104                 :            :                                     {
     105                 :       2191 :                                         xSMgr = rxSMgr;
     106                 :       2191 :                                         changeLocale( rLocale, eLang );
     107                 :       2191 :                                         bInitialized = true;
     108                 :       2191 :                                     }
     109                 :            : 
     110                 :       8868 :             void                changeLocale( ::com::sun::star::lang::Locale& rLocale, LanguageType eLang )
     111                 :            :                                     {
     112      [ +  +  + ]:       8868 :                                         switch ( eLang )
     113                 :            :                                         {
     114                 :            :                                             case LANGUAGE_SYSTEM :
     115                 :       5181 :                                                 pCurrent = pSystem;
     116                 :       5181 :                                             break;
     117                 :            :                                             case LANGUAGE_ENGLISH_US :
     118         [ +  + ]:       3651 :                                                 if ( !pEnglish )
     119         [ +  - ]:        544 :                                                     pEnglish = new LocaleDataWrapper( xSMgr, rLocale );
     120                 :       3651 :                                                 pCurrent = pEnglish;
     121                 :       3651 :                                             break;
     122                 :            :                                             default:
     123         [ +  + ]:         36 :                                                 if ( !pAny )
     124                 :            :                                                 {
     125         [ +  - ]:         15 :                                                     pAny = new LocaleDataWrapper( xSMgr, rLocale );
     126                 :         15 :                                                     eLastAnyLanguage = eLang;
     127                 :            :                                                 }
     128         [ +  + ]:         21 :                                                 else if ( eLastAnyLanguage != eLang )
     129                 :            :                                                 {
     130                 :          6 :                                                     pAny->setLocale( rLocale );
     131                 :          6 :                                                     eLastAnyLanguage = eLang;
     132                 :            :                                                 }
     133                 :         36 :                                                 pCurrent = pAny;
     134                 :            :                                         }
     135                 :       8868 :                                         eCurrentLanguage = eLang;
     136                 :       8868 :                                     }
     137                 :            : 
     138                 :          0 :             LanguageType        getCurrentLanguage() const
     139                 :          0 :                                     { return eCurrentLanguage; }
     140                 :            : 
     141                 :            :             LocaleDataWrapper*  getAnyLocale()
     142                 :            :                                     {
     143                 :            :                                         if ( !pAny )
     144                 :            :                                         {
     145                 :            :                                             pAny = new LocaleDataWrapper( xSMgr, pCurrent->getLocale() );
     146                 :            :                                             eLastAnyLanguage = eCurrentLanguage;
     147                 :            :                                         }
     148                 :            :                                         else if ( pCurrent != pAny )
     149                 :            :                                         {
     150                 :            :                                             pAny->setLocale( pCurrent->getLocale() );
     151                 :            :                                             eLastAnyLanguage = eCurrentLanguage;
     152                 :            :                                         }
     153                 :            :                                         return pAny;
     154                 :            :                                     }
     155                 :            : 
     156                 :    2028591 :     const   LocaleDataWrapper*  get() const         { return pCurrent; }
     157                 :       8345 :     const   LocaleDataWrapper*  operator->() const  { return get(); }
     158                 :        582 :     const   LocaleDataWrapper&  operator*() const   { return *get(); }
     159                 :            : };
     160                 :            : 
     161                 :            : /** Load a calendar only if it's needed.
     162                 :            :     SvNumberformatter uses it upon switching locales.
     163                 :            :     @ATTENTION If the default ctor is used the init() method MUST be called
     164                 :            :     before accessing the calendar.
     165                 :            :  */
     166                 :            : class OnDemandCalendarWrapper
     167                 :            : {
     168                 :            :         ::com::sun::star::uno::Reference< ::com::sun::star::lang::XMultiServiceFactory > xSMgr;
     169                 :            :             ::com::sun::star::lang::Locale  aLocale;
     170                 :            :     mutable CalendarWrapper*    pPtr;
     171                 :            :     mutable bool                bValid;
     172                 :            :             bool                bInitialized;
     173                 :            : 
     174                 :            : public:
     175                 :       2191 :                                 OnDemandCalendarWrapper()
     176                 :            :                                     : pPtr(0)
     177                 :            :                                     , bValid(false)
     178                 :       2191 :                                     , bInitialized(false)
     179                 :       2191 :                                     {}
     180                 :            :                                 OnDemandCalendarWrapper(
     181                 :            :                                     const ::com::sun::star::uno::Reference< ::com::sun::star::lang::XMultiServiceFactory >& rxSMgr,
     182                 :            :                                     ::com::sun::star::lang::Locale& rLocale
     183                 :            :                                     )
     184                 :            :                                     : bValid(false)
     185                 :            :                                     , bInitialized(false)
     186                 :            :                                     {
     187                 :            :                                         init( rxSMgr, rLocale );
     188                 :            :                                     }
     189                 :       1990 :                                 ~OnDemandCalendarWrapper()
     190                 :       1990 :                                     {
     191 [ +  + ][ +  - ]:       1990 :                                         delete pPtr;
     192                 :       1990 :                                     }
     193                 :            : 
     194                 :            :             bool                isInitialized() const   { return bInitialized; }
     195                 :            : 
     196                 :            :             bool                is() const      { return pPtr != NULL; }
     197                 :            : 
     198                 :       2191 :             void                init(
     199                 :            :                                     const ::com::sun::star::uno::Reference< ::com::sun::star::lang::XMultiServiceFactory >& rxSMgr,
     200                 :            :                                     ::com::sun::star::lang::Locale& rLocale
     201                 :            :                                     )
     202                 :            :                                     {
     203                 :       2191 :                                         xSMgr = rxSMgr;
     204                 :       2191 :                                         changeLocale( rLocale );
     205         [ -  + ]:       2191 :                                         if ( pPtr )
     206                 :            :                                         {
     207         [ #  # ]:          0 :                                             delete pPtr;
     208                 :          0 :                                             pPtr = NULL;
     209                 :            :                                         }
     210                 :       2191 :                                         bInitialized = true;
     211                 :       2191 :                                     }
     212                 :            : 
     213                 :       8868 :             void                changeLocale( ::com::sun::star::lang::Locale& rLocale )
     214                 :            :                                     {
     215                 :       8868 :                                         bValid = false;
     216                 :       8868 :                                         aLocale = rLocale;
     217                 :       8868 :                                     }
     218                 :            : 
     219                 :       3157 :             CalendarWrapper*    get() const
     220                 :            :                                     {
     221         [ +  + ]:       3157 :                                         if ( !bValid )
     222                 :            :                                         {
     223         [ +  + ]:        149 :                                             if ( !pPtr )
     224         [ +  - ]:        130 :                                                 pPtr = new CalendarWrapper( xSMgr );
     225                 :        149 :                                             pPtr->loadDefaultCalendar( aLocale );
     226                 :        149 :                                             bValid = true;
     227                 :            :                                         }
     228                 :       3157 :                                         return pPtr;
     229                 :            :                                     }
     230                 :            : 
     231                 :            :             CalendarWrapper*    operator->()    { return get(); }
     232                 :            :             CalendarWrapper&    operator*()     { return *get(); }
     233                 :            : };
     234                 :            : 
     235                 :            : /** Load a transliteration only if it's needed.
     236                 :            :     SvNumberformatter uses it upon switching locales.
     237                 :            :     @ATTENTION If the default ctor is used the init() method MUST be called
     238                 :            :     before accessing the transliteration.
     239                 :            :  */
     240                 :            : class OnDemandTransliterationWrapper
     241                 :            : {
     242                 :            :         ::com::sun::star::uno::Reference< ::com::sun::star::lang::XMultiServiceFactory > xSMgr;
     243                 :            :             LanguageType        eLanguage;
     244                 :            :             ::com::sun::star::i18n::TransliterationModules  nType;
     245                 :            :     mutable ::utl::TransliterationWrapper*  pPtr;
     246                 :            :     mutable bool                bValid;
     247                 :            :             bool                bInitialized;
     248                 :            : 
     249                 :            : public:
     250                 :      32827 :                                 OnDemandTransliterationWrapper()
     251                 :            :                                     : eLanguage( LANGUAGE_SYSTEM )
     252                 :            :                                     , pPtr(0)
     253                 :            :                                     , bValid(false)
     254                 :      32827 :                                     , bInitialized(false)
     255                 :      32827 :                                     {}
     256                 :            :                                 OnDemandTransliterationWrapper(
     257                 :            :                                     const ::com::sun::star::uno::Reference< ::com::sun::star::lang::XMultiServiceFactory >& rxSMgr,
     258                 :            :                                     LanguageType eLang,
     259                 :            :                                     ::com::sun::star::i18n::TransliterationModules nTypeP
     260                 :            :                                     )
     261                 :            :                                     : bValid(false)
     262                 :            :                                     , bInitialized(false)
     263                 :            :                                     {
     264                 :            :                                         init( rxSMgr, eLang, nTypeP );
     265                 :            :                                     }
     266                 :      32378 :                                 ~OnDemandTransliterationWrapper()
     267                 :      32378 :                                     {
     268 [ +  + ][ +  - ]:      32378 :                                         delete pPtr;
     269                 :      32378 :                                     }
     270                 :            : 
     271                 :          0 :             bool                isInitialized() const   { return bInitialized; }
     272                 :            : 
     273                 :            :             bool                is() const      { return pPtr != NULL; }
     274                 :            : 
     275                 :       2191 :             void                init(
     276                 :            :                                     const ::com::sun::star::uno::Reference< ::com::sun::star::lang::XMultiServiceFactory >& rxSMgr,
     277                 :            :                                     LanguageType eLang,
     278                 :            :                                     ::com::sun::star::i18n::TransliterationModules nTypeP
     279                 :            :                                     )
     280                 :            :                                     {
     281                 :       2191 :                                         xSMgr = rxSMgr;
     282                 :       2191 :                                         nType = nTypeP;
     283                 :       2191 :                                         changeLocale( eLang );
     284         [ -  + ]:       2191 :                                         if ( pPtr )
     285                 :            :                                         {
     286         [ #  # ]:          0 :                                             delete pPtr;
     287                 :          0 :                                             pPtr = NULL;
     288                 :            :                                         }
     289                 :       2191 :                                         bInitialized = true;
     290                 :       2191 :                                     }
     291                 :            : 
     292                 :       8868 :             void                changeLocale( LanguageType eLang )
     293                 :            :                                     {
     294                 :       8868 :                                         bValid = false;
     295                 :       8868 :                                         eLanguage = eLang;
     296                 :       8868 :                                     }
     297                 :            : 
     298                 :       2389 :     const   ::utl::TransliterationWrapper*  get() const
     299                 :            :                                     {
     300         [ +  + ]:       2389 :                                         if ( !bValid )
     301                 :            :                                         {
     302         [ +  + ]:        147 :                                             if ( !pPtr )
     303         [ +  - ]:        129 :                                                 pPtr = new ::utl::TransliterationWrapper( xSMgr, nType );
     304                 :        147 :                                             pPtr->loadModuleIfNeeded( eLanguage );
     305                 :        147 :                                             bValid = true;
     306                 :            :                                         }
     307                 :       2389 :                                         return pPtr;
     308                 :            :                                     }
     309                 :            : 
     310                 :            :     const   ::utl::TransliterationWrapper*  getForModule( const String& rModule, LanguageType eLang ) const
     311                 :            :                                     {
     312                 :            :                                         if ( !pPtr )
     313                 :            :                                             pPtr = new ::utl::TransliterationWrapper( xSMgr, nType );
     314                 :            :                                         pPtr->loadModuleByImplName( rModule, eLang );
     315                 :            :                                         bValid = false; // reforce settings change in get()
     316                 :            :                                         return pPtr;
     317                 :            :                                     }
     318                 :            : 
     319                 :          0 :     const   ::utl::TransliterationWrapper*  operator->() const  { return get(); }
     320                 :            :     const   ::utl::TransliterationWrapper&  operator*() const   { return *get(); }
     321                 :            : };
     322                 :            : 
     323                 :            : /** Load a native number service wrapper only if it's needed.
     324                 :            :     SvNumberformatter uses it.
     325                 :            : 
     326                 :            :     @ATTENTION
     327                 :            :     If the default ctor is used the init() method MUST be called
     328                 :            :     before accessing the native number supplier.
     329                 :            :  */
     330                 :            : class OnDemandNativeNumberWrapper
     331                 :            : {
     332                 :            :         ::com::sun::star::uno::Reference< ::com::sun::star::lang::XMultiServiceFactory > xSMgr;
     333                 :            :     mutable NativeNumberWrapper*    pPtr;
     334                 :            :             bool                bInitialized;
     335                 :            : 
     336                 :            : public:
     337                 :       2191 :                                 OnDemandNativeNumberWrapper()
     338                 :            :                                     : pPtr(0)
     339                 :       2191 :                                     , bInitialized(false)
     340                 :       2191 :                                     {}
     341                 :            :                                 OnDemandNativeNumberWrapper(
     342                 :            :                                     const ::com::sun::star::uno::Reference< ::com::sun::star::lang::XMultiServiceFactory >& rxSMgr
     343                 :            :                                     )
     344                 :            :                                     : pPtr(0)
     345                 :            :                                     , bInitialized(false)
     346                 :            :                                     {
     347                 :            :                                         init( rxSMgr );
     348                 :            :                                     }
     349                 :       1990 :                                 ~OnDemandNativeNumberWrapper()
     350                 :       1990 :                                     {
     351 [ -  + ][ #  # ]:       1990 :                                         delete pPtr;
     352                 :       1990 :                                     }
     353                 :            : 
     354                 :            :             bool                isInitialized() const   { return bInitialized; }
     355                 :            : 
     356                 :       2191 :             void                init(
     357                 :            :                                     const ::com::sun::star::uno::Reference< ::com::sun::star::lang::XMultiServiceFactory >& rxSMgr
     358                 :            :                                     )
     359                 :            :                                     {
     360                 :       2191 :                                         xSMgr = rxSMgr;
     361         [ -  + ]:       2191 :                                         if ( pPtr )
     362                 :            :                                         {
     363         [ #  # ]:          0 :                                             delete pPtr;
     364                 :          0 :                                             pPtr = NULL;
     365                 :            :                                         }
     366                 :       2191 :                                         bInitialized = true;
     367                 :       2191 :                                     }
     368                 :            : 
     369                 :            :             bool                is() const      { return pPtr != NULL; }
     370                 :            : 
     371                 :          0 :             NativeNumberWrapper*    get() const
     372                 :            :                                     {
     373         [ #  # ]:          0 :                                         if ( !pPtr )
     374         [ #  # ]:          0 :                                             pPtr = new NativeNumberWrapper( xSMgr );
     375                 :          0 :                                         return pPtr;
     376                 :            :                                     }
     377                 :            : 
     378                 :            :             NativeNumberWrapper*    operator->()    { return get(); }
     379                 :            :             NativeNumberWrapper&    operator*()     { return *get(); }
     380                 :            : };
     381                 :            : 
     382                 :            : #endif // INCLUDED_SVTOOLS_ONDEMAND_HXX
     383                 :            : 
     384                 :            : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10