2 *******************************************************************************
3 * Copyright (C) 2001-2004, International Business Machines Corporation. *
4 * All Rights Reserved. *
5 *******************************************************************************
8 #include "unicode/utypes.h"
10 #if !UCONFIG_NO_SERVICE
15 #undef SERVICE_REFCOUNT
17 // in case we use the refcount stuff
22 ******************************************************************
25 const UChar
ICUServiceKey::PREFIX_DELIMITER
= 0x002F; /* '/' */
27 ICUServiceKey::ICUServiceKey(const UnicodeString
& id
)
31 ICUServiceKey::~ICUServiceKey()
36 ICUServiceKey::getID() const
42 ICUServiceKey::canonicalID(UnicodeString
& result
) const
44 return result
.append(_id
);
48 ICUServiceKey::currentID(UnicodeString
& result
) const
50 return canonicalID(result
);
54 ICUServiceKey::currentDescriptor(UnicodeString
& result
) const
57 result
.append(PREFIX_DELIMITER
);
58 return currentID(result
);
62 ICUServiceKey::fallback()
68 ICUServiceKey::isFallbackOf(const UnicodeString
& id
) const
74 ICUServiceKey::prefix(UnicodeString
& result
) const
80 ICUServiceKey::parsePrefix(UnicodeString
& result
)
82 int32_t n
= result
.indexOf(PREFIX_DELIMITER
);
91 ICUServiceKey::parseSuffix(UnicodeString
& result
)
93 int32_t n
= result
.indexOf(PREFIX_DELIMITER
);
95 result
.remove(0, n
+1);
102 ICUServiceKey::debug(UnicodeString
& result
) const
105 result
.append(" id: ");
111 ICUServiceKey::debugClass(UnicodeString
& result
) const
113 return result
.append("ICUServiceKey");
117 UOBJECT_DEFINE_RTTI_IMPLEMENTATION(ICUServiceKey
)
120 ******************************************************************
123 SimpleFactory::SimpleFactory(UObject
* instanceToAdopt
, const UnicodeString
& id
, UBool visible
)
124 : _instance(instanceToAdopt
), _id(id
), _visible(visible
)
128 SimpleFactory::~SimpleFactory()
134 SimpleFactory::create(const ICUServiceKey
& key
, const ICUService
* service
, UErrorCode
& status
) const
136 if (U_SUCCESS(status
)) {
138 if (_id
== key
.currentID(temp
)) {
139 return service
->cloneInstance(_instance
);
146 SimpleFactory::updateVisibleIDs(Hashtable
& result
, UErrorCode
& status
) const
149 result
.put(_id
, (void*)this, status
); // cast away const
156 SimpleFactory::getDisplayName(const UnicodeString
& id
, const Locale
& /* locale */, UnicodeString
& result
) const
158 if (_visible
&& _id
== id
) {
168 SimpleFactory::debug(UnicodeString
& toAppendTo
) const
170 debugClass(toAppendTo
);
171 toAppendTo
.append(" id: ");
172 toAppendTo
.append(_id
);
173 toAppendTo
.append(", visible: ");
174 toAppendTo
.append(_visible
? "T" : "F");
179 SimpleFactory::debugClass(UnicodeString
& toAppendTo
) const
181 return toAppendTo
.append("SimpleFactory");
185 UOBJECT_DEFINE_RTTI_IMPLEMENTATION(SimpleFactory
)
188 ******************************************************************
191 UOBJECT_DEFINE_RTTI_IMPLEMENTATION(ServiceListener
)
194 ******************************************************************
197 // Record the actual id for this service in the cache, so we can return it
198 // even if we succeed later with a different id.
199 class CacheEntry
: public UMemory
{
204 UnicodeString actualDescriptor
;
208 * Releases a reference to the shared resource.
214 CacheEntry(const UnicodeString
& _actualDescriptor
, UObject
* _service
)
215 : refcount(1), actualDescriptor(_actualDescriptor
), service(_service
) {
219 * Instantiation creates an initial reference, so don't call this
220 * unless you're creating a new pointer to this. Management of
221 * that pointer will have to know how to deal with refcounts.
222 * Return true if the resource has not already been released.
230 * Destructions removes a reference, so don't call this unless
231 * you're removing pointer to this somewhere. Management of that
232 * pointer will have to know how to deal with refcounts. Once
233 * the refcount drops to zero, the resource is released. Return
234 * false if the resouce has been released.
236 CacheEntry
* unref() {
237 if ((--refcount
) == 0) {
245 * Return TRUE if there is at least one reference to this and the
246 * resource has not been released.
248 UBool
isShared() const {
253 // UObjectDeleter for serviceCache
255 static void U_CALLCONV
256 cacheDeleter(void* obj
) {
258 ((CacheEntry
*)obj
)->unref();
262 * Deleter for UObjects
264 static void U_CALLCONV
265 deleteUObject(void *obj
) {
267 delete (UObject
*) obj
;
272 ******************************************************************
275 class DNCache
: public UMemory
{
280 DNCache(const Locale
& _locale
)
281 : cache(), locale(_locale
)
283 // cache.setKeyDeleter(uhash_deleteUnicodeString);
289 ******************************************************************
293 StringPair::create(const UnicodeString
& displayName
,
294 const UnicodeString
& id
,
297 if (U_SUCCESS(status
)) {
298 StringPair
* sp
= new StringPair(displayName
, id
);
299 if (sp
== NULL
|| sp
->isBogus()) {
300 status
= U_MEMORY_ALLOCATION_ERROR
;
310 StringPair::isBogus() const {
311 return displayName
.isBogus() || id
.isBogus();
314 StringPair::StringPair(const UnicodeString
& _displayName
,
315 const UnicodeString
& _id
)
316 : displayName(_displayName
)
321 U_CAPI
void U_EXPORT2
322 userv_deleteStringPair(void *obj
) {
324 delete (StringPair
*) obj
;
328 ******************************************************************
331 ICUService::ICUService()
343 ICUService::ICUService(const UnicodeString
& newName
)
354 ICUService::~ICUService()
366 ICUService::get(const UnicodeString
& descriptor
, UErrorCode
& status
) const
368 return get(descriptor
, NULL
, status
);
372 ICUService::get(const UnicodeString
& descriptor
, UnicodeString
* actualReturn
, UErrorCode
& status
) const
374 UObject
* result
= NULL
;
375 ICUServiceKey
* key
= createKey(&descriptor
, status
);
377 result
= getKey(*key
, actualReturn
, status
);
384 ICUService::getKey(ICUServiceKey
& key
, UErrorCode
& status
) const
386 return getKey(key
, NULL
, status
);
389 // this is a vector that subclasses of ICUService can override to further customize the result object
390 // before returning it. All other public get functions should call this one.
393 ICUService::getKey(ICUServiceKey
& key
, UnicodeString
* actualReturn
, UErrorCode
& status
) const
395 return getKey(key
, actualReturn
, NULL
, status
);
398 // make it possible to call reentrantly on systems that don't have reentrant mutexes.
399 // we can use this simple approach since we know the situation where we're calling
400 // reentrantly even without knowing the thread.
401 class XMutex
: public UMemory
{
403 inline XMutex(UMTX
*mutex
, UBool reentering
)
405 , fActive(!reentering
)
407 if (fActive
) umtx_lock(fMutex
);
410 if (fActive
) umtx_unlock(fMutex
);
418 struct UVectorDeleter
{
420 UVectorDeleter() : _obj(NULL
) {}
421 ~UVectorDeleter() { delete _obj
; }
424 // called only by factories, treat as private
426 ICUService::getKey(ICUServiceKey
& key
, UnicodeString
* actualReturn
, const ICUServiceFactory
* factory
, UErrorCode
& status
) const
428 if (U_FAILURE(status
)) {
433 return handleDefault(key
, actualReturn
, status
);
436 ICUService
* ncthis
= (ICUService
*)this; // cast away semantic const
438 CacheEntry
* result
= NULL
;
440 // The factory list can't be modified until we're done,
441 // otherwise we might update the cache with an invalid result.
442 // The cache has to stay in synch with the factory list.
443 // ICU doesn't have monitors so we can't use rw locks, so
444 // we single-thread everything using this service, for now.
446 // if factory is not null, we're calling from within the mutex,
447 // and since some unix machines don't have reentrant mutexes we
448 // need to make sure not to try to lock it again.
449 XMutex(&ncthis
->lock
, factory
!= NULL
);
451 if (serviceCache
== NULL
) {
452 ncthis
->serviceCache
= new Hashtable(status
);
453 if (U_FAILURE(status
)) {
457 serviceCache
->setValueDeleter(cacheDeleter
);
460 UnicodeString currentDescriptor
;
461 UVectorDeleter cacheDescriptorList
;
462 UBool putInCache
= FALSE
;
464 int32_t startIndex
= 0;
465 int32_t limit
= factories
->size();
466 UBool cacheResult
= TRUE
;
468 if (factory
!= NULL
) {
469 for (int32_t i
= 0; i
< limit
; ++i
) {
470 if (factory
== (const ICUServiceFactory
*)factories
->elementAt(i
)) {
475 if (startIndex
== 0) {
476 // throw new InternalError("Factory " + factory + "not registered with service: " + this);
477 status
= U_ILLEGAL_ARGUMENT_ERROR
;
484 currentDescriptor
.remove();
485 key
.currentDescriptor(currentDescriptor
);
486 result
= (CacheEntry
*)serviceCache
->get(currentDescriptor
);
487 if (result
!= NULL
) {
491 // first test of cache failed, so we'll have to update
492 // the cache if we eventually succeed-- that is, if we're
493 // going to update the cache at all.
496 int32_t index
= startIndex
;
497 while (index
< limit
) {
498 ICUServiceFactory
* f
= (ICUServiceFactory
*)factories
->elementAt(index
++);
499 UObject
* service
= f
->create(key
, this, status
);
500 if (U_FAILURE(status
)) {
504 if (service
!= NULL
) {
505 result
= new CacheEntry(currentDescriptor
, service
);
506 if (result
== NULL
) {
508 status
= U_MEMORY_ALLOCATION_ERROR
;
516 // prepare to load the cache with all additional ids that
517 // will resolve to result, assuming we'll succeed. We
518 // don't want to keep querying on an id that's going to
519 // fallback to the one that succeeded, we want to hit the
520 // cache the first time next goaround.
521 if (cacheDescriptorList
._obj
== NULL
) {
522 cacheDescriptorList
._obj
= new UVector(uhash_deleteUnicodeString
, NULL
, 5, status
);
523 if (U_FAILURE(status
)) {
527 UnicodeString
* idToCache
= new UnicodeString(currentDescriptor
);
528 if (idToCache
== NULL
|| idToCache
->isBogus()) {
529 status
= U_MEMORY_ALLOCATION_ERROR
;
533 cacheDescriptorList
._obj
->addElement(idToCache
, status
);
534 if (U_FAILURE(status
)) {
537 } while (key
.fallback());
540 if (result
!= NULL
) {
541 if (putInCache
&& cacheResult
) {
542 serviceCache
->put(result
->actualDescriptor
, result
, status
);
543 if (U_FAILURE(status
)) {
548 if (cacheDescriptorList
._obj
!= NULL
) {
549 for (int32_t i
= cacheDescriptorList
._obj
->size(); --i
>= 0;) {
550 UnicodeString
* desc
= (UnicodeString
*)cacheDescriptorList
._obj
->elementAt(i
);
551 serviceCache
->put(*desc
, result
, status
);
552 if (U_FAILURE(status
)) {
558 cacheDescriptorList
._obj
->removeElementAt(i
);
563 if (actualReturn
!= NULL
) {
565 if (result
->actualDescriptor
.indexOf((UChar
)0x2f) == 0) { // U+002f=slash (/)
566 actualReturn
->remove();
567 actualReturn
->append(result
->actualDescriptor
,
569 result
->actualDescriptor
.length() - 1);
571 *actualReturn
= result
->actualDescriptor
;
574 if (actualReturn
->isBogus()) {
575 status
= U_MEMORY_ALLOCATION_ERROR
;
581 UObject
* service
= cloneInstance(result
->service
);
582 if (putInCache
&& !cacheResult
) {
589 return handleDefault(key
, actualReturn
, status
);
593 ICUService::handleDefault(const ICUServiceKey
& /* key */, UnicodeString
* /* actualIDReturn */, UErrorCode
& /* status */) const
599 ICUService::getVisibleIDs(UVector
& result
, UErrorCode
& status
) const {
600 return getVisibleIDs(result
, NULL
, status
);
604 ICUService::getVisibleIDs(UVector
& result
, const UnicodeString
* matchID
, UErrorCode
& status
) const
606 result
.removeAllElements();
608 if (U_FAILURE(status
)) {
612 ICUService
* ncthis
= (ICUService
*)this; // cast away semantic const
614 Mutex
mutex(&ncthis
->lock
);
615 const Hashtable
* map
= getVisibleIDMap(status
);
617 ICUServiceKey
* fallbackKey
= createKey(matchID
, status
);
619 for (int32_t pos
= -1;;) {
620 const UHashElement
* e
= map
->nextElement(pos
);
625 const UnicodeString
* id
= (const UnicodeString
*)e
->key
.pointer
;
626 if (fallbackKey
!= NULL
) {
627 if (!fallbackKey
->isFallbackOf(*id
)) {
632 UnicodeString
* idClone
= new UnicodeString(*id
);
633 if (idClone
== NULL
|| idClone
->isBogus()) {
635 status
= U_MEMORY_ALLOCATION_ERROR
;
638 result
.addElement(idClone
, status
);
639 if (U_FAILURE(status
)) {
647 if (U_FAILURE(status
)) {
648 result
.removeAllElements();
654 ICUService::getVisibleIDMap(UErrorCode
& status
) const {
655 if (U_FAILURE(status
)) return NULL
;
657 // must only be called when lock is already held
659 ICUService
* ncthis
= (ICUService
*)this; // cast away semantic const
660 if (idCache
== NULL
) {
661 ncthis
->idCache
= new Hashtable(status
);
662 if (idCache
== NULL
) {
663 status
= U_MEMORY_ALLOCATION_ERROR
;
664 } else if (factories
!= NULL
) {
665 for (int32_t pos
= factories
->size(); --pos
>= 0;) {
666 ICUServiceFactory
* f
= (ICUServiceFactory
*)factories
->elementAt(pos
);
667 f
->updateVisibleIDs(*idCache
, status
);
669 if (U_FAILURE(status
)) {
671 ncthis
->idCache
= NULL
;
681 ICUService::getDisplayName(const UnicodeString
& id
, UnicodeString
& result
) const
683 return getDisplayName(id
, result
, Locale::getDefault());
687 ICUService::getDisplayName(const UnicodeString
& id
, UnicodeString
& result
, const Locale
& locale
) const
690 ICUService
* ncthis
= (ICUService
*)this; // cast away semantic const
691 UErrorCode status
= U_ZERO_ERROR
;
692 Mutex
mutex(&ncthis
->lock
);
693 const Hashtable
* map
= getVisibleIDMap(status
);
695 ICUServiceFactory
* f
= (ICUServiceFactory
*)map
->get(id
);
697 f
->getDisplayName(id
, locale
, result
);
702 UErrorCode status
= U_ZERO_ERROR
;
703 ICUServiceKey
* fallbackKey
= createKey(&id
, status
);
704 while (fallbackKey
->fallback()) {
706 fallbackKey
->currentID(us
);
707 f
= (ICUServiceFactory
*)map
->get(us
);
709 f
->getDisplayName(id
, locale
, result
);
722 ICUService::getDisplayNames(UVector
& result
, UErrorCode
& status
) const
724 return getDisplayNames(result
, Locale::getDefault(), NULL
, status
);
729 ICUService::getDisplayNames(UVector
& result
, const Locale
& locale
, UErrorCode
& status
) const
731 return getDisplayNames(result
, locale
, NULL
, status
);
735 ICUService::getDisplayNames(UVector
& result
,
736 const Locale
& locale
,
737 const UnicodeString
* matchID
,
738 UErrorCode
& status
) const
740 result
.removeAllElements();
741 if (U_SUCCESS(status
)) {
742 ICUService
* ncthis
= (ICUService
*)this; // cast away semantic const
743 Mutex
mutex(&ncthis
->lock
);
745 if (dnCache
!= NULL
&& dnCache
->locale
!= locale
) {
747 ncthis
->dnCache
= NULL
;
750 if (dnCache
== NULL
) {
751 const Hashtable
* m
= getVisibleIDMap(status
);
753 ncthis
->dnCache
= new DNCache(locale
);
754 if (dnCache
== NULL
) {
755 status
= U_MEMORY_ALLOCATION_ERROR
;
760 const UHashElement
* entry
= NULL
;
761 while ((entry
= m
->nextElement(pos
)) != NULL
) {
762 const UnicodeString
* id
= (const UnicodeString
*)entry
->key
.pointer
;
763 ICUServiceFactory
* f
= (ICUServiceFactory
*)entry
->value
.pointer
;
765 f
->getDisplayName(*id
, locale
, dname
);
766 if (dname
.isBogus()) {
767 status
= U_MEMORY_ALLOCATION_ERROR
;
769 dnCache
->cache
.put(dname
, (void*)id
, status
); // share pointer with visibleIDMap
770 if (U_SUCCESS(status
)) {
775 ncthis
->dnCache
= NULL
;
782 ICUServiceKey
* matchKey
= createKey(matchID
, status
);
784 const UHashElement
*entry
= NULL
;
785 while ((entry
= dnCache
->cache
.nextElement(pos
)) != NULL
) {
786 const UnicodeString
* id
= (const UnicodeString
*)entry
->value
.pointer
;
787 if (matchKey
!= NULL
&& !matchKey
->isFallbackOf(*id
)) {
790 const UnicodeString
* dn
= (const UnicodeString
*)entry
->key
.pointer
;
791 StringPair
* sp
= StringPair::create(*id
, *dn
, status
);
792 result
.addElement(sp
, status
);
793 if (U_FAILURE(status
)) {
794 result
.removeAllElements();
804 ICUService::registerInstance(UObject
* objToAdopt
, const UnicodeString
& id
, UErrorCode
& status
)
806 return registerInstance(objToAdopt
, id
, TRUE
, status
);
810 ICUService::registerInstance(UObject
* objToAdopt
, const UnicodeString
& id
, UBool visible
, UErrorCode
& status
)
812 ICUServiceKey
* key
= createKey(&id
, status
);
814 UnicodeString canonicalID
;
815 key
->canonicalID(canonicalID
);
818 ICUServiceFactory
* f
= createSimpleFactory(objToAdopt
, canonicalID
, visible
, status
);
820 return registerFactory(f
, status
);
828 ICUService::createSimpleFactory(UObject
* objToAdopt
, const UnicodeString
& id
, UBool visible
, UErrorCode
& status
)
830 if (U_SUCCESS(status
)) {
831 if ((objToAdopt
!= NULL
) && (!id
.isBogus())) {
832 return new SimpleFactory(objToAdopt
, id
, visible
);
834 status
= U_ILLEGAL_ARGUMENT_ERROR
;
840 ICUService::registerFactory(ICUServiceFactory
* factoryToAdopt
, UErrorCode
& status
)
842 if (U_SUCCESS(status
) && factoryToAdopt
!= NULL
) {
845 if (factories
== NULL
) {
846 factories
= new UVector(deleteUObject
, NULL
, status
);
847 if (U_FAILURE(status
)) {
852 factories
->insertElementAt(factoryToAdopt
, 0, status
);
853 if (U_SUCCESS(status
)) {
856 delete factoryToAdopt
;
857 factoryToAdopt
= NULL
;
861 if (factoryToAdopt
!= NULL
) {
865 return (URegistryKey
)factoryToAdopt
;
869 ICUService::unregister(URegistryKey rkey
, UErrorCode
& status
)
871 ICUServiceFactory
*factory
= (ICUServiceFactory
*)rkey
;
872 UBool result
= FALSE
;
873 if (factory
!= NULL
&& factories
!= NULL
) {
876 if (factories
->removeElement(factory
)) {
880 status
= U_ILLEGAL_ARGUMENT_ERROR
;
895 reInitializeFactories();
902 ICUService::reInitializeFactories()
904 if (factories
!= NULL
) {
905 factories
->removeAllElements();
910 ICUService::isDefault() const
912 return countFactories() == 0;
916 ICUService::createKey(const UnicodeString
* id
, UErrorCode
& status
) const
918 return (U_FAILURE(status
) || id
== NULL
) ? NULL
: new ICUServiceKey(*id
);
922 ICUService::clearCaches()
924 // callers synchronize before use
926 delete dnCache
; dnCache
= NULL
;
927 delete idCache
; idCache
= NULL
;
928 delete serviceCache
; serviceCache
= NULL
;
932 ICUService::clearServiceCache()
934 // callers synchronize before use
935 delete serviceCache
; serviceCache
= NULL
;
939 ICUService::acceptsListener(const EventListener
& l
) const
941 return l
.getDynamicClassID() == ServiceListener::getStaticClassID();
945 ICUService::notifyListener(EventListener
& l
) const
947 ((ServiceListener
&)l
).serviceChanged(*this);
951 ICUService::getName(UnicodeString
& result
) const
953 return result
.append(name
);
957 ICUService::countFactories() const
959 return factories
== NULL
? 0 : factories
->size();
963 ICUService::getTimestamp() const
970 /* UCONFIG_NO_SERVICE */