LibreOffice Module svl (master) 1
ondemand.hxx
Go to the documentation of this file.
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#pragma once
21
22#include <com/sun/star/uno/Reference.hxx>
23#include <i18nlangtag/lang.h>
31#include <optional>
32
33/*
34 On demand instantiation and initialization of several i18n wrappers,
35 helping the number formatter to not perform worse than it already does.
36 */
37
53{
54 css::uno::Reference<css::uno::XComponentContext> m_xContext;
58 std::optional<LocaleDataWrapper> moEnglish;
59 std::optional<LocaleDataWrapper> moAny;
60 int nCurrent; // 0 == system, 1 == english, 2 == any
62
63public:
66 , nCurrent(0)
67 , bInitialized(false)
68 {
70 }
71
72 bool isInitialized() const { return bInitialized; }
73
74 void init(const css::uno::Reference<css::uno::XComponentContext>& rxContext,
75 const LanguageTag& rLanguageTag)
76 {
77 m_xContext = rxContext;
78 changeLocale(rLanguageTag);
79 bInitialized = true;
80 }
81
82 void changeLocale(const LanguageTag& rLanguageTag)
83 {
84 LanguageType eLang = rLanguageTag.getLanguageType(false);
85 if (eLang == LANGUAGE_SYSTEM)
86 nCurrent = 0;
87 else if (eLang == LANGUAGE_ENGLISH_US)
88 {
89 if (!moEnglish)
90 moEnglish.emplace(m_xContext, rLanguageTag);
91 nCurrent = 1;
92 }
93 else
94 {
95 if (!moAny)
96 {
97 moAny.emplace(m_xContext, rLanguageTag);
98 eLastAnyLanguage = eLang;
99 }
100 else if (eLastAnyLanguage != eLang)
101 {
102 moAny.emplace(m_xContext, rLanguageTag);
103 eLastAnyLanguage = eLang;
104 }
105 nCurrent = 2;
106 }
107 eCurrentLanguage = eLang;
108 }
109
111
112 const LocaleDataWrapper* get() const
113 {
114 switch (nCurrent)
115 {
116 case 0:
117 return &aSysLocale.GetLocaleData();
118 case 1:
119 return &*moEnglish;
120 case 2:
121 return &*moAny;
122 default:
123 assert(false);
124 return nullptr;
125 }
126 }
127 const LocaleDataWrapper* operator->() const { return get(); }
128 const LocaleDataWrapper& operator*() const { return *get(); }
129};
130
140{
141 css::uno::Reference<css::uno::XComponentContext> m_xContext;
142 css::lang::Locale aEnglishLocale;
143 css::lang::Locale aLocale;
144 mutable css::lang::Locale aLastAnyLocale;
145 mutable std::optional<CalendarWrapper> moEnglish;
146 mutable std::optional<CalendarWrapper> moAny;
147
148public:
150 {
151 LanguageTag aEnglishLanguageTag(LANGUAGE_ENGLISH_US);
152 aEnglishLocale = aEnglishLanguageTag.getLocale();
154 }
155
156 void init(const css::uno::Reference<css::uno::XComponentContext>& rxContext,
157 const css::lang::Locale& rLocale)
158 {
159 m_xContext = rxContext;
160 changeLocale(rLocale);
161 moEnglish.reset();
162 moAny.reset();
163 }
164
165 void changeLocale(const css::lang::Locale& rLocale) { aLocale = rLocale; }
166
168 {
169 CalendarWrapper* pPtr;
170 if (aLocale == aEnglishLocale)
171 {
172 if (!moEnglish)
173 {
174 moEnglish.emplace(m_xContext);
175 moEnglish->loadDefaultCalendar(aEnglishLocale);
176 }
177 pPtr = &*moEnglish;
178 }
179 else
180 {
181 if (!moAny)
182 {
183 moAny.emplace(m_xContext);
184 moAny->loadDefaultCalendar(aLocale);
186 }
187 else if (aLocale != aLastAnyLocale)
188 {
189 moAny->loadDefaultCalendar(aLocale);
191 }
192 pPtr = &*moAny;
193 }
194 return pPtr;
195 }
196};
197
204{
205 css::uno::Reference<css::uno::XComponentContext> m_xContext;
208 mutable std::optional<::utl::TransliterationWrapper> moTransliterate;
209 mutable bool bValid;
211
212public:
216 , bValid(false)
217 , bInitialized(false)
218 {
219 }
220
221 bool isInitialized() const { return bInitialized; }
222
223 void init(const css::uno::Reference<css::uno::XComponentContext>& rxContext, LanguageType eLang)
224 {
225 m_xContext = rxContext;
226 nType = TransliterationFlags::IGNORE_CASE;
227 changeLocale(eLang);
228 moTransliterate.reset();
229 bInitialized = true;
230 }
231
233 {
234 bValid = false;
235 eLanguage = eLang;
236 }
237
238 const ::utl::TransliterationWrapper* get() const
239 {
240 if (!bValid)
241 {
242 if (!moTransliterate)
244 moTransliterate->loadModuleIfNeeded(eLanguage);
245 bValid = true;
246 }
247 return &*moTransliterate;
248 }
249
250 const ::utl::TransliterationWrapper* operator->() const { return get(); }
251};
252
261{
262 css::uno::Reference<css::uno::XComponentContext> m_xContext;
263 mutable std::optional<NativeNumberWrapper> moNativeNumber;
264
265public:
267
268 void init(const css::uno::Reference<css::uno::XComponentContext>& rxContext)
269 {
270 m_xContext = rxContext;
271 moNativeNumber.reset();
272 }
273
274 NativeNumberWrapper* get() const
275 {
276 if (!moNativeNumber)
277 moNativeNumber.emplace(m_xContext);
278 return &*moNativeNumber;
279 }
280};
281
294{
295 std::optional<CharClass> moCharClass1;
296 std::optional<CharClass> moCharClass2;
297 int nCurrent; // -1 == uninitialised, 0 == class1, 1 == class2
298
299public:
301 : nCurrent(-1)
302 {
303 }
304
305 void changeLocale(const css::uno::Reference<css::uno::XComponentContext>& xContext,
306 const LanguageTag& rLanguageTag)
307 {
308 // check for existing match
309 if (moCharClass1 && moCharClass1->getLanguageTag() == rLanguageTag)
310 {
311 nCurrent = 0;
312 return;
313 }
314 if (moCharClass2 && moCharClass2->getLanguageTag() == rLanguageTag)
315 {
316 nCurrent = 1;
317 return;
318 }
319 // no match - update one the current entries
320 if (nCurrent == -1 || nCurrent == 1)
321 {
322 moCharClass1.emplace(xContext, rLanguageTag);
323 nCurrent = 0;
324 }
325 else
326 {
327 moCharClass2.emplace(xContext, rLanguageTag);
328 nCurrent = 1;
329 }
330 }
331
332 const CharClass* get() const
333 {
334 switch (nCurrent)
335 {
336 case 0:
337 return &*moCharClass1;
338 case 1:
339 return &*moCharClass2;
340 default:
341 assert(false);
342 return nullptr;
343 }
344 }
345 const CharClass* operator->() const { return get(); }
346 const CharClass& operator*() const { return *get(); }
347};
348
349/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
LanguageType getLanguageType(bool bResolveSystem=true) const
const css::lang::Locale & getLocale(bool bResolveSystem=true) const
Load a calendar only if it's needed.
Definition: ondemand.hxx:140
std::optional< CalendarWrapper > moEnglish
Definition: ondemand.hxx:145
void init(const css::uno::Reference< css::uno::XComponentContext > &rxContext, const css::lang::Locale &rLocale)
Definition: ondemand.hxx:156
css::lang::Locale aLocale
Definition: ondemand.hxx:143
void changeLocale(const css::lang::Locale &rLocale)
Definition: ondemand.hxx:165
CalendarWrapper * get() const
Definition: ondemand.hxx:167
css::lang::Locale aLastAnyLocale
Definition: ondemand.hxx:144
std::optional< CalendarWrapper > moAny
Definition: ondemand.hxx:146
css::uno::Reference< css::uno::XComponentContext > m_xContext
Definition: ondemand.hxx:141
css::lang::Locale aEnglishLocale
Definition: ondemand.hxx:142
SvNumberformatter uses it upon switching locales.
Definition: ondemand.hxx:294
const CharClass * get() const
Definition: ondemand.hxx:332
std::optional< CharClass > moCharClass2
Definition: ondemand.hxx:296
std::optional< CharClass > moCharClass1
Definition: ondemand.hxx:295
void changeLocale(const css::uno::Reference< css::uno::XComponentContext > &xContext, const LanguageTag &rLanguageTag)
Definition: ondemand.hxx:305
const CharClass * operator->() const
Definition: ondemand.hxx:345
const CharClass & operator*() const
Definition: ondemand.hxx:346
Switch between LANGUAGE_SYSTEM and LANGUAGE_ENGLISH_US and any other LocaleDataWrapper.
Definition: ondemand.hxx:53
void init(const css::uno::Reference< css::uno::XComponentContext > &rxContext, const LanguageTag &rLanguageTag)
Definition: ondemand.hxx:74
bool isInitialized() const
Definition: ondemand.hxx:72
css::uno::Reference< css::uno::XComponentContext > m_xContext
Definition: ondemand.hxx:54
std::optional< LocaleDataWrapper > moAny
Definition: ondemand.hxx:59
void changeLocale(const LanguageTag &rLanguageTag)
Definition: ondemand.hxx:82
std::optional< LocaleDataWrapper > moEnglish
Definition: ondemand.hxx:58
LanguageType eLastAnyLanguage
Definition: ondemand.hxx:57
LanguageType eCurrentLanguage
Definition: ondemand.hxx:56
LanguageType getCurrentLanguage() const
Definition: ondemand.hxx:110
const LocaleDataWrapper * get() const
Definition: ondemand.hxx:112
const LocaleDataWrapper * operator->() const
Definition: ondemand.hxx:127
const LocaleDataWrapper & operator*() const
Definition: ondemand.hxx:128
SvtSysLocale aSysLocale
Definition: ondemand.hxx:55
Load a native number service wrapper only if it's needed.
Definition: ondemand.hxx:261
NativeNumberWrapper * get() const
Definition: ondemand.hxx:274
void init(const css::uno::Reference< css::uno::XComponentContext > &rxContext)
Definition: ondemand.hxx:268
std::optional< NativeNumberWrapper > moNativeNumber
Definition: ondemand.hxx:263
css::uno::Reference< css::uno::XComponentContext > m_xContext
Definition: ondemand.hxx:262
Load a transliteration only if it's needed.
Definition: ondemand.hxx:204
std::optional<::utl::TransliterationWrapper > moTransliterate
Definition: ondemand.hxx:208
TransliterationFlags nType
Definition: ondemand.hxx:207
void changeLocale(LanguageType eLang)
Definition: ondemand.hxx:232
const ::utl::TransliterationWrapper * get() const
Definition: ondemand.hxx:238
void init(const css::uno::Reference< css::uno::XComponentContext > &rxContext, LanguageType eLang)
Definition: ondemand.hxx:223
const ::utl::TransliterationWrapper * operator->() const
Definition: ondemand.hxx:250
css::uno::Reference< css::uno::XComponentContext > m_xContext
Definition: ondemand.hxx:205
const LocaleDataWrapper & GetLocaleData() const
#define LANGUAGE_SYSTEM
#define LANGUAGE_DONTKNOW
#define LANGUAGE_ENGLISH_US
NONE
TransliterationFlags