// © 2016 and later: Unicode, Inc. and others. // License & terms of use: http://www.unicode.org/copyright.html /** ******************************************************************************* * Copyright (C) 2001-2011, International Business Machines Corporation. * * All Rights Reserved. * ******************************************************************************* */ #ifndef ICUSERV_H #define ICUSERV_H #include "unicode/utypes.h" #if UCONFIG_NO_SERVICE U_NAMESPACE_BEGIN /* * Allow the declaration of APIs with pointers to ICUService * even when service is removed from the build. */ class ICUService; U_NAMESPACE_END #else #include "unicode/unistr.h" #include "unicode/locid.h" #include "unicode/umisc.h" #include "hash.h" #include "uvector.h" #include "servnotf.h" class ICUServiceTest; U_NAMESPACE_BEGIN class ICUServiceKey; class ICUServiceFactory; class SimpleFactory; class ServiceListener; class ICUService; class DNCache; /******************************************************************* * ICUServiceKey */ /** * <p>ICUServiceKeys are used to communicate with factories to * generate an instance of the service. ICUServiceKeys define how * ids are canonicalized, provide both a current id and a current * descriptor to use in querying the cache and factories, and * determine the fallback strategy.</p> * * <p>ICUServiceKeys provide both a currentDescriptor and a currentID. * The descriptor contains an optional prefix, followed by '/' * and the currentID. Factories that handle complex keys, * for example number format factories that generate multiple * kinds of formatters for the same locale, use the descriptor * to provide a fully unique identifier for the service object, * while using the currentID (in this case, the locale string), * as the visible IDs that can be localized.</p> * * <p>The default implementation of ICUServiceKey has no fallbacks and * has no custom descriptors.</p> */ class U_COMMON_API ICUServiceKey : public UObject { … }; /******************************************************************* * ICUServiceFactory */ /** * <p>An implementing ICUServiceFactory generates the service objects maintained by the * service. A factory generates a service object from a key, * updates id->factory mappings, and returns the display name for * a supported id.</p> */ class U_COMMON_API ICUServiceFactory : public UObject { … }; /* ****************************************************************** */ /** * <p>A default implementation of factory. This provides default * implementations for subclasses, and implements a singleton * factory that matches a single ID and returns a single * (possibly deferred-initialized) instance. This implements * updateVisibleIDs to add a mapping from its ID to itself * if visible is true, or to remove any existing mapping * for its ID if visible is false. No localization of display * names is performed.</p> */ class U_COMMON_API SimpleFactory : public ICUServiceFactory { … }; /* ****************************************************************** */ /** * <p>ServiceListener is the listener that ICUService provides by default. * ICUService will notify this listener when factories are added to * or removed from the service. Subclasses can provide * different listener interfaces that extend EventListener, and modify * acceptsListener and notifyListener as appropriate.</p> */ class U_COMMON_API ServiceListener : public EventListener { … }; /* ****************************************************************** */ /** * <p>A StringPair holds a displayName/ID pair. ICUService uses it * as the array elements returned by getDisplayNames. */ class U_COMMON_API StringPair : public UMemory { … }; /******************************************************************* * ICUService */ /** * <p>A Service provides access to service objects that implement a * particular service, e.g. transliterators. Users provide a String * id (for example, a locale string) to the service, and get back an * object for that id. Service objects can be any kind of object. A * new service object is returned for each query. The caller is * responsible for deleting it.</p> * * <p>Services 'canonicalize' the query ID and use the canonical ID to * query for the service. The service also defines a mechanism to * 'fallback' the ID multiple times. Clients can optionally request * the actual ID that was matched by a query when they use an ID to * retrieve a service object.</p> * * <p>Service objects are instantiated by ICUServiceFactory objects * registered with the service. The service queries each * ICUServiceFactory in turn, from most recently registered to * earliest registered, until one returns a service object. If none * responds with a service object, a fallback ID is generated, and the * process repeats until a service object is returned or until the ID * has no further fallbacks.</p> * * <p>In ICU 2.4, UObject (the base class of service instances) does * not define a polymorphic clone function. ICUService uses clones to * manage ownership. Thus, for now, ICUService defines an abstract * method, cloneInstance, that clients must implement to create clones * of the service instances. This may change in future releases of * ICU.</p> * * <p>ICUServiceFactories can be dynamically registered and * unregistered with the service. When registered, an * ICUServiceFactory is installed at the head of the factory list, and * so gets 'first crack' at any keys or fallback keys. When * unregistered, it is removed from the service and can no longer be * located through it. Service objects generated by this factory and * held by the client are unaffected.</p> * * <p>If a service has variants (e.g., the different variants of * BreakIterator) an ICUServiceFactory can use the prefix of the * ICUServiceKey to determine the variant of a service to generate. * If it does not support all variants, it can request * previously-registered factories to handle the ones it does not * support.</p> * * <p>ICUService uses ICUServiceKeys to query factories and perform * fallback. The ICUServiceKey defines the canonical form of the ID, * and implements the fallback strategy. Custom ICUServiceKeys can be * defined that parse complex IDs into components that * ICUServiceFactories can more easily use. The ICUServiceKey can * cache the results of this parsing to save repeated effort. * ICUService provides convenience APIs that take UnicodeStrings and * generate default ICUServiceKeys for use in querying.</p> * * <p>ICUService provides API to get the list of IDs publicly * supported by the service (although queries aren't restricted to * this list). This list contains only 'simple' IDs, and not fully * unique IDs. ICUServiceFactories are associated with each simple ID * and the responsible factory can also return a human-readable * localized version of the simple ID, for use in user interfaces. * ICUService can also provide an array of the all the localized * visible IDs and their corresponding internal IDs.</p> * * <p>ICUService implements ICUNotifier, so that clients can register * to receive notification when factories are added or removed from * the service. ICUService provides a default EventListener * subinterface, ServiceListener, which can be registered with the * service. When the service changes, the ServiceListener's * serviceChanged method is called with the service as the * argument.</p> * * <p>The ICUService API is both rich and generic, and it is expected * that most implementations will statically 'wrap' ICUService to * present a more appropriate API-- for example, to declare the type * of the objects returned from get, to limit the factories that can * be registered with the service, or to define their own listener * interface with a custom callback method. They might also customize * ICUService by overriding it, for example, to customize the * ICUServiceKey and fallback strategy. ICULocaleService is a * subclass of ICUService that uses Locale names as IDs and uses * ICUServiceKeys that implement the standard resource bundle fallback * strategy. Most clients will wish to subclass it instead of * ICUService.</p> */ class U_COMMON_API ICUService : public ICUNotifier { … }; U_NAMESPACE_END /* UCONFIG_NO_SERVICE */ #endif /* ICUSERV_H */ #endif