summaryrefslogtreecommitdiff
path: root/libs/samba/src/main/cpp/jni.hpp
blob: 3b5078a686ac2a2c7b4637f780de2527de9f6f69 (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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
#ifndef CLEVERSYNC_JNI_HPP
#define CLEVERSYNC_JNI_HPP

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

#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);

JNIEnv* NonFatalAttachCurrentThread();

}  // namespace internal

template<class T>
class Ref {
 public:
  constexpr Ref() : ptr_(nullptr) {}
  constexpr Ref(std::nullptr_t) : ptr_(nullptr) {}
  Ref(const Ref<T>&) = delete;

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

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

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

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

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

  JNIEnv* env_;
  T ptr_;
};

constexpr jint JNI_VERSION = JNI_VERSION_1_2;

JNIEnv* AttachCurrentThread();

JNIEnv* OnLoad(JavaVM* vm);

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

  LocalRef(LocalRef<T> &&ref) noexcept : Ref<T>(ref.env_, ref.release()) {}

  LocalRef& operator=(const Ref<T>& other) = delete;

  LocalRef& operator=(LocalRef<T> &&ref) noexcept {
    free();
    this->env_ = ref.env_;
    this->ptr_ = ref.release();
    return *this;
  }

 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;

  ParamRef& operator=(const Ref<T>& other) = delete;

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

template<class T>
class GlobalRef : public Ref<T> {
 public:
  constexpr GlobalRef() : Ref<T>() {}
  constexpr GlobalRef(std::nullptr_t) : Ref<T>() {}
  GlobalRef(JNIEnv* env, T ptr) : Ref<T>() {
    if (ptr) {
      if (!env)
        env = AttachCurrentThread();
      this->ptr_ = static_cast<T>(env->NewGlobalRef(static_cast<jobject>(ptr)));
    }
  }
  GlobalRef(const Ref<T>& other) : Ref<T>() {
    if (other) {
      auto* env = AttachCurrentThread();
      this->ptr_ = static_cast<T>(env->NewGlobalRef(static_cast<jobject>(other.get())));
    }
  }
  GlobalRef(GlobalRef<T>&& other) noexcept : Ref<T>(nullptr, other.ptr_) {
    other.ptr_ = nullptr;
    return *this;
  }

  ~GlobalRef() override { free(); }

  GlobalRef& operator=(const Ref<T>& other) {
    free();
    if (other) {
      auto* env = AttachCurrentThread();
      this->ptr_ = other ? static_cast<T>(env->NewGlobalRef(static_cast<jobject>(other.get()))) : nullptr;
    } else {
      this->ptr_ = nullptr;
    }
    return *this;
  }

  GlobalRef& operator=(GlobalRef<T>&& other) {
    free();
    this->ptr_ = other.ptr_;
    other.ptr_ = nullptr;
  }

 protected:
  T release_to_local() override {
    if (this->ptr_) {
      auto* env = AttachCurrentThread();
      auto ret = static_cast<T>(env->NewLocalRef(static_cast<jobject>(this->ptr_)));
      env->DeleteGlobalRef(static_cast<jobject>(this->ptr_));
      return ret;
    }
    return nullptr;
  }
  void del() override { free(); }

 private:
  void free() {
    if (this->ptr_) {
      auto* env = internal::NonFatalAttachCurrentThread();
      if (env)
        env->DeleteGlobalRef(static_cast<jobject>(this->ptr_));
    }
  }
};

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

LocalRef<jthrowable> ExceptionOccurred(JNIEnv* env);

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

template<typename Out, typename... Params>
LocalRef<Out> CallStaticObjectMethod(JNIEnv* env, const Ref<jclass>& clazz, jmethodID method, Params... params) {
  return {env, static_cast<Out>(env->CallStaticObjectMethod(clazz.get(), method, params...))};
}

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

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

LocalRef<jobjectArray> CreateArray(JNIEnv* env, const Ref<jclass>& element_class, std::vector<LocalRef<jobject>> objects);

namespace internal {

template<typename T>
inline 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);
}

inline void _abort_if_null(const char* file, int line, JNIEnv* env, jmethodID ref) {
  if (ref) [[likely]] return;

  _abort_with_exception(file, line, env);
}

}  // namespace internal

}  // namespace jni

#endif // CLEVERSYNC_JNI_HPP