fontservices/textshaperplugin/IcuSource/common/serv.h
changeset 0 1fb32624e06b
equal deleted inserted replaced
-1:000000000000 0:1fb32624e06b
       
     1 /**
       
     2  *******************************************************************************
       
     3  * Copyright (C) 2001-2005, International Business Machines Corporation.       *
       
     4  * All Rights Reserved.                                                        *
       
     5  *******************************************************************************
       
     6  */
       
     7 
       
     8 #ifndef ICUSERV_H
       
     9 #define ICUSERV_H
       
    10 
       
    11 #include "unicode/utypes.h"
       
    12 
       
    13 #if UCONFIG_NO_SERVICE
       
    14 
       
    15 U_NAMESPACE_BEGIN
       
    16 
       
    17 /*
       
    18  * Allow the declaration of APIs with pointers to ICUService
       
    19  * even when service is removed from the build.
       
    20  */
       
    21 class ICUService;
       
    22 
       
    23 U_NAMESPACE_END
       
    24 
       
    25 #else
       
    26 
       
    27 #include "unicode/unistr.h"
       
    28 #include "unicode/locid.h"
       
    29 
       
    30 #include "hash.h"
       
    31 #include "uvector.h"
       
    32 #include "servnotf.h"
       
    33 
       
    34 class ICUServiceTest;
       
    35 
       
    36 U_NAMESPACE_BEGIN
       
    37 
       
    38 class ICUServiceKey;
       
    39 class ICUServiceFactory;
       
    40 class SimpleFactory;
       
    41 class ServiceListener;
       
    42 class ICUService;
       
    43 
       
    44 class DNCache;
       
    45 
       
    46 /*******************************************************************
       
    47  * ICUServiceKey
       
    48  */
       
    49 
       
    50 /**
       
    51  * <p>ICUServiceKeys are used to communicate with factories to
       
    52  * generate an instance of the service.  ICUServiceKeys define how
       
    53  * ids are canonicalized, provide both a current id and a current
       
    54  * descriptor to use in querying the cache and factories, and
       
    55  * determine the fallback strategy.</p>
       
    56  *
       
    57  * <p>ICUServiceKeys provide both a currentDescriptor and a currentID.
       
    58  * The descriptor contains an optional prefix, followed by '/'
       
    59  * and the currentID.  Factories that handle complex keys,
       
    60  * for example number format factories that generate multiple
       
    61  * kinds of formatters for the same locale, use the descriptor 
       
    62  * to provide a fully unique identifier for the service object, 
       
    63  * while using the currentID (in this case, the locale string),
       
    64  * as the visible IDs that can be localized.</p>
       
    65  *
       
    66  * <p>The default implementation of ICUServiceKey has no fallbacks and
       
    67  * has no custom descriptors.</p> 
       
    68  */
       
    69 class U_COMMON_API ICUServiceKey : public UObject {
       
    70  private: 
       
    71   const UnicodeString _id;
       
    72 
       
    73  protected:
       
    74   static const UChar PREFIX_DELIMITER;
       
    75 
       
    76  public:
       
    77 
       
    78   /**
       
    79    * <p>Construct a key from an id.</p>
       
    80    *
       
    81    * @param id the ID from which to construct the key.
       
    82    */
       
    83   ICUServiceKey(const UnicodeString& id);
       
    84 
       
    85   /**
       
    86    * <p>Virtual destructor.</p>
       
    87    */
       
    88   virtual ~ICUServiceKey();
       
    89 
       
    90  /**
       
    91   * <p>Return the original ID used to construct this key.</p>
       
    92   *
       
    93   * @return the ID used to construct this key.
       
    94   */
       
    95   virtual const UnicodeString& getID() const;
       
    96 
       
    97  /**
       
    98   * <p>Return the canonical version of the original ID.  This implementation
       
    99   * appends the original ID to result.  Result is returned as a convenience.</p>
       
   100   *
       
   101   * @param result the output parameter to which the id will be appended.
       
   102   * @return the modified result.
       
   103   */
       
   104   virtual UnicodeString& canonicalID(UnicodeString& result) const;
       
   105 
       
   106  /**
       
   107   * <p>Return the (canonical) current ID.  This implementation appends
       
   108   * the canonical ID to result.  Result is returned as a convenience.</p>
       
   109   *
       
   110   * @param result the output parameter to which the current id will be appended.
       
   111   * @return the modified result.  
       
   112   */
       
   113   virtual UnicodeString& currentID(UnicodeString& result) const;
       
   114 
       
   115  /**
       
   116   * <p>Return the current descriptor.  This implementation appends
       
   117   * the current descriptor to result.  Result is returned as a convenience.</p>
       
   118   *
       
   119   * <p>The current descriptor is used to fully
       
   120   * identify an instance of the service in the cache.  A
       
   121   * factory may handle all descriptors for an ID, or just a
       
   122   * particular descriptor.  The factory can either parse the
       
   123   * descriptor or use custom API on the key in order to
       
   124   * instantiate the service.</p>
       
   125   *
       
   126   * @param result the output parameter to which the current id will be appended.
       
   127   * @return the modified result.  
       
   128   */
       
   129   virtual UnicodeString& currentDescriptor(UnicodeString& result) const;
       
   130 
       
   131  /**
       
   132   * <p>If the key has a fallback, modify the key and return true,
       
   133   * otherwise return false.  The current ID will change if there
       
   134   * is a fallback.  No currentIDs should be repeated, and fallback
       
   135   * must eventually return false.  This implementation has no fallbacks
       
   136   * and always returns false.</p>
       
   137   *
       
   138   * @return TRUE if the ICUServiceKey changed to a valid fallback value.
       
   139   */
       
   140   virtual UBool fallback();
       
   141 
       
   142  /**
       
   143   * <p>Return TRUE if a key created from id matches, or would eventually
       
   144   * fallback to match, the canonical ID of this ICUServiceKey.</p>
       
   145   *
       
   146   * @param id the id to test.
       
   147   * @return TRUE if this ICUServiceKey's canonical ID is a fallback of id.
       
   148   */
       
   149   virtual UBool isFallbackOf(const UnicodeString& id) const;
       
   150 
       
   151  /**
       
   152   * <p>Return the prefix.  This implementation leaves result unchanged.
       
   153   * Result is returned as a convenience.</p>
       
   154   *
       
   155   * @param result the output parameter to which the prefix will be appended.
       
   156   * @return the modified result.
       
   157   */
       
   158   virtual UnicodeString& prefix(UnicodeString& result) const;
       
   159 
       
   160  /**
       
   161   * <p>A utility to parse the prefix out of a descriptor string.  Only
       
   162   * the (undelimited) prefix, if any, remains in result.  Result is returned as a 
       
   163   * convenience.</p>
       
   164   *
       
   165   * @param result an input/output parameter that on entry is a descriptor, and 
       
   166   * on exit is the prefix of that descriptor.
       
   167   * @return the modified result.
       
   168   */
       
   169   static UnicodeString& parsePrefix(UnicodeString& result);
       
   170 
       
   171   /**
       
   172   * <p>A utility to parse the suffix out of a descriptor string.  Only
       
   173   * the (undelimited) suffix, if any, remains in result.  Result is returned as a 
       
   174   * convenience.</p>
       
   175   *
       
   176   * @param result an input/output parameter that on entry is a descriptor, and 
       
   177   * on exit is the suffix of that descriptor.
       
   178   * @return the modified result.
       
   179   */
       
   180   static UnicodeString& parseSuffix(UnicodeString& result);
       
   181 
       
   182 public:
       
   183   /**
       
   184    * UObject RTTI boilerplate.
       
   185    */
       
   186   static UClassID U_EXPORT2 getStaticClassID();
       
   187 
       
   188   /**
       
   189    * UObject RTTI boilerplate.
       
   190    */
       
   191   virtual UClassID getDynamicClassID() const;
       
   192 
       
   193 #ifdef SERVICE_DEBUG
       
   194  public:
       
   195   virtual UnicodeString& debug(UnicodeString& result) const;
       
   196   virtual UnicodeString& debugClass(UnicodeString& result) const;
       
   197 #endif
       
   198 
       
   199 };
       
   200 
       
   201  /*******************************************************************
       
   202   * ICUServiceFactory
       
   203   */
       
   204 
       
   205  /**
       
   206   * <p>An implementing ICUServiceFactory generates the service objects maintained by the
       
   207   * service.  A factory generates a service object from a key,
       
   208   * updates id->factory mappings, and returns the display name for
       
   209   * a supported id.</p>
       
   210   */
       
   211 class U_COMMON_API ICUServiceFactory : public UObject {
       
   212  public:
       
   213 
       
   214     /**
       
   215      * <p>Create a service object from the key, if this factory
       
   216      * supports the key.  Otherwise, return NULL.</p>
       
   217      *
       
   218      * <p>If the factory supports the key, then it can call
       
   219      * the service's getKey(ICUServiceKey, String[], ICUServiceFactory) method
       
   220      * passing itself as the factory to get the object that
       
   221      * the service would have created prior to the factory's
       
   222      * registration with the service.  This can change the
       
   223      * key, so any information required from the key should
       
   224      * be extracted before making such a callback.</p>
       
   225      *
       
   226      * @param key the service key.
       
   227      * @param service the service with which this factory is registered.
       
   228      * @param status the error code status.
       
   229      * @return the service object, or NULL if the factory does not support the key.
       
   230      */
       
   231     virtual UObject* create(const ICUServiceKey& key, const ICUService* service, UErrorCode& status) const = 0;
       
   232 
       
   233     /**
       
   234      * <p>Update result to reflect the IDs (not descriptors) that this
       
   235      * factory publicly handles.  Result contains mappings from ID to
       
   236      * factory.  On entry it will contain all (visible) mappings from
       
   237      * previously-registered factories.</p>
       
   238      *
       
   239      * <p>This function, together with getDisplayName, are used to
       
   240      * support ICUService::getDisplayNames.  The factory determines
       
   241      * which IDs (of those it supports) it will make visible, and of
       
   242      * those, which it will provide localized display names for.  In
       
   243      * most cases it will register mappings from all IDs it supports
       
   244      * to itself.</p>
       
   245      *
       
   246      * @param result the mapping table to update.
       
   247      * @param status the error code status.
       
   248      */
       
   249     virtual void updateVisibleIDs(Hashtable& result, UErrorCode& status) const = 0;
       
   250 
       
   251     /**
       
   252      * <p>Return, in result, the display name of the id in the provided locale.
       
   253      * This is an id, not a descriptor.  If the id is 
       
   254      * not visible, sets result to bogus.  If the
       
   255      * incoming result is bogus, it remains bogus.  Result is returned as a
       
   256      * convenience.  Results are not defined if id is not one supported by this
       
   257          * factory.</p>
       
   258      *
       
   259      * @param id a visible id supported by this factory.
       
   260      * @param locale the locale for which to generate the corresponding localized display name.
       
   261      * @param result output parameter to hold the display name.
       
   262      * @return result.
       
   263      */
       
   264     virtual UnicodeString& getDisplayName(const UnicodeString& id, const Locale& locale, UnicodeString& result) const = 0;
       
   265 };
       
   266 
       
   267 /*
       
   268  ******************************************************************
       
   269  */
       
   270 
       
   271  /**
       
   272   * <p>A default implementation of factory.  This provides default
       
   273   * implementations for subclasses, and implements a singleton
       
   274   * factory that matches a single ID and returns a single
       
   275   * (possibly deferred-initialized) instance.  This implements
       
   276   * updateVisibleIDs to add a mapping from its ID to itself
       
   277   * if visible is true, or to remove any existing mapping
       
   278   * for its ID if visible is false.  No localization of display
       
   279   * names is performed.</p>
       
   280   */
       
   281 class U_COMMON_API SimpleFactory : public ICUServiceFactory {
       
   282  protected:
       
   283   UObject* _instance;
       
   284   const UnicodeString _id;
       
   285   const UBool _visible;
       
   286 
       
   287  public:
       
   288   /**
       
   289    * <p>Construct a SimpleFactory that maps a single ID to a single 
       
   290    * service instance.  If visible is TRUE, the ID will be visible.
       
   291    * The instance must not be NULL.  The SimpleFactory will adopt
       
   292    * the instance, which must not be changed subsequent to this call.</p>
       
   293    *
       
   294    * @param instanceToAdopt the service instance to adopt.
       
   295    * @param id the ID to assign to this service instance.
       
   296    * @param visible if TRUE, the ID will be visible.
       
   297    */
       
   298   SimpleFactory(UObject* instanceToAdopt, const UnicodeString& id, UBool visible = TRUE);
       
   299 
       
   300   /**
       
   301    * <p>Destructor.</p>
       
   302    */
       
   303   virtual ~SimpleFactory();
       
   304 
       
   305   /**
       
   306    * <p>This implementation returns a clone of the service instance if the factory's ID is equal to
       
   307    * the key's currentID.  Service and prefix are ignored.</p>
       
   308    *
       
   309    * @param key the service key.
       
   310    * @param service the service with which this factory is registered.
       
   311    * @param status the error code status.
       
   312    * @return the service object, or NULL if the factory does not support the key.
       
   313    */
       
   314   virtual UObject* create(const ICUServiceKey& key, const ICUService* service, UErrorCode& status) const;
       
   315 
       
   316   /**
       
   317    * <p>This implementation adds a mapping from ID -> this to result if visible is TRUE, 
       
   318    * otherwise it removes ID from result.</p>
       
   319    *
       
   320    * @param result the mapping table to update.
       
   321    * @param status the error code status.
       
   322    */
       
   323   virtual void updateVisibleIDs(Hashtable& result, UErrorCode& status) const;
       
   324 
       
   325   /**
       
   326    * <p>This implementation returns the factory ID if it equals id and visible is TRUE,
       
   327    * otherwise it returns the empty string.  (This implementation provides
       
   328    * no localized id information.)</p>
       
   329    *
       
   330    * @param id a visible id supported by this factory.
       
   331    * @param locale the locale for which to generate the corresponding localized display name.
       
   332    * @param result output parameter to hold the display name.
       
   333    * @return result.
       
   334    */
       
   335   virtual UnicodeString& getDisplayName(const UnicodeString& id, const Locale& locale, UnicodeString& result) const;
       
   336 
       
   337 public:
       
   338  /**
       
   339   * UObject RTTI boilerplate.
       
   340   */
       
   341   static UClassID U_EXPORT2 getStaticClassID();
       
   342 
       
   343  /**
       
   344   * UObject RTTI boilerplate.
       
   345   */
       
   346   virtual UClassID getDynamicClassID() const;
       
   347 
       
   348 #ifdef SERVICE_DEBUG
       
   349  public:
       
   350   virtual UnicodeString& debug(UnicodeString& toAppendTo) const;
       
   351   virtual UnicodeString& debugClass(UnicodeString& toAppendTo) const;
       
   352 #endif
       
   353 
       
   354 };
       
   355 
       
   356 /*
       
   357  ******************************************************************
       
   358  */
       
   359 
       
   360 /**
       
   361  * <p>ServiceListener is the listener that ICUService provides by default.
       
   362  * ICUService will notifiy this listener when factories are added to
       
   363  * or removed from the service.  Subclasses can provide
       
   364  * different listener interfaces that extend EventListener, and modify
       
   365  * acceptsListener and notifyListener as appropriate.</p>
       
   366  */
       
   367 class U_COMMON_API ServiceListener : public EventListener {
       
   368 public:
       
   369     /**
       
   370      * <p>This method is called when the service changes. At the time of the
       
   371      * call this listener is registered with the service.  It must
       
   372      * not modify the notifier in the context of this call.</p>
       
   373      * 
       
   374      * @param service the service that changed.
       
   375      */
       
   376     virtual void serviceChanged(const ICUService& service) const = 0;
       
   377     
       
   378 public:
       
   379     /**
       
   380      * UObject RTTI boilerplate.
       
   381      */
       
   382     static UClassID U_EXPORT2 getStaticClassID();
       
   383     
       
   384     /**
       
   385      * UObject RTTI boilerplate.
       
   386      */
       
   387     virtual UClassID getDynamicClassID() const;
       
   388     
       
   389 };
       
   390 
       
   391 /*
       
   392  ******************************************************************
       
   393  */
       
   394 
       
   395 /**
       
   396  * <p>A StringPair holds a displayName/ID pair.  ICUService uses it
       
   397  * as the array elements returned by getDisplayNames.
       
   398  */
       
   399 class U_COMMON_API StringPair : public UMemory {
       
   400 public:
       
   401   /**
       
   402    * <p>The display name of the pair.</p>
       
   403    */
       
   404   const UnicodeString displayName;
       
   405 
       
   406   /**
       
   407    * <p>The ID of the pair.</p>
       
   408    */
       
   409   const UnicodeString id;
       
   410 
       
   411   /**
       
   412    * <p>Creates a string pair from a displayName and an ID.</p>
       
   413    *
       
   414    * @param displayName the displayName.
       
   415    * @param id the ID.
       
   416    * @param status the error code status.
       
   417    * @return a StringPair if the creation was successful, otherwise NULL.
       
   418    */
       
   419   static StringPair* create(const UnicodeString& displayName, 
       
   420                             const UnicodeString& id,
       
   421                             UErrorCode& status);
       
   422 
       
   423   /**
       
   424    * <p>Return TRUE if either string of the pair is bogus.</p>
       
   425    * @return TRUE if either string of the pair is bogus.
       
   426    */
       
   427   UBool isBogus() const;
       
   428 
       
   429 private:
       
   430   StringPair(const UnicodeString& displayName, const UnicodeString& id);
       
   431 };
       
   432 
       
   433 /**
       
   434  * Deleter for StringPairs
       
   435  */
       
   436 U_CAPI void U_EXPORT2
       
   437 userv_deleteStringPair(void *obj);
       
   438 
       
   439 /**
       
   440  * Opaque type returned by registerInstance and registerFactory.
       
   441  */
       
   442 typedef const void* URegistryKey;
       
   443 
       
   444 /*******************************************************************
       
   445  * ICUService
       
   446  */
       
   447 
       
   448  /**
       
   449  * <p>A Service provides access to service objects that implement a
       
   450  * particular service, e.g. transliterators.  Users provide a String
       
   451  * id (for example, a locale string) to the service, and get back an
       
   452  * object for that id.  Service objects can be any kind of object.  A
       
   453  * new service object is returned for each query. The caller is
       
   454  * responsible for deleting it.</p>
       
   455  *
       
   456  * <p>Services 'canonicalize' the query ID and use the canonical ID to
       
   457  * query for the service.  The service also defines a mechanism to
       
   458  * 'fallback' the ID multiple times.  Clients can optionally request
       
   459  * the actual ID that was matched by a query when they use an ID to
       
   460  * retrieve a service object.</p>
       
   461  *
       
   462  * <p>Service objects are instantiated by ICUServiceFactory objects
       
   463  * registered with the service.  The service queries each
       
   464  * ICUServiceFactory in turn, from most recently registered to
       
   465  * earliest registered, until one returns a service object.  If none
       
   466  * responds with a service object, a fallback ID is generated, and the
       
   467  * process repeats until a service object is returned or until the ID
       
   468  * has no further fallbacks.</p>
       
   469  *
       
   470  * <p>In ICU 2.4, UObject (the base class of service instances) does
       
   471  * not define a polymorphic clone function.  ICUService uses clones to
       
   472  * manage ownership.  Thus, for now, ICUService defines an abstract
       
   473  * method, cloneInstance, that clients must implement to create clones
       
   474  * of the service instances.  This may change in future releases of
       
   475  * ICU.</p>
       
   476  *
       
   477  * <p>ICUServiceFactories can be dynamically registered and
       
   478  * unregistered with the service.  When registered, an
       
   479  * ICUServiceFactory is installed at the head of the factory list, and
       
   480  * so gets 'first crack' at any keys or fallback keys.  When
       
   481  * unregistered, it is removed from the service and can no longer be
       
   482  * located through it.  Service objects generated by this factory and
       
   483  * held by the client are unaffected.</p>
       
   484  *
       
   485  * <p>If a service has variants (e.g., the different variants of
       
   486  * BreakIterator) an ICUServiceFactory can use the prefix of the
       
   487  * ICUServiceKey to determine the variant of a service to generate.
       
   488  * If it does not support all variants, it can request
       
   489  * previously-registered factories to handle the ones it does not
       
   490  * support.</p>
       
   491  *
       
   492  * <p>ICUService uses ICUServiceKeys to query factories and perform
       
   493  * fallback.  The ICUServiceKey defines the canonical form of the ID,
       
   494  * and implements the fallback strategy.  Custom ICUServiceKeys can be
       
   495  * defined that parse complex IDs into components that
       
   496  * ICUServiceFactories can more easily use.  The ICUServiceKey can
       
   497  * cache the results of this parsing to save repeated effort.
       
   498  * ICUService provides convenience APIs that take UnicodeStrings and
       
   499  * generate default ICUServiceKeys for use in querying.</p>
       
   500  *
       
   501  * <p>ICUService provides API to get the list of IDs publicly
       
   502  * supported by the service (although queries aren't restricted to
       
   503  * this list).  This list contains only 'simple' IDs, and not fully
       
   504  * unique IDs.  ICUServiceFactories are associated with each simple ID
       
   505  * and the responsible factory can also return a human-readable
       
   506  * localized version of the simple ID, for use in user interfaces.
       
   507  * ICUService can also provide an array of the all the localized
       
   508  * visible IDs and their corresponding internal IDs.</p>
       
   509  *
       
   510  * <p>ICUService implements ICUNotifier, so that clients can register
       
   511  * to receive notification when factories are added or removed from
       
   512  * the service.  ICUService provides a default EventListener
       
   513  * subinterface, ServiceListener, which can be registered with the
       
   514  * service.  When the service changes, the ServiceListener's
       
   515  * serviceChanged method is called with the service as the
       
   516  * argument.</p>
       
   517  *
       
   518  * <p>The ICUService API is both rich and generic, and it is expected
       
   519  * that most implementations will statically 'wrap' ICUService to
       
   520  * present a more appropriate API-- for example, to declare the type
       
   521  * of the objects returned from get, to limit the factories that can
       
   522  * be registered with the service, or to define their own listener
       
   523  * interface with a custom callback method.  They might also customize
       
   524  * ICUService by overriding it, for example, to customize the
       
   525  * ICUServiceKey and fallback strategy.  ICULocaleService is a
       
   526  * subclass of ICUService that uses Locale names as IDs and uses
       
   527  * ICUServiceKeys that implement the standard resource bundle fallback
       
   528  * strategy.  Most clients will wish to subclass it instead of
       
   529  * ICUService.</p> 
       
   530  */
       
   531 class U_COMMON_API ICUService : public ICUNotifier {
       
   532  protected: 
       
   533     /**
       
   534      * Name useful for debugging.
       
   535      */
       
   536     const UnicodeString name;
       
   537 
       
   538  private:
       
   539 
       
   540     /**
       
   541      * single lock used by this service.
       
   542      */
       
   543     UMTX lock;
       
   544 
       
   545     /**
       
   546      * Timestamp so iterators can be fail-fast.
       
   547      */
       
   548     uint32_t timestamp;
       
   549 
       
   550     /**
       
   551      * All the factories registered with this service.
       
   552      */
       
   553     UVector* factories;
       
   554 
       
   555     /**
       
   556      * The service cache.
       
   557      */
       
   558     Hashtable* serviceCache;
       
   559 
       
   560     /**
       
   561      * The ID cache.
       
   562      */
       
   563     Hashtable* idCache;
       
   564 
       
   565     /**
       
   566      * The name cache.
       
   567      */
       
   568     DNCache* dnCache;
       
   569 
       
   570     /**
       
   571      * Constructor.
       
   572      */
       
   573  public:
       
   574     /**
       
   575      * <p>Construct a new ICUService.</p>
       
   576      */
       
   577     ICUService();
       
   578 
       
   579     /**
       
   580      * <p>Construct with a name (useful for debugging).</p>
       
   581      *
       
   582      * @param name a name to use in debugging.
       
   583      */
       
   584     ICUService(const UnicodeString& name);
       
   585 
       
   586     /**
       
   587      * <p>Destructor.</p>
       
   588      */
       
   589     virtual ~ICUService();
       
   590 
       
   591     /**
       
   592      * <p>Return the name of this service. This will be the empty string if none was assigned.
       
   593      * Returns result as a convenience.</p>
       
   594      *
       
   595      * @param result an output parameter to contain the name of this service.
       
   596      * @return the name of this service.
       
   597      */
       
   598     UnicodeString& getName(UnicodeString& result) const;
       
   599 
       
   600     /**
       
   601      * <p>Convenience override for get(ICUServiceKey&, UnicodeString*). This uses
       
   602      * createKey to create a key for the provided descriptor.</p>
       
   603      *
       
   604      * @param descriptor the descriptor.
       
   605      * @param status the error code status.
       
   606      * @return the service instance, or NULL.
       
   607      */
       
   608     UObject* get(const UnicodeString& descriptor, UErrorCode& status) const;
       
   609 
       
   610     /**
       
   611      * <p>Convenience override for get(ICUServiceKey&, UnicodeString*).  This uses
       
   612      * createKey to create a key from the provided descriptor.</p>
       
   613      *
       
   614      * @param descriptor the descriptor.
       
   615      * @param actualReturn a pointer to a UnicodeString to hold the matched descriptor, or NULL.
       
   616      * @param status the error code status.
       
   617      * @return the service instance, or NULL.
       
   618      */
       
   619     UObject* get(const UnicodeString& descriptor, UnicodeString* actualReturn, UErrorCode& status) const;
       
   620 
       
   621     /**
       
   622      * <p>Convenience override for get(ICUServiceKey&, UnicodeString*).</p>
       
   623      *
       
   624      * @param key the key.
       
   625      * @param status the error code status.
       
   626      * @return the service instance, or NULL.
       
   627      */
       
   628     UObject* getKey(ICUServiceKey& key, UErrorCode& status) const;
       
   629 
       
   630     /**
       
   631      * <p>Given a key, return a service object, and, if actualReturn
       
   632      * is not NULL, the descriptor with which it was found in the
       
   633      * first element of actualReturn.  If no service object matches
       
   634      * this key, returns NULL and leaves actualReturn unchanged.</p>
       
   635      *
       
   636      * <p>This queries the cache using the key's descriptor, and if no
       
   637      * object in the cache matches, tries the key on each
       
   638      * registered factory, in order.  If none generates a service
       
   639      * object for the key, repeats the process with each fallback of
       
   640      * the key, until either a factory returns a service object, or the key
       
   641      * has no fallback.  If no object is found, the result of handleDefault
       
   642      * is returned.</p>
       
   643      *
       
   644      * <p>Subclasses can override this method to further customize the 
       
   645      * result before returning it.
       
   646      *
       
   647      * @param key the key.
       
   648      * @param actualReturn a pointer to a UnicodeString to hold the matched descriptor, or NULL.
       
   649      * @param status the error code status.
       
   650      * @return the service instance, or NULL.
       
   651      */
       
   652     virtual UObject* getKey(ICUServiceKey& key, UnicodeString* actualReturn, UErrorCode& status) const;
       
   653 
       
   654     /**
       
   655      * <p>This version of getKey is only called by ICUServiceFactories within the scope
       
   656      * of a previous getKey call, to determine what previously-registered factories would
       
   657      * have returned.  For details, see getKey(ICUServiceKey&, UErrorCode&).  Subclasses
       
   658      * should not call it directly, but call through one of the other get functions.</p>
       
   659      * 
       
   660      * @param key the key.
       
   661      * @param actualReturn a pointer to a UnicodeString to hold the matched descriptor, or NULL.
       
   662      * @param factory the factory making the recursive call.
       
   663      * @param status the error code status.
       
   664      * @return the service instance, or NULL.
       
   665      */
       
   666     UObject* getKey(ICUServiceKey& key, UnicodeString* actualReturn, const ICUServiceFactory* factory, UErrorCode& status) const;
       
   667 
       
   668     /**
       
   669      * <p>Convenience override for getVisibleIDs(String) that passes null
       
   670      * as the fallback, thus returning all visible IDs.</p>
       
   671      *
       
   672      * @param result a vector to hold the returned IDs.
       
   673      * @param status the error code status.
       
   674      * @return the result vector.
       
   675      */
       
   676     UVector& getVisibleIDs(UVector& result, UErrorCode& status) const;
       
   677 
       
   678     /**
       
   679      * <p>Return a snapshot of the visible IDs for this service.  This
       
   680      * list will not change as ICUServiceFactories are added or removed, but the
       
   681      * supported IDs will, so there is no guarantee that all and only
       
   682      * the IDs in the returned list will be visible and supported by the
       
   683      * service in subsequent calls.</p>
       
   684      *
       
   685      * <p>The IDs are returned as pointers to UnicodeStrings.  The
       
   686      * caller owns the IDs.  Previous contents of result are discarded before
       
   687      * new elements, if any, are added.</p>
       
   688      *
       
   689      * <p>matchID is passed to createKey to create a key.  If the key
       
   690      * is not NULL, its isFallbackOf method is used to filter out IDs
       
   691      * that don't match the key or have it as a fallback.</p>
       
   692      *
       
   693      * @param result a vector to hold the returned IDs.
       
   694      * @param matchID an ID used to filter the result, or NULL if all IDs are desired.
       
   695      * @param status the error code status.
       
   696      * @return the result vector.
       
   697      */
       
   698     UVector& getVisibleIDs(UVector& result, const UnicodeString* matchID, UErrorCode& status) const;
       
   699 
       
   700     /**
       
   701      * <p>Convenience override for getDisplayName(const UnicodeString&, const Locale&, UnicodeString&) that
       
   702      * uses the current default locale.</p>
       
   703      *
       
   704      * @param id the ID for which to retrieve the localized displayName.
       
   705      * @param result an output parameter to hold the display name.
       
   706      * @return the modified result.
       
   707      */
       
   708     UnicodeString& getDisplayName(const UnicodeString& id, UnicodeString& result) const;
       
   709 
       
   710     /**
       
   711      * <p>Given a visible ID, return the display name in the requested locale.
       
   712      * If there is no directly supported ID corresponding to this ID, result is
       
   713      * set to bogus.</p>
       
   714      *
       
   715      * @param id the ID for which to retrieve the localized displayName.
       
   716      * @param result an output parameter to hold the display name.
       
   717      * @param locale the locale in which to localize the ID.
       
   718      * @return the modified result.
       
   719      */
       
   720     UnicodeString& getDisplayName(const UnicodeString& id, UnicodeString& result, const Locale& locale) const;
       
   721 
       
   722     /**
       
   723      * <p>Convenience override of getDisplayNames(const Locale&, const UnicodeString*) that 
       
   724      * uses the current default Locale as the locale and NULL for
       
   725      * the matchID.</p>
       
   726      *
       
   727      * @param result a vector to hold the returned displayName/id StringPairs.
       
   728      * @param status the error code status.
       
   729      * @return the modified result vector.
       
   730      */
       
   731     UVector& getDisplayNames(UVector& result, UErrorCode& status) const;
       
   732 
       
   733     /**
       
   734      * <p>Convenience override of getDisplayNames(const Locale&, const UnicodeString*) that 
       
   735      * uses NULL for the matchID.</p>
       
   736      *
       
   737      * @param result a vector to hold the returned displayName/id StringPairs.
       
   738      * @param locale the locale in which to localize the ID.
       
   739      * @param status the error code status.
       
   740      * @return the modified result vector.
       
   741      */
       
   742     UVector& getDisplayNames(UVector& result, const Locale& locale, UErrorCode& status) const;
       
   743 
       
   744     /**
       
   745      * <p>Return a snapshot of the mapping from display names to visible
       
   746      * IDs for this service.  This set will not change as factories
       
   747      * are added or removed, but the supported IDs will, so there is
       
   748      * no guarantee that all and only the IDs in the returned map will
       
   749      * be visible and supported by the service in subsequent calls,
       
   750      * nor is there any guarantee that the current display names match
       
   751      * those in the result.</p>
       
   752      *
       
   753      * <p>The names are returned as pointers to StringPairs, which
       
   754      * contain both the displayName and the corresponding ID.  The
       
   755      * caller owns the StringPairs.  Previous contents of result are
       
   756      * discarded before new elements, if any, are added.</p>
       
   757      *
       
   758      * <p>matchID is passed to createKey to create a key.  If the key
       
   759      * is not NULL, its isFallbackOf method is used to filter out IDs
       
   760      * that don't match the key or have it as a fallback.</p>
       
   761      *
       
   762      * @param result a vector to hold the returned displayName/id StringPairs.
       
   763      * @param locale the locale in which to localize the ID.
       
   764      * @param matchID an ID used to filter the result, or NULL if all IDs are desired.
       
   765      * @param status the error code status.
       
   766      * @return the result vector.  */
       
   767     UVector& getDisplayNames(UVector& result,
       
   768                              const Locale& locale, 
       
   769                              const UnicodeString* matchID, 
       
   770                              UErrorCode& status) const;
       
   771 
       
   772     /**
       
   773      * <p>A convenience override of registerInstance(UObject*, const UnicodeString&, UBool)
       
   774      * that defaults visible to TRUE.</p>
       
   775      *
       
   776      * @param objToAdopt the object to register and adopt.
       
   777      * @param id the ID to assign to this object.
       
   778      * @param status the error code status.
       
   779      * @return a registry key that can be passed to unregister to unregister
       
   780      * (and discard) this instance.
       
   781      */
       
   782     URegistryKey registerInstance(UObject* objToAdopt, const UnicodeString& id, UErrorCode& status);
       
   783 
       
   784     /**
       
   785      * <p>Register a service instance with the provided ID.  The ID will be 
       
   786      * canonicalized.  The canonicalized ID will be returned by
       
   787      * getVisibleIDs if visible is TRUE.  The service instance will be adopted and
       
   788      * must not be modified subsequent to this call.</p>
       
   789      *
       
   790      * <p>This issues a serviceChanged notification to registered listeners.</p>
       
   791      *
       
   792      * <p>This implementation wraps the object using
       
   793      * createSimpleFactory, and calls registerFactory.</p>
       
   794      *
       
   795      * @param objToAdopt the object to register and adopt.
       
   796      * @param id the ID to assign to this object.
       
   797      * @param visible TRUE if getVisibleIDs is to return this ID.
       
   798      * @param status the error code status.
       
   799      * @return a registry key that can be passed to unregister() to unregister
       
   800      * (and discard) this instance.
       
   801      */
       
   802     virtual URegistryKey registerInstance(UObject* objToAdopt, const UnicodeString& id, UBool visible, UErrorCode& status);
       
   803 
       
   804     /**
       
   805      * <p>Register an ICUServiceFactory.  Returns a registry key that
       
   806      * can be used to unregister the factory.  The factory
       
   807      * must not be modified subsequent to this call.  The service owns
       
   808      * all registered factories. In case of an error, the factory is
       
   809      * deleted.</p>
       
   810      *
       
   811      * <p>This issues a serviceChanged notification to registered listeners.</p>
       
   812      *
       
   813      * <p>The default implementation accepts all factories.</p>
       
   814      *
       
   815      * @param factoryToAdopt the factory to register and adopt.
       
   816      * @param status the error code status.
       
   817      * @return a registry key that can be passed to unregister to unregister
       
   818      * (and discard) this factory.
       
   819      */
       
   820     virtual URegistryKey registerFactory(ICUServiceFactory* factoryToAdopt, UErrorCode& status);
       
   821 
       
   822     /**
       
   823      * <p>Unregister a factory using a registry key returned by
       
   824      * registerInstance or registerFactory.  After a successful call,
       
   825      * the factory will be removed from the service factory list and
       
   826      * deleted, and the key becomes invalid.</p>
       
   827      *
       
   828      * <p>This issues a serviceChanged notification to registered
       
   829      * listeners.</p>
       
   830      *
       
   831      * @param rkey the registry key.
       
   832      * @param status the error code status.  
       
   833      * @return TRUE if the call successfully unregistered the factory.
       
   834      */
       
   835     virtual UBool unregister(URegistryKey rkey, UErrorCode& status);
       
   836 
       
   837     /**
       
   838      * </p>Reset the service to the default factories.  The factory
       
   839      * lock is acquired and then reInitializeFactories is called.</p>
       
   840      *
       
   841      * <p>This issues a serviceChanged notification to registered listeners.</p>
       
   842      */
       
   843     virtual void reset(void);
       
   844 
       
   845     /**
       
   846      * <p>Return TRUE if the service is in its default state.</p>
       
   847      *
       
   848      * <p>The default implementation returns TRUE if there are no 
       
   849      * factories registered.</p>
       
   850      */
       
   851     virtual UBool isDefault(void) const;
       
   852 
       
   853     /**
       
   854      * <p>Create a key from an ID.  If ID is NULL, returns NULL.</p>
       
   855      *
       
   856      * <p>The default implementation creates an ICUServiceKey instance.
       
   857      * Subclasses can override to define more useful keys appropriate
       
   858      * to the factories they accept.</p>
       
   859      *
       
   860      * @param a pointer to the ID for which to create a default ICUServiceKey.
       
   861      * @param status the error code status.
       
   862      * @return the ICUServiceKey corresponding to ID, or NULL.
       
   863      */
       
   864     virtual ICUServiceKey* createKey(const UnicodeString* id, UErrorCode& status) const;
       
   865 
       
   866     /**
       
   867      * <p>Clone object so that caller can own the copy.  In ICU2.4, UObject doesn't define
       
   868      * clone, so we need an instance-aware method that knows how to do this.
       
   869      * This is public so factories can call it, but should really be protected.</p>
       
   870      *
       
   871      * @param instance the service instance to clone.
       
   872      * @return a clone of the passed-in instance, or NULL if cloning was unsuccessful.
       
   873      */
       
   874     virtual UObject* cloneInstance(UObject* instance) const = 0;
       
   875 
       
   876 
       
   877     /************************************************************************
       
   878      * Subclassing API
       
   879      */
       
   880 
       
   881  protected:
       
   882 
       
   883     /**
       
   884      * <p>Create a factory that wraps a single service object.  Called by registerInstance.</p>
       
   885      *
       
   886      * <p>The default implementation returns an instance of SimpleFactory.</p>
       
   887      *
       
   888      * @param instanceToAdopt the service instance to adopt.
       
   889      * @param id the ID to assign to this service instance.
       
   890      * @param visible if TRUE, the ID will be visible.
       
   891      * @param status the error code status.
       
   892      * @return an instance of ICUServiceFactory that maps this instance to the provided ID.
       
   893      */
       
   894     virtual ICUServiceFactory* createSimpleFactory(UObject* instanceToAdopt, const UnicodeString& id, UBool visible, UErrorCode& status);
       
   895 
       
   896     /**
       
   897      * <p>Reinitialize the factory list to its default state.  After this call, isDefault()
       
   898      * must return TRUE.</p>
       
   899      *
       
   900      * <p>This issues a serviceChanged notification to registered listeners.</p>
       
   901      *
       
   902      * <p>The default implementation clears the factory list.
       
   903      * Subclasses can override to provide other default initialization
       
   904      * of the factory list.  Subclasses must not call this method
       
   905      * directly, since it must only be called while holding write
       
   906      * access to the factory list.</p>
       
   907      */
       
   908     virtual void reInitializeFactories(void);
       
   909 
       
   910     /**
       
   911      * <p>Default handler for this service if no factory in the factory list
       
   912      * handled the key passed to getKey.</p>
       
   913      *
       
   914      * <p>The default implementation returns NULL.</p>
       
   915      *
       
   916      * @param key the key.
       
   917      * @param actualReturn a pointer to a UnicodeString to hold the matched descriptor, or NULL.
       
   918      * @param status the error code status.
       
   919      * @return the service instance, or NULL.
       
   920      */
       
   921     virtual UObject* handleDefault(const ICUServiceKey& key, UnicodeString* actualReturn, UErrorCode& status) const;
       
   922 
       
   923     /**
       
   924      * <p>Clear caches maintained by this service.</p>
       
   925      *
       
   926      * <p>Subclasses can override if they implement additional caches
       
   927      * that need to be cleared when the service changes.  Subclasses
       
   928      * should generally not call this method directly, as it must only
       
   929      * be called while synchronized on the factory lock.</p>
       
   930      */
       
   931     virtual void clearCaches(void);
       
   932 
       
   933     /**
       
   934      * <p>Return true if the listener is accepted.</p>
       
   935      *
       
   936      * <p>The default implementation accepts the listener if it is
       
   937      * a ServiceListener.  Subclasses can override this to accept
       
   938      * different listeners.</p>
       
   939      *
       
   940      * @param l the listener to test.
       
   941      * @return TRUE if the service accepts the listener.
       
   942      */
       
   943     virtual UBool acceptsListener(const EventListener& l) const;
       
   944 
       
   945     /**
       
   946      * <p>Notify the listener of a service change.</p>
       
   947      *
       
   948      * <p>The default implementation assumes a ServiceListener.
       
   949      * If acceptsListener has been overridden to accept different
       
   950      * listeners, this should be overridden as well.</p>
       
   951      *
       
   952      * @param l the listener to notify.
       
   953      */
       
   954     virtual void notifyListener(EventListener& l) const;
       
   955 
       
   956     /************************************************************************
       
   957      * Utilities for subclasses.
       
   958      */
       
   959 
       
   960     /**
       
   961      * <p>Clear only the service cache.</p>
       
   962      *
       
   963      * <p>This can be called by subclasses when a change affects the service
       
   964      * cache but not the ID caches, e.g., when the default locale changes
       
   965      * the resolution of IDs also changes, requiring the cache to be
       
   966      * flushed, but not the visible IDs themselves.</p>
       
   967      */
       
   968     void clearServiceCache(void);
       
   969 
       
   970     /**
       
   971      * <p>Return a map from visible IDs to factories.
       
   972      * This must only be called when the mutex is held.</p>
       
   973      *
       
   974      * @param status the error code status.
       
   975      * @return a Hashtable containing mappings from visible
       
   976      * IDs to factories.
       
   977      */
       
   978     const Hashtable* getVisibleIDMap(UErrorCode& status) const;
       
   979 
       
   980     /**
       
   981      * <p>Allow subclasses to read the time stamp.</p>
       
   982      *
       
   983      * @return the timestamp.
       
   984      */
       
   985     int32_t getTimestamp(void) const;
       
   986 
       
   987     /**
       
   988      * <p>Return the number of registered factories.</p>
       
   989      *
       
   990      * @return the number of factories registered at the time of the call.
       
   991      */
       
   992     int32_t countFactories(void) const;
       
   993 
       
   994 private:
       
   995 
       
   996     friend class ::ICUServiceTest; // give tests access to countFactories.
       
   997 };
       
   998 
       
   999 U_NAMESPACE_END
       
  1000 
       
  1001     /* UCONFIG_NO_SERVICE */
       
  1002 #endif
       
  1003 
       
  1004     /* ICUSERV_H */
       
  1005 #endif
       
  1006