Bump version to 6.4-15
[LibreOffice.git] / include / svl / ondemand.hxx
blob5ce944a969861cc157d96ffcaf31426d47adf471
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
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 #ifndef INCLUDED_SVL_ONDEMAND_HXX
21 #define INCLUDED_SVL_ONDEMAND_HXX
23 #include <memory>
24 #include <unotools/syslocale.hxx>
25 #include <i18nlangtag/lang.h>
26 #include <unotools/localedatawrapper.hxx>
27 #include <unotools/calendarwrapper.hxx>
28 #include <unotools/transliterationwrapper.hxx>
29 #include <unotools/nativenumberwrapper.hxx>
30 #include <com/sun/star/uno/Reference.hxx>
31 #include <i18nutil/transliteration.hxx>
34 On demand instantiation and initialization of several i18n wrappers,
35 helping the number formatter to not perform worse than it already does.
38 /** @short
39 Switch between LANGUAGE_SYSTEM and LANGUAGE_ENGLISH_US and any other
40 LocaleDataWrapper.
41 SvNumberformatter uses it upon switching locales.
43 @descr
44 Avoids reloading and analysing of locale data again and again.
46 @ATTENTION
47 If the default ctor is used the init() method MUST be called before
48 accessing any locale data. The passed parameters Locale and LanguageType
49 must match each other.
52 class OnDemandLocaleDataWrapper
54 css::uno::Reference< css::uno::XComponentContext > m_xContext;
55 SvtSysLocale aSysLocale;
56 LanguageType eCurrentLanguage;
57 LanguageType eLastAnyLanguage;
58 const LocaleDataWrapper* pSystem;
59 std::unique_ptr<const LocaleDataWrapper> pEnglish;
60 std::unique_ptr< LocaleDataWrapper> pAny;
61 const LocaleDataWrapper* pCurrent;
62 bool bInitialized;
64 public:
65 OnDemandLocaleDataWrapper()
66 : eLastAnyLanguage( LANGUAGE_DONTKNOW )
67 , bInitialized(false)
69 pCurrent = pSystem = aSysLocale.GetLocaleDataPtr();
70 eCurrentLanguage = LANGUAGE_SYSTEM;
73 bool isInitialized() const { return bInitialized; }
75 void init(
76 const css::uno::Reference< css::uno::XComponentContext >& rxContext,
77 const LanguageTag& rLanguageTag
80 m_xContext = rxContext;
81 changeLocale( rLanguageTag );
82 bInitialized = true;
85 void changeLocale( const LanguageTag& rLanguageTag )
87 LanguageType eLang = rLanguageTag.getLanguageType( false);
88 if ( eLang == LANGUAGE_SYSTEM )
89 pCurrent = pSystem;
90 else if ( eLang == LANGUAGE_ENGLISH_US )
92 if ( !pEnglish )
93 pEnglish.reset( new LocaleDataWrapper( m_xContext, rLanguageTag ) );
94 pCurrent = pEnglish.get();
96 else
98 if ( !pAny )
100 pAny.reset( new LocaleDataWrapper( m_xContext, rLanguageTag ) );
101 eLastAnyLanguage = eLang;
103 else if ( eLastAnyLanguage != eLang )
105 pAny->setLanguageTag( rLanguageTag );
106 eLastAnyLanguage = eLang;
108 pCurrent = pAny.get();
110 eCurrentLanguage = eLang;
113 LanguageType getCurrentLanguage() const
114 { return eCurrentLanguage; }
116 const LocaleDataWrapper* get() const { return pCurrent; }
117 const LocaleDataWrapper* operator->() const { return get(); }
118 const LocaleDataWrapper& operator*() const { return *pCurrent; }
121 /** Load a calendar only if it's needed. Keep calendar for "en-US" locale
122 separately, as there can be alternation between locale dependent and
123 locale independent formats.
124 SvNumberformatter uses it upon switching locales.
126 @ATTENTION If the default ctor is used the init() method MUST be called
127 before accessing the calendar.
129 class OnDemandCalendarWrapper
131 css::uno::Reference< css::uno::XComponentContext > m_xContext;
132 css::lang::Locale aEnglishLocale;
133 css::lang::Locale aLocale;
134 mutable css::lang::Locale aLastAnyLocale;
135 mutable std::unique_ptr<CalendarWrapper> pEnglishPtr;
136 mutable std::unique_ptr<CalendarWrapper> pAnyPtr;
138 public:
139 OnDemandCalendarWrapper()
141 LanguageTag aEnglishLanguageTag(LANGUAGE_ENGLISH_US);
142 aEnglishLocale = aEnglishLanguageTag.getLocale();
143 aLastAnyLocale = aEnglishLocale;
146 void init(
147 const css::uno::Reference< css::uno::XComponentContext >& rxContext,
148 const css::lang::Locale& rLocale
151 m_xContext = rxContext;
152 changeLocale( rLocale );
153 pEnglishPtr.reset();
154 pAnyPtr.reset();
157 void changeLocale( const css::lang::Locale& rLocale )
159 aLocale = rLocale;
162 CalendarWrapper* get() const
164 CalendarWrapper* pPtr;
165 if ( aLocale == aEnglishLocale )
167 if (!pEnglishPtr)
169 pEnglishPtr.reset( new CalendarWrapper( m_xContext ));
170 pEnglishPtr->loadDefaultCalendar( aEnglishLocale );
172 pPtr = pEnglishPtr.get();
174 else
176 if ( !pAnyPtr )
178 pAnyPtr.reset(new CalendarWrapper( m_xContext ));
179 pAnyPtr->loadDefaultCalendar(aLocale);
180 aLastAnyLocale = aLocale;
182 else if ( aLocale != aLastAnyLocale )
184 pAnyPtr->loadDefaultCalendar( aLocale );
185 aLastAnyLocale = aLocale;
187 pPtr = pAnyPtr.get();
189 return pPtr;
194 /** Load a transliteration only if it's needed.
195 SvNumberformatter uses it upon switching locales.
196 @ATTENTION If the default ctor is used the init() method MUST be called
197 before accessing the transliteration.
199 class OnDemandTransliterationWrapper
201 css::uno::Reference< css::uno::XComponentContext > m_xContext;
202 LanguageType eLanguage;
203 TransliterationFlags nType;
204 mutable std::unique_ptr<::utl::TransliterationWrapper>
205 pPtr;
206 mutable bool bValid;
207 bool bInitialized;
209 public:
210 OnDemandTransliterationWrapper()
211 : eLanguage( LANGUAGE_SYSTEM )
212 , nType(TransliterationFlags::NONE)
213 , bValid(false)
214 , bInitialized(false)
217 bool isInitialized() const { return bInitialized; }
219 void init(
220 const css::uno::Reference< css::uno::XComponentContext >& rxContext,
221 LanguageType eLang
224 m_xContext = rxContext;
225 nType = TransliterationFlags::IGNORE_CASE;
226 changeLocale( eLang );
227 pPtr.reset();
228 bInitialized = true;
231 void changeLocale( LanguageType eLang )
233 bValid = false;
234 eLanguage = eLang;
237 const ::utl::TransliterationWrapper* get() const
239 if ( !bValid )
241 if ( !pPtr )
242 pPtr.reset( new ::utl::TransliterationWrapper( m_xContext, nType ) );
243 pPtr->loadModuleIfNeeded( eLanguage );
244 bValid = true;
246 return pPtr.get();
249 const ::utl::TransliterationWrapper* operator->() const { return get(); }
252 /** Load a native number service wrapper only if it's needed.
253 SvNumberformatter uses it.
255 @ATTENTION
256 If the default ctor is used the init() method MUST be called
257 before accessing the native number supplier.
259 class OnDemandNativeNumberWrapper
261 css::uno::Reference< css::uno::XComponentContext > m_xContext;
262 mutable std::unique_ptr<NativeNumberWrapper>
263 pPtr;
265 public:
266 OnDemandNativeNumberWrapper()
269 void init(
270 const css::uno::Reference< css::uno::XComponentContext >& rxContext
273 m_xContext = rxContext;
274 pPtr.reset();
277 NativeNumberWrapper* get() const
279 if ( !pPtr )
280 pPtr.reset(new NativeNumberWrapper( m_xContext ));
281 return pPtr.get();
286 #endif // INCLUDED_SVL_ONDEMAND_HXX
288 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */