mirror of
https://github.com/unicode-org/icu.git
synced 2025-04-13 17:01:16 +00:00
ICU-1079 add ID and context param to factory
X-SVN-Rev: 6276
This commit is contained in:
parent
e29f86269e
commit
85276d3bcc
6 changed files with 81 additions and 65 deletions
|
@ -12,55 +12,26 @@
|
|||
|
||||
U_NAMESPACE_BEGIN
|
||||
|
||||
|
||||
/**
|
||||
* System registration hook.
|
||||
*/
|
||||
void NormalizationTransliterator::registerIDs() {
|
||||
Transliterator::_registerFactory(UnicodeString("Any-NFC", ""), _createNFC);
|
||||
Transliterator::_registerFactory(UnicodeString("Any-NFKC", ""), _createNFKC);
|
||||
Transliterator::_registerFactory(UnicodeString("Any-NFD", ""), _createNFD);
|
||||
Transliterator::_registerFactory(UnicodeString("Any-NFKD", ""), _createNFKD);
|
||||
Transliterator::_registerFactory(UnicodeString("Any-NFC", ""),
|
||||
_create, integerToken(UNORM_NFC));
|
||||
Transliterator::_registerFactory(UnicodeString("Any-NFKC", ""),
|
||||
_create, integerToken(UNORM_NFKC));
|
||||
Transliterator::_registerFactory(UnicodeString("Any-NFD", ""),
|
||||
_create, integerToken(UNORM_NFD));
|
||||
Transliterator::_registerFactory(UnicodeString("Any-NFKD", ""),
|
||||
_create, integerToken(UNORM_NFKD));
|
||||
}
|
||||
|
||||
/**
|
||||
* Factory methods
|
||||
*/
|
||||
Transliterator* NormalizationTransliterator::_createNFC() {
|
||||
return new NormalizationTransliterator(UNICODE_STRING("NFC", 3),
|
||||
UNORM_NFC, 0);
|
||||
}
|
||||
Transliterator* NormalizationTransliterator::_createNFKC() {
|
||||
return new NormalizationTransliterator(UNICODE_STRING("NFKC", 4),
|
||||
UNORM_NFKC, 0);
|
||||
}
|
||||
Transliterator* NormalizationTransliterator::_createNFD() {
|
||||
return new NormalizationTransliterator(UNICODE_STRING("NFD", 3),
|
||||
UNORM_NFD, 0);
|
||||
}
|
||||
Transliterator* NormalizationTransliterator::_createNFKD() {
|
||||
return new NormalizationTransliterator(UNICODE_STRING("NFKD", 4),
|
||||
UNORM_NFKD, 0);
|
||||
}
|
||||
|
||||
/**
|
||||
* Factory method.
|
||||
*/
|
||||
NormalizationTransliterator*
|
||||
NormalizationTransliterator::createInstance(UNormalizationMode mode,
|
||||
int32_t opt) {
|
||||
switch(mode) {
|
||||
case UNORM_NFC:
|
||||
return (NormalizationTransliterator *)_createNFC();
|
||||
case UNORM_NFKC:
|
||||
return (NormalizationTransliterator *)_createNFKC();
|
||||
case UNORM_NFD:
|
||||
return (NormalizationTransliterator *)_createNFD();
|
||||
case UNORM_NFKD:
|
||||
return (NormalizationTransliterator *)_createNFKD();
|
||||
default:
|
||||
return 0;
|
||||
}
|
||||
Transliterator* NormalizationTransliterator::_create(const UnicodeString& ID,
|
||||
Token context) {
|
||||
return new NormalizationTransliterator(ID, (UNormalizationMode) context.integer, 0);
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
@ -1258,19 +1258,21 @@ void Transliterator::skipSpaces(const UnicodeString& str,
|
|||
|
||||
// For public consumption
|
||||
void Transliterator::registerFactory(const UnicodeString& id,
|
||||
Transliterator::Factory factory) {
|
||||
Transliterator::Factory factory,
|
||||
Transliterator::Token context) {
|
||||
if (registry == 0) {
|
||||
initializeRegistry();
|
||||
}
|
||||
Mutex lock(®istryMutex);
|
||||
_registerFactory(id, factory);
|
||||
_registerFactory(id, factory, context);
|
||||
}
|
||||
|
||||
// To be called only by Transliterator subclasses that are called
|
||||
// to register themselves by initializeRegistry().
|
||||
void Transliterator::_registerFactory(const UnicodeString& id,
|
||||
Transliterator::Factory factory) {
|
||||
registry->put(id, factory, TRUE);
|
||||
Transliterator::Factory factory,
|
||||
Transliterator::Token context) {
|
||||
registry->put(id, factory, context, TRUE);
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
@ -285,12 +285,16 @@ public:
|
|||
union {
|
||||
Transliterator* prototype; // For PROTOTYPE
|
||||
TransliterationRuleData* data; // For RBT_DATA, COMPOUND_RBT
|
||||
Transliterator::Factory factory; // For FACTORY
|
||||
struct {
|
||||
Transliterator::Factory function;
|
||||
Transliterator::Token context;
|
||||
} factory; // For FACTORY
|
||||
} u;
|
||||
Entry();
|
||||
~Entry();
|
||||
void adoptPrototype(Transliterator* adopted);
|
||||
void setFactory(Transliterator::Factory factory);
|
||||
void setFactory(Transliterator::Factory factory,
|
||||
Transliterator::Token context);
|
||||
};
|
||||
|
||||
Entry::Entry() {
|
||||
|
@ -320,12 +324,14 @@ void Entry::adoptPrototype(Transliterator* adopted) {
|
|||
u.prototype = adopted;
|
||||
}
|
||||
|
||||
void Entry::setFactory(Transliterator::Factory factory) {
|
||||
void Entry::setFactory(Transliterator::Factory factory,
|
||||
Transliterator::Token context) {
|
||||
if (entryType == PROTOTYPE) {
|
||||
delete u.prototype;
|
||||
}
|
||||
entryType = FACTORY;
|
||||
u.factory = factory;
|
||||
u.factory.function = factory;
|
||||
u.factory.context = context;
|
||||
}
|
||||
|
||||
// UObjectDeleter for Hashtable::setValueDeleter
|
||||
|
@ -373,9 +379,10 @@ void TransliteratorRegistry::put(Transliterator* adoptedProto,
|
|||
|
||||
void TransliteratorRegistry::put(const UnicodeString& ID,
|
||||
Transliterator::Factory factory,
|
||||
Transliterator::Token context,
|
||||
UBool visible) {
|
||||
Entry *entry = new Entry();
|
||||
entry->setFactory(factory);
|
||||
entry->setFactory(factory, context);
|
||||
registerEntry(ID, entry, visible);
|
||||
}
|
||||
|
||||
|
@ -909,7 +916,7 @@ Transliterator* TransliteratorRegistry::instantiateEntry(const UnicodeString& ID
|
|||
aliasReturn = new TransliteratorAlias(entry->stringArg);
|
||||
return 0;
|
||||
} else if (entry->entryType == Entry::FACTORY) {
|
||||
return entry->u.factory();
|
||||
return entry->u.factory.function(ID, entry->u.factory.context);
|
||||
} else if (entry->entryType == Entry::COMPOUND_RBT) {
|
||||
UnicodeString id("_", "");
|
||||
Transliterator *t = new RuleBasedTransliterator(id, entry->u.data);
|
||||
|
|
|
@ -138,6 +138,7 @@ class TransliteratorRegistry {
|
|||
*/
|
||||
void put(const UnicodeString& ID,
|
||||
Transliterator::Factory factory,
|
||||
Transliterator::Token context,
|
||||
UBool visible);
|
||||
|
||||
/**
|
||||
|
|
|
@ -19,7 +19,7 @@ U_NAMESPACE_BEGIN
|
|||
/**
|
||||
* A transliterator that performs normalization.
|
||||
* @author Alan Liu
|
||||
* @version $RCSfile: nortrans.h,v $ $Revision: 1.4 $ $Date: 2001/10/08 23:25:22 $
|
||||
* @version $RCSfile: nortrans.h,v $ $Revision: 1.5 $ $Date: 2001/10/17 17:29:33 $
|
||||
*/
|
||||
class U_I18N_API NormalizationTransliterator : public Transliterator {
|
||||
|
||||
|
@ -35,12 +35,6 @@ class U_I18N_API NormalizationTransliterator : public Transliterator {
|
|||
|
||||
public:
|
||||
|
||||
/**
|
||||
* Factory method.
|
||||
*/
|
||||
static NormalizationTransliterator* createInstance(UNormalizationMode mode,
|
||||
int32_t opt=0);
|
||||
|
||||
/**
|
||||
* Destructor.
|
||||
*/
|
||||
|
@ -78,10 +72,8 @@ class U_I18N_API NormalizationTransliterator : public Transliterator {
|
|||
private:
|
||||
|
||||
// Transliterator::Factory methods
|
||||
static Transliterator* _createNFC();
|
||||
static Transliterator* _createNFKC();
|
||||
static Transliterator* _createNFD();
|
||||
static Transliterator* _createNFKD();
|
||||
static Transliterator* _create(const UnicodeString& ID,
|
||||
Token context);
|
||||
|
||||
/**
|
||||
* Constructs a transliterator. This method is private.
|
||||
|
|
|
@ -248,9 +248,35 @@ private:
|
|||
public:
|
||||
|
||||
/**
|
||||
* A function that creates and returns a Transliterator.
|
||||
* A context integer or pointer for a factory function, passed by
|
||||
* value.
|
||||
*/
|
||||
typedef Transliterator* (*Factory)(void);
|
||||
union Token {
|
||||
int32_t integer;
|
||||
void* pointer;
|
||||
};
|
||||
|
||||
/**
|
||||
* Return a token containing an integer.
|
||||
*/
|
||||
inline static Token integerToken(int32_t);
|
||||
|
||||
/**
|
||||
* Return a token containing a pointer.
|
||||
*/
|
||||
inline static Token pointerToken(void*);
|
||||
|
||||
/**
|
||||
* A function that creates and returns a Transliterator. When
|
||||
* invoked, it will be passed the ID string that is being
|
||||
* instantiated, together with the context pointer that was passed
|
||||
* in when the factory function was first registered. Many
|
||||
* factory functions will ignore both parameters, however,
|
||||
* functions that are registered to more than one ID may use the
|
||||
* ID or the context parameter to parameterize the transliterator
|
||||
* they create.
|
||||
*/
|
||||
typedef Transliterator* (*Factory)(const UnicodeString& ID, Token context);
|
||||
|
||||
protected:
|
||||
|
||||
|
@ -741,9 +767,13 @@ public:
|
|||
* @param id the ID being registered
|
||||
* @param factory a function pointer that will be copied and
|
||||
* called later when the given ID is passed to createInstance()
|
||||
* @param context a context pointer that will be stored and
|
||||
* later passed to the factory function when an ID matching
|
||||
* the registration ID is being instantiated with this factory.
|
||||
*/
|
||||
static void registerFactory(const UnicodeString& id,
|
||||
Factory factory);
|
||||
Factory factory,
|
||||
Token context);
|
||||
|
||||
/**
|
||||
* Registers a instance <tt>obj</tt> of a subclass of
|
||||
|
@ -769,7 +799,8 @@ private:
|
|||
friend class NormalizationTransliterator;
|
||||
|
||||
static void _registerFactory(const UnicodeString& id,
|
||||
Factory factory);
|
||||
Factory factory,
|
||||
Token context);
|
||||
|
||||
public:
|
||||
|
||||
|
@ -988,6 +1019,18 @@ inline void Transliterator::setID(const UnicodeString& id) {
|
|||
ID = id;
|
||||
}
|
||||
|
||||
inline Transliterator::Token Transliterator::integerToken(int32_t i) {
|
||||
Token t;
|
||||
t.integer = i;
|
||||
return t;
|
||||
}
|
||||
|
||||
inline Transliterator::Token Transliterator::pointerToken(void* p) {
|
||||
Token t;
|
||||
t.pointer = p;
|
||||
return t;
|
||||
}
|
||||
|
||||
/**
|
||||
* Definitions for deprecated API
|
||||
* @deprecated Remove after Aug 2002
|
||||
|
|
Loading…
Add table
Reference in a new issue