1 /*
2  *  Copyright 2018 The WebRTC project authors. All Rights Reserved.
3  *
4  *  Use of this source code is governed by a BSD-style license
5  *  that can be found in the LICENSE file in the root of the source
6  *  tree. An additional intellectual property rights grant can be found
7  *  in the file PATENTS.  All contributing project authors may
8  *  be found in the AUTHORS file in the root of the source tree.
9  */
10 
11 // Originally this class is from Chromium.
12 // https://cs.chromium.org/chromium/src/base/android/jni_int_wrapper.h.
13 
14 #ifndef SDK_ANDROID_NATIVE_API_JNI_JNI_INT_WRAPPER_H_
15 #define SDK_ANDROID_NATIVE_API_JNI_JNI_INT_WRAPPER_H_
16 
17 // Wrapper used to receive int when calling Java from native. The wrapper
18 // disallows automatic conversion of anything besides int32_t to a jint.
19 // Checking is only done in debugging builds.
20 
21 #ifdef NDEBUG
22 
23 typedef jint JniIntWrapper;
24 
25 // This inline is sufficiently trivial that it does not change the
26 // final code generated by g++.
as_jint(JniIntWrapper wrapper)27 inline jint as_jint(JniIntWrapper wrapper) {
28   return wrapper;
29 }
30 
31 #else
32 
33 class JniIntWrapper {
34  public:
JniIntWrapper()35   JniIntWrapper() : i_(0) {}
JniIntWrapper(int32_t i)36   JniIntWrapper(int32_t i) : i_(i) {}  // NOLINT(runtime/explicit)
JniIntWrapper(const JniIntWrapper & ji)37   explicit JniIntWrapper(const JniIntWrapper& ji) : i_(ji.i_) {}
38 
as_jint()39   jint as_jint() const { return i_; }
40 
41   // If you get an "invokes a deleted function" error at the lines below it is
42   // because you used an implicit conversion to convert e.g. a long to an
43   // int32_t when calling Java. We disallow this. If you want a lossy
44   // conversion, please use an explicit conversion in your C++ code.
45   JniIntWrapper(uint32_t) = delete;  // NOLINT(runtime/explicit)
46   JniIntWrapper(uint64_t) = delete;  // NOLINT(runtime/explicit)
47   JniIntWrapper(int64_t) = delete;   // NOLINT(runtime/explicit)
48 
49  private:
50   const jint i_;
51 };
52 
as_jint(const JniIntWrapper & wrapper)53 inline jint as_jint(const JniIntWrapper& wrapper) {
54   return wrapper.as_jint();
55 }
56 
57 #endif  // NDEBUG
58 
59 #endif  // SDK_ANDROID_NATIVE_API_JNI_JNI_INT_WRAPPER_H_
60