michael@0: /** michael@0: ******************************************************************************* michael@0: * Copyright (C) 2001-2011, International Business Machines Corporation. * michael@0: * All Rights Reserved. * michael@0: ******************************************************************************* michael@0: */ michael@0: michael@0: #ifndef ICUSERV_H michael@0: #define ICUSERV_H michael@0: michael@0: #include "unicode/utypes.h" michael@0: michael@0: #if UCONFIG_NO_SERVICE michael@0: michael@0: U_NAMESPACE_BEGIN michael@0: michael@0: /* michael@0: * Allow the declaration of APIs with pointers to ICUService michael@0: * even when service is removed from the build. michael@0: */ michael@0: class ICUService; michael@0: michael@0: U_NAMESPACE_END michael@0: michael@0: #else michael@0: michael@0: #include "unicode/unistr.h" michael@0: #include "unicode/locid.h" michael@0: #include "unicode/umisc.h" michael@0: michael@0: #include "hash.h" michael@0: #include "uvector.h" michael@0: #include "servnotf.h" michael@0: michael@0: class ICUServiceTest; michael@0: michael@0: U_NAMESPACE_BEGIN michael@0: michael@0: class ICUServiceKey; michael@0: class ICUServiceFactory; michael@0: class SimpleFactory; michael@0: class ServiceListener; michael@0: class ICUService; michael@0: michael@0: class DNCache; michael@0: michael@0: /******************************************************************* michael@0: * ICUServiceKey michael@0: */ michael@0: michael@0: /** michael@0: *

ICUServiceKeys are used to communicate with factories to michael@0: * generate an instance of the service. ICUServiceKeys define how michael@0: * ids are canonicalized, provide both a current id and a current michael@0: * descriptor to use in querying the cache and factories, and michael@0: * determine the fallback strategy.

michael@0: * michael@0: *

ICUServiceKeys provide both a currentDescriptor and a currentID. michael@0: * The descriptor contains an optional prefix, followed by '/' michael@0: * and the currentID. Factories that handle complex keys, michael@0: * for example number format factories that generate multiple michael@0: * kinds of formatters for the same locale, use the descriptor michael@0: * to provide a fully unique identifier for the service object, michael@0: * while using the currentID (in this case, the locale string), michael@0: * as the visible IDs that can be localized.

michael@0: * michael@0: *

The default implementation of ICUServiceKey has no fallbacks and michael@0: * has no custom descriptors.

michael@0: */ michael@0: class U_COMMON_API ICUServiceKey : public UObject { michael@0: private: michael@0: const UnicodeString _id; michael@0: michael@0: protected: michael@0: static const UChar PREFIX_DELIMITER; michael@0: michael@0: public: michael@0: michael@0: /** michael@0: *

Construct a key from an id.

michael@0: * michael@0: * @param id the ID from which to construct the key. michael@0: */ michael@0: ICUServiceKey(const UnicodeString& id); michael@0: michael@0: /** michael@0: *

Virtual destructor.

michael@0: */ michael@0: virtual ~ICUServiceKey(); michael@0: michael@0: /** michael@0: *

Return the original ID used to construct this key.

michael@0: * michael@0: * @return the ID used to construct this key. michael@0: */ michael@0: virtual const UnicodeString& getID() const; michael@0: michael@0: /** michael@0: *

Return the canonical version of the original ID. This implementation michael@0: * appends the original ID to result. Result is returned as a convenience.

michael@0: * michael@0: * @param result the output parameter to which the id will be appended. michael@0: * @return the modified result. michael@0: */ michael@0: virtual UnicodeString& canonicalID(UnicodeString& result) const; michael@0: michael@0: /** michael@0: *

Return the (canonical) current ID. This implementation appends michael@0: * the canonical ID to result. Result is returned as a convenience.

michael@0: * michael@0: * @param result the output parameter to which the current id will be appended. michael@0: * @return the modified result. michael@0: */ michael@0: virtual UnicodeString& currentID(UnicodeString& result) const; michael@0: michael@0: /** michael@0: *

Return the current descriptor. This implementation appends michael@0: * the current descriptor to result. Result is returned as a convenience.

michael@0: * michael@0: *

The current descriptor is used to fully michael@0: * identify an instance of the service in the cache. A michael@0: * factory may handle all descriptors for an ID, or just a michael@0: * particular descriptor. The factory can either parse the michael@0: * descriptor or use custom API on the key in order to michael@0: * instantiate the service.

michael@0: * michael@0: * @param result the output parameter to which the current id will be appended. michael@0: * @return the modified result. michael@0: */ michael@0: virtual UnicodeString& currentDescriptor(UnicodeString& result) const; michael@0: michael@0: /** michael@0: *

If the key has a fallback, modify the key and return true, michael@0: * otherwise return false. The current ID will change if there michael@0: * is a fallback. No currentIDs should be repeated, and fallback michael@0: * must eventually return false. This implementation has no fallbacks michael@0: * and always returns false.

michael@0: * michael@0: * @return TRUE if the ICUServiceKey changed to a valid fallback value. michael@0: */ michael@0: virtual UBool fallback(); michael@0: michael@0: /** michael@0: *

Return TRUE if a key created from id matches, or would eventually michael@0: * fallback to match, the canonical ID of this ICUServiceKey.

michael@0: * michael@0: * @param id the id to test. michael@0: * @return TRUE if this ICUServiceKey's canonical ID is a fallback of id. michael@0: */ michael@0: virtual UBool isFallbackOf(const UnicodeString& id) const; michael@0: michael@0: /** michael@0: *

Return the prefix. This implementation leaves result unchanged. michael@0: * Result is returned as a convenience.

michael@0: * michael@0: * @param result the output parameter to which the prefix will be appended. michael@0: * @return the modified result. michael@0: */ michael@0: virtual UnicodeString& prefix(UnicodeString& result) const; michael@0: michael@0: /** michael@0: *

A utility to parse the prefix out of a descriptor string. Only michael@0: * the (undelimited) prefix, if any, remains in result. Result is returned as a michael@0: * convenience.

michael@0: * michael@0: * @param result an input/output parameter that on entry is a descriptor, and michael@0: * on exit is the prefix of that descriptor. michael@0: * @return the modified result. michael@0: */ michael@0: static UnicodeString& parsePrefix(UnicodeString& result); michael@0: michael@0: /** michael@0: *

A utility to parse the suffix out of a descriptor string. Only michael@0: * the (undelimited) suffix, if any, remains in result. Result is returned as a michael@0: * convenience.

michael@0: * michael@0: * @param result an input/output parameter that on entry is a descriptor, and michael@0: * on exit is the suffix of that descriptor. michael@0: * @return the modified result. michael@0: */ michael@0: static UnicodeString& parseSuffix(UnicodeString& result); michael@0: michael@0: public: michael@0: /** michael@0: * UObject RTTI boilerplate. michael@0: */ michael@0: static UClassID U_EXPORT2 getStaticClassID(); michael@0: michael@0: /** michael@0: * UObject RTTI boilerplate. michael@0: */ michael@0: virtual UClassID getDynamicClassID() const; michael@0: michael@0: #ifdef SERVICE_DEBUG michael@0: public: michael@0: virtual UnicodeString& debug(UnicodeString& result) const; michael@0: virtual UnicodeString& debugClass(UnicodeString& result) const; michael@0: #endif michael@0: michael@0: }; michael@0: michael@0: /******************************************************************* michael@0: * ICUServiceFactory michael@0: */ michael@0: michael@0: /** michael@0: *

An implementing ICUServiceFactory generates the service objects maintained by the michael@0: * service. A factory generates a service object from a key, michael@0: * updates id->factory mappings, and returns the display name for michael@0: * a supported id.

michael@0: */ michael@0: class U_COMMON_API ICUServiceFactory : public UObject { michael@0: public: michael@0: virtual ~ICUServiceFactory(); michael@0: michael@0: /** michael@0: *

Create a service object from the key, if this factory michael@0: * supports the key. Otherwise, return NULL.

michael@0: * michael@0: *

If the factory supports the key, then it can call michael@0: * the service's getKey(ICUServiceKey, String[], ICUServiceFactory) method michael@0: * passing itself as the factory to get the object that michael@0: * the service would have created prior to the factory's michael@0: * registration with the service. This can change the michael@0: * key, so any information required from the key should michael@0: * be extracted before making such a callback.

michael@0: * michael@0: * @param key the service key. michael@0: * @param service the service with which this factory is registered. michael@0: * @param status the error code status. michael@0: * @return the service object, or NULL if the factory does not support the key. michael@0: */ michael@0: virtual UObject* create(const ICUServiceKey& key, const ICUService* service, UErrorCode& status) const = 0; michael@0: michael@0: /** michael@0: *

Update result to reflect the IDs (not descriptors) that this michael@0: * factory publicly handles. Result contains mappings from ID to michael@0: * factory. On entry it will contain all (visible) mappings from michael@0: * previously-registered factories.

michael@0: * michael@0: *

This function, together with getDisplayName, are used to michael@0: * support ICUService::getDisplayNames. The factory determines michael@0: * which IDs (of those it supports) it will make visible, and of michael@0: * those, which it will provide localized display names for. In michael@0: * most cases it will register mappings from all IDs it supports michael@0: * to itself.

michael@0: * michael@0: * @param result the mapping table to update. michael@0: * @param status the error code status. michael@0: */ michael@0: virtual void updateVisibleIDs(Hashtable& result, UErrorCode& status) const = 0; michael@0: michael@0: /** michael@0: *

Return, in result, the display name of the id in the provided locale. michael@0: * This is an id, not a descriptor. If the id is michael@0: * not visible, sets result to bogus. If the michael@0: * incoming result is bogus, it remains bogus. Result is returned as a michael@0: * convenience. Results are not defined if id is not one supported by this michael@0: * factory.

michael@0: * michael@0: * @param id a visible id supported by this factory. michael@0: * @param locale the locale for which to generate the corresponding localized display name. michael@0: * @param result output parameter to hold the display name. michael@0: * @return result. michael@0: */ michael@0: virtual UnicodeString& getDisplayName(const UnicodeString& id, const Locale& locale, UnicodeString& result) const = 0; michael@0: }; michael@0: michael@0: /* michael@0: ****************************************************************** michael@0: */ michael@0: michael@0: /** michael@0: *

A default implementation of factory. This provides default michael@0: * implementations for subclasses, and implements a singleton michael@0: * factory that matches a single ID and returns a single michael@0: * (possibly deferred-initialized) instance. This implements michael@0: * updateVisibleIDs to add a mapping from its ID to itself michael@0: * if visible is true, or to remove any existing mapping michael@0: * for its ID if visible is false. No localization of display michael@0: * names is performed.

michael@0: */ michael@0: class U_COMMON_API SimpleFactory : public ICUServiceFactory { michael@0: protected: michael@0: UObject* _instance; michael@0: const UnicodeString _id; michael@0: const UBool _visible; michael@0: michael@0: public: michael@0: /** michael@0: *

Construct a SimpleFactory that maps a single ID to a single michael@0: * service instance. If visible is TRUE, the ID will be visible. michael@0: * The instance must not be NULL. The SimpleFactory will adopt michael@0: * the instance, which must not be changed subsequent to this call.

michael@0: * michael@0: * @param instanceToAdopt the service instance to adopt. michael@0: * @param id the ID to assign to this service instance. michael@0: * @param visible if TRUE, the ID will be visible. michael@0: */ michael@0: SimpleFactory(UObject* instanceToAdopt, const UnicodeString& id, UBool visible = TRUE); michael@0: michael@0: /** michael@0: *

Destructor.

michael@0: */ michael@0: virtual ~SimpleFactory(); michael@0: michael@0: /** michael@0: *

This implementation returns a clone of the service instance if the factory's ID is equal to michael@0: * the key's currentID. Service and prefix are ignored.

michael@0: * michael@0: * @param key the service key. michael@0: * @param service the service with which this factory is registered. michael@0: * @param status the error code status. michael@0: * @return the service object, or NULL if the factory does not support the key. michael@0: */ michael@0: virtual UObject* create(const ICUServiceKey& key, const ICUService* service, UErrorCode& status) const; michael@0: michael@0: /** michael@0: *

This implementation adds a mapping from ID -> this to result if visible is TRUE, michael@0: * otherwise it removes ID from result.

michael@0: * michael@0: * @param result the mapping table to update. michael@0: * @param status the error code status. michael@0: */ michael@0: virtual void updateVisibleIDs(Hashtable& result, UErrorCode& status) const; michael@0: michael@0: /** michael@0: *

This implementation returns the factory ID if it equals id and visible is TRUE, michael@0: * otherwise it returns the empty string. (This implementation provides michael@0: * no localized id information.)

michael@0: * michael@0: * @param id a visible id supported by this factory. michael@0: * @param locale the locale for which to generate the corresponding localized display name. michael@0: * @param result output parameter to hold the display name. michael@0: * @return result. michael@0: */ michael@0: virtual UnicodeString& getDisplayName(const UnicodeString& id, const Locale& locale, UnicodeString& result) const; michael@0: michael@0: public: michael@0: /** michael@0: * UObject RTTI boilerplate. michael@0: */ michael@0: static UClassID U_EXPORT2 getStaticClassID(); michael@0: michael@0: /** michael@0: * UObject RTTI boilerplate. michael@0: */ michael@0: virtual UClassID getDynamicClassID() const; michael@0: michael@0: #ifdef SERVICE_DEBUG michael@0: public: michael@0: virtual UnicodeString& debug(UnicodeString& toAppendTo) const; michael@0: virtual UnicodeString& debugClass(UnicodeString& toAppendTo) const; michael@0: #endif michael@0: michael@0: }; michael@0: michael@0: /* michael@0: ****************************************************************** michael@0: */ michael@0: michael@0: /** michael@0: *

ServiceListener is the listener that ICUService provides by default. michael@0: * ICUService will notifiy this listener when factories are added to michael@0: * or removed from the service. Subclasses can provide michael@0: * different listener interfaces that extend EventListener, and modify michael@0: * acceptsListener and notifyListener as appropriate.

michael@0: */ michael@0: class U_COMMON_API ServiceListener : public EventListener { michael@0: public: michael@0: virtual ~ServiceListener(); michael@0: michael@0: /** michael@0: *

This method is called when the service changes. At the time of the michael@0: * call this listener is registered with the service. It must michael@0: * not modify the notifier in the context of this call.

michael@0: * michael@0: * @param service the service that changed. michael@0: */ michael@0: virtual void serviceChanged(const ICUService& service) const = 0; michael@0: michael@0: public: michael@0: /** michael@0: * UObject RTTI boilerplate. michael@0: */ michael@0: static UClassID U_EXPORT2 getStaticClassID(); michael@0: michael@0: /** michael@0: * UObject RTTI boilerplate. michael@0: */ michael@0: virtual UClassID getDynamicClassID() const; michael@0: michael@0: }; michael@0: michael@0: /* michael@0: ****************************************************************** michael@0: */ michael@0: michael@0: /** michael@0: *

A StringPair holds a displayName/ID pair. ICUService uses it michael@0: * as the array elements returned by getDisplayNames. michael@0: */ michael@0: class U_COMMON_API StringPair : public UMemory { michael@0: public: michael@0: /** michael@0: *

The display name of the pair.

michael@0: */ michael@0: const UnicodeString displayName; michael@0: michael@0: /** michael@0: *

The ID of the pair.

michael@0: */ michael@0: const UnicodeString id; michael@0: michael@0: /** michael@0: *

Creates a string pair from a displayName and an ID.

michael@0: * michael@0: * @param displayName the displayName. michael@0: * @param id the ID. michael@0: * @param status the error code status. michael@0: * @return a StringPair if the creation was successful, otherwise NULL. michael@0: */ michael@0: static StringPair* create(const UnicodeString& displayName, michael@0: const UnicodeString& id, michael@0: UErrorCode& status); michael@0: michael@0: /** michael@0: *

Return TRUE if either string of the pair is bogus.

michael@0: * @return TRUE if either string of the pair is bogus. michael@0: */ michael@0: UBool isBogus() const; michael@0: michael@0: private: michael@0: StringPair(const UnicodeString& displayName, const UnicodeString& id); michael@0: }; michael@0: michael@0: /******************************************************************* michael@0: * ICUService michael@0: */ michael@0: michael@0: /** michael@0: *

A Service provides access to service objects that implement a michael@0: * particular service, e.g. transliterators. Users provide a String michael@0: * id (for example, a locale string) to the service, and get back an michael@0: * object for that id. Service objects can be any kind of object. A michael@0: * new service object is returned for each query. The caller is michael@0: * responsible for deleting it.

michael@0: * michael@0: *

Services 'canonicalize' the query ID and use the canonical ID to michael@0: * query for the service. The service also defines a mechanism to michael@0: * 'fallback' the ID multiple times. Clients can optionally request michael@0: * the actual ID that was matched by a query when they use an ID to michael@0: * retrieve a service object.

michael@0: * michael@0: *

Service objects are instantiated by ICUServiceFactory objects michael@0: * registered with the service. The service queries each michael@0: * ICUServiceFactory in turn, from most recently registered to michael@0: * earliest registered, until one returns a service object. If none michael@0: * responds with a service object, a fallback ID is generated, and the michael@0: * process repeats until a service object is returned or until the ID michael@0: * has no further fallbacks.

michael@0: * michael@0: *

In ICU 2.4, UObject (the base class of service instances) does michael@0: * not define a polymorphic clone function. ICUService uses clones to michael@0: * manage ownership. Thus, for now, ICUService defines an abstract michael@0: * method, cloneInstance, that clients must implement to create clones michael@0: * of the service instances. This may change in future releases of michael@0: * ICU.

michael@0: * michael@0: *

ICUServiceFactories can be dynamically registered and michael@0: * unregistered with the service. When registered, an michael@0: * ICUServiceFactory is installed at the head of the factory list, and michael@0: * so gets 'first crack' at any keys or fallback keys. When michael@0: * unregistered, it is removed from the service and can no longer be michael@0: * located through it. Service objects generated by this factory and michael@0: * held by the client are unaffected.

michael@0: * michael@0: *

If a service has variants (e.g., the different variants of michael@0: * BreakIterator) an ICUServiceFactory can use the prefix of the michael@0: * ICUServiceKey to determine the variant of a service to generate. michael@0: * If it does not support all variants, it can request michael@0: * previously-registered factories to handle the ones it does not michael@0: * support.

michael@0: * michael@0: *

ICUService uses ICUServiceKeys to query factories and perform michael@0: * fallback. The ICUServiceKey defines the canonical form of the ID, michael@0: * and implements the fallback strategy. Custom ICUServiceKeys can be michael@0: * defined that parse complex IDs into components that michael@0: * ICUServiceFactories can more easily use. The ICUServiceKey can michael@0: * cache the results of this parsing to save repeated effort. michael@0: * ICUService provides convenience APIs that take UnicodeStrings and michael@0: * generate default ICUServiceKeys for use in querying.

michael@0: * michael@0: *

ICUService provides API to get the list of IDs publicly michael@0: * supported by the service (although queries aren't restricted to michael@0: * this list). This list contains only 'simple' IDs, and not fully michael@0: * unique IDs. ICUServiceFactories are associated with each simple ID michael@0: * and the responsible factory can also return a human-readable michael@0: * localized version of the simple ID, for use in user interfaces. michael@0: * ICUService can also provide an array of the all the localized michael@0: * visible IDs and their corresponding internal IDs.

michael@0: * michael@0: *

ICUService implements ICUNotifier, so that clients can register michael@0: * to receive notification when factories are added or removed from michael@0: * the service. ICUService provides a default EventListener michael@0: * subinterface, ServiceListener, which can be registered with the michael@0: * service. When the service changes, the ServiceListener's michael@0: * serviceChanged method is called with the service as the michael@0: * argument.

michael@0: * michael@0: *

The ICUService API is both rich and generic, and it is expected michael@0: * that most implementations will statically 'wrap' ICUService to michael@0: * present a more appropriate API-- for example, to declare the type michael@0: * of the objects returned from get, to limit the factories that can michael@0: * be registered with the service, or to define their own listener michael@0: * interface with a custom callback method. They might also customize michael@0: * ICUService by overriding it, for example, to customize the michael@0: * ICUServiceKey and fallback strategy. ICULocaleService is a michael@0: * subclass of ICUService that uses Locale names as IDs and uses michael@0: * ICUServiceKeys that implement the standard resource bundle fallback michael@0: * strategy. Most clients will wish to subclass it instead of michael@0: * ICUService.

michael@0: */ michael@0: class U_COMMON_API ICUService : public ICUNotifier { michael@0: protected: michael@0: /** michael@0: * Name useful for debugging. michael@0: */ michael@0: const UnicodeString name; michael@0: michael@0: private: michael@0: michael@0: /** michael@0: * Timestamp so iterators can be fail-fast. michael@0: */ michael@0: uint32_t timestamp; michael@0: michael@0: /** michael@0: * All the factories registered with this service. michael@0: */ michael@0: UVector* factories; michael@0: michael@0: /** michael@0: * The service cache. michael@0: */ michael@0: Hashtable* serviceCache; michael@0: michael@0: /** michael@0: * The ID cache. michael@0: */ michael@0: Hashtable* idCache; michael@0: michael@0: /** michael@0: * The name cache. michael@0: */ michael@0: DNCache* dnCache; michael@0: michael@0: /** michael@0: * Constructor. michael@0: */ michael@0: public: michael@0: /** michael@0: *

Construct a new ICUService.

michael@0: */ michael@0: ICUService(); michael@0: michael@0: /** michael@0: *

Construct with a name (useful for debugging).

michael@0: * michael@0: * @param name a name to use in debugging. michael@0: */ michael@0: ICUService(const UnicodeString& name); michael@0: michael@0: /** michael@0: *

Destructor.

michael@0: */ michael@0: virtual ~ICUService(); michael@0: michael@0: /** michael@0: *

Return the name of this service. This will be the empty string if none was assigned. michael@0: * Returns result as a convenience.

michael@0: * michael@0: * @param result an output parameter to contain the name of this service. michael@0: * @return the name of this service. michael@0: */ michael@0: UnicodeString& getName(UnicodeString& result) const; michael@0: michael@0: /** michael@0: *

Convenience override for get(ICUServiceKey&, UnicodeString*). This uses michael@0: * createKey to create a key for the provided descriptor.

michael@0: * michael@0: * @param descriptor the descriptor. michael@0: * @param status the error code status. michael@0: * @return the service instance, or NULL. michael@0: */ michael@0: UObject* get(const UnicodeString& descriptor, UErrorCode& status) const; michael@0: michael@0: /** michael@0: *

Convenience override for get(ICUServiceKey&, UnicodeString*). This uses michael@0: * createKey to create a key from the provided descriptor.

michael@0: * michael@0: * @param descriptor the descriptor. michael@0: * @param actualReturn a pointer to a UnicodeString to hold the matched descriptor, or NULL. michael@0: * @param status the error code status. michael@0: * @return the service instance, or NULL. michael@0: */ michael@0: UObject* get(const UnicodeString& descriptor, UnicodeString* actualReturn, UErrorCode& status) const; michael@0: michael@0: /** michael@0: *

Convenience override for get(ICUServiceKey&, UnicodeString*).

michael@0: * michael@0: * @param key the key. michael@0: * @param status the error code status. michael@0: * @return the service instance, or NULL. michael@0: */ michael@0: UObject* getKey(ICUServiceKey& key, UErrorCode& status) const; michael@0: michael@0: /** michael@0: *

Given a key, return a service object, and, if actualReturn michael@0: * is not NULL, the descriptor with which it was found in the michael@0: * first element of actualReturn. If no service object matches michael@0: * this key, returns NULL and leaves actualReturn unchanged.

michael@0: * michael@0: *

This queries the cache using the key's descriptor, and if no michael@0: * object in the cache matches, tries the key on each michael@0: * registered factory, in order. If none generates a service michael@0: * object for the key, repeats the process with each fallback of michael@0: * the key, until either a factory returns a service object, or the key michael@0: * has no fallback. If no object is found, the result of handleDefault michael@0: * is returned.

michael@0: * michael@0: *

Subclasses can override this method to further customize the michael@0: * result before returning it. michael@0: * michael@0: * @param key the key. michael@0: * @param actualReturn a pointer to a UnicodeString to hold the matched descriptor, or NULL. michael@0: * @param status the error code status. michael@0: * @return the service instance, or NULL. michael@0: */ michael@0: virtual UObject* getKey(ICUServiceKey& key, UnicodeString* actualReturn, UErrorCode& status) const; michael@0: michael@0: /** michael@0: *

This version of getKey is only called by ICUServiceFactories within the scope michael@0: * of a previous getKey call, to determine what previously-registered factories would michael@0: * have returned. For details, see getKey(ICUServiceKey&, UErrorCode&). Subclasses michael@0: * should not call it directly, but call through one of the other get functions.

michael@0: * michael@0: * @param key the key. michael@0: * @param actualReturn a pointer to a UnicodeString to hold the matched descriptor, or NULL. michael@0: * @param factory the factory making the recursive call. michael@0: * @param status the error code status. michael@0: * @return the service instance, or NULL. michael@0: */ michael@0: UObject* getKey(ICUServiceKey& key, UnicodeString* actualReturn, const ICUServiceFactory* factory, UErrorCode& status) const; michael@0: michael@0: /** michael@0: *

Convenience override for getVisibleIDs(String) that passes null michael@0: * as the fallback, thus returning all visible IDs.

michael@0: * michael@0: * @param result a vector to hold the returned IDs. michael@0: * @param status the error code status. michael@0: * @return the result vector. michael@0: */ michael@0: UVector& getVisibleIDs(UVector& result, UErrorCode& status) const; michael@0: michael@0: /** michael@0: *

Return a snapshot of the visible IDs for this service. This michael@0: * list will not change as ICUServiceFactories are added or removed, but the michael@0: * supported IDs will, so there is no guarantee that all and only michael@0: * the IDs in the returned list will be visible and supported by the michael@0: * service in subsequent calls.

michael@0: * michael@0: *

The IDs are returned as pointers to UnicodeStrings. The michael@0: * caller owns the IDs. Previous contents of result are discarded before michael@0: * new elements, if any, are added.

michael@0: * michael@0: *

matchID is passed to createKey to create a key. If the key michael@0: * is not NULL, its isFallbackOf method is used to filter out IDs michael@0: * that don't match the key or have it as a fallback.

michael@0: * michael@0: * @param result a vector to hold the returned IDs. michael@0: * @param matchID an ID used to filter the result, or NULL if all IDs are desired. michael@0: * @param status the error code status. michael@0: * @return the result vector. michael@0: */ michael@0: UVector& getVisibleIDs(UVector& result, const UnicodeString* matchID, UErrorCode& status) const; michael@0: michael@0: /** michael@0: *

Convenience override for getDisplayName(const UnicodeString&, const Locale&, UnicodeString&) that michael@0: * uses the current default locale.

michael@0: * michael@0: * @param id the ID for which to retrieve the localized displayName. michael@0: * @param result an output parameter to hold the display name. michael@0: * @return the modified result. michael@0: */ michael@0: UnicodeString& getDisplayName(const UnicodeString& id, UnicodeString& result) const; michael@0: michael@0: /** michael@0: *

Given a visible ID, return the display name in the requested locale. michael@0: * If there is no directly supported ID corresponding to this ID, result is michael@0: * set to bogus.

michael@0: * michael@0: * @param id the ID for which to retrieve the localized displayName. michael@0: * @param result an output parameter to hold the display name. michael@0: * @param locale the locale in which to localize the ID. michael@0: * @return the modified result. michael@0: */ michael@0: UnicodeString& getDisplayName(const UnicodeString& id, UnicodeString& result, const Locale& locale) const; michael@0: michael@0: /** michael@0: *

Convenience override of getDisplayNames(const Locale&, const UnicodeString*) that michael@0: * uses the current default Locale as the locale and NULL for michael@0: * the matchID.

michael@0: * michael@0: * @param result a vector to hold the returned displayName/id StringPairs. michael@0: * @param status the error code status. michael@0: * @return the modified result vector. michael@0: */ michael@0: UVector& getDisplayNames(UVector& result, UErrorCode& status) const; michael@0: michael@0: /** michael@0: *

Convenience override of getDisplayNames(const Locale&, const UnicodeString*) that michael@0: * uses NULL for the matchID.

michael@0: * michael@0: * @param result a vector to hold the returned displayName/id StringPairs. michael@0: * @param locale the locale in which to localize the ID. michael@0: * @param status the error code status. michael@0: * @return the modified result vector. michael@0: */ michael@0: UVector& getDisplayNames(UVector& result, const Locale& locale, UErrorCode& status) const; michael@0: michael@0: /** michael@0: *

Return a snapshot of the mapping from display names to visible michael@0: * IDs for this service. This set will not change as factories michael@0: * are added or removed, but the supported IDs will, so there is michael@0: * no guarantee that all and only the IDs in the returned map will michael@0: * be visible and supported by the service in subsequent calls, michael@0: * nor is there any guarantee that the current display names match michael@0: * those in the result.

michael@0: * michael@0: *

The names are returned as pointers to StringPairs, which michael@0: * contain both the displayName and the corresponding ID. The michael@0: * caller owns the StringPairs. Previous contents of result are michael@0: * discarded before new elements, if any, are added.

michael@0: * michael@0: *

matchID is passed to createKey to create a key. If the key michael@0: * is not NULL, its isFallbackOf method is used to filter out IDs michael@0: * that don't match the key or have it as a fallback.

michael@0: * michael@0: * @param result a vector to hold the returned displayName/id StringPairs. michael@0: * @param locale the locale in which to localize the ID. michael@0: * @param matchID an ID used to filter the result, or NULL if all IDs are desired. michael@0: * @param status the error code status. michael@0: * @return the result vector. */ michael@0: UVector& getDisplayNames(UVector& result, michael@0: const Locale& locale, michael@0: const UnicodeString* matchID, michael@0: UErrorCode& status) const; michael@0: michael@0: /** michael@0: *

A convenience override of registerInstance(UObject*, const UnicodeString&, UBool) michael@0: * that defaults visible to TRUE.

michael@0: * michael@0: * @param objToAdopt the object to register and adopt. michael@0: * @param id the ID to assign to this object. michael@0: * @param status the error code status. michael@0: * @return a registry key that can be passed to unregister to unregister michael@0: * (and discard) this instance. michael@0: */ michael@0: URegistryKey registerInstance(UObject* objToAdopt, const UnicodeString& id, UErrorCode& status); michael@0: michael@0: /** michael@0: *

Register a service instance with the provided ID. The ID will be michael@0: * canonicalized. The canonicalized ID will be returned by michael@0: * getVisibleIDs if visible is TRUE. The service instance will be adopted and michael@0: * must not be modified subsequent to this call.

michael@0: * michael@0: *

This issues a serviceChanged notification to registered listeners.

michael@0: * michael@0: *

This implementation wraps the object using michael@0: * createSimpleFactory, and calls registerFactory.

michael@0: * michael@0: * @param objToAdopt the object to register and adopt. michael@0: * @param id the ID to assign to this object. michael@0: * @param visible TRUE if getVisibleIDs is to return this ID. michael@0: * @param status the error code status. michael@0: * @return a registry key that can be passed to unregister() to unregister michael@0: * (and discard) this instance. michael@0: */ michael@0: virtual URegistryKey registerInstance(UObject* objToAdopt, const UnicodeString& id, UBool visible, UErrorCode& status); michael@0: michael@0: /** michael@0: *

Register an ICUServiceFactory. Returns a registry key that michael@0: * can be used to unregister the factory. The factory michael@0: * must not be modified subsequent to this call. The service owns michael@0: * all registered factories. In case of an error, the factory is michael@0: * deleted.

michael@0: * michael@0: *

This issues a serviceChanged notification to registered listeners.

michael@0: * michael@0: *

The default implementation accepts all factories.

michael@0: * michael@0: * @param factoryToAdopt the factory to register and adopt. michael@0: * @param status the error code status. michael@0: * @return a registry key that can be passed to unregister to unregister michael@0: * (and discard) this factory. michael@0: */ michael@0: virtual URegistryKey registerFactory(ICUServiceFactory* factoryToAdopt, UErrorCode& status); michael@0: michael@0: /** michael@0: *

Unregister a factory using a registry key returned by michael@0: * registerInstance or registerFactory. After a successful call, michael@0: * the factory will be removed from the service factory list and michael@0: * deleted, and the key becomes invalid.

michael@0: * michael@0: *

This issues a serviceChanged notification to registered michael@0: * listeners.

michael@0: * michael@0: * @param rkey the registry key. michael@0: * @param status the error code status. michael@0: * @return TRUE if the call successfully unregistered the factory. michael@0: */ michael@0: virtual UBool unregister(URegistryKey rkey, UErrorCode& status); michael@0: michael@0: /** michael@0: *

Reset the service to the default factories. The factory michael@0: * lock is acquired and then reInitializeFactories is called.

michael@0: * michael@0: *

This issues a serviceChanged notification to registered listeners.

michael@0: */ michael@0: virtual void reset(void); michael@0: michael@0: /** michael@0: *

Return TRUE if the service is in its default state.

michael@0: * michael@0: *

The default implementation returns TRUE if there are no michael@0: * factories registered.

michael@0: */ michael@0: virtual UBool isDefault(void) const; michael@0: michael@0: /** michael@0: *

Create a key from an ID. If ID is NULL, returns NULL.

michael@0: * michael@0: *

The default implementation creates an ICUServiceKey instance. michael@0: * Subclasses can override to define more useful keys appropriate michael@0: * to the factories they accept.

michael@0: * michael@0: * @param a pointer to the ID for which to create a default ICUServiceKey. michael@0: * @param status the error code status. michael@0: * @return the ICUServiceKey corresponding to ID, or NULL. michael@0: */ michael@0: virtual ICUServiceKey* createKey(const UnicodeString* id, UErrorCode& status) const; michael@0: michael@0: /** michael@0: *

Clone object so that caller can own the copy. In ICU2.4, UObject doesn't define michael@0: * clone, so we need an instance-aware method that knows how to do this. michael@0: * This is public so factories can call it, but should really be protected.

michael@0: * michael@0: * @param instance the service instance to clone. michael@0: * @return a clone of the passed-in instance, or NULL if cloning was unsuccessful. michael@0: */ michael@0: virtual UObject* cloneInstance(UObject* instance) const = 0; michael@0: michael@0: michael@0: /************************************************************************ michael@0: * Subclassing API michael@0: */ michael@0: michael@0: protected: michael@0: michael@0: /** michael@0: *

Create a factory that wraps a single service object. Called by registerInstance.

michael@0: * michael@0: *

The default implementation returns an instance of SimpleFactory.

michael@0: * michael@0: * @param instanceToAdopt the service instance to adopt. michael@0: * @param id the ID to assign to this service instance. michael@0: * @param visible if TRUE, the ID will be visible. michael@0: * @param status the error code status. michael@0: * @return an instance of ICUServiceFactory that maps this instance to the provided ID. michael@0: */ michael@0: virtual ICUServiceFactory* createSimpleFactory(UObject* instanceToAdopt, const UnicodeString& id, UBool visible, UErrorCode& status); michael@0: michael@0: /** michael@0: *

Reinitialize the factory list to its default state. After this call, isDefault() michael@0: * must return TRUE.

michael@0: * michael@0: *

This issues a serviceChanged notification to registered listeners.

michael@0: * michael@0: *

The default implementation clears the factory list. michael@0: * Subclasses can override to provide other default initialization michael@0: * of the factory list. Subclasses must not call this method michael@0: * directly, since it must only be called while holding write michael@0: * access to the factory list.

michael@0: */ michael@0: virtual void reInitializeFactories(void); michael@0: michael@0: /** michael@0: *

Default handler for this service if no factory in the factory list michael@0: * handled the key passed to getKey.

michael@0: * michael@0: *

The default implementation returns NULL.

michael@0: * michael@0: * @param key the key. michael@0: * @param actualReturn a pointer to a UnicodeString to hold the matched descriptor, or NULL. michael@0: * @param status the error code status. michael@0: * @return the service instance, or NULL. michael@0: */ michael@0: virtual UObject* handleDefault(const ICUServiceKey& key, UnicodeString* actualReturn, UErrorCode& status) const; michael@0: michael@0: /** michael@0: *

Clear caches maintained by this service.

michael@0: * michael@0: *

Subclasses can override if they implement additional caches michael@0: * that need to be cleared when the service changes. Subclasses michael@0: * should generally not call this method directly, as it must only michael@0: * be called while synchronized on the factory lock.

michael@0: */ michael@0: virtual void clearCaches(void); michael@0: michael@0: /** michael@0: *

Return true if the listener is accepted.

michael@0: * michael@0: *

The default implementation accepts the listener if it is michael@0: * a ServiceListener. Subclasses can override this to accept michael@0: * different listeners.

michael@0: * michael@0: * @param l the listener to test. michael@0: * @return TRUE if the service accepts the listener. michael@0: */ michael@0: virtual UBool acceptsListener(const EventListener& l) const; michael@0: michael@0: /** michael@0: *

Notify the listener of a service change.

michael@0: * michael@0: *

The default implementation assumes a ServiceListener. michael@0: * If acceptsListener has been overridden to accept different michael@0: * listeners, this should be overridden as well.

michael@0: * michael@0: * @param l the listener to notify. michael@0: */ michael@0: virtual void notifyListener(EventListener& l) const; michael@0: michael@0: /************************************************************************ michael@0: * Utilities for subclasses. michael@0: */ michael@0: michael@0: /** michael@0: *

Clear only the service cache.

michael@0: * michael@0: *

This can be called by subclasses when a change affects the service michael@0: * cache but not the ID caches, e.g., when the default locale changes michael@0: * the resolution of IDs also changes, requiring the cache to be michael@0: * flushed, but not the visible IDs themselves.

michael@0: */ michael@0: void clearServiceCache(void); michael@0: michael@0: /** michael@0: *

Return a map from visible IDs to factories. michael@0: * This must only be called when the mutex is held.

michael@0: * michael@0: * @param status the error code status. michael@0: * @return a Hashtable containing mappings from visible michael@0: * IDs to factories. michael@0: */ michael@0: const Hashtable* getVisibleIDMap(UErrorCode& status) const; michael@0: michael@0: /** michael@0: *

Allow subclasses to read the time stamp.

michael@0: * michael@0: * @return the timestamp. michael@0: */ michael@0: int32_t getTimestamp(void) const; michael@0: michael@0: /** michael@0: *

Return the number of registered factories.

michael@0: * michael@0: * @return the number of factories registered at the time of the call. michael@0: */ michael@0: int32_t countFactories(void) const; michael@0: michael@0: private: michael@0: michael@0: friend class ::ICUServiceTest; // give tests access to countFactories. michael@0: }; michael@0: michael@0: U_NAMESPACE_END michael@0: michael@0: /* UCONFIG_NO_SERVICE */ michael@0: #endif michael@0: michael@0: /* ICUSERV_H */ michael@0: #endif michael@0: