Commit ed6ef3c5 authored by Jacob Potter's avatar Jacob Potter

Naming fixes: switch towards Proxy instead of Wrapper

parent 15002676
......@@ -8,7 +8,7 @@ namespace djinni_generated {
NativeTextboxListener::NativeTextboxListener() : djinni::JniInterfaceJavaExt<::textsort::TextboxListener, NativeTextboxListener>() {}
NativeTextboxListener::JavaProxy::JavaProxy(jobject obj) : JniWrapperCacheEntry(obj) {}
NativeTextboxListener::JavaProxy::JavaProxy(jobject obj) : JavaProxyCacheEntry(obj) {}
void NativeTextboxListener::JavaProxy::JavaProxy::update(const ::textsort::ItemList & c_items) {
JNIEnv * const jniEnv = djinni::jniGetThreadEnv();
......
......@@ -18,15 +18,15 @@ public:
const djinni::GlobalRef<jclass> clazz { djinni::jniFindClass("com/dropbox/textsort/TextboxListener") };
const jmethodID method_update { djinni::jniGetMethodID(clazz.get(), "update", "(Lcom/dropbox/textsort/ItemList;)V") };
class JavaProxy final : djinni::JniWrapperCacheEntry, public ::textsort::TextboxListener {
class JavaProxy final : djinni::JavaProxyCacheEntry, public ::textsort::TextboxListener {
public:
JavaProxy(jobject obj);
virtual void update(const ::textsort::ItemList & items) override;
private:
using djinni::JniWrapperCacheEntry::getGlobalRef;
using djinni::JavaProxyCacheEntry::getGlobalRef;
friend class djinni::JniInterfaceJavaExt<::textsort::TextboxListener, NativeTextboxListener>;
friend class djinni::JniWrapperCache<JavaProxy>;
friend class djinni::JavaProxyCache<JavaProxy>;
};
private:
......
......@@ -210,7 +210,7 @@ class JNIGenerator(spec: Spec) extends Generator(spec) {
}
if (i.ext.java) {
w.wl
w.w(s"class JavaProxy final : djinni::JniWrapperCacheEntry, public ${withNs(spec.cppNamespace, idCpp.ty(ident))}").bracedSemi {
w.w(s"class JavaProxy final : djinni::JavaProxyCacheEntry, public ${withNs(spec.cppNamespace, idCpp.ty(ident))}").bracedSemi {
w.wlOutdent(s"public:")
w.wl(s"JavaProxy(jobject obj);")
for (m <- i.methods) {
......@@ -220,9 +220,9 @@ class JNIGenerator(spec: Spec) extends Generator(spec) {
}
w.wl
w.wlOutdent(s"private:")
w.wl(s"using djinni::JniWrapperCacheEntry::getGlobalRef;")
w.wl(s"using djinni::JavaProxyCacheEntry::getGlobalRef;")
w.wl(s"friend class djinni::JniInterfaceJavaExt<$selfQ, $jniClassName>;")
w.wl(s"friend class djinni::JniWrapperCache<JavaProxy>;")
w.wl(s"friend class djinni::JavaProxyCache<JavaProxy>;")
}
}
w.wl
......@@ -239,7 +239,7 @@ class JNIGenerator(spec: Spec) extends Generator(spec) {
w.wl
if (i.ext.java) {
writeJniTypeParams(w, typeParams)
w.wl(s"$jniClassName::JavaProxy::JavaProxy(jobject obj) : JniWrapperCacheEntry(obj) {}")
w.wl(s"$jniClassName::JavaProxy::JavaProxy(jobject obj) : JavaProxyCacheEntry(obj) {}")
for (m <- i.methods) {
w.wl
......
......@@ -417,33 +417,33 @@ void jniSetPendingFromCurrent(JNIEnv * env, const char * /*ctx*/) noexcept {
}
}
struct JniWrapperCacheState {
struct JavaProxyCacheState {
std::mutex mtx;
std::unordered_map<jobject, std::weak_ptr<void>, JavaIdentityHash, JavaIdentityEquals> m;
int counter = 0;
static JniWrapperCacheState & get() {
static JniWrapperCacheState st;
static JavaProxyCacheState & get() {
static JavaProxyCacheState st;
return st;
}
};
JniWrapperCacheEntry::JniWrapperCacheEntry(jobject localRef, JNIEnv * env)
JavaProxyCacheEntry::JavaProxyCacheEntry(jobject localRef, JNIEnv * env)
: m_globalRef(env, localRef) {
DJINNI_ASSERT(m_globalRef, env);
}
JniWrapperCacheEntry::JniWrapperCacheEntry(jobject localRef)
: JniWrapperCacheEntry(localRef, jniGetThreadEnv()) {}
JavaProxyCacheEntry::JavaProxyCacheEntry(jobject localRef)
: JavaProxyCacheEntry(localRef, jniGetThreadEnv()) {}
JniWrapperCacheEntry::~JniWrapperCacheEntry() noexcept {
JniWrapperCacheState & st = JniWrapperCacheState::get();
JavaProxyCacheEntry::~JavaProxyCacheEntry() noexcept {
JavaProxyCacheState & st = JavaProxyCacheState::get();
const std::lock_guard<std::mutex> lock(st.mtx);
st.m.erase(m_globalRef.get());
}
std::shared_ptr<void> jniWrapperCacheLookup(jobject obj, std::pair<std::shared_ptr<void>, jobject>(*factory)(jobject)) {
JniWrapperCacheState & st = JniWrapperCacheState::get();
std::shared_ptr<void> javaProxyCacheLookup(jobject obj, std::pair<std::shared_ptr<void>, jobject>(*factory)(jobject)) {
JavaProxyCacheState & st = JavaProxyCacheState::get();
const std::lock_guard<std::mutex> lock(st.mtx);
const auto it = st.m.find(obj);
......
......@@ -202,7 +202,7 @@ jfieldID jniGetFieldID(jclass clazz, const char * name, const char * sig);
*
* This is used for automatically wrapping a Java object that exposes some interface
* with a C++ object that calls back into the JVM, such as a listener. Calling
* JniWrapperCache<T>::get(jobj, ...) the first time will construct a T and return a
* JavaProxyCache<T>::get(jobj, ...) the first time will construct a T and return a
* shared_ptr to it, and also save a weak_ptr to the new object internally. The constructed
* T contains a strong GlobalRef to jobj. As long as something in C++ maintains a strong
* reference to the wrapper, future calls to get(jobj) will return the *same* wrapper object.
......@@ -212,11 +212,11 @@ jfieldID jniGetFieldID(jclass clazz, const char * name, const char * sig);
* _____________ | | | | |
* | | | | JniImplFooListener | <=========== | Foo |
* | FooListener | <============ | : public FooListener, | shared_ptr |___________|
* |_____________| GlobalRef | JniWrapperCacheEntry |
* |_____________| GlobalRef | JavaProxyCacheEntry |
* | |________________________|
* | ^ ______________________
* | \ | |
* | - - - - - - | JniWrapperCache |
* | - - - - - - | JavaProxyCache |
* | weak_ptr | <JniImplFooListener> |
* | |______________________|
*
......@@ -238,32 +238,32 @@ jfieldID jniGetFieldID(jclass clazz, const char * name, const char * sig);
* a shared_ptr to the object (JniImplFooListener, in the diagram above), as well as the
* jobject *global* ref contained inside.
*/
std::shared_ptr<void> jniWrapperCacheLookup(jobject obj, std::pair<std::shared_ptr<void>,
std::shared_ptr<void> javaProxyCacheLookup(jobject obj, std::pair<std::shared_ptr<void>,
jobject>(*factory)(jobject));
class JniWrapperCacheEntry {
class JavaProxyCacheEntry {
public:
jobject getGlobalRef() {
return m_globalRef.get();
}
protected:
JniWrapperCacheEntry(jobject localRef, JNIEnv * env); // env used only for construction
JniWrapperCacheEntry(jobject localRef);
JavaProxyCacheEntry(jobject localRef, JNIEnv * env); // env used only for construction
JavaProxyCacheEntry(jobject localRef);
virtual ~JniWrapperCacheEntry() noexcept;
virtual ~JavaProxyCacheEntry() noexcept;
JniWrapperCacheEntry(const JniWrapperCacheEntry & other) = delete;
JniWrapperCacheEntry & operator=(const JniWrapperCacheEntry & other) = delete;
JavaProxyCacheEntry(const JavaProxyCacheEntry & other) = delete;
JavaProxyCacheEntry & operator=(const JavaProxyCacheEntry & other) = delete;
private:
const GlobalRef<jobject> m_globalRef;
};
template <class T>
class JniWrapperCache {
class JavaProxyCache {
public:
using Entry = JniWrapperCacheEntry;
using Entry = JavaProxyCacheEntry;
static std::pair<std::shared_ptr<void>, jobject> factory(jobject obj) {
std::shared_ptr<T> ret = std::make_shared<T>(obj);
......@@ -275,10 +275,10 @@ public:
* construct a new one with obj, save it, and return it.
*/
static std::shared_ptr<T> get(jobject obj) {
static_assert(std::is_base_of<JniWrapperCacheEntry, T>::value,
"JniWrapperCache can only be used with T if T derives from Entry<T>");
static_assert(std::is_base_of<JavaProxyCacheEntry, T>::value,
"JavaProxyCache can only be used with T if T derives from Entry<T>");
return std::static_pointer_cast<T>(jniWrapperCacheLookup(obj, &factory));
return std::static_pointer_cast<T>(javaProxyCacheLookup(obj, &factory));
}
};
......@@ -324,7 +324,7 @@ public:
if (j == 0) {
return nullptr;
}
return JniWrapperCache<typename Self::JavaProxy>::get(j);
return JavaProxyCache<typename Self::JavaProxy>::get(j);
}
};
......
......@@ -9,7 +9,7 @@ namespace djinni_generated {
NativeClientInterface::NativeClientInterface() : djinni::JniInterfaceJavaExt<ClientInterface, NativeClientInterface>() {}
NativeClientInterface::JavaProxy::JavaProxy(jobject obj) : JniWrapperCacheEntry(obj) {}
NativeClientInterface::JavaProxy::JavaProxy(jobject obj) : JavaProxyCacheEntry(obj) {}
ClientReturnedRecord NativeClientInterface::JavaProxy::JavaProxy::get_record(const std::string & c_utf8string) {
JNIEnv * const jniEnv = djinni::jniGetThreadEnv();
......
......@@ -18,15 +18,15 @@ public:
const djinni::GlobalRef<jclass> clazz { djinni::jniFindClass("com/dropbox/djinni/test/ClientInterface") };
const jmethodID method_getRecord { djinni::jniGetMethodID(clazz.get(), "getRecord", "(Ljava/lang/String;)Lcom/dropbox/djinni/test/ClientReturnedRecord;") };
class JavaProxy final : djinni::JniWrapperCacheEntry, public ClientInterface {
class JavaProxy final : djinni::JavaProxyCacheEntry, public ClientInterface {
public:
JavaProxy(jobject obj);
virtual ClientReturnedRecord get_record(const std::string & utf8string) override;
private:
using djinni::JniWrapperCacheEntry::getGlobalRef;
using djinni::JavaProxyCacheEntry::getGlobalRef;
friend class djinni::JniInterfaceJavaExt<ClientInterface, NativeClientInterface>;
friend class djinni::JniWrapperCache<JavaProxy>;
friend class djinni::JavaProxyCache<JavaProxy>;
};
private:
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment