/**
*******************************************************************************
- * Copyright (C) 2001-2004, International Business Machines Corporation and *
- * others. All Rights Reserved. *
- *******************************************************************************
- *
+ * Copyright (C) 2001-2012, International Business Machines Corporation and
+ * others. All Rights Reserved.
*******************************************************************************
*/
+#include "utypeinfo.h" // for 'typeid' to work
+
#include "unicode/utypes.h"
#if !UCONFIG_NO_SERVICE
#include "icusvtst.h"
-#include "iculserv.h"
+#include "servloc.h"
#include <stdio.h>
class ICUNSubclass : public ICUNotifier {
public:
- UBool acceptsListener(const EventListener& l) const {
+ UBool acceptsListener(const EventListener& /*l*/) const {
return TRUE;
// return l instanceof MyListener;
}
- virtual void notifyListener(EventListener& l) const {
+ virtual void notifyListener(EventListener& /*l*/) const {
}
};
+// This factory does nothing
+class LKFSubclass0 : public LocaleKeyFactory {
+public:
+ LKFSubclass0()
+ : LocaleKeyFactory(VISIBLE, "LKFSubclass0")
+ {
+ }
+};
+
class LKFSubclass : public LocaleKeyFactory {
Hashtable table;
virtual ~Integer() {
}
- virtual UBool operator==(const UObject& other) const
- {
- return other.getDynamicClassID() == getStaticClassID() &&
- _val == ((Integer&)other)._val;
- }
-
public:
/**
* UObject boilerplate.
*/
+ static UClassID getStaticClassID() {
+ return (UClassID)&fgClassID;
+ }
+
virtual UClassID getDynamicClassID() const {
return getStaticClassID();
}
- static UClassID getStaticClassID() {
- return (UClassID)&fgClassID;
+ virtual UBool operator==(const UObject& other) const
+ {
+ return typeid(*this) == typeid(other) &&
+ _val == ((Integer&)other)._val;
}
public:
virtual ICUServiceFactory* createSimpleFactory(UObject* obj, const UnicodeString& id, UBool visible, UErrorCode& status)
{
- if (U_SUCCESS(status) && obj && obj->getDynamicClassID() == Integer::getStaticClassID()) {
- return new SimpleFactory((Integer*)obj, id, visible);
+ Integer* i;
+ if (U_SUCCESS(status) && obj && (i = dynamic_cast<Integer*>(obj)) != NULL) {
+ return new SimpleFactory(i, id, visible);
}
return NULL;
}
if (obj == NULL) {
result.append("NULL");
} else {
- UClassID id = obj->getDynamicClassID();
- if (id == UnicodeString::getStaticClassID()) {
- result.append(*(UnicodeString*)obj);
- } else if (id == Locale::getStaticClassID()) {
- result.append(((Locale*)obj)->getName());
- } else if (id == Integer::getStaticClassID()) {
- sprintf(buffer, "%d", (int)((Integer*)obj)->_val);
+ const UnicodeString* s;
+ const Locale* loc;
+ const Integer* i;
+ if ((s = dynamic_cast<const UnicodeString*>(obj)) != NULL) {
+ result.append(*s);
+ } else if ((loc = dynamic_cast<const Locale*>(obj)) != NULL) {
+ result.append(loc->getName());
+ } else if ((i = dynamic_cast<const Integer*>(obj)) != NULL) {
+ sprintf(buffer, "%d", (int)i->_val);
result.append(buffer);
} else {
sprintf(buffer, "%p", (const void*)obj);
if (equ) {
logln(temp);
} else {
- errln(temp);
+ dataerrln(temp);
}
}
// should not be able to locate invisible services
{
UErrorCode status = U_ZERO_ERROR;
- UVector ids(uhash_deleteUnicodeString, uhash_compareUnicodeString, status);
+ UVector ids(uprv_deleteUObject, uhash_compareUnicodeString, status);
service.getVisibleIDs(ids, status);
UnicodeString target = "en_US_BAR";
confirmBoolean("18) find invisible", !ids.contains(&target));
/*
******************************************************************
*/
+class TestStringSimpleKeyService : public ICUService {
+public:
+
+ virtual ICUServiceFactory* createSimpleFactory(UObject* obj, const UnicodeString& id, UBool visible, UErrorCode& status)
+ {
+ // We could put this type check into ICUService itself, but we'd still
+ // have to implement cloneInstance. Otherwise we could just tell the service
+ // what the object type is when we create it, and the default implementation
+ // could handle everything for us. Phooey.
+ if (obj && dynamic_cast<UnicodeString*>(obj) != NULL) {
+ return ICUService::createSimpleFactory(obj, id, visible, status);
+ }
+ return NULL;
+ }
+
+ virtual UObject* cloneInstance(UObject* instance) const {
+ return instance ? new UnicodeString(*(UnicodeString*)instance) : NULL;
+ }
+};
class TestStringService : public ICUService {
public:
virtual ICUServiceFactory* createSimpleFactory(UObject* obj, const UnicodeString& id, UBool visible, UErrorCode& /* status */)
{
- if (obj && obj->getDynamicClassID() == UnicodeString::getStaticClassID()) {
- return new SimpleFactory((UnicodeString*)obj, id, visible);
+ UnicodeString* s;
+ if (obj && (s = dynamic_cast<UnicodeString*>(obj)) != NULL) {
+ return new SimpleFactory(s, id, visible);
}
return NULL;
}
return result;
}
- virtual UClassID getDynamicClassID() const {
- return getStaticClassID();
- }
-
static UClassID getStaticClassID() {
return (UClassID)&fgClassID;
}
+ virtual UClassID getDynamicClassID() const {
+ return getStaticClassID();
+ }
+
private:
static const char fgClassID;
};
public:
TestMultipleKeyStringFactory(const UnicodeString ids[], int32_t count, const UnicodeString& factoryID)
: _status(U_ZERO_ERROR)
- , _ids(uhash_deleteUnicodeString, uhash_compareUnicodeString, count, _status)
+ , _ids(uprv_deleteUObject, uhash_compareUnicodeString, count, _status)
, _factoryID(factoryID + ": ")
{
for (int i = 0; i < count; ++i) {
return result;
}
- virtual UClassID getDynamicClassID() const {
- return getStaticClassID();
- }
-
static UClassID getStaticClassID() {
return (UClassID)&fgClassID;
}
+ virtual UClassID getDynamicClassID() const {
+ return getStaticClassID();
+ }
+
private:
static const char fgClassID;
};
// iterate over the visual ids returned by the multiple factory
{
UErrorCode status = U_ZERO_ERROR;
- UVector ids(uhash_deleteUnicodeString, uhash_compareUnicodeString, 0, status);
+ UVector ids(uprv_deleteUObject, uhash_compareUnicodeString, 0, status);
service.getVisibleIDs(ids, status);
for (int i = 0; i < ids.size(); ++i) {
const UnicodeString* id = (const UnicodeString*)ids[i];
// iterate over the display names
{
UErrorCode status = U_ZERO_ERROR;
- UVector names(userv_deleteStringPair, NULL, status);
- service.getDisplayNames(names, Locale::getGerman(), status);
+ UVector names(status);
+ service.getDisplayNames(names, status);
for (int i = 0; i < names.size(); ++i) {
const StringPair* pair = (const StringPair*)names[i];
logln(" " + pair->displayName + " --> " + pair->id);
// Rad dude's surfer gal 'replaces' Later's surfer gal
{
UErrorCode status = U_ZERO_ERROR;
- UVector names(userv_deleteStringPair, NULL, status);
+ UVector names(status);
service.getDisplayNames(names, Locale("es"), status);
for (int i = 0; i < names.size(); ++i) {
const StringPair* pair = (const StringPair*)names[i];
logln(" " + pair->displayName + " --> " + pair->id);
}
- confirmIdentical("26) display names", names.size(), 7);
+ confirmIdentical("29) display names", names.size(), 7);
}
// we should get the display name corresponding to the actual id
{
UErrorCode status = U_ZERO_ERROR;
- UVector ids(uhash_deleteUnicodeString, uhash_compareUnicodeString, 0, status);
+ UVector ids(uprv_deleteUObject, uhash_compareUnicodeString, 0, status);
service.getVisibleIDs(ids, status);
for (int i = 0; i < ids.size(); ++i) {
const UnicodeString* id = (const UnicodeString*)ids[i];
// list all of the resources
{
UErrorCode status = U_ZERO_ERROR;
- UVector ids(uhash_deleteUnicodeString, uhash_compareUnicodeString, 0, status);
+ UVector ids(uprv_deleteUObject, uhash_compareUnicodeString, 0, status);
service.getVisibleIDs(ids, status);
logln("all visible ids:");
for (int i = 0; i < ids.size(); ++i) {
// this should be fast since the display names were cached.
{
UErrorCode status = U_ZERO_ERROR;
- UVector names(userv_deleteStringPair, NULL, status);
+ UVector names(status);
service.getDisplayNames(names, Locale::getGermany(), status);
logln("service display names for de_DE");
for (int i = 0; i < names.size(); ++i) {
logln(UnicodeString("\n --- ") + idNames[i] + " ---");
{
UErrorCode status = U_ZERO_ERROR;
- UVector names(userv_deleteStringPair, NULL, status);
+ UVector names(status);
service.getDisplayNames(names, idNames[i], status);
for (int i = 0; i < names.size(); ++i) {
const StringPair* pair = (const StringPair*)names[i];
service.registerInstance(root, "", status);
service.registerInstance(german, "de", status);
service.registerInstance(germany, Locale::getGermany(), status);
- service.registerInstance(japanese, "ja", status);
+ service.registerInstance(japanese, (UnicodeString)"ja", TRUE, status);
service.registerInstance(japan, Locale::getJapan(), status);
{
{
UErrorCode status = U_ZERO_ERROR;
- UVector ids(uhash_deleteUnicodeString, uhash_compareUnicodeString, 0, status);
+ UVector ids(uprv_deleteUObject, uhash_compareUnicodeString, 0, status);
service.getVisibleIDs(ids, status);
logln("all visible ids:");
for (int i = 0; i < ids.size(); ++i) {
Locale::setDefault(loc, status);
{
UErrorCode status = U_ZERO_ERROR;
- UVector ids(uhash_deleteUnicodeString, uhash_compareUnicodeString, 0, status);
+ UVector ids(uprv_deleteUObject, uhash_compareUnicodeString, 0, status);
service.getVisibleIDs(ids, status);
logln("all visible ids:");
for (int i = 0; i < ids.size(); ++i) {
/**
* UObject boilerplate.
*/
- virtual UClassID getDynamicClassID() const {
- return getStaticClassID();
- }
-
static UClassID getStaticClassID() {
return (UClassID)&fgClassID;
}
+ virtual UClassID getDynamicClassID() const {
+ return getStaticClassID();
+ }
+
private:
static const char fgClassID;
static UnicodeString* greetingID;
}
// ICUService
- TestStringService service;
- service.registerFactory(sf, status);
-
- {
- UnicodeString* result = (UnicodeString*)service.get("object", status);
- if (result) {
- logln("object is: " + *result);
- delete result;
- } else {
- errln("could not get object");
- }
- }
+ {
+ TestStringService service;
+ service.registerFactory(sf, status);
+
+ {
+ UnicodeString* result = (UnicodeString*)service.get("object", status);
+ if (result) {
+ logln("object is: " + *result);
+ delete result;
+ } else {
+ errln("could not get object");
+ }
+ }
+ }
+ }
+
+ // ICUServiceKey
+ {
+ UErrorCode status = U_ZERO_ERROR;
+ UnicodeString* howdy = new UnicodeString("Howdy");
+
+ TestStringSimpleKeyService service;
+ service.registerInstance(howdy, "Greetings", status);
+ {
+ UnicodeString* result = (UnicodeString*)service.get("Greetings", status);
+ if (result) {
+ logln("object is: " + *result);
+ delete result;
+ } else {
+ errln("could not get object");
+ }
+ }
+
+ UVector ids(uprv_deleteUObject, uhash_compareUnicodeString, status);
+ // yuck, this is awkward to use. All because we pass null in an overload.
+ // TODO: change this.
+ UnicodeString str("Greet");
+ service.getVisibleIDs(ids, &str, status);
+ confirmIdentical("no fallback of greet", ids.size(), 0);
}
// ICULocaleService
}
delete obj;
delete key;
- }
+ key = LocaleKey::createWithCanonicalFallback(&primary, &fallback, 123, status);
+ if (U_SUCCESS(status)) {
+ UnicodeString str;
+ key->currentDescriptor(str);
+ key->parsePrefix(str);
+ if (str != "123") {
+ errln("did not get expected prefix");
+ }
+ delete key;
+ }
+
+ // coverage, getSupportedIDs is either overridden or the calling method is
+ LKFSubclass0 lkFactory;
+ Hashtable table0;
+ lkFactory.updateVisibleIDs(table0, status);
+ if (table0.count() != 0) {
+ errln("LKF returned non-empty hashtable");
+ }
-#if 0
- // ResourceBundleFactory
- ICUResourceBundleFactory rbf = new ICUResourceBundleFactory();
- logln("RB: " + rbf.create(lkey, null));
- // ICUNotifier
+ // ResourceBundleFactory
+ key = LocaleKey::createWithCanonicalFallback(&primary, &fallback, status);
+ ICUResourceBundleFactory rbf;
+ UObject* icurb = rbf.create(*key, NULL, status);
+ if (icurb != NULL) {
+ logln("got resource bundle for key");
+ delete icurb;
+ }
+ delete key;
+ }
+
+ #if 0
+ // ICUNotifier
ICUNotifier nf = new ICUNSubclass();
try {
nf.addListener(null);