• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* Copyright 2016 The TensorFlow Authors. All Rights Reserved.
2 
3 Licensed under the Apache License, Version 2.0 (the "License");
4 you may not use this file except in compliance with the License.
5 You may obtain a copy of the License at
6 
7     http://www.apache.org/licenses/LICENSE-2.0
8 
9 Unless required by applicable law or agreed to in writing, software
10 distributed under the License is distributed on an "AS IS" BASIS,
11 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 See the License for the specific language governing permissions and
13 limitations under the License.
14 ==============================================================================*/
15 
16 #ifndef TENSORFLOW_EXAMPLES_ANDROID_JNI_OBJECT_TRACKING_JNI_UTILS_H_
17 #define TENSORFLOW_EXAMPLES_ANDROID_JNI_OBJECT_TRACKING_JNI_UTILS_H_
18 
19 #include <stdint.h>
20 
21 #include "tensorflow/examples/android/jni/object_tracking/utils.h"
22 
23 // The JniLongField class is used to access Java fields from native code. This
24 // technique of hiding pointers to native objects in opaque Java fields is how
25 // the Android hardware libraries work. This reduces the amount of static
26 // native methods and makes it easier to manage the lifetime of native objects.
27 class JniLongField {
28  public:
JniLongField(const char * field_name)29   JniLongField(const char* field_name)
30       : field_name_(field_name), field_ID_(0) {}
31 
get(JNIEnv * env,jobject thiz)32   int64_t get(JNIEnv* env, jobject thiz) {
33     if (field_ID_ == 0) {
34       jclass cls = env->GetObjectClass(thiz);
35       CHECK_ALWAYS(cls != 0, "Unable to find class");
36       field_ID_ = env->GetFieldID(cls, field_name_, "J");
37       CHECK_ALWAYS(field_ID_ != 0,
38           "Unable to find field %s. (Check proguard cfg)", field_name_);
39     }
40 
41     return env->GetLongField(thiz, field_ID_);
42   }
43 
set(JNIEnv * env,jobject thiz,int64_t value)44   void set(JNIEnv* env, jobject thiz, int64_t value) {
45     if (field_ID_ == 0) {
46       jclass cls = env->GetObjectClass(thiz);
47       CHECK_ALWAYS(cls != 0, "Unable to find class");
48       field_ID_ = env->GetFieldID(cls, field_name_, "J");
49       CHECK_ALWAYS(field_ID_ != 0,
50           "Unable to find field %s (Check proguard cfg)", field_name_);
51     }
52 
53     env->SetLongField(thiz, field_ID_, value);
54   }
55 
56  private:
57   const char* const field_name_;
58 
59   // This is just a cache
60   jfieldID field_ID_;
61 };
62 
63 #endif  // TENSORFLOW_EXAMPLES_ANDROID_JNI_OBJECT_TRACKING_JNI_UTILS_H_
64