]> git.saurik.com Git - apple/icu.git/blame - icuSources/common/iculserv.h
ICU-3.13.tar.gz
[apple/icu.git] / icuSources / common / iculserv.h
CommitLineData
b75a7d8f
A
1/**
2 *******************************************************************************
3 * Copyright (C) 2001-2003, International Business Machines Corporation and *
4 * others. All Rights Reserved. *
5 *******************************************************************************
6 *
7 *******************************************************************************
8 */
9#ifndef ICULSERV_H
10#define ICULSERV_H
11
12#include "unicode/utypes.h"
13
14#if UCONFIG_NO_SERVICE
15
16U_NAMESPACE_BEGIN
17
18/*
19 * Allow the declaration of APIs with pointers to ICUService
20 * even when service is removed from the build.
21 */
22class ICULocaleService;
23
24U_NAMESPACE_END
25
26#else
27
28#include "unicode/uobject.h"
29#include "unicode/unistr.h"
30#include "unicode/chariter.h"
31#include "unicode/locid.h"
32#include "unicode/ubrk.h"
33#include "unicode/strenum.h"
34
35#include "hash.h"
36#include "uvector.h"
37
38#include "icuserv.h"
39
40U_NAMESPACE_BEGIN
41
42class ICULocaleService;
43
44class LocaleKey;
45class LocaleKeyFactory;
46class SimpleLocaleKeyFactory;
47class ServiceListener;
48
49/*
50 ******************************************************************
51 */
52
53/**
54 * A subclass of Key that implements a locale fallback mechanism.
55 * The first locale to search for is the locale provided by the
56 * client, and the fallback locale to search for is the current
57 * default locale. If a prefix is present, the currentDescriptor
58 * includes it before the locale proper, separated by "/". This
59 * is the default key instantiated by ICULocaleService.</p>
60 *
61 * <p>Canonicalization adjusts the locale string so that the
62 * section before the first understore is in lower case, and the rest
63 * is in upper case, with no trailing underscores.</p>
64 */
65
66class U_COMMON_API LocaleKey : public ICUServiceKey {
67 private:
68 int32_t _kind;
69 UnicodeString _primaryID;
70 UnicodeString _fallbackID;
71 UnicodeString _currentID;
72
73 public:
74 static const int32_t KIND_ANY; // = -1;
75
76 /**
77 * Create a LocaleKey with canonical primary and fallback IDs.
78 */
79 static LocaleKey* createWithCanonicalFallback(const UnicodeString* primaryID,
80 const UnicodeString* canonicalFallbackID,
81 UErrorCode& status);
82
83 /**
84 * Create a LocaleKey with canonical primary and fallback IDs.
85 */
86 static LocaleKey* createWithCanonicalFallback(const UnicodeString* primaryID,
87 const UnicodeString* canonicalFallbackID,
88 int32_t kind,
89 UErrorCode& status);
90
91 protected:
92 /**
93 * PrimaryID is the user's requested locale string,
94 * canonicalPrimaryID is this string in canonical form,
95 * fallbackID is the current default locale's string in
96 * canonical form.
97 */
98 LocaleKey(const UnicodeString& primaryID,
99 const UnicodeString& canonicalPrimaryID,
100 const UnicodeString* canonicalFallbackID,
101 int32_t kind);
102
103 public:
104 /**
105 * Append the prefix associated with the kind, or nothing if the kind is KIND_ANY.
106 */
107 virtual UnicodeString& prefix(UnicodeString& result) const;
108
109 /**
110 * Return the kind code associated with this key.
111 */
112 virtual int32_t kind() const;
113
114 /**
115 * Return the canonicalID.
116 */
117 virtual UnicodeString& canonicalID(UnicodeString& result) const;
118
119 /**
120 * Return the currentID.
121 */
122 virtual UnicodeString& currentID(UnicodeString& result) const;
123
124 /**
125 * Return the (canonical) current descriptor, or null if no current id.
126 */
127 virtual UnicodeString& currentDescriptor(UnicodeString& result) const;
128
129 /**
130 * Convenience method to return the locale corresponding to the (canonical) original ID.
131 */
132 virtual Locale& canonicalLocale(Locale& result) const;
133
134 /**
135 * Convenience method to return the locale corresponding to the (canonical) current ID.
136 */
137 virtual Locale& currentLocale(Locale& result) const;
138
139 /**
140 * If the key has a fallback, modify the key and return true,
141 * otherwise return false.</p>
142 *
143 * <p>First falls back through the primary ID, then through
144 * the fallbackID. The final fallback is the empty string,
145 * unless the primary id was the empty string, in which case
146 * there is no fallback.
147 */
148 virtual UBool fallback();
149
150 /**
151 * Return true if a key created from id matches, or would eventually
152 * fallback to match, the canonical ID of this key.
153 */
154 virtual UBool isFallbackOf(const UnicodeString& id) const;
155
156 public:
157 /**
158 * UObject boilerplate.
159 */
160 static inline UClassID getStaticClassID() {
161 return (UClassID)&fgClassID;
162 }
163
164 virtual UClassID getDynamicClassID() const {
165 return getStaticClassID();
166 }
167
168#ifdef SERVICE_DEBUG
169 public:
170 virtual UnicodeString& debug(UnicodeString& result) const;
171 virtual UnicodeString& debugClass(UnicodeString& result) const;
172#endif
173
174 private:
175 static const char fgClassID;
176};
177
178/*
179 ******************************************************************
180 */
181
182/**
183 * A subclass of ICUServiceFactory that uses LocaleKeys, and is able to
184 * 'cover' more specific locales with more general locales that it
185 * supports.
186 *
187 * <p>Coverage may be either of the values VISIBLE or INVISIBLE.
188 *
189 * <p>'Visible' indicates that the specific locale(s) supported by
190 * the factory are registered in getSupportedIDs, 'Invisible'
191 * indicates that they are not.
192 *
193 * <p>Localization of visible ids is handled
194 * by the handling factory, regardless of kind.
195 */
196class U_COMMON_API LocaleKeyFactory : public ICUServiceFactory {
197protected:
198 const UnicodeString _name;
199 const int32_t _coverage;
200
201public:
202 enum {
203 /**
204 * Coverage value indicating that the factory makes
205 * its locales visible, and does not cover more specific
206 * locales.
207 */
208 VISIBLE = 0,
209
210 /**
211 * Coverage value indicating that the factory does not make
212 * its locales visible, and does not cover more specific
213 * locales.
214 */
215 INVISIBLE = 1
216 };
217
218 /**
219 * Destructor.
220 */
221 virtual ~LocaleKeyFactory();
222
223protected:
224 /**
225 * Constructor used by subclasses.
226 */
227 LocaleKeyFactory(int32_t coverage);
228
229 /**
230 * Constructor used by subclasses.
231 */
232 LocaleKeyFactory(int32_t coverage, const UnicodeString& name);
233
234 /**
235 * Implement superclass abstract method. This checks the currentID of
236 * the key against the supported IDs, and passes the canonicalLocale and
237 * kind off to handleCreate (which subclasses must implement).
238 */
239public:
240 virtual UObject* create(const ICUServiceKey& key, const ICUService* service, UErrorCode& status) const;
241
242protected:
243 virtual UBool handlesKey(const ICUServiceKey& key, UErrorCode& status) const;
244
245public:
246 /**
247 * Override of superclass method. This adjusts the result based
248 * on the coverage rule for this factory.
249 */
250 void updateVisibleIDs(Hashtable& result, UErrorCode& status) const;
251
252 /**
253 * Return a localized name for the locale represented by id.
254 */
255 UnicodeString& getDisplayName(const UnicodeString& id, const Locale& locale, UnicodeString& result) const;
256
257protected:
258 /**
259 * Utility method used by create(ICUServiceKey, ICUService). Subclasses can implement
260 * this instead of create. The default returns NULL.
261 */
262 virtual UObject* handleCreate(const Locale& loc, int32_t kind, const ICUService* service, UErrorCode& status) const;
263
264 /**
265 * Return true if this id is one the factory supports (visible or
266 * otherwise).
267 */
268 virtual UBool isSupportedID(const UnicodeString& id, UErrorCode& status) const;
269
270 /**
271 * Return the set of ids that this factory supports (visible or
272 * otherwise). This can be called often and might need to be
273 * cached if it is expensive to create.
274 */
275 virtual const Hashtable* getSupportedIDs(UErrorCode& status) const;
276
277 public:
278 /**
279 * UObject boilerplate.
280 */
281 static inline UClassID getStaticClassID() {
282 return (UClassID)&fgClassID;
283 }
284
285 virtual UClassID getDynamicClassID() const {
286 return getStaticClassID();
287 }
288
289#ifdef SERVICE_DEBUG
290 public:
291 virtual UnicodeString& debug(UnicodeString& result) const;
292 virtual UnicodeString& debugClass(UnicodeString& result) const;
293#endif
294
295 private:
296 static const char fgClassID;
297};
298
299/*
300 ******************************************************************
301 */
302
303/**
304 * A LocaleKeyFactory that just returns a single object for a kind/locale.
305 */
306
307class U_COMMON_API SimpleLocaleKeyFactory : public LocaleKeyFactory {
308 private:
309 UObject* _obj;
310 UnicodeString _id;
311 const int32_t _kind;
312
313 public:
314 SimpleLocaleKeyFactory(UObject* objToAdopt,
315 const UnicodeString& locale,
316 int32_t kind,
317 int32_t coverage);
318
319 SimpleLocaleKeyFactory(UObject* objToAdopt,
320 const Locale& locale,
321 int32_t kind,
322 int32_t coverage);
323
324 /**
325 * Destructor.
326 */
327 virtual ~SimpleLocaleKeyFactory();
328
329 /**
330 * Override of superclass method. Returns the service object if kind/locale match. Service is not used.
331 */
332 UObject* create(const ICUServiceKey& key, const ICUService* service, UErrorCode& status) const;
333
334 /**
335 * Override of superclass method. This adjusts the result based
336 * on the coverage rule for this factory.
337 */
338 void updateVisibleIDs(Hashtable& result, UErrorCode& status) const;
339
340 protected:
341 /**
342 * Return true if this id is equal to the locale name.
343 */
344 virtual UBool isSupportedID(const UnicodeString& id, UErrorCode& status) const;
345
346
347 public:
348 /**
349 * UObject boilerplate.
350 */
351 static inline UClassID getStaticClassID() {
352 return (UClassID)&fgClassID;
353 }
354
355 virtual UClassID getDynamicClassID() const {
356 return getStaticClassID();
357 }
358
359#ifdef SERVICE_DEBUG
360 public:
361 virtual UnicodeString& debug(UnicodeString& result) const;
362 virtual UnicodeString& debugClass(UnicodeString& result) const;
363#endif
364
365 private:
366 static const char fgClassID;
367};
368
369/*
370 ******************************************************************
371 */
372
373/**
374 * A LocaleKeyFactory that creates a service based on the ICU locale data.
375 * This is a base class for most ICU factories. Subclasses instantiate it
376 * with a constructor that takes a bundle name, which determines the supported
377 * IDs. Subclasses then override handleCreate to create the actual service
378 * object. The default implementation returns a resource bundle.
379 */
380class U_COMMON_API ICUResourceBundleFactory : public LocaleKeyFactory
381{
382 protected:
383 UnicodeString _bundleName;
384
385 public:
386 /**
387 * Convenience constructor that uses the main ICU bundle name.
388 */
389 ICUResourceBundleFactory();
390
391 /**
392 * A service factory based on ICU resource data in resources
393 * with the given name.
394 */
395 ICUResourceBundleFactory(const UnicodeString& bundleName);
396
397protected:
398 /**
399 * Return the supported IDs. This is the set of all locale names in ICULocaleData.
400 */
401 virtual const Hashtable* getSupportedIDs(UErrorCode& status) const;
402
403 /**
404 * Create the service. The default implementation returns the resource bundle
405 * for the locale, ignoring kind, and service.
406 */
407 virtual UObject* handleCreate(const Locale& loc, int32_t kind, const ICUService* service, UErrorCode& status) const;
408
409 public:
410 /**
411 * UObject boilerplate.
412 */
413 virtual UClassID getDynamicClassID() const {
414 return getStaticClassID();
415 }
416
417 static UClassID getStaticClassID() {
418 return (UClassID)&fgClassID;
419 }
420
421#ifdef SERVICE_DEBUG
422 public:
423 virtual UnicodeString& debug(UnicodeString& result) const;
424 virtual UnicodeString& debugClass(UnicodeString& result) const;
425#endif
426
427 private:
428 static const char fgClassID;
429};
430
431/*
432 ******************************************************************
433 */
434
435class U_COMMON_API ICULocaleService : public ICUService
436{
437 private:
438 Locale fallbackLocale;
439 UnicodeString fallbackLocaleName;
440 UMTX llock;
441
442 public:
443 /**
444 * Construct an ICULocaleService.
445 */
446 ICULocaleService();
447
448 /**
449 * Construct an ICULocaleService with a name (useful for debugging).
450 */
451 ICULocaleService(const UnicodeString& name);
452
453 /**
454 * Destructor.
455 */
456 virtual ~ICULocaleService();
457
458#if 0
459 // redeclare because of overload resolution rules?
460 // no, causes ambiguities since both UnicodeString and Locale have constructors that take a const char*
461 // need some compiler flag to remove warnings
462 UObject* get(const UnicodeString& descriptor, UErrorCode& status) const {
463 return ICUService::get(descriptor, status);
464 }
465
466 UObject* get(const UnicodeString& descriptor, UnicodeString* actualReturn, UErrorCode& status) const {
467 return ICUService::get(descriptor, actualReturn, status);
468 }
469#endif
470
471 /**
472 * Convenience override for callers using locales. This calls
473 * get(Locale, int, Locale[]) with KIND_ANY for kind and null for
474 * actualReturn.
475 */
476 UObject* get(const Locale& locale, UErrorCode& status) const;
477
478 /**
479 * Convenience override for callers using locales. This calls
480 * get(Locale, int, Locale[]) with a null actualReturn.
481 */
482 UObject* get(const Locale& locale, int32_t kind, UErrorCode& status) const;
483
484 /**
485 * Convenience override for callers using locales. This calls
486 * get(Locale, String, Locale[]) with a null kind.
487 */
488 UObject* get(const Locale& locale, Locale* actualReturn, UErrorCode& status) const;
489
490 /**
491 * Convenience override for callers using locales. This uses
492 * createKey(Locale.toString(), kind) to create a key, calls getKey, and then
493 * if actualReturn is not null, returns the actualResult from
494 * getKey (stripping any prefix) into a Locale.
495 */
496 UObject* get(const Locale& locale, int32_t kind, Locale* actualReturn, UErrorCode& status) const;
497
498 /**
499 * Convenience override for callers using locales. This calls
500 * registerObject(Object, Locale, int32_t kind, int coverage)
501 * passing KIND_ANY for the kind, and VISIBLE for the coverage.
502 */
503 virtual URegistryKey registerInstance(UObject* objToAdopt, const Locale& locale, UErrorCode& status);
504
505 /**
506 * Convenience function for callers using locales. This calls
507 * registerObject(Object, Locale, int kind, int coverage)
508 * passing VISIBLE for the coverage.
509 */
510 virtual URegistryKey registerInstance(UObject* objToAdopt, const Locale& locale, int32_t kind, UErrorCode& status);
511
512 /**
513 * Convenience function for callers using locales. This instantiates
514 * a SimpleLocaleKeyFactory, and registers the factory.
515 */
516 virtual URegistryKey registerInstance(UObject* objToAdopt, const Locale& locale, int32_t kind, int32_t coverage, UErrorCode& status);
517
518
519 /**
520 * (Stop compiler from complaining about hidden overrides.)
521 * Since both UnicodeString and Locale have constructors that take const char*, adding a public
522 * method that takes UnicodeString causes ambiguity at call sites that use const char*.
523 * We really need a flag that is understood by all compilers that will suppress the warning about
524 * hidden overrides.
525 */
526 virtual URegistryKey registerInstance(UObject* objToAdopt, const UnicodeString& locale, UBool visible, UErrorCode& status);
527
528 /**
529 * Convenience method for callers using locales. This returns the standard
530 * service ID enumeration.
531 */
532 virtual StringEnumeration* getAvailableLocales(void) const;
533
534 protected:
535
536 /**
537 * Return the name of the current fallback locale. If it has changed since this was
538 * last accessed, the service cache is cleared.
539 */
540 const UnicodeString& validateFallbackLocale() const;
541
542 /**
543 * Override superclass createKey method.
544 */
545 virtual ICUServiceKey* createKey(const UnicodeString* id, UErrorCode& status) const;
546
547 /**
548 * Additional createKey that takes a kind.
549 */
550 virtual ICUServiceKey* createKey(const UnicodeString* id, int32_t kind, UErrorCode& status) const;
551
552 friend class ServiceEnumeration;
553};
554
555// temporary utility functions, till I know where to find them
556// in header so tests can also access them
557
558class U_COMMON_API LocaleUtility {
559public:
560 static UnicodeString& canonicalLocaleString(const UnicodeString* id, UnicodeString& result);
561 static Locale& initLocaleFromName(const UnicodeString& id, Locale& result);
562 static UnicodeString& initNameFromLocale(const Locale& locale, UnicodeString& result);
563 static const Hashtable* getAvailableLocaleNames(const UnicodeString& bundleID);
564 static UBool isFallbackOf(const UnicodeString& root, const UnicodeString& child);
565 static UBool cleanup(void);
566};
567
568U_NAMESPACE_END
569
570 /* UCONFIG_NO_SERVICE */
571#endif
572
573 /* ICULSERV_H */
574#endif
575