1 /* 2 * Copyright (C) 2010 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 SRC_ANDROID_SDK_NATIVEHELPER_SCOPED_UTF_CHARS_H_ 18 #define SRC_ANDROID_SDK_NATIVEHELPER_SCOPED_UTF_CHARS_H_ 19 20 // Copied from 21 // https://cs.android.com/android/platform/superproject/main/+/main:libnativehelper/header_only_include/nativehelper/scoped_utf_chars.h;drc=4be05051ef76b2c24d8385732a892401eb45d911 22 23 #include <stddef.h> 24 #include <string.h> 25 26 #include <jni.h> 27 28 #include "src/android_sdk/nativehelper/nativehelper_utils.h" 29 30 // Protect this with __has_include to cope with `stl: "none"` users. 31 #if __has_include(<string_view>) 32 #include <string_view> 33 #endif 34 35 // A smart pointer that provides read-only access to a Java string's UTF chars. 36 // Unlike GetStringUTFChars, we throw NullPointerException rather than abort if 37 // passed a null jstring, and c_str will return nullptr. 38 // This makes the correct idiom very simple: 39 // 40 // ScopedUtfChars name(env, java_name); 41 // if (name.c_str() == nullptr) { 42 // return nullptr; 43 // } 44 // 45 // Also consider using `GET_UTF_OR_RETURN`, a shorthand for the 4 lines above. 46 class ScopedUtfChars { 47 public: ScopedUtfChars(JNIEnv * env,jstring s)48 ScopedUtfChars(JNIEnv* env, jstring s) noexcept : env_(env), string_(s) { 49 if (s == nullptr) { 50 utf_chars_ = nullptr; 51 jniThrowNullPointerException(env); 52 } else { 53 utf_chars_ = env->GetStringUTFChars(s, nullptr); 54 } 55 } 56 ScopedUtfChars(ScopedUtfChars && rhs)57 ScopedUtfChars(ScopedUtfChars&& rhs) noexcept 58 : env_(rhs.env_), string_(rhs.string_), utf_chars_(rhs.utf_chars_) { 59 rhs.env_ = nullptr; 60 rhs.string_ = nullptr; 61 rhs.utf_chars_ = nullptr; 62 } 63 ~ScopedUtfChars()64 ~ScopedUtfChars() noexcept { release_string(); } 65 66 ScopedUtfChars& operator=(ScopedUtfChars&& rhs) noexcept { 67 if (this != &rhs) { 68 // Delete the currently owned UTF chars. 69 release_string(); 70 71 // Move the rhs ScopedUtfChars and zero it out. 72 env_ = rhs.env_; 73 string_ = rhs.string_; 74 utf_chars_ = rhs.utf_chars_; 75 rhs.env_ = nullptr; 76 rhs.string_ = nullptr; 77 rhs.utf_chars_ = nullptr; 78 } 79 return *this; 80 } 81 c_str()82 const char* c_str() const noexcept { return utf_chars_; } 83 size()84 size_t size() const noexcept { return strlen(utf_chars_); } 85 86 const char& operator[](size_t n) const noexcept { return utf_chars_[n]; } 87 88 #if __has_include(<string_view>) string_view()89 operator std::string_view() const noexcept { return utf_chars_; } 90 #endif // SRC_ANDROID_SDK_NATIVEHELPER_SCOPED_UTF_CHARS_H_ 91 92 private: release_string()93 void release_string() noexcept { 94 if (utf_chars_) { 95 env_->ReleaseStringUTFChars(string_, utf_chars_); 96 } 97 } 98 99 JNIEnv* env_; 100 jstring string_; 101 const char* utf_chars_; 102 103 DISALLOW_COPY_AND_ASSIGN(ScopedUtfChars); 104 }; 105 106 #endif // SRC_ANDROID_SDK_NATIVEHELPER_SCOPED_UTF_CHARS_H_ 107