X-Git-Url: https://git.saurik.com/apple/icu.git/blobdiff_plain/46f4442e9a5a4f3b98b7c1083586332f6a8a99a4..4f1e1a09ce4daed860e35d359ce2fceccb0764e8:/icuSources/common/serv.cpp diff --git a/icuSources/common/serv.cpp b/icuSources/common/serv.cpp index f94fd439..959ab604 100644 --- a/icuSources/common/serv.cpp +++ b/icuSources/common/serv.cpp @@ -1,7 +1,9 @@ +// © 2016 and later: Unicode, Inc. and others. +// License & terms of use: http://www.unicode.org/copyright.html /** ******************************************************************************* -* Copyright (C) 2001-2008, International Business Machines Corporation. * -* All Rights Reserved. * +* Copyright (C) 2001-2014, International Business Machines Corporation. +* All Rights Reserved. ******************************************************************************* */ @@ -102,7 +104,7 @@ UnicodeString& ICUServiceKey::debug(UnicodeString& result) const { debugClass(result); - result.append(" id: "); + result.append((UnicodeString)" id: "); result.append(_id); return result; } @@ -110,7 +112,7 @@ ICUServiceKey::debug(UnicodeString& result) const UnicodeString& ICUServiceKey::debugClass(UnicodeString& result) const { - return result.append("ICUServiceKey"); + return result.append((UnicodeString)"ICUServiceKey"); } #endif @@ -120,6 +122,8 @@ UOBJECT_DEFINE_RTTI_IMPLEMENTATION(ICUServiceKey) ****************************************************************** */ +ICUServiceFactory::~ICUServiceFactory() {} + SimpleFactory::SimpleFactory(UObject* instanceToAdopt, const UnicodeString& id, UBool visible) : _instance(instanceToAdopt), _id(id), _visible(visible) { @@ -168,17 +172,17 @@ UnicodeString& SimpleFactory::debug(UnicodeString& toAppendTo) const { debugClass(toAppendTo); - toAppendTo.append(" id: "); + toAppendTo.append((UnicodeString)" id: "); toAppendTo.append(_id); - toAppendTo.append(", visible: "); - toAppendTo.append(_visible ? "T" : "F"); + toAppendTo.append((UnicodeString)", visible: "); + toAppendTo.append(_visible ? (UnicodeString)"T" : (UnicodeString)"F"); return toAppendTo; } UnicodeString& SimpleFactory::debugClass(UnicodeString& toAppendTo) const { - return toAppendTo.append("SimpleFactory"); + return toAppendTo.append((UnicodeString)"SimpleFactory"); } #endif @@ -188,6 +192,8 @@ UOBJECT_DEFINE_RTTI_IMPLEMENTATION(SimpleFactory) ****************************************************************** */ +ServiceListener::~ServiceListener() {} + UOBJECT_DEFINE_RTTI_IMPLEMENTATION(ServiceListener) /* @@ -278,7 +284,7 @@ public: DNCache(const Locale& _locale) : cache(), locale(_locale) { - // cache.setKeyDeleter(uhash_deleteUnicodeString); + // cache.setKeyDeleter(uprv_deleteUObject); } }; @@ -327,28 +333,26 @@ U_CDECL_END ****************************************************************** */ +static UMutex lock = U_MUTEX_INITIALIZER; + ICUService::ICUService() : name() -, lock(0) , timestamp(0) , factories(NULL) , serviceCache(NULL) , idCache(NULL) , dnCache(NULL) { - umtx_init(&lock); } ICUService::ICUService(const UnicodeString& newName) : name(newName) -, lock(0) , timestamp(0) , factories(NULL) , serviceCache(NULL) , idCache(NULL) , dnCache(NULL) { - umtx_init(&lock); } ICUService::~ICUService() @@ -359,7 +363,6 @@ ICUService::~ICUService() delete factories; factories = NULL; } - umtx_destroy(&lock); } UObject* @@ -400,7 +403,7 @@ ICUService::getKey(ICUServiceKey& key, UnicodeString* actualReturn, UErrorCode& // reentrantly even without knowing the thread. class XMutex : public UMemory { public: - inline XMutex(UMTX *mutex, UBool reentering) + inline XMutex(UMutex *mutex, UBool reentering) : fMutex(mutex) , fActive(!reentering) { @@ -411,7 +414,7 @@ public: } private: - UMTX *fMutex; + UMutex *fMutex; UBool fActive; }; @@ -446,7 +449,7 @@ ICUService::getKey(ICUServiceKey& key, UnicodeString* actualReturn, const ICUSer // if factory is not null, we're calling from within the mutex, // and since some unix machines don't have reentrant mutexes we // need to make sure not to try to lock it again. - XMutex mutex(&ncthis->lock, factory != NULL); + XMutex mutex(&lock, factory != NULL); if (serviceCache == NULL) { ncthis->serviceCache = new Hashtable(status); @@ -522,7 +525,7 @@ ICUService::getKey(ICUServiceKey& key, UnicodeString* actualReturn, const ICUSer // fallback to the one that succeeded, we want to hit the // cache the first time next goaround. if (cacheDescriptorList._obj == NULL) { - cacheDescriptorList._obj = new UVector(uhash_deleteUnicodeString, NULL, 5, status); + cacheDescriptorList._obj = new UVector(uprv_deleteUObject, NULL, 5, status); if (U_FAILURE(status)) { return NULL; } @@ -544,16 +547,15 @@ outerEnd: if (putInCache && cacheResult) { serviceCache->put(result->actualDescriptor, result, status); if (U_FAILURE(status)) { - delete result; return NULL; } if (cacheDescriptorList._obj != NULL) { for (int32_t i = cacheDescriptorList._obj->size(); --i >= 0;) { UnicodeString* desc = (UnicodeString*)cacheDescriptorList._obj->elementAt(i); + serviceCache->put(*desc, result, status); if (U_FAILURE(status)) { - delete result; return NULL; } @@ -612,14 +614,13 @@ ICUService::getVisibleIDs(UVector& result, const UnicodeString* matchID, UErrorC return result; } - ICUService * ncthis = (ICUService*)this; // cast away semantic const { - Mutex mutex(&ncthis->lock); + Mutex mutex(&lock); const Hashtable* map = getVisibleIDMap(status); if (map != NULL) { ICUServiceKey* fallbackKey = createKey(matchID, status); - for (int32_t pos = -1;;) { + for (int32_t pos = UHASH_FIRST;;) { const UHashElement* e = map->nextElement(pos); if (e == NULL) { break; @@ -690,9 +691,8 @@ UnicodeString& ICUService::getDisplayName(const UnicodeString& id, UnicodeString& result, const Locale& locale) const { { - ICUService* ncthis = (ICUService*)this; // cast away semantic const UErrorCode status = U_ZERO_ERROR; - Mutex mutex(&ncthis->lock); + Mutex mutex(&lock); const Hashtable* map = getVisibleIDMap(status); if (map != NULL) { ICUServiceFactory* f = (ICUServiceFactory*)map->get(id); @@ -744,7 +744,7 @@ ICUService::getDisplayNames(UVector& result, result.setDeleter(userv_deleteStringPair); if (U_SUCCESS(status)) { ICUService* ncthis = (ICUService*)this; // cast away semantic const - Mutex mutex(&ncthis->lock); + Mutex mutex(&lock); if (dnCache != NULL && dnCache->locale != locale) { delete dnCache; @@ -753,32 +753,33 @@ ICUService::getDisplayNames(UVector& result, if (dnCache == NULL) { const Hashtable* m = getVisibleIDMap(status); - if (m != NULL) { - ncthis->dnCache = new DNCache(locale); - if (dnCache == NULL) { - status = U_MEMORY_ALLOCATION_ERROR; - return result; - } + if (U_FAILURE(status)) { + return result; + } + ncthis->dnCache = new DNCache(locale); + if (dnCache == NULL) { + status = U_MEMORY_ALLOCATION_ERROR; + return result; + } - int32_t pos = -1; - const UHashElement* entry = NULL; - while ((entry = m->nextElement(pos)) != NULL) { - const UnicodeString* id = (const UnicodeString*)entry->key.pointer; - ICUServiceFactory* f = (ICUServiceFactory*)entry->value.pointer; - UnicodeString dname; - f->getDisplayName(*id, locale, dname); - if (dname.isBogus()) { - status = U_MEMORY_ALLOCATION_ERROR; - } else { - dnCache->cache.put(dname, (void*)id, status); // share pointer with visibleIDMap - if (U_SUCCESS(status)) { - continue; - } + int32_t pos = UHASH_FIRST; + const UHashElement* entry = NULL; + while ((entry = m->nextElement(pos)) != NULL) { + const UnicodeString* id = (const UnicodeString*)entry->key.pointer; + ICUServiceFactory* f = (ICUServiceFactory*)entry->value.pointer; + UnicodeString dname; + f->getDisplayName(*id, locale, dname); + if (dname.isBogus()) { + status = U_MEMORY_ALLOCATION_ERROR; + } else { + dnCache->cache.put(dname, (void*)id, status); // share pointer with visibleIDMap + if (U_SUCCESS(status)) { + continue; } - delete dnCache; - ncthis->dnCache = NULL; - return result; } + delete dnCache; + ncthis->dnCache = NULL; + return result; } } } @@ -788,7 +789,7 @@ ICUService::getDisplayNames(UVector& result, * nextElement(pos) will skip the position at pos and begin the iteration * at the next position, which in this case will be 0. */ - int32_t pos = -1; + int32_t pos = UHASH_FIRST; const UHashElement *entry = NULL; while ((entry = dnCache->cache.nextElement(pos)) != NULL) { const UnicodeString* id = (const UnicodeString*)entry->value.pointer; @@ -948,7 +949,7 @@ ICUService::clearServiceCache() UBool ICUService::acceptsListener(const EventListener& l) const { - return l.getDynamicClassID() == ServiceListener::getStaticClassID(); + return dynamic_cast(&l) != NULL; } void