1 // © 2016 and later: Unicode, Inc. and others.
2 // License & terms of use: http://www.unicode.org/copyright.html
4 *******************************************************************************
5 * Copyright (C) 2001-2014, International Business Machines Corporation.
7 *******************************************************************************
10 #include "unicode/utypes.h"
12 #if !UCONFIG_NO_SERVICE
17 #undef SERVICE_REFCOUNT
19 // in case we use the refcount stuff
24 ******************************************************************
27 const UChar
ICUServiceKey::PREFIX_DELIMITER
= 0x002F; /* '/' */
29 ICUServiceKey::ICUServiceKey(const UnicodeString
& id
)
33 ICUServiceKey::~ICUServiceKey()
38 ICUServiceKey::getID() const
44 ICUServiceKey::canonicalID(UnicodeString
& result
) const
46 return result
.append(_id
);
50 ICUServiceKey::currentID(UnicodeString
& result
) const
52 return canonicalID(result
);
56 ICUServiceKey::currentDescriptor(UnicodeString
& result
) const
59 result
.append(PREFIX_DELIMITER
);
60 return currentID(result
);
64 ICUServiceKey::fallback()
70 ICUServiceKey::isFallbackOf(const UnicodeString
& id
) const
76 ICUServiceKey::prefix(UnicodeString
& result
) const
82 ICUServiceKey::parsePrefix(UnicodeString
& result
)
84 int32_t n
= result
.indexOf(PREFIX_DELIMITER
);
93 ICUServiceKey::parseSuffix(UnicodeString
& result
)
95 int32_t n
= result
.indexOf(PREFIX_DELIMITER
);
97 result
.remove(0, n
+1);
104 ICUServiceKey::debug(UnicodeString
& result
) const
107 result
.append((UnicodeString
)" id: ");
113 ICUServiceKey::debugClass(UnicodeString
& result
) const
115 return result
.append((UnicodeString
)"ICUServiceKey");
119 UOBJECT_DEFINE_RTTI_IMPLEMENTATION(ICUServiceKey
)
122 ******************************************************************
125 ICUServiceFactory::~ICUServiceFactory() {}
127 SimpleFactory::SimpleFactory(UObject
* instanceToAdopt
, const UnicodeString
& id
, UBool visible
)
128 : _instance(instanceToAdopt
), _id(id
), _visible(visible
)
132 SimpleFactory::~SimpleFactory()
138 SimpleFactory::create(const ICUServiceKey
& key
, const ICUService
* service
, UErrorCode
& status
) const
140 if (U_SUCCESS(status
)) {
142 if (_id
== key
.currentID(temp
)) {
143 return service
->cloneInstance(_instance
);
150 SimpleFactory::updateVisibleIDs(Hashtable
& result
, UErrorCode
& status
) const
153 result
.put(_id
, (void*)this, status
); // cast away const
160 SimpleFactory::getDisplayName(const UnicodeString
& id
, const Locale
& /* locale */, UnicodeString
& result
) const
162 if (_visible
&& _id
== id
) {
172 SimpleFactory::debug(UnicodeString
& toAppendTo
) const
174 debugClass(toAppendTo
);
175 toAppendTo
.append((UnicodeString
)" id: ");
176 toAppendTo
.append(_id
);
177 toAppendTo
.append((UnicodeString
)", visible: ");
178 toAppendTo
.append(_visible
? (UnicodeString
)"T" : (UnicodeString
)"F");
183 SimpleFactory::debugClass(UnicodeString
& toAppendTo
) const
185 return toAppendTo
.append((UnicodeString
)"SimpleFactory");
189 UOBJECT_DEFINE_RTTI_IMPLEMENTATION(SimpleFactory
)
192 ******************************************************************
195 ServiceListener::~ServiceListener() {}
197 UOBJECT_DEFINE_RTTI_IMPLEMENTATION(ServiceListener
)
200 ******************************************************************
203 // Record the actual id for this service in the cache, so we can return it
204 // even if we succeed later with a different id.
205 class CacheEntry
: public UMemory
{
210 UnicodeString actualDescriptor
;
214 * Releases a reference to the shared resource.
220 CacheEntry(const UnicodeString
& _actualDescriptor
, UObject
* _service
)
221 : refcount(1), actualDescriptor(_actualDescriptor
), service(_service
) {
225 * Instantiation creates an initial reference, so don't call this
226 * unless you're creating a new pointer to this. Management of
227 * that pointer will have to know how to deal with refcounts.
228 * Return true if the resource has not already been released.
236 * Destructions removes a reference, so don't call this unless
237 * you're removing pointer to this somewhere. Management of that
238 * pointer will have to know how to deal with refcounts. Once
239 * the refcount drops to zero, the resource is released. Return
240 * false if the resouce has been released.
242 CacheEntry
* unref() {
243 if ((--refcount
) == 0) {
251 * Return TRUE if there is at least one reference to this and the
252 * resource has not been released.
254 UBool
isShared() const {
259 // UObjectDeleter for serviceCache
261 static void U_CALLCONV
262 cacheDeleter(void* obj
) {
263 U_NAMESPACE_USE ((CacheEntry
*)obj
)->unref();
267 * Deleter for UObjects
269 static void U_CALLCONV
270 deleteUObject(void *obj
) {
271 U_NAMESPACE_USE
delete (UObject
*) obj
;
276 ******************************************************************
279 class DNCache
: public UMemory
{
284 DNCache(const Locale
& _locale
)
285 : cache(), locale(_locale
)
287 // cache.setKeyDeleter(uprv_deleteUObject);
293 ******************************************************************
297 StringPair::create(const UnicodeString
& displayName
,
298 const UnicodeString
& id
,
301 if (U_SUCCESS(status
)) {
302 StringPair
* sp
= new StringPair(displayName
, id
);
303 if (sp
== NULL
|| sp
->isBogus()) {
304 status
= U_MEMORY_ALLOCATION_ERROR
;
314 StringPair::isBogus() const {
315 return displayName
.isBogus() || id
.isBogus();
318 StringPair::StringPair(const UnicodeString
& _displayName
,
319 const UnicodeString
& _id
)
320 : displayName(_displayName
)
326 static void U_CALLCONV
327 userv_deleteStringPair(void *obj
) {
328 U_NAMESPACE_USE
delete (StringPair
*) obj
;
333 ******************************************************************
336 static UMutex lock
= U_MUTEX_INITIALIZER
;
338 ICUService::ICUService()
348 ICUService::ICUService(const UnicodeString
& newName
)
358 ICUService::~ICUService()
369 ICUService::get(const UnicodeString
& descriptor
, UErrorCode
& status
) const
371 return get(descriptor
, NULL
, status
);
375 ICUService::get(const UnicodeString
& descriptor
, UnicodeString
* actualReturn
, UErrorCode
& status
) const
377 UObject
* result
= NULL
;
378 ICUServiceKey
* key
= createKey(&descriptor
, status
);
380 result
= getKey(*key
, actualReturn
, status
);
387 ICUService::getKey(ICUServiceKey
& key
, UErrorCode
& status
) const
389 return getKey(key
, NULL
, status
);
392 // this is a vector that subclasses of ICUService can override to further customize the result object
393 // before returning it. All other public get functions should call this one.
396 ICUService::getKey(ICUServiceKey
& key
, UnicodeString
* actualReturn
, UErrorCode
& status
) const
398 return getKey(key
, actualReturn
, NULL
, status
);
401 // make it possible to call reentrantly on systems that don't have reentrant mutexes.
402 // we can use this simple approach since we know the situation where we're calling
403 // reentrantly even without knowing the thread.
404 class XMutex
: public UMemory
{
406 inline XMutex(UMutex
*mutex
, UBool reentering
)
408 , fActive(!reentering
)
410 if (fActive
) umtx_lock(fMutex
);
413 if (fActive
) umtx_unlock(fMutex
);
421 struct UVectorDeleter
{
423 UVectorDeleter() : _obj(NULL
) {}
424 ~UVectorDeleter() { delete _obj
; }
427 // called only by factories, treat as private
429 ICUService::getKey(ICUServiceKey
& key
, UnicodeString
* actualReturn
, const ICUServiceFactory
* factory
, UErrorCode
& status
) const
431 if (U_FAILURE(status
)) {
436 return handleDefault(key
, actualReturn
, status
);
439 ICUService
* ncthis
= (ICUService
*)this; // cast away semantic const
441 CacheEntry
* result
= NULL
;
443 // The factory list can't be modified until we're done,
444 // otherwise we might update the cache with an invalid result.
445 // The cache has to stay in synch with the factory list.
446 // ICU doesn't have monitors so we can't use rw locks, so
447 // we single-thread everything using this service, for now.
449 // if factory is not null, we're calling from within the mutex,
450 // and since some unix machines don't have reentrant mutexes we
451 // need to make sure not to try to lock it again.
452 XMutex
mutex(&lock
, factory
!= NULL
);
454 if (serviceCache
== NULL
) {
455 ncthis
->serviceCache
= new Hashtable(status
);
456 if (ncthis
->serviceCache
== NULL
) {
459 if (U_FAILURE(status
)) {
463 serviceCache
->setValueDeleter(cacheDeleter
);
466 UnicodeString currentDescriptor
;
467 UVectorDeleter cacheDescriptorList
;
468 UBool putInCache
= FALSE
;
470 int32_t startIndex
= 0;
471 int32_t limit
= factories
->size();
472 UBool cacheResult
= TRUE
;
474 if (factory
!= NULL
) {
475 for (int32_t i
= 0; i
< limit
; ++i
) {
476 if (factory
== (const ICUServiceFactory
*)factories
->elementAt(i
)) {
481 if (startIndex
== 0) {
482 // throw new InternalError("Factory " + factory + "not registered with service: " + this);
483 status
= U_ILLEGAL_ARGUMENT_ERROR
;
490 currentDescriptor
.remove();
491 key
.currentDescriptor(currentDescriptor
);
492 result
= (CacheEntry
*)serviceCache
->get(currentDescriptor
);
493 if (result
!= NULL
) {
497 // first test of cache failed, so we'll have to update
498 // the cache if we eventually succeed-- that is, if we're
499 // going to update the cache at all.
502 int32_t index
= startIndex
;
503 while (index
< limit
) {
504 ICUServiceFactory
* f
= (ICUServiceFactory
*)factories
->elementAt(index
++);
505 UObject
* service
= f
->create(key
, this, status
);
506 if (U_FAILURE(status
)) {
510 if (service
!= NULL
) {
511 result
= new CacheEntry(currentDescriptor
, service
);
512 if (result
== NULL
) {
514 status
= U_MEMORY_ALLOCATION_ERROR
;
522 // prepare to load the cache with all additional ids that
523 // will resolve to result, assuming we'll succeed. We
524 // don't want to keep querying on an id that's going to
525 // fallback to the one that succeeded, we want to hit the
526 // cache the first time next goaround.
527 if (cacheDescriptorList
._obj
== NULL
) {
528 cacheDescriptorList
._obj
= new UVector(uprv_deleteUObject
, NULL
, 5, status
);
529 if (U_FAILURE(status
)) {
533 UnicodeString
* idToCache
= new UnicodeString(currentDescriptor
);
534 if (idToCache
== NULL
|| idToCache
->isBogus()) {
535 status
= U_MEMORY_ALLOCATION_ERROR
;
539 cacheDescriptorList
._obj
->addElement(idToCache
, status
);
540 if (U_FAILURE(status
)) {
543 } while (key
.fallback());
546 if (result
!= NULL
) {
547 if (putInCache
&& cacheResult
) {
548 serviceCache
->put(result
->actualDescriptor
, result
, status
);
549 if (U_FAILURE(status
)) {
554 if (cacheDescriptorList
._obj
!= NULL
) {
555 for (int32_t i
= cacheDescriptorList
._obj
->size(); --i
>= 0;) {
556 UnicodeString
* desc
= (UnicodeString
*)cacheDescriptorList
._obj
->elementAt(i
);
557 serviceCache
->put(*desc
, result
, status
);
558 if (U_FAILURE(status
)) {
564 cacheDescriptorList
._obj
->removeElementAt(i
);
569 if (actualReturn
!= NULL
) {
571 if (result
->actualDescriptor
.indexOf((UChar
)0x2f) == 0) { // U+002f=slash (/)
572 actualReturn
->remove();
573 actualReturn
->append(result
->actualDescriptor
,
575 result
->actualDescriptor
.length() - 1);
577 *actualReturn
= result
->actualDescriptor
;
580 if (actualReturn
->isBogus()) {
581 status
= U_MEMORY_ALLOCATION_ERROR
;
587 UObject
* service
= cloneInstance(result
->service
);
588 if (putInCache
&& !cacheResult
) {
595 return handleDefault(key
, actualReturn
, status
);
599 ICUService::handleDefault(const ICUServiceKey
& /* key */, UnicodeString
* /* actualIDReturn */, UErrorCode
& /* status */) const
605 ICUService::getVisibleIDs(UVector
& result
, UErrorCode
& status
) const {
606 return getVisibleIDs(result
, NULL
, status
);
610 ICUService::getVisibleIDs(UVector
& result
, const UnicodeString
* matchID
, UErrorCode
& status
) const
612 result
.removeAllElements();
614 if (U_FAILURE(status
)) {
620 const Hashtable
* map
= getVisibleIDMap(status
);
622 ICUServiceKey
* fallbackKey
= createKey(matchID
, status
);
624 for (int32_t pos
= UHASH_FIRST
;;) {
625 const UHashElement
* e
= map
->nextElement(pos
);
630 const UnicodeString
* id
= (const UnicodeString
*)e
->key
.pointer
;
631 if (fallbackKey
!= NULL
) {
632 if (!fallbackKey
->isFallbackOf(*id
)) {
637 UnicodeString
* idClone
= new UnicodeString(*id
);
638 if (idClone
== NULL
|| idClone
->isBogus()) {
640 status
= U_MEMORY_ALLOCATION_ERROR
;
643 result
.addElement(idClone
, status
);
644 if (U_FAILURE(status
)) {
652 if (U_FAILURE(status
)) {
653 result
.removeAllElements();
659 ICUService::getVisibleIDMap(UErrorCode
& status
) const {
660 if (U_FAILURE(status
)) return NULL
;
662 // must only be called when lock is already held
664 ICUService
* ncthis
= (ICUService
*)this; // cast away semantic const
665 if (idCache
== NULL
) {
666 ncthis
->idCache
= new Hashtable(status
);
667 if (idCache
== NULL
) {
668 status
= U_MEMORY_ALLOCATION_ERROR
;
669 } else if (factories
!= NULL
) {
670 for (int32_t pos
= factories
->size(); --pos
>= 0;) {
671 ICUServiceFactory
* f
= (ICUServiceFactory
*)factories
->elementAt(pos
);
672 f
->updateVisibleIDs(*idCache
, status
);
674 if (U_FAILURE(status
)) {
676 ncthis
->idCache
= NULL
;
686 ICUService::getDisplayName(const UnicodeString
& id
, UnicodeString
& result
) const
688 return getDisplayName(id
, result
, Locale::getDefault());
692 ICUService::getDisplayName(const UnicodeString
& id
, UnicodeString
& result
, const Locale
& locale
) const
695 UErrorCode status
= U_ZERO_ERROR
;
697 const Hashtable
* map
= getVisibleIDMap(status
);
699 ICUServiceFactory
* f
= (ICUServiceFactory
*)map
->get(id
);
701 f
->getDisplayName(id
, locale
, result
);
706 UErrorCode status
= U_ZERO_ERROR
;
707 ICUServiceKey
* fallbackKey
= createKey(&id
, status
);
708 while (fallbackKey
->fallback()) {
710 fallbackKey
->currentID(us
);
711 f
= (ICUServiceFactory
*)map
->get(us
);
713 f
->getDisplayName(id
, locale
, result
);
726 ICUService::getDisplayNames(UVector
& result
, UErrorCode
& status
) const
728 return getDisplayNames(result
, Locale::getDefault(), NULL
, status
);
733 ICUService::getDisplayNames(UVector
& result
, const Locale
& locale
, UErrorCode
& status
) const
735 return getDisplayNames(result
, locale
, NULL
, status
);
739 ICUService::getDisplayNames(UVector
& result
,
740 const Locale
& locale
,
741 const UnicodeString
* matchID
,
742 UErrorCode
& status
) const
744 result
.removeAllElements();
745 result
.setDeleter(userv_deleteStringPair
);
746 if (U_SUCCESS(status
)) {
747 ICUService
* ncthis
= (ICUService
*)this; // cast away semantic const
750 if (dnCache
!= NULL
&& dnCache
->locale
!= locale
) {
752 ncthis
->dnCache
= NULL
;
755 if (dnCache
== NULL
) {
756 const Hashtable
* m
= getVisibleIDMap(status
);
757 if (U_FAILURE(status
)) {
760 ncthis
->dnCache
= new DNCache(locale
);
761 if (dnCache
== NULL
) {
762 status
= U_MEMORY_ALLOCATION_ERROR
;
766 int32_t pos
= UHASH_FIRST
;
767 const UHashElement
* entry
= NULL
;
768 while ((entry
= m
->nextElement(pos
)) != NULL
) {
769 const UnicodeString
* id
= (const UnicodeString
*)entry
->key
.pointer
;
770 ICUServiceFactory
* f
= (ICUServiceFactory
*)entry
->value
.pointer
;
772 f
->getDisplayName(*id
, locale
, dname
);
773 if (dname
.isBogus()) {
774 status
= U_MEMORY_ALLOCATION_ERROR
;
776 dnCache
->cache
.put(dname
, (void*)id
, status
); // share pointer with visibleIDMap
777 if (U_SUCCESS(status
)) {
782 ncthis
->dnCache
= NULL
;
788 ICUServiceKey
* matchKey
= createKey(matchID
, status
);
789 /* To ensure that all elements in the hashtable are iterated, set pos to -1.
790 * nextElement(pos) will skip the position at pos and begin the iteration
791 * at the next position, which in this case will be 0.
793 int32_t pos
= UHASH_FIRST
;
794 const UHashElement
*entry
= NULL
;
795 while ((entry
= dnCache
->cache
.nextElement(pos
)) != NULL
) {
796 const UnicodeString
* id
= (const UnicodeString
*)entry
->value
.pointer
;
797 if (matchKey
!= NULL
&& !matchKey
->isFallbackOf(*id
)) {
800 const UnicodeString
* dn
= (const UnicodeString
*)entry
->key
.pointer
;
801 StringPair
* sp
= StringPair::create(*id
, *dn
, status
);
802 result
.addElement(sp
, status
);
803 if (U_FAILURE(status
)) {
804 result
.removeAllElements();
814 ICUService::registerInstance(UObject
* objToAdopt
, const UnicodeString
& id
, UErrorCode
& status
)
816 return registerInstance(objToAdopt
, id
, TRUE
, status
);
820 ICUService::registerInstance(UObject
* objToAdopt
, const UnicodeString
& id
, UBool visible
, UErrorCode
& status
)
822 ICUServiceKey
* key
= createKey(&id
, status
);
824 UnicodeString canonicalID
;
825 key
->canonicalID(canonicalID
);
828 ICUServiceFactory
* f
= createSimpleFactory(objToAdopt
, canonicalID
, visible
, status
);
830 return registerFactory(f
, status
);
838 ICUService::createSimpleFactory(UObject
* objToAdopt
, const UnicodeString
& id
, UBool visible
, UErrorCode
& status
)
840 if (U_SUCCESS(status
)) {
841 if ((objToAdopt
!= NULL
) && (!id
.isBogus())) {
842 return new SimpleFactory(objToAdopt
, id
, visible
);
844 status
= U_ILLEGAL_ARGUMENT_ERROR
;
850 ICUService::registerFactory(ICUServiceFactory
* factoryToAdopt
, UErrorCode
& status
)
852 if (U_SUCCESS(status
) && factoryToAdopt
!= NULL
) {
855 if (factories
== NULL
) {
856 factories
= new UVector(deleteUObject
, NULL
, status
);
857 if (U_FAILURE(status
)) {
862 factories
->insertElementAt(factoryToAdopt
, 0, status
);
863 if (U_SUCCESS(status
)) {
866 delete factoryToAdopt
;
867 factoryToAdopt
= NULL
;
871 if (factoryToAdopt
!= NULL
) {
875 return (URegistryKey
)factoryToAdopt
;
879 ICUService::unregister(URegistryKey rkey
, UErrorCode
& status
)
881 ICUServiceFactory
*factory
= (ICUServiceFactory
*)rkey
;
882 UBool result
= FALSE
;
883 if (factory
!= NULL
&& factories
!= NULL
) {
886 if (factories
->removeElement(factory
)) {
890 status
= U_ILLEGAL_ARGUMENT_ERROR
;
905 reInitializeFactories();
912 ICUService::reInitializeFactories()
914 if (factories
!= NULL
) {
915 factories
->removeAllElements();
920 ICUService::isDefault() const
922 return countFactories() == 0;
926 ICUService::createKey(const UnicodeString
* id
, UErrorCode
& status
) const
928 return (U_FAILURE(status
) || id
== NULL
) ? NULL
: new ICUServiceKey(*id
);
932 ICUService::clearCaches()
934 // callers synchronize before use
940 delete serviceCache
; serviceCache
= NULL
;
944 ICUService::clearServiceCache()
946 // callers synchronize before use
947 delete serviceCache
; serviceCache
= NULL
;
951 ICUService::acceptsListener(const EventListener
& l
) const
953 return dynamic_cast<const ServiceListener
*>(&l
) != NULL
;
957 ICUService::notifyListener(EventListener
& l
) const
959 ((ServiceListener
&)l
).serviceChanged(*this);
963 ICUService::getName(UnicodeString
& result
) const
965 return result
.append(name
);
969 ICUService::countFactories() const
971 return factories
== NULL
? 0 : factories
->size();
975 ICUService::getTimestamp() const
982 /* UCONFIG_NO_SERVICE */