Plasma Engine  2.0
Loading...
Searching...
No Matches
AndroidJni.h
1#pragma once
2
3#if PL_ENABLED(PL_PLATFORM_ANDROID)
4
5# include <Foundation/Logging/Log.h>
6# include <Foundation/Strings/StringBuilder.h>
7# include <jni.h>
8
9class plJniObject;
10class plJniClass;
11class plJniString;
12
14enum class plJniErrorState
15{
17 SUCCESS = 0,
18
20 PENDING_EXCEPTION,
21
23 NO_MATCHING_METHOD,
24
26 AMBIGUOUS_CALL,
27
29 NO_MATCHING_FIELD,
30
32 CALL_ON_NULL_OBJECT,
33
35 CLASS_NOT_FOUND,
36};
37
54class PL_FOUNDATION_DLL plJniAttachment
55{
56public:
58 plJniAttachment();
59
61 ~plJniAttachment();
62
66 static plJniObject GetActivity();
67
69 static JNIEnv* GetEnv();
70
75 static plJniErrorState GetLastError();
76
81 static void ClearLastError();
82
86 static bool HasPendingException();
87
91 static plJniObject GetPendingException();
92
94 static void ClearPendingException();
95
97 static void SetLastError(plJniErrorState state);
98
100 static bool FailOnPendingErrorOrException();
101
102private:
103 static thread_local JNIEnv* s_env;
104 static thread_local bool s_ownsEnv;
105 static thread_local int s_attachCount;
106 static thread_local plJniErrorState s_lastError;
107
108 plJniAttachment(const plJniAttachment&);
109 plJniAttachment& operator=(const plJniAttachment&);
110};
111
113enum class plJniOwnerShip
114{
116 OWN,
117
119 COPY,
120
122 BORROW
123};
124
126class PL_FOUNDATION_DLL plJniObject
127{
128public:
130 plJniObject();
131
136 inline plJniObject(jobject object, plJniOwnerShip ownerShip);
137
139 inline plJniObject(const plJniObject& other);
140
142 inline plJniObject(plJniObject&& other);
143
145 inline plJniObject& operator=(const plJniObject& other);
146
148 inline plJniObject& operator=(plJniObject&& other);
149
151 inline virtual ~plJniObject();
152
167 inline bool operator==(const plJniObject& other) const;
168
183 inline bool operator!=(const plJniObject& other) const;
184
186 bool IsNull() const { return m_object == nullptr; }
187
189 jobject GetHandle() const;
190
194 plJniClass GetClass() const;
195
199 plJniString ToString() const;
200
202 bool IsInstanceOf(const plJniClass& clazz) const;
203
279 template <typename Ret = void, typename... Args>
280 Ret Call(const char* name, const Args&... args) const;
281
291 template <typename Ret>
292 Ret GetField(const char* name) const;
293
304 template <typename T>
305 void SetField(const char* name, const T& arg) const;
306
308 template <typename Ret, typename... Args>
309 Ret UnsafeCall(const char* name, const char* signature, const Args&... args) const;
310
312 template <typename Ret>
313 Ret UnsafeGetField(const char* name, const char* signature) const;
314
316 template <typename T>
317 void UnsafeSetField(const char* name, const char* signature, const T& arg) const;
318
319protected:
320 inline void Reset();
321 inline jobject GetJObject() const;
322
323 static void DumpTypes(const plJniClass* inputTypes, int N, const plJniClass* returnType);
324
325 static int CompareMethodSpecificity(const plJniObject& method1, const plJniObject& method2);
326 static bool IsMethodViable(bool bStatic, const plJniObject& candidateMethod, const plJniClass& returnType, plJniClass* inputTypes, int N);
327 static plJniObject FindMethod(bool bStatic, const char* name, const plJniClass& type, const plJniClass& returnType, plJniClass* inputTypes, int N);
328
329 static int CompareConstructorSpecificity(const plJniObject& method1, const plJniObject& method2);
330 static bool IsConstructorViable(const plJniObject& candidateMethod, plJniClass* inputTypes, int N);
331 static plJniObject FindConstructor(const plJniClass& type, plJniClass* inputTypes, int N);
332
333private:
334 jobject m_object;
335 jclass m_class;
336 bool m_own;
337};
338
345class PL_FOUNDATION_DLL plJniString : public plJniObject
346{
347public:
349 plJniString();
350
352 plJniString(const char* str);
353
358 plJniString(jstring string, plJniOwnerShip ownerShip);
359
361 plJniString(const plJniString& other);
362
364 plJniString(plJniString&& other);
365
367 plJniString& operator=(const plJniString& other);
368
370 plJniString& operator=(plJniString&& other);
371
373 virtual ~plJniString();
374
376 const char* GetData() const;
377
378private:
379 const char* m_utf;
380};
381
383class PL_FOUNDATION_DLL plJniClass : public plJniObject
384{
385public:
387 plJniClass();
388
403 plJniClass(const char* className);
404
409 plJniClass(jclass clazz, plJniOwnerShip ownerShip);
410
412 plJniClass(const plJniClass& other);
413
415 plJniClass(plJniClass&& other);
416
418 plJniClass& operator=(const plJniClass& other);
419
421 plJniClass& operator=(plJniClass&& other);
422
424 jclass GetHandle() const;
425
441 template <typename... Args>
442 plJniObject CreateInstance(const Args&... args) const;
443
448 bool IsAssignableFrom(const plJniClass& other) const;
449
451 bool IsPrimitive();
452
472 template <typename Ret = void, typename... Args>
473 Ret CallStatic(const char* name, const Args&... args) const;
474
478 template <typename Ret>
479 Ret GetStaticField(const char* name) const;
480
485 template <typename T>
486 void SetStaticField(const char* name, const T& arg) const;
487
489 template <typename Ret, typename... Args>
490 Ret UnsafeCallStatic(const char* name, const char* signature, const Args&... args) const;
491
493 template <typename Ret>
494 Ret UnsafeGetStaticField(const char* name, const char* signature) const;
495
497 template <typename T>
498 void UnsafeSetStaticField(const char* name, const char* signature, const T& arg) const;
499};
500
501# include <Foundation/Basics/Platform/Android/AndroidJni.inl>
502
503#endif
PL_ALWAYS_INLINE const plStringBuilder & ToString(bool value, plStringBuilder &out_sResult)
Converts a bool to a string.
Definition ConversionUtils.h:176