lime
Lime is a C++ library implementing Open Whisper System Signal protocol
ownership.hpp
Go to the documentation of this file.
1 #pragma once
2 
3 #include <jni/types.hpp>
4 #include <jni/wrapping.hpp>
5 #include <jni/typed_methods.hpp>
6 
7 namespace jni
8  {
10  {
11  void operator()(JNIEnv* env) const
12  {
13  if (env)
14  {
15  env->PopLocalFrame(nullptr);
16  }
17  }
18  };
19 
20  using UniqueLocalFrame = std::unique_ptr< JNIEnv, LocalFrameDeleter >;
21 
22 
23  using RefDeletionMethod = void (JNIEnv::*)(::jobject);
24 
25  template < RefDeletionMethod DeleteRef >
27  {
28  private:
29  JNIEnv* env = nullptr;
30 
31  public:
32  DefaultRefDeleter() = default;
33  DefaultRefDeleter(JNIEnv& e) : env(&e) {}
34 
35  void operator()(jobject* p) const
36  {
37  if (p)
38  {
39  assert(env);
40  (env->*DeleteRef)(Unwrap(p));
41  }
42  }
43  };
44 
45 
46  template < class T, template < RefDeletionMethod > class Deleter = DefaultRefDeleter >
47  using UniqueGlobalRef = std::unique_ptr< T, Deleter<&JNIEnv::DeleteGlobalRef> >;
48 
49  template < class T, template < RefDeletionMethod > class Deleter = DefaultRefDeleter >
50  using UniqueWeakGlobalRef = std::unique_ptr< T, Deleter<&JNIEnv::DeleteWeakGlobalRef> >;
51 
52  // Not parameterized by Deleter because local references should be short-lived enough
53  // that DefaultRefDeleter suffices in all cases.
54  template < class T >
55  using UniqueLocalRef = std::unique_ptr< T, DefaultRefDeleter<&JNIEnv::DeleteLocalRef> >;
56 
57 
59  {
60  private:
61  JNIEnv* env = nullptr;
62  jstring* string = nullptr;
63 
64  public:
65  StringCharsDeleter() = default;
66  StringCharsDeleter(JNIEnv& e, jstring& s) : env(&e), string(&s) {}
67 
68  void operator()(const char16_t* p) const
69  {
70  if (p)
71  {
72  assert(env);
73  assert(string);
74  env->ReleaseStringChars(Unwrap(string), Unwrap(p));
75  }
76  }
77  };
78 
79  using UniqueStringChars = std::unique_ptr< const char16_t, StringCharsDeleter >;
80 
81 
83  {
84  private:
85  JNIEnv* env = nullptr;
86  jstring* string = nullptr;
87 
88  public:
89  StringUTFCharsDeleter() = default;
90  StringUTFCharsDeleter(JNIEnv& e, jstring& s) : env(&e), string(&s) {}
91 
92  void operator()(const char* p) const
93  {
94  if (p)
95  {
96  assert(env);
97  assert(string);
98  env->ReleaseStringUTFChars(Unwrap(string), p);
99  }
100  }
101  };
102 
103  using UniqueStringUTFChars = std::unique_ptr< const char, StringUTFCharsDeleter >;
104 
105 
107  {
108  private:
109  JNIEnv* env = nullptr;
110  jstring* string = nullptr;
111 
112  public:
113  StringCriticalDeleter() = default;
114  StringCriticalDeleter(JNIEnv& e, jstring& s) : env(&e), string(&s) {}
115 
116  void operator()(const char16_t* p) const
117  {
118  if (p)
119  {
120  assert(env);
121  assert(string);
122  env->ReleaseStringCritical(Unwrap(string), Unwrap(p));
123  }
124  }
125  };
126 
127  using UniqueStringCritical = std::unique_ptr< const char16_t, StringCriticalDeleter >;
128 
129 
130  template < class E >
132  {
133  private:
134  JNIEnv* env = nullptr;
135  jarray<E>* array = nullptr;
136 
137  public:
138  ArrayElementsDeleter() = default;
139  ArrayElementsDeleter(JNIEnv& e, jarray<E>& a) : env(&e), array(&a) {}
140 
141  void operator()(E* p) const
142  {
143  if (p)
144  {
145  assert(env);
146  assert(array);
147  (env->*(TypedMethods<E>::ReleaseArrayElements))(Unwrap(array), p, JNI_ABORT);
148  }
149  }
150  };
151 
152  template < class E >
153  using UniqueArrayElements = std::unique_ptr< E, ArrayElementsDeleter<E> >;
154 
155 
156  template < class E >
158  {
159  private:
160  JNIEnv* env = nullptr;
161  jarray<E>* array = nullptr;
162 
163  public:
164  PrimitiveArrayCriticalDeleter() = default;
165  PrimitiveArrayCriticalDeleter(JNIEnv& e, jarray<E>& a) : env(&e), array(&a) {}
166 
167  void operator()(void* p) const
168  {
169  if (p)
170  {
171  assert(env);
172  assert(array);
173  env->ReleasePrimitiveArrayCritical(Unwrap(array), p, JNI_ABORT);
174  }
175  }
176  };
177 
178  template < class E >
179  using UniquePrimitiveArrayCritical = std::unique_ptr< void, PrimitiveArrayCriticalDeleter<E> >;
180 
181 
183  {
184  private:
185  JNIEnv* env = nullptr;
186 
187  public:
188  MonitorDeleter() = default;
189  MonitorDeleter(JNIEnv& e) : env(&e) {}
190 
191  void operator()(jobject* p) const
192  {
193  if (p)
194  {
195  assert(env);
196  env->MonitorExit(Unwrap(p));
197  }
198  }
199  };
200 
201  using UniqueMonitor = std::unique_ptr< jobject, MonitorDeleter >;
202 
203 
205  {
206  private:
207  JavaVM* vm = nullptr;
208 
209  public:
210  JNIEnvDeleter() = default;
211  JNIEnvDeleter(JavaVM& v) : vm(&v) {}
212 
213  void operator()(JNIEnv* p) const
214  {
215  if (p)
216  {
217  assert(vm);
218  vm->DetachCurrentThread();
219  }
220  }
221  };
222 
223  using UniqueEnv = std::unique_ptr< JNIEnv, JNIEnvDeleter >;
224  }
std::unique_ptr< T, DefaultRefDeleter<&JNIEnv::DeleteLocalRef > > UniqueLocalRef
Definition: ownership.hpp:55
std::unique_ptr< JNIEnv, LocalFrameDeleter > UniqueLocalFrame
Definition: ownership.hpp:20
void operator()(JNIEnv *p) const
Definition: ownership.hpp:213
void operator()(jobject *p) const
Definition: ownership.hpp:191
std::unique_ptr< jobject, MonitorDeleter > UniqueMonitor
Definition: ownership.hpp:201
Definition: ownership.hpp:157
Definition: ownership.hpp:9
std::unique_ptr< const char16_t, StringCharsDeleter > UniqueStringChars
Definition: ownership.hpp:79
void operator()(const char16_t *p) const
Definition: ownership.hpp:116
JNIEnvDeleter(JavaVM &v)
Definition: ownership.hpp:211
std::unique_ptr< const char16_t, StringCriticalDeleter > UniqueStringCritical
Definition: ownership.hpp:127
StringUTFCharsDeleter(JNIEnv &e, jstring &s)
Definition: ownership.hpp:90
void operator()(const char16_t *p) const
Definition: ownership.hpp:68
std::unique_ptr< const char, StringUTFCharsDeleter > UniqueStringUTFChars
Definition: ownership.hpp:103
Definition: ownership.hpp:26
MonitorDeleter(JNIEnv &e)
Definition: ownership.hpp:189
DefaultRefDeleter(JNIEnv &e)
Definition: ownership.hpp:33
void operator()(void *p) const
Definition: ownership.hpp:167
Definition: advanced_ownership.hpp:5
std::unique_ptr< T, Deleter<&JNIEnv::DeleteGlobalRef > > UniqueGlobalRef
Definition: ownership.hpp:47
ArrayElementsDeleter(JNIEnv &e, jarray< E > &a)
Definition: ownership.hpp:139
void operator()(E *p) const
Definition: ownership.hpp:141
Definition: ownership.hpp:204
std::unique_ptr< T, Deleter<&JNIEnv::DeleteWeakGlobalRef > > UniqueWeakGlobalRef
Definition: ownership.hpp:50
Definition: ownership.hpp:82
void operator()(JNIEnv *env) const
Definition: ownership.hpp:11
Definition: ownership.hpp:58
auto Unwrap(W &&w)
Definition: wrapping.hpp:22
void(JNIEnv::*)(::jobject) RefDeletionMethod
Definition: ownership.hpp:23
Definition: typed_methods.hpp:7
void operator()(jobject *p) const
Definition: ownership.hpp:35
std::unique_ptr< JNIEnv, JNIEnvDeleter > UniqueEnv
Definition: ownership.hpp:223
std::unique_ptr< void, PrimitiveArrayCriticalDeleter< E > > UniquePrimitiveArrayCritical
Definition: ownership.hpp:179
Definition: ownership.hpp:106
Definition: ownership.hpp:182
std::unique_ptr< E, ArrayElementsDeleter< E > > UniqueArrayElements
Definition: ownership.hpp:153
PrimitiveArrayCriticalDeleter(JNIEnv &e, jarray< E > &a)
Definition: ownership.hpp:165
StringCharsDeleter(JNIEnv &e, jstring &s)
Definition: ownership.hpp:66
Definition: types.hpp:39
StringCriticalDeleter(JNIEnv &e, jstring &s)
Definition: ownership.hpp:114
Definition: types.hpp:30
void operator()(const char *p) const
Definition: ownership.hpp:92
Definition: ownership.hpp:131