2002-11-06 22:14:49 +00:00
|
|
|
/**
|
|
|
|
*******************************************************************************
|
|
|
|
* Copyright (C) 2001-2002, International Business Machines Corporation. *
|
|
|
|
* All Rights Reserved. *
|
|
|
|
*******************************************************************************
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "unicode/utypes.h"
|
|
|
|
|
|
|
|
#if !UCONFIG_NO_SERVICE
|
|
|
|
|
|
|
|
#include "icuserv.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
|
|
|
|
|
|
|
|
// A reference counted wrapper for an object. Creation and access is
|
|
|
|
// through RefHandle.
|
|
|
|
|
|
|
|
#ifdef SERVICE_REFCOUNT
|
|
|
|
|
|
|
|
#include "unicode/strenum.h"
|
|
|
|
|
|
|
|
/*
|
|
|
|
******************************************************************
|
|
|
|
*/
|
|
|
|
|
|
|
|
class RefCounted {
|
|
|
|
private:
|
|
|
|
int32_t _count;
|
|
|
|
UObject* _obj;
|
|
|
|
|
|
|
|
friend class RefHandle;
|
|
|
|
|
|
|
|
RefCounted(UObject* objectToAdopt) : _count(1), _obj(objectToAdopt) {}
|
|
|
|
~RefCounted() { delete _obj; }
|
|
|
|
void ref() { umtx_atomic_inc(&_count); }
|
|
|
|
void unref() { if (umtx_atomic_dec(&_count) == 0) { delete this; }}
|
|
|
|
};
|
|
|
|
|
|
|
|
/*
|
|
|
|
******************************************************************
|
|
|
|
*/
|
|
|
|
|
|
|
|
// Reference counted handle for an object
|
|
|
|
class RefHandle {
|
|
|
|
RefCounted* _ref;
|
|
|
|
|
|
|
|
public:
|
|
|
|
RefHandle() : _ref(NULL) {}
|
|
|
|
RefHandle(UObject* obj) : _ref(new RefCounted(obj)) {}
|
|
|
|
RefHandle(const RefHandle& rhs) : _ref(NULL) { operator=(rhs); }
|
|
|
|
~RefHandle() { if (_ref) _ref->unref(); }
|
|
|
|
RefHandle& operator=(const RefHandle& rhs) {
|
|
|
|
if (rhs._ref) rhs._ref->ref();
|
|
|
|
if (_ref) _ref->unref();
|
|
|
|
_ref = rhs._ref;
|
|
|
|
}
|
|
|
|
const UObject* get() const { return _ref ? _ref->_obj : NULL; }
|
|
|
|
};
|
|
|
|
|
|
|
|
/*
|
|
|
|
******************************************************************
|
|
|
|
*/
|
|
|
|
|
|
|
|
// Generic enumeration class with fail-fast behavior.
|
|
|
|
|
|
|
|
class MapEnumeration : public UObject, public StringEnumeration
|
|
|
|
{
|
|
|
|
private:
|
|
|
|
UChar* _buffer;
|
|
|
|
int _buflen;
|
|
|
|
|
|
|
|
protected:
|
|
|
|
const ICUService* _service;
|
|
|
|
uint32_t _timestamp;
|
|
|
|
RefHandle _table;
|
2002-11-20 17:16:11 +00:00
|
|
|
ICUServiceKey* _filter;
|
2002-11-06 22:14:49 +00:00
|
|
|
int32_t _position;
|
|
|
|
int32_t _count;
|
|
|
|
|
|
|
|
protected:
|
2002-11-20 17:16:11 +00:00
|
|
|
MapEnumeration(ICUService* service, int32_t timestamp, RefHandle& table, ICUServiceKey* filter = NULL)
|
2002-11-06 22:14:49 +00:00
|
|
|
: _buffer(NULL)
|
|
|
|
, _buflen(0)
|
|
|
|
, _service(service)
|
|
|
|
, _timestamp(timestamp)
|
|
|
|
, _table(table)
|
|
|
|
, _filter(filter)
|
|
|
|
, _position(0)
|
|
|
|
, _count(((const Hashtable*)table.get())->count())
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual ~MapEnumeration()
|
|
|
|
{
|
|
|
|
delete _filter;
|
|
|
|
}
|
|
|
|
|
|
|
|
int32_t count(UErrorCode& status) const
|
|
|
|
{
|
|
|
|
return U_SUCCESS(status) ? _count : 0;
|
|
|
|
}
|
|
|
|
|
2002-11-14 17:48:26 +00:00
|
|
|
const char* next(UErrorCode& status) {
|
|
|
|
const UnicodeString* us = snext(status);
|
|
|
|
if (us) {
|
|
|
|
int newlen;
|
|
|
|
for (newlen = us->extract((char*)_buffer, _buflen / sizeof(char), NULL, status);
|
|
|
|
status == U_STRING_NOT_TERMINATED_WARNING || status == U_BUFFER_OVERFLOW_ERROR;)
|
|
|
|
{
|
|
|
|
resizeBuffer((newlen + 1) * sizeof(char));
|
|
|
|
status = U_ZERO_ERROR;
|
2002-11-06 22:14:49 +00:00
|
|
|
}
|
2002-11-14 17:48:26 +00:00
|
|
|
|
|
|
|
if (U_SUCCESS(status)) {
|
|
|
|
((char*)_buffer)[newlen] = 0;
|
|
|
|
return (const char*)_buffer;
|
|
|
|
}
|
2002-11-06 22:14:49 +00:00
|
|
|
}
|
2002-11-14 17:48:26 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
2002-11-06 22:14:49 +00:00
|
|
|
|
2002-11-14 17:48:26 +00:00
|
|
|
const UChar* unext(UErrorCode& status) {
|
|
|
|
const UnicodeString* us = snext(status);
|
|
|
|
if (us) {
|
|
|
|
int newlen;
|
|
|
|
for (newlen = us->extract((UChar*)_buffer, _buflen / sizeof(UChar), NULL, status);
|
|
|
|
status == U_STRING_NOT_TERMINATED_WARNING || status == U_BUFFER_OVERFLOW_ERROR;)
|
|
|
|
{
|
|
|
|
resizeBuffer((newlen + 1) * sizeof(UChar));
|
|
|
|
status = U_ZERO_ERROR;
|
2002-11-06 22:14:49 +00:00
|
|
|
}
|
2002-11-14 17:48:26 +00:00
|
|
|
|
|
|
|
if (U_SUCCESS(status)) {
|
|
|
|
((UChar*)_buffer)[newlen] = 0;
|
|
|
|
return (const UChar*)_buffer;
|
|
|
|
}
|
2002-11-06 22:14:49 +00:00
|
|
|
}
|
2002-11-14 17:48:26 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
2002-11-06 22:14:49 +00:00
|
|
|
|
|
|
|
const UnicodeString* snext(UErrorCode& status)
|
|
|
|
{
|
|
|
|
if (U_SUCCESS(status)) {
|
|
|
|
if (_timestamp != _service->_timestamp) {
|
|
|
|
status = U_ENUM_OUT_OF_SYNCH_ERROR;
|
|
|
|
} else {
|
|
|
|
return internalNext((Hashtable*)_table.get());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
void reset(UErrorCode& status)
|
|
|
|
{
|
|
|
|
if (U_SUCCESS(status)) {
|
|
|
|
service->reset(this);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
protected:
|
|
|
|
virtual const UnicodeString* internalNext(Hashtable* table) = 0;
|
|
|
|
|
|
|
|
private:
|
|
|
|
void reset(RefHandle& table, int32_t timestamp)
|
|
|
|
{
|
|
|
|
_table = table;
|
|
|
|
_timestamp = timestamp;
|
|
|
|
_position = 0;
|
|
|
|
_count = ((const Hashtable*)table.get())->count();
|
|
|
|
}
|
|
|
|
|
|
|
|
friend class ICUService;
|
|
|
|
};
|
|
|
|
|
|
|
|
/*
|
|
|
|
******************************************************************
|
|
|
|
*/
|
|
|
|
|
|
|
|
// An enumeration over the visible ids in a service. The ids
|
|
|
|
// are in the hashtable, which is refcounted, so it will not
|
|
|
|
// disappear as long as the enumeration exists even if the
|
|
|
|
// service itself unrefs it. For "fail-fast" behavior the
|
|
|
|
// enumeration checks the timestamp of the service, but this
|
|
|
|
// is not a guarantee that the result the client receives will
|
|
|
|
// still be valid once the function returns.
|
|
|
|
|
|
|
|
class IDEnumeration : public MapEnumeration {
|
|
|
|
public:
|
2002-11-20 17:16:11 +00:00
|
|
|
IDEnumeration(ICUService* service, int32_t timestamp, RefHandle& table, ICUServiceKey* filter = NULL)
|
2002-11-06 22:14:49 +00:00
|
|
|
: MapEnumeration(service, timestamp, table, filter)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
protected:
|
|
|
|
const UnicodeString* internalNext(Hashtable* table) {
|
|
|
|
while (TRUE) {
|
|
|
|
const UnicodeString* elem = (const UnicodeString*)(table->nextElement(_position).key.pointer);
|
|
|
|
if (elem == NULL ||
|
|
|
|
_filter == NULL ||
|
|
|
|
_filter->isFallbackOf(*elem)) {
|
|
|
|
return elem;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
/*
|
|
|
|
******************************************************************
|
|
|
|
*/
|
|
|
|
|
|
|
|
class DisplayEnumeration : public MapEnumeration {
|
|
|
|
private:
|
|
|
|
Locale _locale;
|
|
|
|
UnicodeString _cache;
|
|
|
|
|
|
|
|
public:
|
2002-11-20 17:16:11 +00:00
|
|
|
DisplayEnumeration(ICUService* service, int32_t timestamp, RefHandle& table, Locale& locale, ICUServiceKey* filter = NULL)
|
2002-11-06 22:14:49 +00:00
|
|
|
: MapEnumeration(service, timestamp, table, filter), _locale(locale)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
protected:
|
|
|
|
const UnicodeString* internalNext(Hashtable* table) {
|
|
|
|
while (TRUE) {
|
|
|
|
UHashElement* elem = table->nextElement(_position);
|
|
|
|
if (elem == NULL) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
const UnicodeString* id = (const UnicodeString*)elem->key.pointer;
|
2002-11-20 17:16:11 +00:00
|
|
|
const ICUServiceFactory* factory = (const ICUServiceFactory*)elem->value.pointer;
|
2002-11-06 22:14:49 +00:00
|
|
|
if (_filter == NULL || _filter->isFallbackOf(*id)) {
|
|
|
|
factory->getDisplayName(*id, cache, locale);
|
|
|
|
return &cache;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
/* SERVICE_REFCOUNT */
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/*
|
|
|
|
******************************************************************
|
|
|
|
*/
|
|
|
|
|
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)
|
2002-11-06 22:14:49 +00:00
|
|
|
: _id(id) {
|
|
|
|
}
|
|
|
|
|
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
|
|
|
{
|
|
|
|
return _id;
|
|
|
|
}
|
|
|
|
|
|
|
|
UnicodeString&
|
2002-11-20 17:16:11 +00:00
|
|
|
ICUServiceKey::canonicalID(UnicodeString& result) const
|
2002-11-06 22:14:49 +00:00
|
|
|
{
|
|
|
|
return result.append(_id);
|
|
|
|
}
|
|
|
|
|
|
|
|
UnicodeString&
|
2002-11-20 17:16:11 +00:00
|
|
|
ICUServiceKey::currentID(UnicodeString& result) const
|
2002-11-06 22:14:49 +00:00
|
|
|
{
|
|
|
|
return canonicalID(result);
|
|
|
|
}
|
|
|
|
|
|
|
|
UnicodeString&
|
2002-11-20 17:16:11 +00:00
|
|
|
ICUServiceKey::currentDescriptor(UnicodeString& result) const
|
2002-11-06 22:14:49 +00:00
|
|
|
{
|
|
|
|
prefix(result);
|
|
|
|
result.append(PREFIX_DELIMITER);
|
|
|
|
return currentID(result);
|
|
|
|
}
|
|
|
|
|
|
|
|
UBool
|
2002-11-20 17:16:11 +00:00
|
|
|
ICUServiceKey::fallback()
|
2002-11-06 22:14:49 +00:00
|
|
|
{
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
UBool
|
2002-11-20 17:16:11 +00:00
|
|
|
ICUServiceKey::isFallbackOf(const UnicodeString& id) const
|
2002-11-06 22:14:49 +00:00
|
|
|
{
|
|
|
|
return id == _id;
|
|
|
|
}
|
|
|
|
|
|
|
|
UnicodeString&
|
2002-11-20 17:16:11 +00:00
|
|
|
ICUServiceKey::prefix(UnicodeString& result) const
|
2002-11-06 22:14:49 +00:00
|
|
|
{
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
UnicodeString&
|
2002-11-20 17:16:11 +00:00
|
|
|
ICUServiceKey::parsePrefix(UnicodeString& result)
|
2002-11-06 22:14:49 +00:00
|
|
|
{
|
|
|
|
int32_t n = result.indexOf(PREFIX_DELIMITER);
|
|
|
|
if (n < 0) {
|
|
|
|
n = 0;
|
|
|
|
}
|
|
|
|
result.remove(n);
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
UnicodeString&
|
2002-11-20 17:16:11 +00:00
|
|
|
ICUServiceKey::parseSuffix(UnicodeString& result)
|
2002-11-06 22:14:49 +00:00
|
|
|
{
|
|
|
|
int32_t n = result.indexOf(PREFIX_DELIMITER);
|
|
|
|
if (n >= 0) {
|
|
|
|
result.remove(0, n+1);
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
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
|
|
|
{
|
|
|
|
debugClass(result);
|
|
|
|
result.append(" id: ");
|
|
|
|
result.append(_id);
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
UnicodeString&
|
2002-11-20 17:16:11 +00:00
|
|
|
ICUServiceKey::debugClass(UnicodeString& result) const
|
2002-11-06 22:14:49 +00:00
|
|
|
{
|
2002-11-20 17:16:11 +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
|
|
|
|
2002-11-20 17:16:11 +00:00
|
|
|
const char ICUServiceKey::fgClassID = '\0';
|
2002-11-06 22:14:49 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
******************************************************************
|
|
|
|
*/
|
|
|
|
|
|
|
|
SimpleFactory::SimpleFactory(UObject* instanceToAdopt, const UnicodeString& id, UBool visible)
|
|
|
|
: _instance(instanceToAdopt), _id(id), _visible(visible)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
SimpleFactory::~SimpleFactory()
|
|
|
|
{
|
|
|
|
delete _instance;
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
{
|
|
|
|
if (U_SUCCESS(status)) {
|
|
|
|
UnicodeString temp;
|
|
|
|
if (_id == key.currentID(temp)) {
|
|
|
|
return service->cloneInstance(_instance);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
SimpleFactory::updateVisibleIDs(Hashtable& result, UErrorCode& status) const
|
|
|
|
{
|
|
|
|
if (_visible) {
|
|
|
|
result.put(_id, (void*)this, status); // cast away const
|
|
|
|
} else {
|
|
|
|
result.remove(_id);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
UnicodeString&
|
|
|
|
SimpleFactory::getDisplayName(const UnicodeString& id, const Locale& locale, UnicodeString& result) const
|
|
|
|
{
|
|
|
|
if (_visible && _id == id) {
|
|
|
|
result = _id;
|
|
|
|
} else {
|
|
|
|
result.setToBogus();
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
{
|
|
|
|
debugClass(toAppendTo);
|
|
|
|
toAppendTo.append(" id: ");
|
|
|
|
toAppendTo.append(_id);
|
|
|
|
toAppendTo.append(", visible: ");
|
|
|
|
toAppendTo.append(_visible ? "T" : "F");
|
|
|
|
return toAppendTo;
|
|
|
|
}
|
|
|
|
|
|
|
|
UnicodeString&
|
|
|
|
SimpleFactory::debugClass(UnicodeString& toAppendTo) const
|
|
|
|
{
|
|
|
|
return toAppendTo.append("SimpleFactory");
|
|
|
|
}
|
2002-11-20 17:16:11 +00:00
|
|
|
#endif
|
2002-11-06 22:14:49 +00:00
|
|
|
|
|
|
|
const char SimpleFactory::fgClassID = '\0';
|
|
|
|
|
|
|
|
/*
|
|
|
|
******************************************************************
|
|
|
|
*/
|
|
|
|
|
|
|
|
const char ServiceListener::fgClassID = '\0';
|
|
|
|
|
|
|
|
/*
|
|
|
|
******************************************************************
|
|
|
|
*/
|
|
|
|
|
|
|
|
// 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:
|
|
|
|
int32_t refcount;
|
|
|
|
|
|
|
|
public:
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
// UObjectDeleter for serviceCache
|
|
|
|
|
|
|
|
U_CAPI void U_EXPORT2
|
|
|
|
cacheDeleter(void* obj) {
|
|
|
|
U_NAMESPACE_USE
|
|
|
|
((CacheEntry*)obj)->unref();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Deleter for UObjects
|
|
|
|
*/
|
|
|
|
U_CAPI void U_EXPORT2
|
|
|
|
deleteUObject(void *obj) {
|
|
|
|
U_NAMESPACE_USE
|
|
|
|
delete (UObject*) obj;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
******************************************************************
|
|
|
|
*/
|
|
|
|
|
2002-12-14 09:19:33 +00:00
|
|
|
class DNCache : public UMemory {
|
2002-11-06 22:14:49 +00:00
|
|
|
public:
|
|
|
|
Hashtable cache;
|
|
|
|
const Locale locale;
|
|
|
|
|
|
|
|
DNCache(const Locale& _locale)
|
|
|
|
: cache(FALSE), locale(_locale)
|
|
|
|
{
|
|
|
|
// cache.setKeyDeleter(uhash_deleteUnicodeString);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
******************************************************************
|
|
|
|
*/
|
|
|
|
|
|
|
|
StringPair*
|
|
|
|
StringPair::create(const UnicodeString& displayName,
|
|
|
|
const UnicodeString& id,
|
|
|
|
UErrorCode& status)
|
|
|
|
{
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
UBool
|
|
|
|
StringPair::isBogus() const {
|
|
|
|
return displayName.isBogus() || id.isBogus();
|
|
|
|
}
|
|
|
|
|
|
|
|
StringPair::StringPair(const UnicodeString& _displayName,
|
|
|
|
const UnicodeString& _id)
|
|
|
|
: displayName(_displayName)
|
|
|
|
, id(_id)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
U_CAPI void U_EXPORT2
|
|
|
|
deleteStringPair(void *obj) {
|
|
|
|
U_NAMESPACE_USE
|
|
|
|
delete (StringPair*) obj;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
******************************************************************
|
|
|
|
*/
|
|
|
|
|
|
|
|
ICUService::ICUService()
|
|
|
|
: name()
|
|
|
|
, lock(0)
|
|
|
|
, timestamp(0)
|
|
|
|
, factories(NULL)
|
|
|
|
, serviceCache(NULL)
|
|
|
|
, idCache(NULL)
|
|
|
|
, dnCache(NULL)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2002-11-24 23:46:04 +00:00
|
|
|
ICUService::ICUService(const UnicodeString& newName)
|
|
|
|
: name(newName)
|
2002-11-06 22:14:49 +00:00
|
|
|
, lock(0)
|
|
|
|
, timestamp(0)
|
|
|
|
, factories(NULL)
|
|
|
|
, serviceCache(NULL)
|
|
|
|
, idCache(NULL)
|
|
|
|
, dnCache(NULL) {
|
|
|
|
}
|
|
|
|
|
|
|
|
ICUService::~ICUService()
|
|
|
|
{
|
2002-11-08 18:49:15 +00:00
|
|
|
{
|
|
|
|
Mutex mutex(&lock);
|
|
|
|
clearCaches();
|
|
|
|
delete factories;
|
|
|
|
factories = NULL;
|
|
|
|
}
|
|
|
|
umtx_destroy(&lock);
|
2002-11-06 22:14:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
UObject*
|
|
|
|
ICUService::get(const UnicodeString& descriptor, UErrorCode& status) const
|
|
|
|
{
|
|
|
|
return get(descriptor, NULL, status);
|
|
|
|
}
|
|
|
|
|
|
|
|
UObject*
|
|
|
|
ICUService::get(const UnicodeString& descriptor, UnicodeString* actualReturn, UErrorCode& status) const
|
|
|
|
{
|
|
|
|
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) {
|
|
|
|
result = getKey(*key, actualReturn, NULL, status);
|
|
|
|
delete key;
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
UObject*
|
2002-11-20 17:16:11 +00:00
|
|
|
ICUService::getKey(ICUServiceKey& key, UErrorCode& status) const
|
2002-11-06 22:14:49 +00:00
|
|
|
{
|
|
|
|
return getKey(key, NULL, NULL, status);
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
{
|
|
|
|
return getKey(key, actualReturn, NULL, status);
|
|
|
|
}
|
|
|
|
|
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:
|
|
|
|
inline XMutex(UMTX *mutex, UBool reentering)
|
|
|
|
: fMutex(mutex)
|
|
|
|
, fActive(!reentering)
|
|
|
|
{
|
|
|
|
if (fActive) umtx_lock(fMutex);
|
|
|
|
}
|
|
|
|
inline ~XMutex() {
|
|
|
|
if (fActive) umtx_unlock(fMutex);
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
UMTX *fMutex;
|
|
|
|
UBool fActive;
|
|
|
|
};
|
|
|
|
|
2002-12-06 00:22:10 +00:00
|
|
|
struct UVectorDeleter {
|
|
|
|
UVector* _obj;
|
|
|
|
UVectorDeleter() : _obj(NULL) {}
|
|
|
|
~UVectorDeleter() { delete _obj; }
|
|
|
|
};
|
|
|
|
|
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
|
|
|
{
|
|
|
|
if (U_FAILURE(status)) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (isDefault()) {
|
|
|
|
return handleDefault(key, actualReturn, status);
|
|
|
|
}
|
|
|
|
|
|
|
|
ICUService* ncthis = (ICUService*)this; // cast away semantic const
|
|
|
|
|
|
|
|
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.
|
2002-11-08 18:49:15 +00:00
|
|
|
|
2002-11-14 17:48:26 +00:00
|
|
|
// 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(&ncthis->lock, factory != NULL);
|
2002-11-06 22:14:49 +00:00
|
|
|
|
|
|
|
if (serviceCache == NULL) {
|
|
|
|
ncthis->serviceCache = new Hashtable(FALSE, status);
|
|
|
|
if (U_FAILURE(status)) {
|
|
|
|
delete serviceCache;
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
serviceCache->setValueDeleter(cacheDeleter);
|
|
|
|
}
|
|
|
|
|
|
|
|
UnicodeString currentDescriptor;
|
2002-12-06 00:22:10 +00:00
|
|
|
UVectorDeleter cacheDescriptorList;
|
2002-11-06 22:14:49 +00:00
|
|
|
UBool putInCache = FALSE;
|
|
|
|
|
|
|
|
int32_t startIndex = 0;
|
|
|
|
int32_t limit = factories->size();
|
|
|
|
UBool cacheResult = TRUE;
|
|
|
|
|
|
|
|
if (factory != NULL) {
|
|
|
|
for (int32_t i = 0; i < limit; ++i) {
|
2002-11-20 17:16:11 +00:00
|
|
|
if (factory == (const ICUServiceFactory*)factories->elementAt(i)) {
|
2002-11-06 22:14:49 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
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.
|
2002-12-06 00:22:10 +00:00
|
|
|
putInCache = TRUE;
|
2002-11-06 22:14:49 +00:00
|
|
|
|
|
|
|
int32_t n = 0;
|
|
|
|
int32_t index = startIndex;
|
|
|
|
while (index < limit) {
|
2002-11-20 17:16:11 +00:00
|
|
|
ICUServiceFactory* f = (ICUServiceFactory*)factories->elementAt(index++);
|
2002-11-06 22:14:49 +00:00
|
|
|
UObject* service = f->create(key, this, status);
|
|
|
|
if (U_FAILURE(status)) {
|
2002-12-04 18:50:01 +00:00
|
|
|
delete service;
|
2002-11-06 22:14:49 +00:00
|
|
|
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.
|
2002-12-06 00:22:10 +00:00
|
|
|
if (cacheDescriptorList._obj == NULL) {
|
|
|
|
cacheDescriptorList._obj = new UVector(uhash_deleteUnicodeString, NULL, 5, status);
|
2002-11-06 22:14:49 +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;
|
|
|
|
}
|
|
|
|
|
2002-12-06 00:22:10 +00:00
|
|
|
cacheDescriptorList._obj->addElement(idToCache, status);
|
2002-11-06 22:14:49 +00:00
|
|
|
if (U_FAILURE(status)) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
} while (key.fallback());
|
|
|
|
outerEnd:
|
|
|
|
|
|
|
|
if (result != NULL) {
|
2002-12-06 00:22:10 +00:00
|
|
|
if (putInCache && cacheResult) {
|
2002-11-06 22:14:49 +00:00
|
|
|
serviceCache->put(result->actualDescriptor, result, status);
|
|
|
|
if (U_FAILURE(status)) {
|
|
|
|
delete result;
|
2002-11-14 17:48:26 +00:00
|
|
|
return NULL;
|
2002-11-06 22:14:49 +00:00
|
|
|
}
|
|
|
|
|
2002-12-06 00:22:10 +00:00
|
|
|
if (cacheDescriptorList._obj != NULL) {
|
|
|
|
for (int32_t i = cacheDescriptorList._obj->size(); --i >= 0;) {
|
|
|
|
UnicodeString* desc = (UnicodeString*)cacheDescriptorList._obj->elementAt(i);
|
2002-11-06 22:14:49 +00:00
|
|
|
serviceCache->put(*desc, result, status);
|
|
|
|
if (U_FAILURE(status)) {
|
|
|
|
delete result;
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
result->ref();
|
2002-12-06 00:22:10 +00:00
|
|
|
cacheDescriptorList._obj->removeElementAt(i);
|
2002-11-06 22:14:49 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (actualReturn != NULL) {
|
|
|
|
// strip null prefix
|
|
|
|
if (result->actualDescriptor.indexOf("/") == 0) {
|
|
|
|
actualReturn->remove();
|
|
|
|
actualReturn->append(result->actualDescriptor,
|
|
|
|
1,
|
|
|
|
result->actualDescriptor.length() - 1);
|
|
|
|
} else {
|
|
|
|
*actualReturn = result->actualDescriptor;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (actualReturn->isBogus()) {
|
|
|
|
status = U_MEMORY_ALLOCATION_ERROR;
|
2002-12-06 00:22:10 +00:00
|
|
|
delete result;
|
2002-11-06 22:14:49 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2002-12-06 00:22:10 +00:00
|
|
|
UObject* service = cloneInstance(result->service);
|
|
|
|
if (putInCache && !cacheResult) {
|
|
|
|
delete result;
|
|
|
|
}
|
|
|
|
return service;
|
2002-11-06 22:14:49 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return handleDefault(key, actualReturn, status);
|
|
|
|
}
|
|
|
|
|
|
|
|
UObject*
|
2002-11-20 17:16:11 +00:00
|
|
|
ICUService::handleDefault(const ICUServiceKey& key, UnicodeString* actualIDReturn, UErrorCode& status) const
|
2002-11-06 22:14:49 +00:00
|
|
|
{
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
UVector&
|
|
|
|
ICUService::getVisibleIDs(UVector& result, UErrorCode& status) const {
|
|
|
|
return getVisibleIDs(result, NULL, status);
|
|
|
|
}
|
|
|
|
|
|
|
|
UVector&
|
|
|
|
ICUService::getVisibleIDs(UVector& result, const UnicodeString* matchID, UErrorCode& status) const
|
|
|
|
{
|
|
|
|
result.removeAllElements();
|
|
|
|
|
|
|
|
if (U_FAILURE(status)) {
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
ICUService * ncthis = (ICUService*)this; // cast away semantic const
|
|
|
|
{
|
|
|
|
Mutex mutex(&ncthis->lock);
|
|
|
|
const Hashtable* map = getVisibleIDMap(status);
|
|
|
|
if (map != NULL) {
|
2002-11-20 17:16:11 +00:00
|
|
|
ICUServiceKey* fallbackKey = createKey(matchID, status);
|
2002-11-06 22:14:49 +00:00
|
|
|
|
|
|
|
for (int32_t pos = 0;;) {
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (U_FAILURE(status)) {
|
|
|
|
result.removeAllElements();
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
const Hashtable*
|
|
|
|
ICUService::getVisibleIDMap(UErrorCode& status) const {
|
|
|
|
if (U_SUCCESS(status) && idCache == NULL) {
|
|
|
|
ICUService* ncthis = (ICUService*)this; // cast away semantic const
|
|
|
|
ncthis->idCache = new Hashtable();
|
|
|
|
if (idCache == NULL) {
|
|
|
|
status = U_MEMORY_ALLOCATION_ERROR;
|
|
|
|
} else if (factories != NULL) {
|
|
|
|
for (int32_t pos = factories->size(); --pos >= 0;) {
|
2002-11-20 17:16:11 +00:00
|
|
|
ICUServiceFactory* f = (ICUServiceFactory*)factories->elementAt(pos);
|
2002-11-06 22:14:49 +00:00
|
|
|
f->updateVisibleIDs(*idCache, status);
|
|
|
|
}
|
|
|
|
if (U_FAILURE(status)) {
|
|
|
|
delete idCache;
|
|
|
|
ncthis->idCache = NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return idCache;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
UnicodeString&
|
|
|
|
ICUService::getDisplayName(const UnicodeString& id, UnicodeString& result) const
|
|
|
|
{
|
|
|
|
return getDisplayName(id, result, Locale::getDefault());
|
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
|
|
|
const Hashtable* map = getVisibleIDMap(status);
|
|
|
|
if (map != NULL) {
|
2002-11-20 17:16:11 +00:00
|
|
|
ICUServiceFactory* f = (ICUServiceFactory*)map->get(id);
|
2002-11-06 22:14:49 +00:00
|
|
|
if (f != NULL) {
|
|
|
|
f->getDisplayName(id, locale, result);
|
2002-11-20 17:16:11 +00:00
|
|
|
return result;
|
2002-11-06 22:14:49 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2002-11-20 17:16:11 +00:00
|
|
|
result.setToBogus();
|
2002-11-06 22:14:49 +00:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
UVector&
|
|
|
|
ICUService::getDisplayNames(UVector& result, UErrorCode& status) const
|
|
|
|
{
|
2002-11-08 00:20:14 +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
|
|
|
|
{
|
|
|
|
return getDisplayNames(result, locale, NULL, status);
|
|
|
|
}
|
|
|
|
|
|
|
|
UVector&
|
|
|
|
ICUService::getDisplayNames(UVector& result,
|
|
|
|
const Locale& locale,
|
|
|
|
const UnicodeString* matchID,
|
|
|
|
UErrorCode& status) const
|
|
|
|
{
|
|
|
|
result.removeAllElements();
|
|
|
|
if (U_SUCCESS(status)) {
|
|
|
|
ICUService* ncthis = (ICUService*)this; // cast away semantic const
|
|
|
|
Mutex mutex(&ncthis->lock);
|
|
|
|
|
|
|
|
if (dnCache != NULL && dnCache->locale != locale) {
|
|
|
|
delete dnCache;
|
|
|
|
ncthis->dnCache = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
int32_t pos = 0;
|
|
|
|
const UHashElement* entry = NULL;
|
|
|
|
while (entry = m->nextElement(pos)) {
|
|
|
|
const UnicodeString* id = (const UnicodeString*)entry->key.pointer;
|
2002-11-20 17:16:11 +00:00
|
|
|
ICUServiceFactory* f = (ICUServiceFactory*)entry->value.pointer;
|
2002-11-24 23:46:04 +00:00
|
|
|
UnicodeString dname;
|
|
|
|
f->getDisplayName(*id, locale, dname);
|
|
|
|
if (dname.isBogus()) {
|
2002-11-06 22:14:49 +00:00
|
|
|
status = U_MEMORY_ALLOCATION_ERROR;
|
|
|
|
} else {
|
2002-11-24 23:46:04 +00:00
|
|
|
dnCache->cache.put(dname, (void*)id, status); // share pointer with visibleIDMap
|
2002-11-06 22:14:49 +00:00
|
|
|
if (U_SUCCESS(status)) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
delete dnCache;
|
|
|
|
ncthis->dnCache = NULL;
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2002-11-20 17:16:11 +00:00
|
|
|
ICUServiceKey* matchKey = createKey(matchID, status);
|
2002-11-06 22:14:49 +00:00
|
|
|
int32_t pos = 0;
|
|
|
|
const UHashElement *entry = NULL;
|
|
|
|
while (entry = dnCache->cache.nextElement(pos)) {
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
delete matchKey;
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
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
|
|
|
{
|
2002-11-20 17:16:11 +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
|
|
|
{
|
2002-11-20 17:16:11 +00:00
|
|
|
ICUServiceKey* key = createKey(&id, status);
|
2002-11-06 22:14:49 +00:00
|
|
|
if (key != NULL) {
|
|
|
|
UnicodeString canonicalID;
|
|
|
|
key->canonicalID(canonicalID);
|
|
|
|
delete key;
|
|
|
|
|
2002-11-20 17:16:11 +00:00
|
|
|
ICUServiceFactory* f = createSimpleFactory(objToAdopt, canonicalID, visible, status);
|
2002-11-06 22:14:49 +00:00
|
|
|
if (f != NULL) {
|
2002-11-08 00:20:14 +00:00
|
|
|
return registerFactory(f, status);
|
2002-11-06 22:14:49 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
delete objToAdopt;
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
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
|
|
|
{
|
2002-11-08 00:20:14 +00:00
|
|
|
if (U_SUCCESS(status)) {
|
|
|
|
if ((objToAdopt != NULL) && (!id.isBogus())) {
|
|
|
|
return new SimpleFactory(objToAdopt, id, visible);
|
|
|
|
}
|
|
|
|
status = U_ILLEGAL_ARGUMENT_ERROR;
|
|
|
|
}
|
|
|
|
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
|
|
|
{
|
2002-11-08 00:20:14 +00:00
|
|
|
if (U_SUCCESS(status) && factoryToAdopt != NULL) {
|
2002-11-06 22:14:49 +00:00
|
|
|
Mutex mutex(&lock);
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (factoryToAdopt != NULL) {
|
|
|
|
notifyChanged();
|
|
|
|
}
|
|
|
|
|
2002-11-20 17:16:11 +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
|
|
|
{
|
2002-11-20 17:16:11 +00:00
|
|
|
ICUServiceFactory *factory = (ICUServiceFactory*)rkey;
|
2002-11-06 22:14:49 +00:00
|
|
|
UBool result = FALSE;
|
|
|
|
if (factory != NULL && factories != NULL) {
|
|
|
|
Mutex mutex(&lock);
|
|
|
|
|
|
|
|
if (factories->removeElement(factory)) {
|
|
|
|
clearCaches();
|
|
|
|
result = TRUE;
|
2002-11-08 00:20:14 +00:00
|
|
|
} else {
|
|
|
|
status = U_ILLEGAL_ARGUMENT_ERROR;
|
|
|
|
delete factory;
|
2002-11-06 22:14:49 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if (result) {
|
|
|
|
notifyChanged();
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
ICUService::reset()
|
|
|
|
{
|
|
|
|
{
|
|
|
|
Mutex mutex(&lock);
|
|
|
|
reInitializeFactories();
|
|
|
|
clearCaches();
|
|
|
|
}
|
|
|
|
notifyChanged();
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
ICUService::reInitializeFactories()
|
|
|
|
{
|
|
|
|
if (factories != NULL) {
|
|
|
|
factories->removeAllElements();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
UBool
|
|
|
|
ICUService::isDefault() const
|
|
|
|
{
|
2002-11-08 00:20:14 +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
|
|
|
{
|
2002-11-20 17:16:11 +00:00
|
|
|
return (U_FAILURE(status) || id == NULL) ? NULL : new ICUServiceKey(*id);
|
2002-11-06 22:14:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
ICUService::clearCaches()
|
|
|
|
{
|
|
|
|
// callers synchronize before use
|
|
|
|
++timestamp;
|
|
|
|
delete dnCache; dnCache = NULL;
|
|
|
|
delete idCache; idCache = NULL;
|
|
|
|
delete serviceCache; serviceCache = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
ICUService::clearServiceCache()
|
|
|
|
{
|
|
|
|
// callers synchronize before use
|
|
|
|
delete serviceCache; serviceCache = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
UBool
|
|
|
|
ICUService::acceptsListener(const EventListener& l) const
|
|
|
|
{
|
|
|
|
return l.getDynamicClassID() == ServiceListener::getStaticClassID();
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
ICUService::notifyListener(EventListener& l) const
|
|
|
|
{
|
|
|
|
((ServiceListener&)l).serviceChanged(*this);
|
|
|
|
}
|
|
|
|
|
|
|
|
UnicodeString&
|
|
|
|
ICUService::getName(UnicodeString& result) const
|
|
|
|
{
|
|
|
|
return result.append(name);
|
|
|
|
}
|
|
|
|
|
|
|
|
int32_t
|
|
|
|
ICUService::countFactories() const
|
|
|
|
{
|
|
|
|
return factories == NULL ? 0 : factories->size();
|
|
|
|
}
|
|
|
|
|
|
|
|
int32_t
|
|
|
|
ICUService::getTimestamp() const
|
|
|
|
{
|
|
|
|
return timestamp;
|
|
|
|
}
|
|
|
|
|
|
|
|
U_NAMESPACE_END
|
|
|
|
|
|
|
|
/* UCONFIG_NO_SERVICE */
|
|
|
|
#endif
|