| 1 |
// © 2016 and later: Unicode, Inc. and others. |
| 2 |
// License & terms of use: http://www.unicode.org/copyright.html |
| 3 |
/* |
| 4 |
********************************************************************** |
| 5 |
* Copyright (c) 2004-2014, International Business Machines |
| 6 |
* Corporation and others. All Rights Reserved. |
| 7 |
********************************************************************** |
| 8 |
* Author: Alan Liu |
| 9 |
* Created: January 16 2004 |
| 10 |
* Since: ICU 2.8 |
| 11 |
********************************************************************** |
| 12 |
*/ |
| 13 |
#ifndef LOCBASED_H |
| 14 |
#define LOCBASED_H |
| 15 |
|
| 16 |
#include "unicode/locid.h" |
| 17 |
#include "unicode/uobject.h" |
| 18 |
|
| 19 |
/** |
| 20 |
* Macro to declare a locale LocaleBased wrapper object for the given |
| 21 |
* object, which must have two members named `validLocale' and |
| 22 |
* `actualLocale' of which are pointers to the internal icu::CharString. |
| 23 |
*/ |
| 24 |
#define U_LOCALE_BASED(varname, objname) \ |
| 25 |
LocaleBased varname((objname).validLocale, (objname).actualLocale) |
| 26 |
|
| 27 |
U_NAMESPACE_BEGIN |
| 28 |
|
| 29 |
class CharString; |
| 30 |
/** |
| 31 |
* A utility class that unifies the implementation of getLocale() by |
| 32 |
* various ICU services. This class is likely to be removed in the |
| 33 |
* ICU 3.0 time frame in favor of an integrated approach with the |
| 34 |
* services framework. |
| 35 |
* @since ICU 2.8 |
| 36 |
*/ |
| 37 |
class U_COMMON_API LocaleBased : public UMemory { |
| 38 |
|
| 39 |
public: |
| 40 |
|
| 41 |
/** |
| 42 |
* Construct a LocaleBased wrapper around the two pointers. These |
| 43 |
* will be aliased for the lifetime of this object. |
| 44 |
*/ |
| 45 |
inline LocaleBased(CharString*& validAlias, CharString*& actualAlias); |
| 46 |
|
| 47 |
/** |
| 48 |
* Return locale meta-data for the service object wrapped by this |
| 49 |
* object. Either the valid or the actual locale may be |
| 50 |
* retrieved. |
| 51 |
* @param valid The valid locale. |
| 52 |
* @param actual The actual locale. |
| 53 |
* @param type either ULOC_VALID_LOCALE or ULOC_ACTUAL_LOCALE |
| 54 |
* @param status input-output error code |
| 55 |
* @return the indicated locale |
| 56 |
*/ |
| 57 |
static Locale getLocale( |
| 58 |
const CharString* valid, const CharString* actual, |
| 59 |
ULocDataLocaleType type, UErrorCode& status); |
| 60 |
|
| 61 |
/** |
| 62 |
* Return the locale ID for the service object wrapped by this |
| 63 |
* object. Either the valid or the actual locale may be |
| 64 |
* retrieved. |
| 65 |
* @param valid The valid locale. |
| 66 |
* @param actual The actual locale. |
| 67 |
* @param type either ULOC_VALID_LOCALE or ULOC_ACTUAL_LOCALE |
| 68 |
* @param status input-output error code |
| 69 |
* @return the indicated locale ID |
| 70 |
*/ |
| 71 |
static const char* getLocaleID( |
| 72 |
const CharString* valid, const CharString* actual, |
| 73 |
ULocDataLocaleType type, UErrorCode& status); |
| 74 |
|
| 75 |
/** |
| 76 |
* Set the locale meta-data for the service object wrapped by this |
| 77 |
* object. If either parameter is zero, it is ignored. |
| 78 |
* @param valid the ID of the valid locale |
| 79 |
* @param actual the ID of the actual locale |
| 80 |
*/ |
| 81 |
void setLocaleIDs(const char* valid, const char* actual, UErrorCode& status); |
| 82 |
void setLocaleIDs(const CharString* valid, const CharString* actual, UErrorCode& status); |
| 83 |
|
| 84 |
static void setLocaleID(const char* id, CharString*& dest, UErrorCode& status); |
| 85 |
static void setLocaleID(const CharString* id, CharString*& dest, UErrorCode& status); |
| 86 |
|
| 87 |
static bool equalIDs(const CharString* left, const CharString* right); |
| 88 |
|
| 89 |
private: |
| 90 |
|
| 91 |
void setValidLocaleID(const CharString* id, UErrorCode& status); |
| 92 |
void setActualLocaleID(const CharString* id, UErrorCode& status); |
| 93 |
void setValidLocaleID(const char* id, UErrorCode& status); |
| 94 |
void setActualLocaleID(const char* id, UErrorCode& status); |
| 95 |
|
| 96 |
CharString*& valid; |
| 97 |
CharString*& actual; |
| 98 |
}; |
| 99 |
|
| 100 |
inline LocaleBased::LocaleBased(CharString*& validAlias, CharString*& actualAlias) : |
| 101 |
valid(validAlias), actual(actualAlias) { |
| 102 |
} |
| 103 |
|
| 104 |
inline void LocaleBased::setValidLocaleID(const CharString* id, UErrorCode& status) { |
| 105 |
setLocaleID(id, valid, status); |
| 106 |
} |
| 107 |
inline void LocaleBased::setActualLocaleID(const CharString* id, UErrorCode& status) { |
| 108 |
setLocaleID(id, actual, status); |
| 109 |
} |
| 110 |
inline void LocaleBased::setValidLocaleID(const char* id, UErrorCode& status) { |
| 111 |
setLocaleID(id, valid, status); |
| 112 |
} |
| 113 |
inline void LocaleBased::setActualLocaleID(const char* id, UErrorCode& status) { |
| 114 |
setLocaleID(id, actual, status); |
| 115 |
} |
| 116 |
|
| 117 |
U_NAMESPACE_END |
| 118 |
|
| 119 |
#endif |