summaryrefslogtreecommitdiff
path: root/libs/samba/src/main/cpp/jni.hpp
blob: 90ca0111376f0d1635dbd741c1c9738c2252c60a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
#ifndef CLEVERSYNC_JNI_HPP
#define CLEVERSYNC_JNI_HPP

#include <jni.h>
#include <string>

#define ABORT_IF_NOT_OK(x) (::jni::internal::_abort_if_not_ok(__FILE__, __LINE__, (x)))
#define ABORT_IF_NULL(env, x) (::jni::internal::_abort_if_null(__FILE__, __LINE__, (env), (x)))

namespace jni {

namespace internal {

void _abort_if_not_ok(const char *file, int line, jint ret);
void _abort_with_exception(const char* file, int line, JNIEnv* env);

}  // namespace internal

template<class T>
class Ref {
 public:
  constexpr Ref() : env_(nullptr), ptr_(0) {}
  Ref(const Ref<T>&) = delete;
  Ref<T>& operator=(const Ref<T>&) = delete;

  [[nodiscard]] T get() const { return ptr_; }
  [[nodiscard]] T release() {
    auto ret = release_to_local();
    ptr_ = 0;
    return ret;
  }

  void reset() {
    del();
    ptr_ = 0;
  }

  explicit operator bool() const { return ptr_ != 0; }

 protected:
  Ref(JNIEnv* env, T ptr): env_(env), ptr_(ptr) {}
  virtual ~Ref() = default;

  virtual T release_to_local() = 0;
  virtual void del() = 0;

  JNIEnv* const env_;
  T ptr_;
};

template<class T>
class LocalRef : public Ref<T> {
 public:
  LocalRef(JNIEnv* env, T ptr): Ref<T>(env, ptr) {}
  ~LocalRef() override { free(); }

 protected:
  T release_to_local() override { return this->ptr_; }
  void del() override { free(); }

 private:
  void free() {
    if (this->ptr_)
      this->env_->DeleteLocalRef(this->ptr_);
  }
};

template<class T>
class ParamRef : public Ref<T> {
 public:
  ParamRef(JNIEnv* env, T ptr) : Ref<T>(env, ptr) {}
  ~ParamRef() override = default;

 protected:
  T release_to_local() override {
    if (this->ptr_)
      return static_cast<T>(this->env_->NewLocalRef(static_cast<jobject>(this->ptr_)));
    return 0;
  }
  void del() override {}
};

template<class T>
class GlobalRef : public Ref<T> {
 public:
  GlobalRef(JNIEnv* env, T ptr) : Ref<T>(env, ptr ? env->NewGlobalRef(ptr) : 0) {}
  explicit GlobalRef(const Ref<T>& other) : Ref<T>(other.env_, other ? other.env_->NewGlobalRef(other.ptr_) : 0) {}

  ~GlobalRef() override { free(); }

 protected:
  T release_to_local() override {
    if (this->ptr_) {
      auto ret = static_cast<T>(this->env_->NewLocalRef(
          static_cast<jobject>(this->ptr_)));
      free();
      return ret;
    }
    return 0;
  }
  void del() override { free(); }

 private:
  void free() {
    if (this->ptr_)
      this->env_->DeleteGlobalRef(this->ptr_);
  }
};

constexpr jint JNI_VERSION = JNI_VERSION_1_2;

JNIEnv* AttachCurrentThread();

JNIEnv* OnLoad(JavaVM* vm);

LocalRef<jclass> FindClass(JNIEnv *env, const char *name);

LocalRef<jthrowable> ExceptionOccurred(JNIEnv* env);

template<typename Out, typename In>
LocalRef<Out> CallObjectMethod(JNIEnv* env, const Ref<In>& object, jmethodID method) {
  return {env, static_cast<Out>(env->CallObjectMethod(object.get(), method))};
}

std::string StringToUTF8(JNIEnv* env, const Ref<jstring>& str);

LocalRef<jstring> UTF8ToString(JNIEnv* env, const std::string& str);

namespace internal {

template<typename T>
void _abort_if_null(const char* file, int line, JNIEnv* env, const jni::Ref<T>& ref) {
  if (ref) [[likely]] return;

  _abort_with_exception(file, line, env);
}

}  // namespace internal

}  // namespace jni

#endif // CLEVERSYNC_JNI_HPP