Elliott Hughes | a942660 | 2014-05-29 17:41:25 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2014 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | #ifndef SCOPED_ICU_LOCALE_H_included |
| 18 | #define SCOPED_ICU_LOCALE_H_included |
| 19 | |
Steven Moreland | cab01ac | 2017-07-18 16:18:36 -0700 | [diff] [blame^] | 20 | #include <nativehelper/JNIHelp.h> |
| 21 | #include <nativehelper/ScopedUtfChars.h> |
Elliott Hughes | a942660 | 2014-05-29 17:41:25 -0700 | [diff] [blame] | 22 | #include "unicode/locid.h" |
| 23 | |
| 24 | class ScopedIcuLocale { |
| 25 | public: |
| 26 | ScopedIcuLocale(JNIEnv* env, jstring javaLocaleName) : mEnv(env) { |
| 27 | mLocale.setToBogus(); |
| 28 | |
| 29 | if (javaLocaleName == NULL) { |
| 30 | jniThrowNullPointerException(mEnv, "javaLocaleName == null"); |
| 31 | return; |
| 32 | } |
| 33 | |
| 34 | const ScopedUtfChars localeName(env, javaLocaleName); |
| 35 | if (localeName.c_str() == NULL) { |
| 36 | return; |
| 37 | } |
| 38 | |
Elliott Hughes | a04b5c3 | 2015-03-06 12:23:34 -0800 | [diff] [blame] | 39 | mLocale = icu::Locale::createFromName(localeName.c_str()); |
Elliott Hughes | a942660 | 2014-05-29 17:41:25 -0700 | [diff] [blame] | 40 | } |
| 41 | |
| 42 | ~ScopedIcuLocale() { |
| 43 | } |
| 44 | |
| 45 | bool valid() const { |
| 46 | return !mLocale.isBogus(); |
| 47 | } |
| 48 | |
Elliott Hughes | a04b5c3 | 2015-03-06 12:23:34 -0800 | [diff] [blame] | 49 | icu::Locale& locale() { |
Elliott Hughes | a942660 | 2014-05-29 17:41:25 -0700 | [diff] [blame] | 50 | return mLocale; |
| 51 | } |
| 52 | |
| 53 | private: |
| 54 | JNIEnv* const mEnv; |
Elliott Hughes | a04b5c3 | 2015-03-06 12:23:34 -0800 | [diff] [blame] | 55 | icu::Locale mLocale; |
Elliott Hughes | a942660 | 2014-05-29 17:41:25 -0700 | [diff] [blame] | 56 | |
| 57 | // Disallow copy and assignment. |
| 58 | ScopedIcuLocale(const ScopedIcuLocale&); |
| 59 | void operator=(const ScopedIcuLocale&); |
| 60 | }; |
| 61 | |
| 62 | #endif // SCOPED_ICU_LOCALE_H_included |