2007-03-15 15:13:29 +00:00
|
|
|
/**
|
|
|
|
*******************************************************************************
|
2011-03-29 00:32:11 +00:00
|
|
|
* Copyright (C) 2001-2011, International Business Machines Corporation.
|
2010-05-19 17:29:33 +00:00
|
|
|
* All Rights Reserved.
|
2007-03-15 15:13:29 +00:00
|
|
|
*******************************************************************************
|
|
|
|
*/
|
2002-11-06 22:14:49 +00:00
|
|
|
|
|
|
|
#include "unicode/utypes.h"
|
|
|
|
|
|
|
|
#if !UCONFIG_NO_SERVICE
|
|
|
|
|
2004-12-21 06:55:20 +00:00
|
|
|
#include "serv.h"
|
2002-11-08 18:49:15 +00:00
|
|
|
#include "umutex.h"
|
2002-11-06 22:14:49 +00:00
|
|
|
|
|
|
|
#undef SERVICE_REFCOUNT
|
|
|
|
|
|
|
|
// in case we use the refcount stuff
|
|
|
|
|
|
|
|
U_NAMESPACE_BEGIN
|
|
|
|
|
|
|
|
/*
|
2007-03-15 15:13:29 +00:00
|
|
|
******************************************************************
|
|
|
|
*/
|
2002-11-06 22:14:49 +00:00
|
|
|
|
2002-11-26 16:03:52 +00:00
|
|
|
const UChar ICUServiceKey::PREFIX_DELIMITER = 0x002F; /* '/' */
|
2002-11-06 22:14:49 +00:00
|
|
|
|
2002-11-20 17:16:11 +00:00
|
|
|
ICUServiceKey::ICUServiceKey(const UnicodeString& id)
|
2007-03-15 15:13:29 +00:00
|
|
|
: _id(id) {
|
2002-11-06 22:14:49 +00:00
|
|
|
}
|
|
|
|
|
2002-11-20 17:16:11 +00:00
|
|
|
ICUServiceKey::~ICUServiceKey()
|
2002-11-06 22:14:49 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
const UnicodeString&
|
2002-11-20 17:16:11 +00:00
|
|
|
ICUServiceKey::getID() const
|
2002-11-06 22:14:49 +00:00
|
|
|
{
|
2007-03-15 15:13:29 +00:00
|
|
|
return _id;
|
2002-11-06 22:14:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
UnicodeString&
|
2002-11-20 17:16:11 +00:00
|
|
|
ICUServiceKey::canonicalID(UnicodeString& result) const
|
2002-11-06 22:14:49 +00:00
|
|
|
{
|
2007-03-15 15:13:29 +00:00
|
|
|
return result.append(_id);
|
2002-11-06 22:14:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
UnicodeString&
|
2002-11-20 17:16:11 +00:00
|
|
|
ICUServiceKey::currentID(UnicodeString& result) const
|
2002-11-06 22:14:49 +00:00
|
|
|
{
|
2007-03-15 15:13:29 +00:00
|
|
|
return canonicalID(result);
|
2002-11-06 22:14:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
UnicodeString&
|
2002-11-20 17:16:11 +00:00
|
|
|
ICUServiceKey::currentDescriptor(UnicodeString& result) const
|
2002-11-06 22:14:49 +00:00
|
|
|
{
|
2007-03-15 15:13:29 +00:00
|
|
|
prefix(result);
|
|
|
|
result.append(PREFIX_DELIMITER);
|
|
|
|
return currentID(result);
|
2002-11-06 22:14:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
UBool
|
2002-11-20 17:16:11 +00:00
|
|
|
ICUServiceKey::fallback()
|
2002-11-06 22:14:49 +00:00
|
|
|
{
|
2007-03-15 15:13:29 +00:00
|
|
|
return FALSE;
|
2002-11-06 22:14:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
UBool
|
2002-11-20 17:16:11 +00:00
|
|
|
ICUServiceKey::isFallbackOf(const UnicodeString& id) const
|
2002-11-06 22:14:49 +00:00
|
|
|
{
|
2007-03-15 15:13:29 +00:00
|
|
|
return id == _id;
|
2002-11-06 22:14:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
UnicodeString&
|
2002-11-20 17:16:11 +00:00
|
|
|
ICUServiceKey::prefix(UnicodeString& result) const
|
2002-11-06 22:14:49 +00:00
|
|
|
{
|
2007-03-15 15:13:29 +00:00
|
|
|
return result;
|
2002-11-06 22:14:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
UnicodeString&
|
2002-11-20 17:16:11 +00:00
|
|
|
ICUServiceKey::parsePrefix(UnicodeString& result)
|
2002-11-06 22:14:49 +00:00
|
|
|
{
|
2007-03-15 15:13:29 +00:00
|
|
|
int32_t n = result.indexOf(PREFIX_DELIMITER);
|
|
|
|
if (n < 0) {
|
|
|
|
n = 0;
|
|
|
|
}
|
|
|
|
result.remove(n);
|
|
|
|
return result;
|
2002-11-06 22:14:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
UnicodeString&
|
2002-11-20 17:16:11 +00:00
|
|
|
ICUServiceKey::parseSuffix(UnicodeString& result)
|
2002-11-06 22:14:49 +00:00
|
|
|
{
|
2007-03-15 15:13:29 +00:00
|
|
|
int32_t n = result.indexOf(PREFIX_DELIMITER);
|
|
|
|
if (n >= 0) {
|
|
|
|
result.remove(0, n+1);
|
|
|
|
}
|
|
|
|
return result;
|
2002-11-06 22:14:49 +00:00
|
|
|
}
|
|
|
|
|
2002-11-20 17:16:11 +00:00
|
|
|
#ifdef SERVICE_DEBUG
|
2002-11-06 22:14:49 +00:00
|
|
|
UnicodeString&
|
2002-11-20 17:16:11 +00:00
|
|
|
ICUServiceKey::debug(UnicodeString& result) const
|
2002-11-06 22:14:49 +00:00
|
|
|
{
|
2007-03-15 15:13:29 +00:00
|
|
|
debugClass(result);
|
|
|
|
result.append(" id: ");
|
|
|
|
result.append(_id);
|
|
|
|
return result;
|
2002-11-06 22:14:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
UnicodeString&
|
2002-11-20 17:16:11 +00:00
|
|
|
ICUServiceKey::debugClass(UnicodeString& result) const
|
2002-11-06 22:14:49 +00:00
|
|
|
{
|
2007-03-15 15:13:29 +00:00
|
|
|
return result.append("ICUServiceKey");
|
2002-11-06 22:14:49 +00:00
|
|
|
}
|
2002-11-20 17:16:11 +00:00
|
|
|
#endif
|
2002-11-06 22:14:49 +00:00
|
|
|
|
2003-08-27 01:01:42 +00:00
|
|
|
UOBJECT_DEFINE_RTTI_IMPLEMENTATION(ICUServiceKey)
|
2002-11-06 22:14:49 +00:00
|
|
|
|
|
|
|
/*
|
2007-03-15 15:13:29 +00:00
|
|
|
******************************************************************
|
|
|
|
*/
|
2002-11-06 22:14:49 +00:00
|
|
|
|
2011-07-26 05:32:25 +00:00
|
|
|
ICUServiceFactory::~ICUServiceFactory() {}
|
|
|
|
|
2002-11-06 22:14:49 +00:00
|
|
|
SimpleFactory::SimpleFactory(UObject* instanceToAdopt, const UnicodeString& id, UBool visible)
|
2007-03-15 15:13:29 +00:00
|
|
|
: _instance(instanceToAdopt), _id(id), _visible(visible)
|
2002-11-06 22:14:49 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
SimpleFactory::~SimpleFactory()
|
|
|
|
{
|
2007-03-15 15:13:29 +00:00
|
|
|
delete _instance;
|
2002-11-06 22:14:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
UObject*
|
2002-11-20 17:16:11 +00:00
|
|
|
SimpleFactory::create(const ICUServiceKey& key, const ICUService* service, UErrorCode& status) const
|
2002-11-06 22:14:49 +00:00
|
|
|
{
|
2007-03-15 15:13:29 +00:00
|
|
|
if (U_SUCCESS(status)) {
|
|
|
|
UnicodeString temp;
|
|
|
|
if (_id == key.currentID(temp)) {
|
|
|
|
return service->cloneInstance(_instance);
|
|
|
|
}
|
2002-11-06 22:14:49 +00:00
|
|
|
}
|
2007-03-15 15:13:29 +00:00
|
|
|
return NULL;
|
2002-11-06 22:14:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
SimpleFactory::updateVisibleIDs(Hashtable& result, UErrorCode& status) const
|
|
|
|
{
|
2007-03-15 15:13:29 +00:00
|
|
|
if (_visible) {
|
|
|
|
result.put(_id, (void*)this, status); // cast away const
|
|
|
|
} else {
|
|
|
|
result.remove(_id);
|
|
|
|
}
|
2002-11-06 22:14:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
UnicodeString&
|
2003-12-02 00:12:32 +00:00
|
|
|
SimpleFactory::getDisplayName(const UnicodeString& id, const Locale& /* locale */, UnicodeString& result) const
|
2002-11-06 22:14:49 +00:00
|
|
|
{
|
2007-03-15 15:13:29 +00:00
|
|
|
if (_visible && _id == id) {
|
|
|
|
result = _id;
|
|
|
|
} else {
|
|
|
|
result.setToBogus();
|
|
|
|
}
|
|
|
|
return result;
|
2002-11-06 22:14:49 +00:00
|
|
|
}
|
|
|
|
|
2002-11-20 17:16:11 +00:00
|
|
|
#ifdef SERVICE_DEBUG
|
2002-11-06 22:14:49 +00:00
|
|
|
UnicodeString&
|
|
|
|
SimpleFactory::debug(UnicodeString& toAppendTo) const
|
|
|
|
{
|
2007-03-15 15:13:29 +00:00
|
|
|
debugClass(toAppendTo);
|
|
|
|
toAppendTo.append(" id: ");
|
|
|
|
toAppendTo.append(_id);
|
|
|
|
toAppendTo.append(", visible: ");
|
|
|
|
toAppendTo.append(_visible ? "T" : "F");
|
|
|
|
return toAppendTo;
|
2002-11-06 22:14:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
UnicodeString&
|
|
|
|
SimpleFactory::debugClass(UnicodeString& toAppendTo) const
|
|
|
|
{
|
2007-03-15 15:13:29 +00:00
|
|
|
return toAppendTo.append("SimpleFactory");
|
2002-11-06 22:14:49 +00:00
|
|
|
}
|
2002-11-20 17:16:11 +00:00
|
|
|
#endif
|
2002-11-06 22:14:49 +00:00
|
|
|
|
2003-08-27 01:01:42 +00:00
|
|
|
UOBJECT_DEFINE_RTTI_IMPLEMENTATION(SimpleFactory)
|
2002-11-06 22:14:49 +00:00
|
|
|
|
|
|
|
/*
|
2007-03-15 15:13:29 +00:00
|
|
|
******************************************************************
|
|
|
|
*/
|
2002-11-06 22:14:49 +00:00
|
|
|
|
2011-07-26 05:32:25 +00:00
|
|
|
ServiceListener::~ServiceListener() {}
|
|
|
|
|
2003-08-27 01:01:42 +00:00
|
|
|
UOBJECT_DEFINE_RTTI_IMPLEMENTATION(ServiceListener)
|
2002-11-06 22:14:49 +00:00
|
|
|
|
|
|
|
/*
|
2007-03-15 15:13:29 +00:00
|
|
|
******************************************************************
|
|
|
|
*/
|
2002-11-06 22:14:49 +00:00
|
|
|
|
2007-03-15 15:13:29 +00:00
|
|
|
// Record the actual id for this service in the cache, so we can return it
|
|
|
|
// even if we succeed later with a different id.
|
2002-12-14 09:19:33 +00:00
|
|
|
class CacheEntry : public UMemory {
|
2002-11-06 22:14:49 +00:00
|
|
|
private:
|
2007-03-15 15:13:29 +00:00
|
|
|
int32_t refcount;
|
2002-11-06 22:14:49 +00:00
|
|
|
|
|
|
|
public:
|
2007-03-15 15:13:29 +00:00
|
|
|
UnicodeString actualDescriptor;
|
|
|
|
UObject* service;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Releases a reference to the shared resource.
|
|
|
|
*/
|
|
|
|
~CacheEntry() {
|
|
|
|
delete service;
|
|
|
|
}
|
|
|
|
|
|
|
|
CacheEntry(const UnicodeString& _actualDescriptor, UObject* _service)
|
|
|
|
: refcount(1), actualDescriptor(_actualDescriptor), service(_service) {
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Instantiation creates an initial reference, so don't call this
|
|
|
|
* unless you're creating a new pointer to this. Management of
|
|
|
|
* that pointer will have to know how to deal with refcounts.
|
|
|
|
* Return true if the resource has not already been released.
|
|
|
|
*/
|
|
|
|
CacheEntry* ref() {
|
|
|
|
++refcount;
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Destructions removes a reference, so don't call this unless
|
|
|
|
* you're removing pointer to this somewhere. Management of that
|
|
|
|
* pointer will have to know how to deal with refcounts. Once
|
|
|
|
* the refcount drops to zero, the resource is released. Return
|
|
|
|
* false if the resouce has been released.
|
|
|
|
*/
|
|
|
|
CacheEntry* unref() {
|
|
|
|
if ((--refcount) == 0) {
|
|
|
|
delete this;
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return TRUE if there is at least one reference to this and the
|
|
|
|
* resource has not been released.
|
|
|
|
*/
|
|
|
|
UBool isShared() const {
|
|
|
|
return refcount > 1;
|
|
|
|
}
|
2002-11-06 22:14:49 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
// UObjectDeleter for serviceCache
|
2003-07-12 07:55:03 +00:00
|
|
|
U_CDECL_BEGIN
|
|
|
|
static void U_CALLCONV
|
2002-11-06 22:14:49 +00:00
|
|
|
cacheDeleter(void* obj) {
|
2007-03-15 15:13:29 +00:00
|
|
|
U_NAMESPACE_USE ((CacheEntry*)obj)->unref();
|
2002-11-06 22:14:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2007-03-15 15:13:29 +00:00
|
|
|
* Deleter for UObjects
|
|
|
|
*/
|
2003-07-12 07:57:40 +00:00
|
|
|
static void U_CALLCONV
|
2002-11-06 22:14:49 +00:00
|
|
|
deleteUObject(void *obj) {
|
2007-03-15 15:13:29 +00:00
|
|
|
U_NAMESPACE_USE delete (UObject*) obj;
|
2002-11-06 22:14:49 +00:00
|
|
|
}
|
2003-07-12 07:57:40 +00:00
|
|
|
U_CDECL_END
|
2002-11-06 22:14:49 +00:00
|
|
|
|
|
|
|
/*
|
2007-03-15 15:13:29 +00:00
|
|
|
******************************************************************
|
|
|
|
*/
|
2002-11-06 22:14:49 +00:00
|
|
|
|
2002-12-14 09:19:33 +00:00
|
|
|
class DNCache : public UMemory {
|
2002-11-06 22:14:49 +00:00
|
|
|
public:
|
2007-03-15 15:13:29 +00:00
|
|
|
Hashtable cache;
|
|
|
|
const Locale locale;
|
|
|
|
|
|
|
|
DNCache(const Locale& _locale)
|
|
|
|
: cache(), locale(_locale)
|
|
|
|
{
|
2011-06-03 05:23:57 +00:00
|
|
|
// cache.setKeyDeleter(uprv_deleteUObject);
|
2007-03-15 15:13:29 +00:00
|
|
|
}
|
2002-11-06 22:14:49 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
2007-03-15 15:13:29 +00:00
|
|
|
******************************************************************
|
|
|
|
*/
|
2002-11-06 22:14:49 +00:00
|
|
|
|
|
|
|
StringPair*
|
|
|
|
StringPair::create(const UnicodeString& displayName,
|
|
|
|
const UnicodeString& id,
|
|
|
|
UErrorCode& status)
|
|
|
|
{
|
2007-03-15 15:13:29 +00:00
|
|
|
if (U_SUCCESS(status)) {
|
|
|
|
StringPair* sp = new StringPair(displayName, id);
|
|
|
|
if (sp == NULL || sp->isBogus()) {
|
|
|
|
status = U_MEMORY_ALLOCATION_ERROR;
|
|
|
|
delete sp;
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
return sp;
|
2002-11-06 22:14:49 +00:00
|
|
|
}
|
2007-03-15 15:13:29 +00:00
|
|
|
return NULL;
|
2002-11-06 22:14:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
UBool
|
|
|
|
StringPair::isBogus() const {
|
2007-03-15 15:13:29 +00:00
|
|
|
return displayName.isBogus() || id.isBogus();
|
2002-11-06 22:14:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
StringPair::StringPair(const UnicodeString& _displayName,
|
|
|
|
const UnicodeString& _id)
|
2007-03-15 15:13:29 +00:00
|
|
|
: displayName(_displayName)
|
|
|
|
, id(_id)
|
2002-11-06 22:14:49 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2007-06-15 06:40:59 +00:00
|
|
|
U_CDECL_BEGIN
|
|
|
|
static void U_CALLCONV
|
2004-01-15 22:27:39 +00:00
|
|
|
userv_deleteStringPair(void *obj) {
|
2007-03-15 15:13:29 +00:00
|
|
|
U_NAMESPACE_USE delete (StringPair*) obj;
|
2002-11-06 22:14:49 +00:00
|
|
|
}
|
2007-06-15 06:40:59 +00:00
|
|
|
U_CDECL_END
|
2002-11-06 22:14:49 +00:00
|
|
|
|
|
|
|
/*
|
2007-03-15 15:13:29 +00:00
|
|
|
******************************************************************
|
|
|
|
*/
|
2002-11-06 22:14:49 +00:00
|
|
|
|
2011-03-29 00:32:11 +00:00
|
|
|
static UMTX lock;
|
|
|
|
|
2002-11-06 22:14:49 +00:00
|
|
|
ICUService::ICUService()
|
2007-03-15 15:13:29 +00:00
|
|
|
: name()
|
|
|
|
, timestamp(0)
|
|
|
|
, factories(NULL)
|
|
|
|
, serviceCache(NULL)
|
|
|
|
, idCache(NULL)
|
|
|
|
, dnCache(NULL)
|
2002-11-06 22:14:49 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2002-11-24 23:46:04 +00:00
|
|
|
ICUService::ICUService(const UnicodeString& newName)
|
2007-03-15 15:13:29 +00:00
|
|
|
: name(newName)
|
|
|
|
, timestamp(0)
|
|
|
|
, factories(NULL)
|
|
|
|
, serviceCache(NULL)
|
|
|
|
, idCache(NULL)
|
|
|
|
, dnCache(NULL)
|
|
|
|
{
|
2002-11-06 22:14:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
ICUService::~ICUService()
|
2007-03-15 15:13:29 +00:00
|
|
|
{
|
|
|
|
{
|
|
|
|
Mutex mutex(&lock);
|
|
|
|
clearCaches();
|
|
|
|
delete factories;
|
|
|
|
factories = NULL;
|
|
|
|
}
|
2002-11-06 22:14:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
UObject*
|
|
|
|
ICUService::get(const UnicodeString& descriptor, UErrorCode& status) const
|
|
|
|
{
|
2007-03-15 15:13:29 +00:00
|
|
|
return get(descriptor, NULL, status);
|
2002-11-06 22:14:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
UObject*
|
|
|
|
ICUService::get(const UnicodeString& descriptor, UnicodeString* actualReturn, UErrorCode& status) const
|
|
|
|
{
|
2007-03-15 15:13:29 +00:00
|
|
|
UObject* result = NULL;
|
2002-11-20 17:16:11 +00:00
|
|
|
ICUServiceKey* key = createKey(&descriptor, status);
|
2002-11-06 22:14:49 +00:00
|
|
|
if (key) {
|
2007-03-15 15:13:29 +00:00
|
|
|
result = getKey(*key, actualReturn, status);
|
|
|
|
delete key;
|
2002-11-06 22:14:49 +00:00
|
|
|
}
|
2007-03-15 15:13:29 +00:00
|
|
|
return result;
|
2002-11-06 22:14:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
UObject*
|
2002-11-20 17:16:11 +00:00
|
|
|
ICUService::getKey(ICUServiceKey& key, UErrorCode& status) const
|
2002-11-06 22:14:49 +00:00
|
|
|
{
|
2007-03-15 15:13:29 +00:00
|
|
|
return getKey(key, NULL, status);
|
2002-11-06 22:14:49 +00:00
|
|
|
}
|
|
|
|
|
2003-05-02 22:17:14 +00:00
|
|
|
// this is a vector that subclasses of ICUService can override to further customize the result object
|
|
|
|
// before returning it. All other public get functions should call this one.
|
|
|
|
|
2002-11-06 22:14:49 +00:00
|
|
|
UObject*
|
2002-11-20 17:16:11 +00:00
|
|
|
ICUService::getKey(ICUServiceKey& key, UnicodeString* actualReturn, UErrorCode& status) const
|
2002-11-06 22:14:49 +00:00
|
|
|
{
|
2007-03-15 15:13:29 +00:00
|
|
|
return getKey(key, actualReturn, NULL, status);
|
2002-11-06 22:14:49 +00:00
|
|
|
}
|
|
|
|
|
2002-11-15 23:52:14 +00:00
|
|
|
// make it possible to call reentrantly on systems that don't have reentrant mutexes.
|
|
|
|
// we can use this simple approach since we know the situation where we're calling
|
|
|
|
// reentrantly even without knowing the thread.
|
2002-11-14 17:48:26 +00:00
|
|
|
class XMutex : public UMemory {
|
|
|
|
public:
|
2007-03-15 15:13:29 +00:00
|
|
|
inline XMutex(UMTX *mutex, UBool reentering)
|
|
|
|
: fMutex(mutex)
|
|
|
|
, fActive(!reentering)
|
|
|
|
{
|
|
|
|
if (fActive) umtx_lock(fMutex);
|
|
|
|
}
|
|
|
|
inline ~XMutex() {
|
|
|
|
if (fActive) umtx_unlock(fMutex);
|
|
|
|
}
|
2002-11-14 17:48:26 +00:00
|
|
|
|
|
|
|
private:
|
2007-03-15 15:13:29 +00:00
|
|
|
UMTX *fMutex;
|
|
|
|
UBool fActive;
|
2002-11-14 17:48:26 +00:00
|
|
|
};
|
|
|
|
|
2002-12-06 00:22:10 +00:00
|
|
|
struct UVectorDeleter {
|
2007-03-15 15:13:29 +00:00
|
|
|
UVector* _obj;
|
|
|
|
UVectorDeleter() : _obj(NULL) {}
|
|
|
|
~UVectorDeleter() { delete _obj; }
|
2002-12-06 00:22:10 +00:00
|
|
|
};
|
2003-05-02 22:17:14 +00:00
|
|
|
|
|
|
|
// called only by factories, treat as private
|
2002-11-06 22:14:49 +00:00
|
|
|
UObject*
|
2002-11-20 17:16:11 +00:00
|
|
|
ICUService::getKey(ICUServiceKey& key, UnicodeString* actualReturn, const ICUServiceFactory* factory, UErrorCode& status) const
|
2002-11-06 22:14:49 +00:00
|
|
|
{
|
2007-03-15 15:13:29 +00:00
|
|
|
if (U_FAILURE(status)) {
|
2002-11-06 22:14:49 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2007-03-15 15:13:29 +00:00
|
|
|
if (isDefault()) {
|
|
|
|
return handleDefault(key, actualReturn, status);
|
|
|
|
}
|
2002-11-06 22:14:49 +00:00
|
|
|
|
2007-03-15 15:13:29 +00:00
|
|
|
ICUService* ncthis = (ICUService*)this; // cast away semantic const
|
2002-11-06 22:14:49 +00:00
|
|
|
|
2007-03-15 15:13:29 +00:00
|
|
|
CacheEntry* result = NULL;
|
|
|
|
{
|
|
|
|
// The factory list can't be modified until we're done,
|
|
|
|
// otherwise we might update the cache with an invalid result.
|
|
|
|
// The cache has to stay in synch with the factory list.
|
|
|
|
// ICU doesn't have monitors so we can't use rw locks, so
|
|
|
|
// we single-thread everything using this service, for now.
|
|
|
|
|
|
|
|
// 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.
|
2011-03-29 00:32:11 +00:00
|
|
|
XMutex mutex(&lock, factory != NULL);
|
2007-03-15 15:13:29 +00:00
|
|
|
|
|
|
|
if (serviceCache == NULL) {
|
|
|
|
ncthis->serviceCache = new Hashtable(status);
|
2008-02-13 09:35:50 +00:00
|
|
|
if (ncthis->serviceCache == NULL) {
|
|
|
|
return NULL;
|
|
|
|
}
|
2007-03-15 15:13:29 +00:00
|
|
|
if (U_FAILURE(status)) {
|
|
|
|
delete serviceCache;
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
serviceCache->setValueDeleter(cacheDeleter);
|
2002-11-06 22:14:49 +00:00
|
|
|
}
|
|
|
|
|
2007-03-15 15:13:29 +00:00
|
|
|
UnicodeString currentDescriptor;
|
|
|
|
UVectorDeleter cacheDescriptorList;
|
|
|
|
UBool putInCache = FALSE;
|
2002-11-06 22:14:49 +00:00
|
|
|
|
2007-03-15 15:13:29 +00:00
|
|
|
int32_t startIndex = 0;
|
|
|
|
int32_t limit = factories->size();
|
|
|
|
UBool cacheResult = TRUE;
|
2002-11-06 22:14:49 +00:00
|
|
|
|
2007-03-15 15:13:29 +00:00
|
|
|
if (factory != NULL) {
|
|
|
|
for (int32_t i = 0; i < limit; ++i) {
|
|
|
|
if (factory == (const ICUServiceFactory*)factories->elementAt(i)) {
|
|
|
|
startIndex = i + 1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (startIndex == 0) {
|
|
|
|
// throw new InternalError("Factory " + factory + "not registered with service: " + this);
|
|
|
|
status = U_ILLEGAL_ARGUMENT_ERROR;
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
cacheResult = FALSE;
|
2002-11-06 22:14:49 +00:00
|
|
|
}
|
|
|
|
|
2007-03-15 15:13:29 +00:00
|
|
|
do {
|
|
|
|
currentDescriptor.remove();
|
|
|
|
key.currentDescriptor(currentDescriptor);
|
|
|
|
result = (CacheEntry*)serviceCache->get(currentDescriptor);
|
|
|
|
if (result != NULL) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
// first test of cache failed, so we'll have to update
|
|
|
|
// the cache if we eventually succeed-- that is, if we're
|
|
|
|
// going to update the cache at all.
|
|
|
|
putInCache = TRUE;
|
|
|
|
|
|
|
|
int32_t index = startIndex;
|
|
|
|
while (index < limit) {
|
|
|
|
ICUServiceFactory* f = (ICUServiceFactory*)factories->elementAt(index++);
|
|
|
|
UObject* service = f->create(key, this, status);
|
|
|
|
if (U_FAILURE(status)) {
|
|
|
|
delete service;
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
if (service != NULL) {
|
|
|
|
result = new CacheEntry(currentDescriptor, service);
|
|
|
|
if (result == NULL) {
|
|
|
|
delete service;
|
|
|
|
status = U_MEMORY_ALLOCATION_ERROR;
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
goto outerEnd;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// prepare to load the cache with all additional ids that
|
|
|
|
// will resolve to result, assuming we'll succeed. We
|
|
|
|
// don't want to keep querying on an id that's going to
|
|
|
|
// fallback to the one that succeeded, we want to hit the
|
|
|
|
// cache the first time next goaround.
|
|
|
|
if (cacheDescriptorList._obj == NULL) {
|
2011-06-03 05:23:57 +00:00
|
|
|
cacheDescriptorList._obj = new UVector(uprv_deleteUObject, NULL, 5, status);
|
2007-03-15 15:13:29 +00:00
|
|
|
if (U_FAILURE(status)) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
UnicodeString* idToCache = new UnicodeString(currentDescriptor);
|
|
|
|
if (idToCache == NULL || idToCache->isBogus()) {
|
|
|
|
status = U_MEMORY_ALLOCATION_ERROR;
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
cacheDescriptorList._obj->addElement(idToCache, status);
|
2002-11-06 22:14:49 +00:00
|
|
|
if (U_FAILURE(status)) {
|
2007-03-15 15:13:29 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
} while (key.fallback());
|
|
|
|
outerEnd:
|
|
|
|
|
|
|
|
if (result != NULL) {
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
result->ref();
|
|
|
|
cacheDescriptorList._obj->removeElementAt(i);
|
|
|
|
}
|
|
|
|
}
|
2002-11-06 22:14:49 +00:00
|
|
|
}
|
|
|
|
|
2007-03-15 15:13:29 +00:00
|
|
|
if (actualReturn != NULL) {
|
|
|
|
// strip null prefix
|
|
|
|
if (result->actualDescriptor.indexOf((UChar)0x2f) == 0) { // U+002f=slash (/)
|
|
|
|
actualReturn->remove();
|
|
|
|
actualReturn->append(result->actualDescriptor,
|
|
|
|
1,
|
|
|
|
result->actualDescriptor.length() - 1);
|
|
|
|
} else {
|
|
|
|
*actualReturn = result->actualDescriptor;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (actualReturn->isBogus()) {
|
|
|
|
status = U_MEMORY_ALLOCATION_ERROR;
|
|
|
|
delete result;
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
}
|
2002-11-06 22:14:49 +00:00
|
|
|
|
2007-03-15 15:13:29 +00:00
|
|
|
UObject* service = cloneInstance(result->service);
|
|
|
|
if (putInCache && !cacheResult) {
|
|
|
|
delete result;
|
|
|
|
}
|
|
|
|
return service;
|
2002-11-06 22:14:49 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-03-15 15:13:29 +00:00
|
|
|
return handleDefault(key, actualReturn, status);
|
2002-11-06 22:14:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
UObject*
|
2003-12-02 01:55:53 +00:00
|
|
|
ICUService::handleDefault(const ICUServiceKey& /* key */, UnicodeString* /* actualIDReturn */, UErrorCode& /* status */) const
|
2002-11-06 22:14:49 +00:00
|
|
|
{
|
2007-03-15 15:13:29 +00:00
|
|
|
return NULL;
|
2002-11-06 22:14:49 +00:00
|
|
|
}
|
2007-03-15 15:13:29 +00:00
|
|
|
|
2002-11-06 22:14:49 +00:00
|
|
|
UVector&
|
|
|
|
ICUService::getVisibleIDs(UVector& result, UErrorCode& status) const {
|
2007-03-15 15:13:29 +00:00
|
|
|
return getVisibleIDs(result, NULL, status);
|
2002-11-06 22:14:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
UVector&
|
|
|
|
ICUService::getVisibleIDs(UVector& result, const UnicodeString* matchID, UErrorCode& status) const
|
|
|
|
{
|
2007-03-15 15:13:29 +00:00
|
|
|
result.removeAllElements();
|
2002-11-06 22:14:49 +00:00
|
|
|
|
2007-03-15 15:13:29 +00:00
|
|
|
if (U_FAILURE(status)) {
|
|
|
|
return result;
|
|
|
|
}
|
2002-11-06 22:14:49 +00:00
|
|
|
|
2007-03-15 15:13:29 +00:00
|
|
|
{
|
2011-03-29 00:32:11 +00:00
|
|
|
Mutex mutex(&lock);
|
2007-03-15 15:13:29 +00:00
|
|
|
const Hashtable* map = getVisibleIDMap(status);
|
|
|
|
if (map != NULL) {
|
|
|
|
ICUServiceKey* fallbackKey = createKey(matchID, status);
|
|
|
|
|
|
|
|
for (int32_t pos = -1;;) {
|
|
|
|
const UHashElement* e = map->nextElement(pos);
|
|
|
|
if (e == NULL) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
const UnicodeString* id = (const UnicodeString*)e->key.pointer;
|
|
|
|
if (fallbackKey != NULL) {
|
|
|
|
if (!fallbackKey->isFallbackOf(*id)) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
UnicodeString* idClone = new UnicodeString(*id);
|
|
|
|
if (idClone == NULL || idClone->isBogus()) {
|
|
|
|
delete idClone;
|
|
|
|
status = U_MEMORY_ALLOCATION_ERROR;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
result.addElement(idClone, status);
|
|
|
|
if (U_FAILURE(status)) {
|
|
|
|
delete idClone;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
delete fallbackKey;
|
2002-11-06 22:14:49 +00:00
|
|
|
}
|
|
|
|
}
|
2007-03-15 15:13:29 +00:00
|
|
|
if (U_FAILURE(status)) {
|
|
|
|
result.removeAllElements();
|
|
|
|
}
|
|
|
|
return result;
|
2002-11-06 22:14:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const Hashtable*
|
|
|
|
ICUService::getVisibleIDMap(UErrorCode& status) const {
|
2007-03-15 15:13:29 +00:00
|
|
|
if (U_FAILURE(status)) return NULL;
|
2003-04-25 18:02:09 +00:00
|
|
|
|
2007-03-15 15:13:29 +00:00
|
|
|
// must only be called when lock is already held
|
2003-05-01 20:52:06 +00:00
|
|
|
|
2007-03-15 15:13:29 +00:00
|
|
|
ICUService* ncthis = (ICUService*)this; // cast away semantic const
|
2002-11-06 22:14:49 +00:00
|
|
|
if (idCache == NULL) {
|
2007-03-15 15:13:29 +00:00
|
|
|
ncthis->idCache = new Hashtable(status);
|
|
|
|
if (idCache == NULL) {
|
|
|
|
status = U_MEMORY_ALLOCATION_ERROR;
|
|
|
|
} else if (factories != NULL) {
|
|
|
|
for (int32_t pos = factories->size(); --pos >= 0;) {
|
|
|
|
ICUServiceFactory* f = (ICUServiceFactory*)factories->elementAt(pos);
|
|
|
|
f->updateVisibleIDs(*idCache, status);
|
|
|
|
}
|
|
|
|
if (U_FAILURE(status)) {
|
|
|
|
delete idCache;
|
|
|
|
ncthis->idCache = NULL;
|
|
|
|
}
|
|
|
|
}
|
2002-11-06 22:14:49 +00:00
|
|
|
}
|
|
|
|
|
2007-03-15 15:13:29 +00:00
|
|
|
return idCache;
|
2002-11-06 22:14:49 +00:00
|
|
|
}
|
|
|
|
|
2007-03-15 15:13:29 +00:00
|
|
|
|
2002-11-06 22:14:49 +00:00
|
|
|
UnicodeString&
|
|
|
|
ICUService::getDisplayName(const UnicodeString& id, UnicodeString& result) const
|
|
|
|
{
|
2007-03-15 15:13:29 +00:00
|
|
|
return getDisplayName(id, result, Locale::getDefault());
|
2002-11-06 22:14:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
UnicodeString&
|
|
|
|
ICUService::getDisplayName(const UnicodeString& id, UnicodeString& result, const Locale& locale) const
|
|
|
|
{
|
2007-03-15 15:13:29 +00:00
|
|
|
{
|
|
|
|
UErrorCode status = U_ZERO_ERROR;
|
2011-03-29 00:32:11 +00:00
|
|
|
Mutex mutex(&lock);
|
2007-03-15 15:13:29 +00:00
|
|
|
const Hashtable* map = getVisibleIDMap(status);
|
|
|
|
if (map != NULL) {
|
|
|
|
ICUServiceFactory* f = (ICUServiceFactory*)map->get(id);
|
|
|
|
if (f != NULL) {
|
|
|
|
f->getDisplayName(id, locale, result);
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
// fallback
|
|
|
|
UErrorCode status = U_ZERO_ERROR;
|
|
|
|
ICUServiceKey* fallbackKey = createKey(&id, status);
|
|
|
|
while (fallbackKey->fallback()) {
|
|
|
|
UnicodeString us;
|
|
|
|
fallbackKey->currentID(us);
|
|
|
|
f = (ICUServiceFactory*)map->get(us);
|
|
|
|
if (f != NULL) {
|
|
|
|
f->getDisplayName(id, locale, result);
|
|
|
|
delete fallbackKey;
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
delete fallbackKey;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
result.setToBogus();
|
|
|
|
return result;
|
2002-11-06 22:14:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
UVector&
|
|
|
|
ICUService::getDisplayNames(UVector& result, UErrorCode& status) const
|
|
|
|
{
|
2007-03-15 15:13:29 +00:00
|
|
|
return getDisplayNames(result, Locale::getDefault(), NULL, status);
|
2002-11-06 22:14:49 +00:00
|
|
|
}
|
|
|
|
|
2002-11-08 00:20:14 +00:00
|
|
|
|
2002-11-06 22:14:49 +00:00
|
|
|
UVector&
|
|
|
|
ICUService::getDisplayNames(UVector& result, const Locale& locale, UErrorCode& status) const
|
|
|
|
{
|
2007-03-15 15:13:29 +00:00
|
|
|
return getDisplayNames(result, locale, NULL, status);
|
2002-11-06 22:14:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
UVector&
|
|
|
|
ICUService::getDisplayNames(UVector& result,
|
|
|
|
const Locale& locale,
|
|
|
|
const UnicodeString* matchID,
|
|
|
|
UErrorCode& status) const
|
|
|
|
{
|
2007-03-15 15:13:29 +00:00
|
|
|
result.removeAllElements();
|
2007-06-15 06:40:59 +00:00
|
|
|
result.setDeleter(userv_deleteStringPair);
|
2007-03-15 15:13:29 +00:00
|
|
|
if (U_SUCCESS(status)) {
|
|
|
|
ICUService* ncthis = (ICUService*)this; // cast away semantic const
|
2011-03-29 00:32:11 +00:00
|
|
|
Mutex mutex(&lock);
|
2002-11-06 22:14:49 +00:00
|
|
|
|
2007-03-15 15:13:29 +00:00
|
|
|
if (dnCache != NULL && dnCache->locale != locale) {
|
|
|
|
delete dnCache;
|
|
|
|
ncthis->dnCache = NULL;
|
2002-11-06 22:14:49 +00:00
|
|
|
}
|
|
|
|
|
2007-03-15 15:13:29 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2008-04-28 17:58:21 +00:00
|
|
|
int32_t pos = -1;
|
2007-03-15 15:13:29 +00:00
|
|
|
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;
|
|
|
|
}
|
2002-11-06 22:14:49 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-03-15 15:13:29 +00:00
|
|
|
ICUServiceKey* matchKey = createKey(matchID, status);
|
2008-04-28 17:58:21 +00:00
|
|
|
/* To ensure that all elements in the hashtable are iterated, set pos to -1.
|
|
|
|
* 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;
|
2007-03-15 15:13:29 +00:00
|
|
|
const UHashElement *entry = NULL;
|
|
|
|
while ((entry = dnCache->cache.nextElement(pos)) != NULL) {
|
|
|
|
const UnicodeString* id = (const UnicodeString*)entry->value.pointer;
|
|
|
|
if (matchKey != NULL && !matchKey->isFallbackOf(*id)) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
const UnicodeString* dn = (const UnicodeString*)entry->key.pointer;
|
|
|
|
StringPair* sp = StringPair::create(*id, *dn, status);
|
|
|
|
result.addElement(sp, status);
|
|
|
|
if (U_FAILURE(status)) {
|
|
|
|
result.removeAllElements();
|
|
|
|
break;
|
|
|
|
}
|
2002-11-06 22:14:49 +00:00
|
|
|
}
|
2007-03-15 15:13:29 +00:00
|
|
|
delete matchKey;
|
2002-11-06 22:14:49 +00:00
|
|
|
|
2007-03-15 15:13:29 +00:00
|
|
|
return result;
|
2002-11-06 22:14:49 +00:00
|
|
|
}
|
|
|
|
|
2002-11-20 17:16:11 +00:00
|
|
|
URegistryKey
|
|
|
|
ICUService::registerInstance(UObject* objToAdopt, const UnicodeString& id, UErrorCode& status)
|
2002-11-06 22:14:49 +00:00
|
|
|
{
|
2007-03-15 15:13:29 +00:00
|
|
|
return registerInstance(objToAdopt, id, TRUE, status);
|
2002-11-06 22:14:49 +00:00
|
|
|
}
|
|
|
|
|
2002-11-20 17:16:11 +00:00
|
|
|
URegistryKey
|
|
|
|
ICUService::registerInstance(UObject* objToAdopt, const UnicodeString& id, UBool visible, UErrorCode& status)
|
2002-11-06 22:14:49 +00:00
|
|
|
{
|
2007-03-15 15:13:29 +00:00
|
|
|
ICUServiceKey* key = createKey(&id, status);
|
|
|
|
if (key != NULL) {
|
|
|
|
UnicodeString canonicalID;
|
|
|
|
key->canonicalID(canonicalID);
|
|
|
|
delete key;
|
2002-11-06 22:14:49 +00:00
|
|
|
|
2007-03-15 15:13:29 +00:00
|
|
|
ICUServiceFactory* f = createSimpleFactory(objToAdopt, canonicalID, visible, status);
|
|
|
|
if (f != NULL) {
|
|
|
|
return registerFactory(f, status);
|
|
|
|
}
|
2002-11-06 22:14:49 +00:00
|
|
|
}
|
2007-03-15 15:13:29 +00:00
|
|
|
delete objToAdopt;
|
|
|
|
return NULL;
|
2002-11-06 22:14:49 +00:00
|
|
|
}
|
|
|
|
|
2002-11-20 17:16:11 +00:00
|
|
|
ICUServiceFactory*
|
2002-11-08 00:20:14 +00:00
|
|
|
ICUService::createSimpleFactory(UObject* objToAdopt, const UnicodeString& id, UBool visible, UErrorCode& status)
|
2002-11-06 22:14:49 +00:00
|
|
|
{
|
2007-03-15 15:13:29 +00:00
|
|
|
if (U_SUCCESS(status)) {
|
|
|
|
if ((objToAdopt != NULL) && (!id.isBogus())) {
|
|
|
|
return new SimpleFactory(objToAdopt, id, visible);
|
|
|
|
}
|
|
|
|
status = U_ILLEGAL_ARGUMENT_ERROR;
|
2002-11-08 00:20:14 +00:00
|
|
|
}
|
2007-03-15 15:13:29 +00:00
|
|
|
return NULL;
|
2002-11-06 22:14:49 +00:00
|
|
|
}
|
|
|
|
|
2002-11-20 17:16:11 +00:00
|
|
|
URegistryKey
|
|
|
|
ICUService::registerFactory(ICUServiceFactory* factoryToAdopt, UErrorCode& status)
|
2002-11-06 22:14:49 +00:00
|
|
|
{
|
2007-03-15 15:13:29 +00:00
|
|
|
if (U_SUCCESS(status) && factoryToAdopt != NULL) {
|
|
|
|
Mutex mutex(&lock);
|
2002-11-06 22:14:49 +00:00
|
|
|
|
2007-03-15 15:13:29 +00:00
|
|
|
if (factories == NULL) {
|
|
|
|
factories = new UVector(deleteUObject, NULL, status);
|
|
|
|
if (U_FAILURE(status)) {
|
|
|
|
delete factories;
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
factories->insertElementAt(factoryToAdopt, 0, status);
|
|
|
|
if (U_SUCCESS(status)) {
|
|
|
|
clearCaches();
|
|
|
|
} else {
|
|
|
|
delete factoryToAdopt;
|
|
|
|
factoryToAdopt = NULL;
|
|
|
|
}
|
2002-11-06 22:14:49 +00:00
|
|
|
}
|
|
|
|
|
2007-03-15 15:13:29 +00:00
|
|
|
if (factoryToAdopt != NULL) {
|
|
|
|
notifyChanged();
|
|
|
|
}
|
2002-11-06 22:14:49 +00:00
|
|
|
|
2007-03-15 15:13:29 +00:00
|
|
|
return (URegistryKey)factoryToAdopt;
|
2002-11-06 22:14:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
UBool
|
2002-11-20 17:16:11 +00:00
|
|
|
ICUService::unregister(URegistryKey rkey, UErrorCode& status)
|
2002-11-06 22:14:49 +00:00
|
|
|
{
|
2007-03-15 15:13:29 +00:00
|
|
|
ICUServiceFactory *factory = (ICUServiceFactory*)rkey;
|
|
|
|
UBool result = FALSE;
|
|
|
|
if (factory != NULL && factories != NULL) {
|
|
|
|
Mutex mutex(&lock);
|
2002-11-06 22:14:49 +00:00
|
|
|
|
2007-03-15 15:13:29 +00:00
|
|
|
if (factories->removeElement(factory)) {
|
|
|
|
clearCaches();
|
|
|
|
result = TRUE;
|
|
|
|
} else {
|
|
|
|
status = U_ILLEGAL_ARGUMENT_ERROR;
|
|
|
|
delete factory;
|
|
|
|
}
|
2002-11-06 22:14:49 +00:00
|
|
|
}
|
2007-03-15 15:13:29 +00:00
|
|
|
if (result) {
|
|
|
|
notifyChanged();
|
|
|
|
}
|
|
|
|
return result;
|
2002-11-06 22:14:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
ICUService::reset()
|
|
|
|
{
|
2007-03-15 15:13:29 +00:00
|
|
|
{
|
|
|
|
Mutex mutex(&lock);
|
|
|
|
reInitializeFactories();
|
|
|
|
clearCaches();
|
|
|
|
}
|
|
|
|
notifyChanged();
|
2002-11-06 22:14:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
ICUService::reInitializeFactories()
|
|
|
|
{
|
2007-03-15 15:13:29 +00:00
|
|
|
if (factories != NULL) {
|
|
|
|
factories->removeAllElements();
|
|
|
|
}
|
2002-11-06 22:14:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
UBool
|
|
|
|
ICUService::isDefault() const
|
|
|
|
{
|
2007-03-15 15:13:29 +00:00
|
|
|
return countFactories() == 0;
|
2002-11-06 22:14:49 +00:00
|
|
|
}
|
|
|
|
|
2002-11-20 17:16:11 +00:00
|
|
|
ICUServiceKey*
|
2002-11-08 00:20:14 +00:00
|
|
|
ICUService::createKey(const UnicodeString* id, UErrorCode& status) const
|
2002-11-06 22:14:49 +00:00
|
|
|
{
|
2007-03-15 15:13:29 +00:00
|
|
|
return (U_FAILURE(status) || id == NULL) ? NULL : new ICUServiceKey(*id);
|
2002-11-06 22:14:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
ICUService::clearCaches()
|
|
|
|
{
|
2007-03-15 15:13:29 +00:00
|
|
|
// callers synchronize before use
|
|
|
|
++timestamp;
|
|
|
|
delete dnCache;
|
|
|
|
dnCache = NULL;
|
|
|
|
delete idCache;
|
|
|
|
idCache = NULL;
|
|
|
|
delete serviceCache; serviceCache = NULL;
|
2002-11-06 22:14:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
ICUService::clearServiceCache()
|
|
|
|
{
|
2007-03-15 15:13:29 +00:00
|
|
|
// callers synchronize before use
|
|
|
|
delete serviceCache; serviceCache = NULL;
|
2002-11-06 22:14:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
UBool
|
|
|
|
ICUService::acceptsListener(const EventListener& l) const
|
|
|
|
{
|
2010-05-19 17:29:33 +00:00
|
|
|
return dynamic_cast<const ServiceListener*>(&l) != NULL;
|
2002-11-06 22:14:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
ICUService::notifyListener(EventListener& l) const
|
|
|
|
{
|
2007-03-15 15:13:29 +00:00
|
|
|
((ServiceListener&)l).serviceChanged(*this);
|
2002-11-06 22:14:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
UnicodeString&
|
|
|
|
ICUService::getName(UnicodeString& result) const
|
|
|
|
{
|
2007-03-15 15:13:29 +00:00
|
|
|
return result.append(name);
|
2002-11-06 22:14:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int32_t
|
|
|
|
ICUService::countFactories() const
|
|
|
|
{
|
2007-03-15 15:13:29 +00:00
|
|
|
return factories == NULL ? 0 : factories->size();
|
2002-11-06 22:14:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int32_t
|
|
|
|
ICUService::getTimestamp() const
|
|
|
|
{
|
2007-03-15 15:13:29 +00:00
|
|
|
return timestamp;
|
2002-11-06 22:14:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
U_NAMESPACE_END
|
|
|
|
|
|
|
|
/* UCONFIG_NO_SERVICE */
|
|
|
|
#endif
|