1*635a8641SAndroid Build Coastguard Worker // Copyright 2014 The Chromium Authors. All rights reserved. 2*635a8641SAndroid Build Coastguard Worker // Use of this source code is governed by a BSD-style license that can be 3*635a8641SAndroid Build Coastguard Worker // found in the LICENSE file. 4*635a8641SAndroid Build Coastguard Worker 5*635a8641SAndroid Build Coastguard Worker #ifndef BASE_ANDROID_JNI_INT_WRAPPER_H_ 6*635a8641SAndroid Build Coastguard Worker #define BASE_ANDROID_JNI_INT_WRAPPER_H_ 7*635a8641SAndroid Build Coastguard Worker 8*635a8641SAndroid Build Coastguard Worker // Wrapper used to receive int when calling Java from native. 9*635a8641SAndroid Build Coastguard Worker // The wrapper disallows automatic conversion of long to int. 10*635a8641SAndroid Build Coastguard Worker // This is to avoid a common anti-pattern where a Java int is used 11*635a8641SAndroid Build Coastguard Worker // to receive a native pointer. Please use a Java long to receive 12*635a8641SAndroid Build Coastguard Worker // native pointers, so that the code works on both 32-bit and 64-bit 13*635a8641SAndroid Build Coastguard Worker // platforms. Note the wrapper allows other lossy conversions into 14*635a8641SAndroid Build Coastguard Worker // jint that could be consider anti-patterns, such as from size_t. 15*635a8641SAndroid Build Coastguard Worker 16*635a8641SAndroid Build Coastguard Worker // Checking is only done in debugging builds. 17*635a8641SAndroid Build Coastguard Worker 18*635a8641SAndroid Build Coastguard Worker #ifdef NDEBUG 19*635a8641SAndroid Build Coastguard Worker 20*635a8641SAndroid Build Coastguard Worker typedef jint JniIntWrapper; 21*635a8641SAndroid Build Coastguard Worker 22*635a8641SAndroid Build Coastguard Worker // This inline is sufficiently trivial that it does not change the 23*635a8641SAndroid Build Coastguard Worker // final code generated by g++. as_jint(JniIntWrapper wrapper)24*635a8641SAndroid Build Coastguard Workerinline jint as_jint(JniIntWrapper wrapper) { 25*635a8641SAndroid Build Coastguard Worker return wrapper; 26*635a8641SAndroid Build Coastguard Worker } 27*635a8641SAndroid Build Coastguard Worker 28*635a8641SAndroid Build Coastguard Worker #else 29*635a8641SAndroid Build Coastguard Worker 30*635a8641SAndroid Build Coastguard Worker class JniIntWrapper { 31*635a8641SAndroid Build Coastguard Worker public: JniIntWrapper()32*635a8641SAndroid Build Coastguard Worker JniIntWrapper() : i_(0) {} JniIntWrapper(int i)33*635a8641SAndroid Build Coastguard Worker JniIntWrapper(int i) : i_(i) {} JniIntWrapper(const JniIntWrapper & ji)34*635a8641SAndroid Build Coastguard Worker JniIntWrapper(const JniIntWrapper& ji) : i_(ji.i_) {} JniIntWrapper(const T & t)35*635a8641SAndroid Build Coastguard Worker template <class T> JniIntWrapper(const T& t) : i_(t) {} as_jint()36*635a8641SAndroid Build Coastguard Worker jint as_jint() const { return i_; } 37*635a8641SAndroid Build Coastguard Worker private: 38*635a8641SAndroid Build Coastguard Worker // If you get an "is private" error at the line below it is because you used 39*635a8641SAndroid Build Coastguard Worker // an implicit conversion to convert a long to an int when calling Java. 40*635a8641SAndroid Build Coastguard Worker // We disallow this, as a common anti-pattern allows converting a native 41*635a8641SAndroid Build Coastguard Worker // pointer (intptr_t) to a Java int. Please use a Java long to represent 42*635a8641SAndroid Build Coastguard Worker // a native pointer. If you want a lossy conversion, please use an 43*635a8641SAndroid Build Coastguard Worker // explicit conversion in your C++ code. Note an error is only seen when 44*635a8641SAndroid Build Coastguard Worker // compiling on a 64-bit platform, as intptr_t is indistinguishable from 45*635a8641SAndroid Build Coastguard Worker // int on 32-bit platforms. 46*635a8641SAndroid Build Coastguard Worker JniIntWrapper(long); 47*635a8641SAndroid Build Coastguard Worker jint i_; 48*635a8641SAndroid Build Coastguard Worker }; 49*635a8641SAndroid Build Coastguard Worker as_jint(const JniIntWrapper & wrapper)50*635a8641SAndroid Build Coastguard Workerinline jint as_jint(const JniIntWrapper& wrapper) { 51*635a8641SAndroid Build Coastguard Worker return wrapper.as_jint(); 52*635a8641SAndroid Build Coastguard Worker } 53*635a8641SAndroid Build Coastguard Worker 54*635a8641SAndroid Build Coastguard Worker #endif // NDEBUG 55*635a8641SAndroid Build Coastguard Worker 56*635a8641SAndroid Build Coastguard Worker #endif // BASE_ANDROID_JNI_INT_WRAPPER_H_ 57