Victor Chang | 7322950 | 2020-09-17 13:39:19 +0100 | [diff] [blame] | 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 size ULOC_FULLNAME_CAPACITY |
| 23 | */ |
| 24 | #define U_LOCALE_BASED(varname, objname) \ |
| 25 | LocaleBased varname((objname).validLocale, (objname).actualLocale) |
| 26 | |
| 27 | U_NAMESPACE_BEGIN |
| 28 | |
| 29 | /** |
| 30 | * A utility class that unifies the implementation of getLocale() by |
| 31 | * various ICU services. This class is likely to be removed in the |
| 32 | * ICU 3.0 time frame in favor of an integrated approach with the |
| 33 | * services framework. |
| 34 | * @since ICU 2.8 |
| 35 | */ |
| 36 | class U_COMMON_API LocaleBased : public UMemory { |
| 37 | |
| 38 | public: |
| 39 | |
| 40 | /** |
| 41 | * Construct a LocaleBased wrapper around the two pointers. These |
| 42 | * will be aliased for the lifetime of this object. |
| 43 | */ |
| 44 | inline LocaleBased(char* validAlias, char* actualAlias); |
| 45 | |
| 46 | /** |
| 47 | * Construct a LocaleBased wrapper around the two const pointers. |
| 48 | * These will be aliased for the lifetime of this object. |
| 49 | */ |
| 50 | inline LocaleBased(const char* validAlias, const char* actualAlias); |
| 51 | |
| 52 | /** |
| 53 | * Return locale meta-data for the service object wrapped by this |
| 54 | * object. Either the valid or the actual locale may be |
| 55 | * retrieved. |
| 56 | * @param type either ULOC_VALID_LOCALE or ULOC_ACTUAL_LOCALE |
| 57 | * @param status input-output error code |
| 58 | * @return the indicated locale |
| 59 | */ |
| 60 | Locale getLocale(ULocDataLocaleType type, UErrorCode& status) const; |
| 61 | |
| 62 | /** |
| 63 | * Return the locale ID for the service object wrapped by this |
| 64 | * object. Either the valid or the actual locale may be |
| 65 | * retrieved. |
| 66 | * @param type either ULOC_VALID_LOCALE or ULOC_ACTUAL_LOCALE |
| 67 | * @param status input-output error code |
| 68 | * @return the indicated locale ID |
| 69 | */ |
| 70 | const char* getLocaleID(ULocDataLocaleType type, UErrorCode& status) const; |
| 71 | |
| 72 | /** |
| 73 | * Set the locale meta-data for the service object wrapped by this |
| 74 | * object. If either parameter is zero, it is ignored. |
| 75 | * @param valid the ID of the valid locale |
| 76 | * @param actual the ID of the actual locale |
| 77 | */ |
| 78 | void setLocaleIDs(const char* valid, const char* actual); |
| 79 | |
| 80 | /** |
| 81 | * Set the locale meta-data for the service object wrapped by this |
| 82 | * object. |
| 83 | * @param valid the ID of the valid locale |
| 84 | * @param actual the ID of the actual locale |
| 85 | */ |
| 86 | void setLocaleIDs(const Locale& valid, const Locale& actual); |
| 87 | |
| 88 | private: |
| 89 | |
| 90 | char* valid; |
| 91 | |
| 92 | char* actual; |
| 93 | }; |
| 94 | |
| 95 | inline LocaleBased::LocaleBased(char* validAlias, char* actualAlias) : |
| 96 | valid(validAlias), actual(actualAlias) { |
| 97 | } |
| 98 | |
| 99 | inline LocaleBased::LocaleBased(const char* validAlias, |
| 100 | const char* actualAlias) : |
| 101 | // ugh: cast away const |
| 102 | valid((char*)validAlias), actual((char*)actualAlias) { |
| 103 | } |
| 104 | |
| 105 | U_NAMESPACE_END |
| 106 | |
| 107 | #endif |