intl/icu/source/common/servloc.h

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/intl/icu/source/common/servloc.h	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,549 @@
     1.4 +/**
     1.5 + *******************************************************************************
     1.6 + * Copyright (C) 2001-2011, International Business Machines Corporation and    *
     1.7 + * others. All Rights Reserved.                                                *
     1.8 + *******************************************************************************
     1.9 + *
    1.10 + *******************************************************************************
    1.11 + */
    1.12 +#ifndef ICULSERV_H
    1.13 +#define ICULSERV_H
    1.14 +
    1.15 +#include "unicode/utypes.h"
    1.16 +
    1.17 +#if UCONFIG_NO_SERVICE
    1.18 +
    1.19 +U_NAMESPACE_BEGIN
    1.20 +
    1.21 +/*
    1.22 + * Allow the declaration of APIs with pointers to ICUService
    1.23 + * even when service is removed from the build.
    1.24 + */
    1.25 +class ICULocaleService;
    1.26 +
    1.27 +U_NAMESPACE_END
    1.28 +
    1.29 +#else
    1.30 +
    1.31 +#include "unicode/unistr.h"
    1.32 +#include "unicode/locid.h"
    1.33 +#include "unicode/strenum.h"
    1.34 +
    1.35 +#include "hash.h"
    1.36 +#include "uvector.h"
    1.37 +
    1.38 +#include "serv.h"
    1.39 +#include "locutil.h"
    1.40 +
    1.41 +U_NAMESPACE_BEGIN
    1.42 +
    1.43 +class ICULocaleService;
    1.44 +
    1.45 +class LocaleKey;
    1.46 +class LocaleKeyFactory;
    1.47 +class SimpleLocaleKeyFactory;
    1.48 +class ServiceListener;
    1.49 +
    1.50 +/*
    1.51 + ******************************************************************
    1.52 + */
    1.53 +
    1.54 +/**
    1.55 + * A subclass of Key that implements a locale fallback mechanism.
    1.56 + * The first locale to search for is the locale provided by the
    1.57 + * client, and the fallback locale to search for is the current
    1.58 + * default locale.  If a prefix is present, the currentDescriptor
    1.59 + * includes it before the locale proper, separated by "/".  This
    1.60 + * is the default key instantiated by ICULocaleService.</p>
    1.61 + *
    1.62 + * <p>Canonicalization adjusts the locale string so that the
    1.63 + * section before the first understore is in lower case, and the rest
    1.64 + * is in upper case, with no trailing underscores.</p> 
    1.65 + */
    1.66 +
    1.67 +class U_COMMON_API LocaleKey : public ICUServiceKey {
    1.68 +  private: 
    1.69 +    int32_t _kind;
    1.70 +    UnicodeString _primaryID;
    1.71 +    UnicodeString _fallbackID;
    1.72 +    UnicodeString _currentID;
    1.73 +
    1.74 +  public:
    1.75 +    enum {
    1.76 +        KIND_ANY = -1
    1.77 +    };
    1.78 +
    1.79 +    /**
    1.80 +     * Create a LocaleKey with canonical primary and fallback IDs.
    1.81 +     */
    1.82 +    static LocaleKey* createWithCanonicalFallback(const UnicodeString* primaryID, 
    1.83 +                                                  const UnicodeString* canonicalFallbackID,
    1.84 +                                                  UErrorCode& status);
    1.85 +
    1.86 +    /**
    1.87 +     * Create a LocaleKey with canonical primary and fallback IDs.
    1.88 +     */
    1.89 +    static LocaleKey* createWithCanonicalFallback(const UnicodeString* primaryID, 
    1.90 +                                                  const UnicodeString* canonicalFallbackID, 
    1.91 +                                                  int32_t kind,
    1.92 +                                                  UErrorCode& status);
    1.93 +
    1.94 +  protected:
    1.95 +    /**
    1.96 +     * PrimaryID is the user's requested locale string,
    1.97 +     * canonicalPrimaryID is this string in canonical form,
    1.98 +     * fallbackID is the current default locale's string in
    1.99 +     * canonical form.
   1.100 +     */
   1.101 +    LocaleKey(const UnicodeString& primaryID, 
   1.102 +              const UnicodeString& canonicalPrimaryID, 
   1.103 +              const UnicodeString* canonicalFallbackID, 
   1.104 +              int32_t kind);
   1.105 +
   1.106 + public:
   1.107 +    /**
   1.108 +     * Append the prefix associated with the kind, or nothing if the kind is KIND_ANY.
   1.109 +     */
   1.110 +    virtual UnicodeString& prefix(UnicodeString& result) const;
   1.111 +
   1.112 +    /**
   1.113 +     * Return the kind code associated with this key.
   1.114 +     */
   1.115 +    virtual int32_t kind() const;
   1.116 +
   1.117 +    /**
   1.118 +     * Return the canonicalID.
   1.119 +     */
   1.120 +    virtual UnicodeString& canonicalID(UnicodeString& result) const;
   1.121 +
   1.122 +    /**
   1.123 +     * Return the currentID.
   1.124 +     */
   1.125 +    virtual UnicodeString& currentID(UnicodeString& result) const;
   1.126 +
   1.127 +    /**
   1.128 +     * Return the (canonical) current descriptor, or null if no current id.
   1.129 +     */
   1.130 +    virtual UnicodeString& currentDescriptor(UnicodeString& result) const;
   1.131 +
   1.132 +    /**
   1.133 +     * Convenience method to return the locale corresponding to the (canonical) original ID.
   1.134 +     */
   1.135 +    virtual Locale& canonicalLocale(Locale& result) const;
   1.136 +
   1.137 +    /**
   1.138 +     * Convenience method to return the locale corresponding to the (canonical) current ID.
   1.139 +     */
   1.140 +    virtual Locale& currentLocale(Locale& result) const;
   1.141 +
   1.142 +    /**
   1.143 +     * If the key has a fallback, modify the key and return true,
   1.144 +     * otherwise return false.</p>
   1.145 +     *
   1.146 +     * <p>First falls back through the primary ID, then through
   1.147 +     * the fallbackID.  The final fallback is the empty string,
   1.148 +     * unless the primary id was the empty string, in which case
   1.149 +     * there is no fallback.  
   1.150 +     */
   1.151 +    virtual UBool fallback();
   1.152 +
   1.153 +    /**
   1.154 +     * Return true if a key created from id matches, or would eventually
   1.155 +     * fallback to match, the canonical ID of this key.  
   1.156 +     */
   1.157 +    virtual UBool isFallbackOf(const UnicodeString& id) const;
   1.158 +    
   1.159 + public:
   1.160 +    /**
   1.161 +     * UObject boilerplate.
   1.162 +     */
   1.163 +    static UClassID U_EXPORT2 getStaticClassID();
   1.164 +
   1.165 +    virtual UClassID getDynamicClassID() const;
   1.166 +
   1.167 +    /**
   1.168 +     * Destructor.
   1.169 +     */
   1.170 +    virtual ~LocaleKey();
   1.171 +
   1.172 +#ifdef SERVICE_DEBUG
   1.173 + public:
   1.174 +    virtual UnicodeString& debug(UnicodeString& result) const;
   1.175 +    virtual UnicodeString& debugClass(UnicodeString& result) const;
   1.176 +#endif
   1.177 +
   1.178 +};
   1.179 +
   1.180 +/*
   1.181 + ******************************************************************
   1.182 + */
   1.183 +
   1.184 +/**
   1.185 + * A subclass of ICUServiceFactory that uses LocaleKeys, and is able to
   1.186 + * 'cover' more specific locales with more general locales that it
   1.187 + * supports.  
   1.188 + *
   1.189 + * <p>Coverage may be either of the values VISIBLE or INVISIBLE.
   1.190 + *
   1.191 + * <p>'Visible' indicates that the specific locale(s) supported by
   1.192 + * the factory are registered in getSupportedIDs, 'Invisible'
   1.193 + * indicates that they are not.
   1.194 + *
   1.195 + * <p>Localization of visible ids is handled
   1.196 + * by the handling factory, regardless of kind.
   1.197 + */
   1.198 +class U_COMMON_API LocaleKeyFactory : public ICUServiceFactory {
   1.199 +protected:
   1.200 +    const UnicodeString _name;
   1.201 +    const int32_t _coverage;
   1.202 +
   1.203 +public:
   1.204 +    enum {
   1.205 +        /**
   1.206 +         * Coverage value indicating that the factory makes
   1.207 +         * its locales visible, and does not cover more specific 
   1.208 +         * locales.
   1.209 +         */
   1.210 +        VISIBLE = 0,
   1.211 +
   1.212 +        /**
   1.213 +         * Coverage value indicating that the factory does not make
   1.214 +         * its locales visible, and does not cover more specific
   1.215 +         * locales.
   1.216 +         */
   1.217 +        INVISIBLE = 1
   1.218 +    };
   1.219 +
   1.220 +    /**
   1.221 +     * Destructor.
   1.222 +     */
   1.223 +    virtual ~LocaleKeyFactory();
   1.224 +
   1.225 +protected:
   1.226 +    /**
   1.227 +     * Constructor used by subclasses.
   1.228 +     */
   1.229 +    LocaleKeyFactory(int32_t coverage);
   1.230 +
   1.231 +    /**
   1.232 +     * Constructor used by subclasses.
   1.233 +     */
   1.234 +    LocaleKeyFactory(int32_t coverage, const UnicodeString& name);
   1.235 +
   1.236 +    /**
   1.237 +     * Implement superclass abstract method.  This checks the currentID of
   1.238 +     * the key against the supported IDs, and passes the canonicalLocale and
   1.239 +     * kind off to handleCreate (which subclasses must implement).
   1.240 +     */
   1.241 +public:
   1.242 +    virtual UObject* create(const ICUServiceKey& key, const ICUService* service, UErrorCode& status) const;
   1.243 +
   1.244 +protected:
   1.245 +    virtual UBool handlesKey(const ICUServiceKey& key, UErrorCode& status) const;
   1.246 +
   1.247 +public:
   1.248 +    /**
   1.249 +     * Override of superclass method.  This adjusts the result based
   1.250 +     * on the coverage rule for this factory.
   1.251 +     */
   1.252 +    virtual void updateVisibleIDs(Hashtable& result, UErrorCode& status) const;
   1.253 +
   1.254 +    /**
   1.255 +     * Return a localized name for the locale represented by id.
   1.256 +     */
   1.257 +    virtual UnicodeString& getDisplayName(const UnicodeString& id, const Locale& locale, UnicodeString& result) const;
   1.258 +
   1.259 +protected:
   1.260 +    /**
   1.261 +     * Utility method used by create(ICUServiceKey, ICUService).  Subclasses can implement
   1.262 +     * this instead of create.  The default returns NULL.
   1.263 +     */
   1.264 +    virtual UObject* handleCreate(const Locale& loc, int32_t kind, const ICUService* service, UErrorCode& status) const;
   1.265 +
   1.266 +   /**
   1.267 +     * Return true if this id is one the factory supports (visible or 
   1.268 +     * otherwise).
   1.269 +     */
   1.270 + //   virtual UBool isSupportedID(const UnicodeString& id, UErrorCode& status) const;
   1.271 +
   1.272 +   /**
   1.273 +     * Return the set of ids that this factory supports (visible or 
   1.274 +     * otherwise).  This can be called often and might need to be
   1.275 +     * cached if it is expensive to create.
   1.276 +     */
   1.277 +    virtual const Hashtable* getSupportedIDs(UErrorCode& status) const;
   1.278 +
   1.279 +public:
   1.280 +    /**
   1.281 +     * UObject boilerplate.
   1.282 +     */
   1.283 +    static UClassID U_EXPORT2 getStaticClassID();
   1.284 +
   1.285 +    virtual UClassID getDynamicClassID() const;
   1.286 +
   1.287 +#ifdef SERVICE_DEBUG
   1.288 + public:
   1.289 +    virtual UnicodeString& debug(UnicodeString& result) const;
   1.290 +    virtual UnicodeString& debugClass(UnicodeString& result) const;
   1.291 +#endif
   1.292 +
   1.293 +};
   1.294 +
   1.295 +/*
   1.296 + ******************************************************************
   1.297 + */
   1.298 +
   1.299 +/**
   1.300 + * A LocaleKeyFactory that just returns a single object for a kind/locale.
   1.301 + */
   1.302 +
   1.303 +class U_COMMON_API SimpleLocaleKeyFactory : public LocaleKeyFactory {
   1.304 + private:
   1.305 +    UObject* _obj;
   1.306 +    UnicodeString _id;
   1.307 +    const int32_t _kind;
   1.308 +
   1.309 + public:
   1.310 +    SimpleLocaleKeyFactory(UObject* objToAdopt, 
   1.311 +                           const UnicodeString& locale, 
   1.312 +                           int32_t kind, 
   1.313 +                           int32_t coverage);
   1.314 +
   1.315 +    SimpleLocaleKeyFactory(UObject* objToAdopt, 
   1.316 +                           const Locale& locale, 
   1.317 +                           int32_t kind, 
   1.318 +                           int32_t coverage);
   1.319 +
   1.320 +    /**
   1.321 +     * Destructor.
   1.322 +     */
   1.323 +    virtual ~SimpleLocaleKeyFactory();
   1.324 +
   1.325 +    /**
   1.326 +     * Override of superclass method.  Returns the service object if kind/locale match.  Service is not used.
   1.327 +     */
   1.328 +    virtual UObject* create(const ICUServiceKey& key, const ICUService* service, UErrorCode& status) const;
   1.329 +
   1.330 +    /**
   1.331 +     * Override of superclass method.  This adjusts the result based
   1.332 +     * on the coverage rule for this factory.
   1.333 +     */
   1.334 +    virtual void updateVisibleIDs(Hashtable& result, UErrorCode& status) const;
   1.335 +
   1.336 + protected:
   1.337 +    /**
   1.338 +     * Return true if this id is equal to the locale name.
   1.339 +     */
   1.340 +    //virtual UBool isSupportedID(const UnicodeString& id, UErrorCode& status) const;
   1.341 +
   1.342 +
   1.343 +public:
   1.344 +    /**
   1.345 +     * UObject boilerplate.
   1.346 +     */
   1.347 +    static UClassID U_EXPORT2 getStaticClassID();
   1.348 +
   1.349 +    virtual UClassID getDynamicClassID() const;
   1.350 +
   1.351 +#ifdef SERVICE_DEBUG
   1.352 + public:
   1.353 +    virtual UnicodeString& debug(UnicodeString& result) const;
   1.354 +    virtual UnicodeString& debugClass(UnicodeString& result) const;
   1.355 +#endif
   1.356 +
   1.357 +};
   1.358 +
   1.359 +/*
   1.360 + ******************************************************************
   1.361 + */
   1.362 +
   1.363 +/**
   1.364 + * A LocaleKeyFactory that creates a service based on the ICU locale data.
   1.365 + * This is a base class for most ICU factories.  Subclasses instantiate it
   1.366 + * with a constructor that takes a bundle name, which determines the supported
   1.367 + * IDs.  Subclasses then override handleCreate to create the actual service
   1.368 + * object.  The default implementation returns a resource bundle.
   1.369 + */
   1.370 +class U_COMMON_API ICUResourceBundleFactory : public LocaleKeyFactory 
   1.371 +{
   1.372 + protected:
   1.373 +    UnicodeString _bundleName;
   1.374 +
   1.375 + public:
   1.376 +    /**
   1.377 +     * Convenience constructor that uses the main ICU bundle name.
   1.378 +     */
   1.379 +    ICUResourceBundleFactory();
   1.380 +
   1.381 +    /**
   1.382 +     * A service factory based on ICU resource data in resources with
   1.383 +     * the given name.  This should be a 'path' that can be passed to
   1.384 +     * ures_openAvailableLocales, such as U_ICUDATA or U_ICUDATA_COLL.
   1.385 +     * The empty string is equivalent to U_ICUDATA.
   1.386 +     */
   1.387 +    ICUResourceBundleFactory(const UnicodeString& bundleName);
   1.388 +
   1.389 +    /**
   1.390 +     * Destructor
   1.391 +     */
   1.392 +    virtual ~ICUResourceBundleFactory();
   1.393 +
   1.394 +protected:
   1.395 +    /**
   1.396 +     * Return the supported IDs.  This is the set of all locale names in ICULocaleData.
   1.397 +     */
   1.398 +    virtual const Hashtable* getSupportedIDs(UErrorCode& status) const;
   1.399 +
   1.400 +    /**
   1.401 +     * Create the service.  The default implementation returns the resource bundle
   1.402 +     * for the locale, ignoring kind, and service.
   1.403 +     */
   1.404 +    virtual UObject* handleCreate(const Locale& loc, int32_t kind, const ICUService* service, UErrorCode& status) const;
   1.405 +
   1.406 +public:
   1.407 +    /**
   1.408 +     * UObject boilerplate.
   1.409 +     */
   1.410 +    static UClassID U_EXPORT2 getStaticClassID();
   1.411 +    virtual UClassID getDynamicClassID() const;
   1.412 +
   1.413 +
   1.414 +#ifdef SERVICE_DEBUG
   1.415 + public:
   1.416 +    virtual UnicodeString& debug(UnicodeString& result) const;
   1.417 +    virtual UnicodeString& debugClass(UnicodeString& result) const;
   1.418 +#endif
   1.419 +
   1.420 +};
   1.421 +
   1.422 +/*
   1.423 + ******************************************************************
   1.424 + */
   1.425 +
   1.426 +class U_COMMON_API ICULocaleService : public ICUService 
   1.427 +{
   1.428 + private:
   1.429 +  Locale fallbackLocale;
   1.430 +  UnicodeString fallbackLocaleName;
   1.431 +
   1.432 + public:
   1.433 +  /**
   1.434 +   * Construct an ICULocaleService.
   1.435 +   */
   1.436 +  ICULocaleService();
   1.437 +
   1.438 +  /**
   1.439 +   * Construct an ICULocaleService with a name (useful for debugging).
   1.440 +   */
   1.441 +  ICULocaleService(const UnicodeString& name);
   1.442 +
   1.443 +  /**
   1.444 +   * Destructor.
   1.445 +   */
   1.446 +  virtual ~ICULocaleService();
   1.447 +
   1.448 +#if 0
   1.449 +  // redeclare because of overload resolution rules?
   1.450 +  // no, causes ambiguities since both UnicodeString and Locale have constructors that take a const char*
   1.451 +  // need some compiler flag to remove warnings 
   1.452 +  UObject* get(const UnicodeString& descriptor, UErrorCode& status) const {
   1.453 +    return ICUService::get(descriptor, status);
   1.454 +  }
   1.455 +
   1.456 +  UObject* get(const UnicodeString& descriptor, UnicodeString* actualReturn, UErrorCode& status) const {
   1.457 +    return ICUService::get(descriptor, actualReturn, status);
   1.458 +  }
   1.459 +#endif
   1.460 +
   1.461 +  /**
   1.462 +   * Convenience override for callers using locales.  This calls
   1.463 +   * get(Locale, int, Locale[]) with KIND_ANY for kind and null for
   1.464 +   * actualReturn.
   1.465 +   */
   1.466 +  UObject* get(const Locale& locale, UErrorCode& status) const;
   1.467 +
   1.468 +  /**
   1.469 +   * Convenience override for callers using locales.  This calls
   1.470 +   * get(Locale, int, Locale[]) with a null actualReturn.
   1.471 +   */
   1.472 +  UObject* get(const Locale& locale, int32_t kind, UErrorCode& status) const;
   1.473 +
   1.474 +  /**
   1.475 +   * Convenience override for callers using locales. This calls
   1.476 +   * get(Locale, String, Locale[]) with a null kind.
   1.477 +   */
   1.478 +  UObject* get(const Locale& locale, Locale* actualReturn, UErrorCode& status) const;
   1.479 +                   
   1.480 +  /**
   1.481 +   * Convenience override for callers using locales.  This uses
   1.482 +   * createKey(Locale.toString(), kind) to create a key, calls getKey, and then
   1.483 +   * if actualReturn is not null, returns the actualResult from
   1.484 +   * getKey (stripping any prefix) into a Locale.  
   1.485 +   */
   1.486 +  UObject* get(const Locale& locale, int32_t kind, Locale* actualReturn, UErrorCode& status) const;
   1.487 +
   1.488 +  /**
   1.489 +   * Convenience override for callers using locales.  This calls
   1.490 +   * registerObject(Object, Locale, int32_t kind, int coverage)
   1.491 +   * passing KIND_ANY for the kind, and VISIBLE for the coverage.
   1.492 +   */
   1.493 +  virtual URegistryKey registerInstance(UObject* objToAdopt, const Locale& locale, UErrorCode& status);
   1.494 +
   1.495 +  /**
   1.496 +   * Convenience function for callers using locales.  This calls
   1.497 +   * registerObject(Object, Locale, int kind, int coverage)
   1.498 +   * passing VISIBLE for the coverage.
   1.499 +   */
   1.500 +  virtual URegistryKey registerInstance(UObject* objToAdopt, const Locale& locale, int32_t kind, UErrorCode& status);
   1.501 +
   1.502 +  /**
   1.503 +   * Convenience function for callers using locales.  This  instantiates
   1.504 +   * a SimpleLocaleKeyFactory, and registers the factory.
   1.505 +   */
   1.506 +  virtual URegistryKey registerInstance(UObject* objToAdopt, const Locale& locale, int32_t kind, int32_t coverage, UErrorCode& status);
   1.507 +
   1.508 +
   1.509 +  /**
   1.510 +   * (Stop compiler from complaining about hidden overrides.)
   1.511 +   * Since both UnicodeString and Locale have constructors that take const char*, adding a public
   1.512 +   * method that takes UnicodeString causes ambiguity at call sites that use const char*.
   1.513 +   * We really need a flag that is understood by all compilers that will suppress the warning about
   1.514 +   * hidden overrides.
   1.515 +   */
   1.516 +  virtual URegistryKey registerInstance(UObject* objToAdopt, const UnicodeString& locale, UBool visible, UErrorCode& status);
   1.517 +
   1.518 +  /**
   1.519 +   * Convenience method for callers using locales.  This returns the standard
   1.520 +   * service ID enumeration.
   1.521 +   */
   1.522 +  virtual StringEnumeration* getAvailableLocales(void) const;
   1.523 +
   1.524 + protected:
   1.525 +
   1.526 +  /**
   1.527 +   * Return the name of the current fallback locale.  If it has changed since this was
   1.528 +   * last accessed, the service cache is cleared.
   1.529 +   */
   1.530 +  const UnicodeString& validateFallbackLocale() const;
   1.531 +
   1.532 +  /**
   1.533 +   * Override superclass createKey method.
   1.534 +   */
   1.535 +  virtual ICUServiceKey* createKey(const UnicodeString* id, UErrorCode& status) const;
   1.536 +
   1.537 +  /**
   1.538 +   * Additional createKey that takes a kind.
   1.539 +   */
   1.540 +  virtual ICUServiceKey* createKey(const UnicodeString* id, int32_t kind, UErrorCode& status) const;
   1.541 +
   1.542 +  friend class ServiceEnumeration;
   1.543 +};
   1.544 +
   1.545 +U_NAMESPACE_END
   1.546 +
   1.547 +    /* UCONFIG_NO_SERVICE */
   1.548 +#endif
   1.549 +
   1.550 +    /* ICULSERV_H */
   1.551 +#endif
   1.552 +

mercurial