Beatmup
BeatmupJavaObjectPool Class Reference

Handles all Java binding-related static data. More...

#include <objectpool.h>

Public Member Functions

template<class Object >
Object * getObject (JNIEnv *jenv, jlong handle)
 Retrieves native object by its handle. More...
 
template<class Object >
Object * getObject (JNIEnv *jenv, jobject obj)
 Retrieves native object from its Java prototype. More...
 
void nullifyHandle (JNIEnv *jenv, jobject obj)
 Sets a handle of a java object to null. More...
 
void addJavaReference (JNIEnv *jenv, jobject jobj, const Beatmup::Object *bobj)
 Creates new global reference on a Java object to avoid garbage collecting. More...
 
void removeJavaReference (JNIEnv *jenv, const Beatmup::Object *bobj)
 Removes a Java reference. More...
 
void removeAllJavaReferences (JNIEnv *jenv, const Beatmup::Object *bobj)
 Removes all Java references to depending objects. More...
 
jobject getJavaReference (const Beatmup::Object *bobj) const
 Returns a reference to the Java object which a given object depends on. More...
 
 BeatmupJavaObjectPool ()
 

Static Public Member Functions

static void throwToJava (JNIEnv *jenv, const char *exceptionClass, const char *message)
 Throws a specific exception. More...
 
static void throwToJava (JNIEnv *jenv, const char *message)
 Throws a general exception. More...
 
static void rethrowToJava (JNIEnv *jenv, std::exception &ex)
 Rethrows a core-caused exception. More...
 

Public Attributes

JavaFactory factory
 
JNIEnv * env
 
jfieldID handleFieldId
 

Static Public Attributes

static const jlong INVALID_HANDLE = std::numeric_limits<jlong>::min()
 

Private Member Functions

bool queryJavaContext (JNIEnv *newEnv)
 Java context check. More...
 

Private Attributes

std::multimap< const Beatmup::Object *, jobjectjavaRefs
 Java global references representing dependencies of internal objects on external Java objects. More...
 
struct {
   JNIEnv *   env
 
   jfieldID   handleFieldId
 
javaContext
 
std::mutex access
 structure access control More...
 

Detailed Description

Handles all Java binding-related static data.

Definition at line 34 of file objectpool.h.

Constructor & Destructor Documentation

◆ BeatmupJavaObjectPool()

BeatmupJavaObjectPool::BeatmupJavaObjectPool ( )
inline

Definition at line 148 of file objectpool.h.

148  {
149  javaContext.env = NULL;
150  }
struct BeatmupJavaObjectPool::@20 javaContext

Member Function Documentation

◆ queryJavaContext()

bool BeatmupJavaObjectPool::queryJavaContext ( JNIEnv *  newEnv)
inlineprivate

Java context check.

Definition at line 47 of file objectpool.h.

47  {
48  if (javaContext.env == newEnv)
49  return true;
50  std::lock_guard<std::mutex> lock(access);
51  javaContext.env = newEnv;
52  jclass cls = newEnv->FindClass("Beatmup/Object");
53  javaContext.handleFieldId = newEnv->GetFieldID(cls, "handle", "J");
54  newEnv->DeleteLocalRef(cls);
55  return false;
56  }
std::mutex access
structure access control
Definition: objectpool.h:58
JNIEnv jclass

◆ getObject() [1/2]

template<class Object >
Object* BeatmupJavaObjectPool::getObject ( JNIEnv *  jenv,
jlong  handle 
)
inline

Retrieves native object by its handle.

Definition at line 68 of file objectpool.h.

68  {
69 #ifdef DEBUG_LOGGING
70  LOG_I("Hitting object #%lld", handle);
71 #endif
72  if (handle == INVALID_HANDLE)
73  return nullptr;
74  return static_cast<Object*>( (void*)handle );
75  }
static const jlong INVALID_HANDLE
Definition: objectpool.h:61
#define LOG_I(...)
Definition: log.h:23
JNIEnv jobject jlong handle

◆ getObject() [2/2]

template<class Object >
Object* BeatmupJavaObjectPool::getObject ( JNIEnv *  jenv,
jobject  obj 
)
inline

Retrieves native object from its Java prototype.

Definition at line 80 of file objectpool.h.

80  {
82  if (!obj)
83  return nullptr;
84  jlong ptr = jenv->GetLongField(obj, javaContext.handleFieldId);
85 #ifdef DEBUG_LOGGING
86  LOG_I("Hitting object #%lld from Java", ptr);
87 #endif
88  if (ptr == INVALID_HANDLE)
89  return nullptr;
90  return static_cast<Object*>( (void*)jenv->GetLongField(obj, javaContext.handleFieldId) );
91  }
bool queryJavaContext(JNIEnv *newEnv)
Java context check.
Definition: objectpool.h:47
JNIEnv * jenv
return() jlong(listener)

◆ nullifyHandle()

void BeatmupJavaObjectPool::nullifyHandle ( JNIEnv *  jenv,
jobject  obj 
)
inline

Sets a handle of a java object to null.

Definition at line 97 of file objectpool.h.

97  {
99  jenv->SetLongField(obj, javaContext.handleFieldId, INVALID_HANDLE);
100  }

◆ addJavaReference()

void BeatmupJavaObjectPool::addJavaReference ( JNIEnv *  jenv,
jobject  jobj,
const Beatmup::Object bobj 
)
inline

Creates new global reference on a Java object to avoid garbage collecting.

Definition at line 105 of file objectpool.h.

105  {
106  std::lock_guard<std::mutex> lock(access);
107  javaRefs.insert(std::make_pair(bobj, jenv->NewGlobalRef(jobj)));
108  }
std::multimap< const Beatmup::Object *, jobject > javaRefs
Java global references representing dependencies of internal objects on external Java objects.
Definition: objectpool.h:36
jobject jobj

◆ removeJavaReference()

void BeatmupJavaObjectPool::removeJavaReference ( JNIEnv *  jenv,
const Beatmup::Object bobj 
)
inline

Removes a Java reference.

Definition at line 114 of file objectpool.h.

114  {
115  std::lock_guard<std::mutex> lock(access);
116  auto ref = javaRefs.find(bobj);
117  if (ref != javaRefs.end()) {
118  jenv->DeleteGlobalRef(ref->second);
119  javaRefs.erase(ref);
120  }
121  }

◆ removeAllJavaReferences()

void BeatmupJavaObjectPool::removeAllJavaReferences ( JNIEnv *  jenv,
const Beatmup::Object bobj 
)
inline

Removes all Java references to depending objects.

Definition at line 127 of file objectpool.h.

127  {
128  std::lock_guard<std::mutex> lock(access);
129  while (true) {
130  auto ref = javaRefs.find(bobj);
131  if (ref == javaRefs.end())
132  break;
133  jenv->DeleteGlobalRef(ref->second);
134  javaRefs.erase(ref);
135  }
136  }

◆ getJavaReference()

jobject BeatmupJavaObjectPool::getJavaReference ( const Beatmup::Object bobj) const
inline

Returns a reference to the Java object which a given object depends on.

Definition at line 142 of file objectpool.h.

142  {
143  return javaRefs.find(bobj)->second;
144  }

◆ throwToJava() [1/2]

static void BeatmupJavaObjectPool::throwToJava ( JNIEnv *  jenv,
const char *  exceptionClass,
const char *  message 
)
inlinestatic

Throws a specific exception.

Definition at line 156 of file objectpool.h.

156  {
157  jclass exClass = jenv->FindClass(exceptionClass);
158  jmethodID constructor = jenv->GetMethodID(exClass, "<init>", "(Ljava/lang/String;)V");
159  jobject exception = jenv->NewObject(exClass, constructor, jenv->NewStringUTF(message));
160  jenv->Throw((jthrowable) exception);
161  jenv->DeleteLocalRef(exClass);
162  }
JNIEnv jobject

◆ throwToJava() [2/2]

static void BeatmupJavaObjectPool::throwToJava ( JNIEnv *  jenv,
const char *  message 
)
inlinestatic

Throws a general exception.

Definition at line 168 of file objectpool.h.

168  {
169  throwToJava(jenv, "Beatmup/Exceptions/CoreException", message);
170  }
static void throwToJava(JNIEnv *jenv, const char *exceptionClass, const char *message)
Throws a specific exception.
Definition: objectpool.h:156

◆ rethrowToJava()

static void BeatmupJavaObjectPool::rethrowToJava ( JNIEnv *  jenv,
std::exception &  ex 
)
inlinestatic

Rethrows a core-caused exception.

Definition at line 176 of file objectpool.h.

176  {
177  throwToJava(jenv, ex.what());
178  }

Member Data Documentation

◆ javaRefs

std::multimap<const Beatmup::Object*, jobject> BeatmupJavaObjectPool::javaRefs
private

Java global references representing dependencies of internal objects on external Java objects.

Definition at line 36 of file objectpool.h.

◆ env

JNIEnv* BeatmupJavaObjectPool::env

Definition at line 39 of file objectpool.h.

◆ handleFieldId

jfieldID BeatmupJavaObjectPool::handleFieldId

Definition at line 40 of file objectpool.h.

◆ 

struct { ... } BeatmupJavaObjectPool::javaContext

◆ access

std::mutex BeatmupJavaObjectPool::access
private

structure access control

Definition at line 58 of file objectpool.h.

◆ INVALID_HANDLE

return BeatmupJavaObjectPool::INVALID_HANDLE = std::numeric_limits<jlong>::min()
static

Definition at line 61 of file objectpool.h.

◆ factory

JavaFactory BeatmupJavaObjectPool::factory

Definition at line 63 of file objectpool.h.


The documentation for this class was generated from the following files: