blob: 851de76c611bb8117eac078881303e7d3d109131 [file] [log] [blame]
Elliott Hughesa9426602014-05-29 17:41:25 -07001/*
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 Morelandcab01ac2017-07-18 16:18:36 -070020#include <nativehelper/JNIHelp.h>
21#include <nativehelper/ScopedUtfChars.h>
Elliott Hughesa9426602014-05-29 17:41:25 -070022#include "unicode/locid.h"
23
24class 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 Hughesa04b5c32015-03-06 12:23:34 -080039 mLocale = icu::Locale::createFromName(localeName.c_str());
Elliott Hughesa9426602014-05-29 17:41:25 -070040 }
41
42 ~ScopedIcuLocale() {
43 }
44
45 bool valid() const {
46 return !mLocale.isBogus();
47 }
48
Elliott Hughesa04b5c32015-03-06 12:23:34 -080049 icu::Locale& locale() {
Elliott Hughesa9426602014-05-29 17:41:25 -070050 return mLocale;
51 }
52
53 private:
54 JNIEnv* const mEnv;
Elliott Hughesa04b5c32015-03-06 12:23:34 -080055 icu::Locale mLocale;
Elliott Hughesa9426602014-05-29 17:41:25 -070056
57 // Disallow copy and assignment.
58 ScopedIcuLocale(const ScopedIcuLocale&);
59 void operator=(const ScopedIcuLocale&);
60};
61
62#endif // SCOPED_ICU_LOCALE_H_included