servloc.h 16 KB

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