Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright (c) 2014 The Chromium Authors. All rights reserved. | |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #ifndef BASE_ANDROID_JNI_INT_WRAPPER_H_ | |
| 6 #define BASE_ANDROID_JNI_INT_WRAPPER_H_ | |
| 7 | |
| 8 #include <stdint.h> | |
| 9 | |
| 10 // Wrapper used to receive int when calling Java from native. | |
| 11 // The wrapper disallows automatic conversion of long to int. | |
| 12 // This is to avoid a common anti-pattern where a Java int is used | |
| 13 // to receive a native pointer. Please use a Java long to receive | |
| 14 // native pointers, so that the code works on both 32-bit and 64-bit | |
| 15 // platforms. Note the wrapper allows other lossy conversions into | |
| 16 // jint that could be consider anti-patterns, such as from size_t. | |
|
bulach
2014/05/02 16:55:56
I think we could do with a
#ifdef NDEBUG
// Don'
| |
| 17 | |
| 18 class jni_int_wrapper { | |
|
bulach
2014/05/02 16:55:56
nit: should be JniIntWrapper
| |
| 19 public: | |
| 20 jni_int_wrapper() : i_(0) {} | |
| 21 jni_int_wrapper(int i) : i_(i) {} | |
| 22 jni_int_wrapper(const jni_int_wrapper& ji) : i_(ji.i_) {} | |
| 23 template <class T> jni_int_wrapper(const T& t) : i_(t) {} | |
| 24 jint asJint() const { return i_; } | |
|
bulach
2014/05/02 16:55:56
nit: and here, as_jint()
| |
| 25 private: | |
| 26 // If you get an "is private" error at the line below it is because you used | |
| 27 // an implicit conversion to convert a long to an int when calling Java. | |
| 28 // We disallow this, as a common anti-pattern allows converting a native | |
| 29 // pointer (intptr_t) to a Java int. Please use a Java long to represent | |
| 30 // a native pointer. If you want a lossy conversion, please use an | |
| 31 // explicit conversion in your C++ code. Note an error is only seen when | |
| 32 // compiling on a 64-bit platform, as intptr_t is indistinguishable from | |
| 33 // int on 32-bit platforms. | |
| 34 jni_int_wrapper(long); | |
| 35 jint i_; | |
| 36 }; | |
| 37 | |
| 38 #endif // BASE_ANDROID_JNI_INT_WRAPPER_H_ | |
| OLD | NEW |