summaryrefslogtreecommitdiff
path: root/deps/icu-small/source/i18n/transreg.h
blob: 041244e1b02d774d0ec81381d5b829f133fba64f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
// © 2016 and later: Unicode, Inc. and others.
// License & terms of use: http://www.unicode.org/copyright.html
/*
**********************************************************************
*   Copyright (c) 2001-2014, International Business Machines
*   Corporation and others.  All Rights Reserved.
**********************************************************************
*   Date        Name        Description
*   08/10/2001  aliu        Creation.
**********************************************************************
*/
#ifndef _TRANSREG_H
#define _TRANSREG_H

#include "unicode/utypes.h"

#if !UCONFIG_NO_TRANSLITERATION

#include "unicode/uobject.h"
#include "unicode/translit.h"
#include "hash.h"
#include "uvector.h"

U_NAMESPACE_BEGIN

class TransliteratorEntry;
class TransliteratorSpec;
class UnicodeString;

//------------------------------------------------------------------
// TransliteratorAlias
//------------------------------------------------------------------

/**
 * A TransliteratorAlias object is returned by get() if the given ID
 * actually translates into something else.  The caller then invokes
 * the create() method on the alias to create the actual
 * transliterator, and deletes the alias.
 *
 * Why all the shenanigans?  To prevent circular calls between
 * the registry code and the transliterator code that deadlocks.
 */
class TransliteratorAlias : public UMemory {
 public:
    /**
     * Construct a simple alias (type == SIMPLE)
     * @param aliasID the given id.
     */
    TransliteratorAlias(const UnicodeString& aliasID, const UnicodeSet* compoundFilter);

    /**
     * Construct a compound RBT alias (type == COMPOUND)
     */
    TransliteratorAlias(const UnicodeString& ID, const UnicodeString& idBlocks,
                        UVector* adoptedTransliterators,
                        const UnicodeSet* compoundFilter);

    /**
     * Construct a rules alias (type = RULES)
     */
    TransliteratorAlias(const UnicodeString& theID,
                        const UnicodeString& rules,
                        UTransDirection dir);

    ~TransliteratorAlias();

    /**
     * The whole point of create() is that the caller must invoke
     * it when the registry mutex is NOT held, to prevent deadlock.
     * It may only be called once.
     *
     * Note: Only call create() if isRuleBased() returns FALSE.
     *
     * This method must be called *outside* of the TransliteratorRegistry
     * mutex.
     */
    Transliterator* create(UParseError&, UErrorCode&);

    /**
     * Return TRUE if this alias is rule-based.  If so, the caller
     * must call parse() on it, then call TransliteratorRegistry::reget().
     */
    UBool isRuleBased() const;

    /**
     * If isRuleBased() returns TRUE, then the caller must call this
     * method, followed by TransliteratorRegistry::reget().  The latter
     * method must be called inside the TransliteratorRegistry mutex.
     *
     * Note: Only call parse() if isRuleBased() returns TRUE.
     *
     * This method must be called *outside* of the TransliteratorRegistry
     * mutex, because it can instantiate Transliterators embedded in
     * the rules via the "&Latin-Arabic()" syntax.
     */
    void parse(TransliteratorParser& parser,
               UParseError& pe, UErrorCode& ec) const;

 private:
    // We actually come in three flavors:
    // 1. Simple alias
    //    Here aliasID is the alias string.  Everything else is
    //    null, zero, empty.
    // 2. CompoundRBT
    //    Here ID is the ID, aliasID is the idBlock, trans is the
    //    contained RBT, and idSplitPoint is the offet in aliasID
    //    where the contained RBT goes.  compoundFilter is the
    //    compound filter, and it is _not_ owned.
    // 3. Rules
    //    Here ID is the ID, aliasID is the rules string.
    //    idSplitPoint is the UTransDirection.
    UnicodeString ID;
    UnicodeString aliasesOrRules;
    UVector* transes; // owned
    const UnicodeSet* compoundFilter; // alias
    UTransDirection direction;
    enum { SIMPLE, COMPOUND, RULES } type;

    TransliteratorAlias(const TransliteratorAlias &other); // forbid copying of this class
    TransliteratorAlias &operator=(const TransliteratorAlias &other); // forbid copying of this class
};


/**
 * A registry of system transliterators.  This is the data structure
 * that implements the mapping between transliterator IDs and the data
 * or function pointers used to create the corresponding
 * transliterators.  There is one instance of the registry that is
 * created statically.
 *
 * The registry consists of a dynamic component -- a hashtable -- and
 * a static component -- locale resource bundles.  The dynamic store
 * is semantically overlaid on the static store, so the static mapping
 * can be dynamically overridden.
 *
 * This is an internal class that is only used by Transliterator.
 * Transliterator maintains one static instance of this class and
 * delegates all registry-related operations to it.
 *
 * @author Alan Liu
 */
class TransliteratorRegistry : public UMemory {

 public:

    /**
     * Contructor
     * @param status Output param set to success/failure code.
     */
    TransliteratorRegistry(UErrorCode& status);

    /**
     * Nonvirtual destructor -- this class is not subclassable.
     */
    ~TransliteratorRegistry();

    //------------------------------------------------------------------
    // Basic public API
    //------------------------------------------------------------------

    /**
     * Given a simple ID (forward direction, no inline filter, not
     * compound) attempt to instantiate it from the registry.  Return
     * 0 on failure.
     *
     * Return a non-NULL aliasReturn value if the ID points to an alias.
     * We cannot instantiate it ourselves because the alias may contain
     * filters or compounds, which we do not understand.  Caller should
     * make aliasReturn NULL before calling.
     * @param ID          the given ID
     * @param aliasReturn output param to receive TransliteratorAlias;
     *                    should be NULL on entry
     * @param parseError  Struct to recieve information on position
     *                    of error if an error is encountered
     * @param status      Output param set to success/failure code.
     */
    Transliterator* get(const UnicodeString& ID,
                        TransliteratorAlias*& aliasReturn,
                        UErrorCode& status);

    /**
     * The caller must call this after calling get(), if [a] calling get()
     * returns an alias, and [b] the alias is rule based.  In that
     * situation the caller must call alias->parse() to do the parsing
     * OUTSIDE THE REGISTRY MUTEX, then call this method to retry
     * instantiating the transliterator.
     *
     * Note: Another alias might be returned by this method.
     *
     * This method (like all public methods of this class) must be called
     * from within the TransliteratorRegistry mutex.
     *
     * @param aliasReturn output param to receive TransliteratorAlias;
     *                    should be NULL on entry
     */
    Transliterator* reget(const UnicodeString& ID,
                          TransliteratorParser& parser,
                          TransliteratorAlias*& aliasReturn,
                          UErrorCode& status);

    /**
     * Register a prototype (adopted).  This adds an entry to the
     * dynamic store, or replaces an existing entry.  Any entry in the
     * underlying static locale resource store is masked.
     */
    void put(Transliterator* adoptedProto,
             UBool visible,
             UErrorCode& ec);

    /**
     * Register an ID and a factory function pointer.  This adds an
     * entry to the dynamic store, or replaces an existing entry.  Any
     * entry in the underlying static locale resource store is masked.
     */
    void put(const UnicodeString& ID,
             Transliterator::Factory factory,
             Transliterator::Token context,
             UBool visible,
             UErrorCode& ec);

    /**
     * Register an ID and a resource name.  This adds an entry to the
     * dynamic store, or replaces an existing entry.  Any entry in the
     * underlying static locale resource store is masked.
     */
    void put(const UnicodeString& ID,
             const UnicodeString& resourceName,
             UTransDirection dir,
             UBool readonlyResourceAlias,
             UBool visible,
             UErrorCode& ec);

    /**
     * Register an ID and an alias ID.  This adds an entry to the
     * dynamic store, or replaces an existing entry.  Any entry in the
     * underlying static locale resource store is masked.
     */
    void put(const UnicodeString& ID,
             const UnicodeString& alias,
             UBool readonlyAliasAlias,
             UBool visible,
             UErrorCode& ec);

    /**
     * Unregister an ID.  This removes an entry from the dynamic store
     * if there is one.  The static locale resource store is
     * unaffected.
     * @param ID    the given ID.
     */
    void remove(const UnicodeString& ID);

    //------------------------------------------------------------------
    // Public ID and spec management
    //------------------------------------------------------------------

    /**
     * Return a StringEnumeration over the IDs currently registered
     * with the system.
     * @internal
     */
    StringEnumeration* getAvailableIDs() const;

    /**
     * == OBSOLETE - remove in ICU 3.4 ==
     * Return the number of IDs currently registered with the system.
     * To retrieve the actual IDs, call getAvailableID(i) with
     * i from 0 to countAvailableIDs() - 1.
     * @return the number of IDs currently registered with the system.
     * @internal
     */
    int32_t countAvailableIDs(void) const;

    /**
     * == OBSOLETE - remove in ICU 3.4 ==
     * Return the index-th available ID.  index must be between 0
     * and countAvailableIDs() - 1, inclusive.  If index is out of
     * range, the result of getAvailableID(0) is returned.
     * @param index the given index.
     * @return the index-th available ID.  index must be between 0
     *         and countAvailableIDs() - 1, inclusive.  If index is out of
     *         range, the result of getAvailableID(0) is returned.
     * @internal
     */
    const UnicodeString& getAvailableID(int32_t index) const;

    /**
     * Return the number of registered source specifiers.
     * @return the number of registered source specifiers.
     */
    int32_t countAvailableSources(void) const;

    /**
     * Return a registered source specifier.
     * @param index which specifier to return, from 0 to n-1, where
     * n = countAvailableSources()
     * @param result fill-in paramter to receive the source specifier.
     * If index is out of range, result will be empty.
     * @return reference to result
     */
    UnicodeString& getAvailableSource(int32_t index,
                                      UnicodeString& result) const;

    /**
     * Return the number of registered target specifiers for a given
     * source specifier.
     * @param source the given source specifier.
     * @return the number of registered target specifiers for a given
     *         source specifier.
     */
    int32_t countAvailableTargets(const UnicodeString& source) const;

    /**
     * Return a registered target specifier for a given source.
     * @param index which specifier to return, from 0 to n-1, where
     * n = countAvailableTargets(source)
     * @param source the source specifier
     * @param result fill-in paramter to receive the target specifier.
     * If source is invalid or if index is out of range, result will
     * be empty.
     * @return reference to result
     */
    UnicodeString& getAvailableTarget(int32_t index,
                                      const UnicodeString& source,
                                      UnicodeString& result) const;

    /**
     * Return the number of registered variant specifiers for a given
     * source-target pair.  There is always at least one variant: If
     * just source-target is registered, then the single variant
     * NO_VARIANT is returned.  If source-target/variant is registered
     * then that variant is returned.
     * @param source the source specifiers
     * @param target the target specifiers
     * @return the number of registered variant specifiers for a given
     *         source-target pair.
     */
    int32_t countAvailableVariants(const UnicodeString& source,
                                   const UnicodeString& target) const;

    /**
     * Return a registered variant specifier for a given source-target
     * pair.  If NO_VARIANT is one of the variants, then it will be
     * at index 0.
     * @param index which specifier to return, from 0 to n-1, where
     * n = countAvailableVariants(source, target)
     * @param source the source specifier
     * @param target the target specifier
     * @param result fill-in paramter to receive the variant
     * specifier.  If source is invalid or if target is invalid or if
     * index is out of range, result will be empty.
     * @return reference to result
     */
    UnicodeString& getAvailableVariant(int32_t index,
                                       const UnicodeString& source,
                                       const UnicodeString& target,
                                       UnicodeString& result) const;

 private:

    //----------------------------------------------------------------
    // Private implementation
    //----------------------------------------------------------------

    TransliteratorEntry* find(const UnicodeString& ID);

    TransliteratorEntry* find(UnicodeString& source,
                UnicodeString& target,
                UnicodeString& variant);

    TransliteratorEntry* findInDynamicStore(const TransliteratorSpec& src,
                              const TransliteratorSpec& trg,
                              const UnicodeString& variant) const;

    TransliteratorEntry* findInStaticStore(const TransliteratorSpec& src,
                             const TransliteratorSpec& trg,
                             const UnicodeString& variant);

    static TransliteratorEntry* findInBundle(const TransliteratorSpec& specToOpen,
                               const TransliteratorSpec& specToFind,
                               const UnicodeString& variant,
                               UTransDirection direction);

    void registerEntry(const UnicodeString& source,
                       const UnicodeString& target,
                       const UnicodeString& variant,
                       TransliteratorEntry* adopted,
                       UBool visible);

    void registerEntry(const UnicodeString& ID,
                       TransliteratorEntry* adopted,
                       UBool visible);

    void registerEntry(const UnicodeString& ID,
                       const UnicodeString& source,
                       const UnicodeString& target,
                       const UnicodeString& variant,
                       TransliteratorEntry* adopted,
                       UBool visible);

    void registerSTV(const UnicodeString& source,
                     const UnicodeString& target,
                     const UnicodeString& variant);

    void removeSTV(const UnicodeString& source,
                   const UnicodeString& target,
                   const UnicodeString& variant);

    Transliterator* instantiateEntry(const UnicodeString& ID,
                                     TransliteratorEntry *entry,
                                     TransliteratorAlias*& aliasReturn,
                                     UErrorCode& status);

    /**
     * A StringEnumeration over the registered IDs in this object.
     */
    class Enumeration : public StringEnumeration {
    public:
        Enumeration(const TransliteratorRegistry& reg);
        virtual ~Enumeration();
        virtual int32_t count(UErrorCode& status) const;
        virtual const UnicodeString* snext(UErrorCode& status);
        virtual void reset(UErrorCode& status);
        static UClassID U_EXPORT2 getStaticClassID();
        virtual UClassID getDynamicClassID() const;
    private:
        int32_t index;
        const TransliteratorRegistry& reg;
    };
    friend class Enumeration;

 private:

    /**
     * Dynamic registry mapping full IDs to Entry objects.  This
     * contains both public and internal entities.  The visibility is
     * controlled by whether an entry is listed in availableIDs and
     * specDAG or not.
     */
    Hashtable registry;

    /**
     * DAG of visible IDs by spec.  Hashtable: source => (Hashtable:
     * target => variant bitmask)
     */
    Hashtable specDAG;

    /**
     * Vector of all variant names
     */
    UVector variantList;

    /**
     * Vector of public full IDs.
     */
    UVector availableIDs;

    TransliteratorRegistry(const TransliteratorRegistry &other); // forbid copying of this class
    TransliteratorRegistry &operator=(const TransliteratorRegistry &other); // forbid copying of this class
};

U_NAMESPACE_END

U_CFUNC UBool utrans_transliterator_cleanup(void);

#endif /* #if !UCONFIG_NO_TRANSLITERATION */

#endif
//eof