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 { ...@@ -8,7 +8,7 @@ namespace djinni_generated {
NativeTextboxListener::NativeTextboxListener() : djinni::JniInterfaceJavaExt<::textsort::TextboxListener, NativeTextboxListener>() {} 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) { void NativeTextboxListener::JavaProxy::JavaProxy::update(const ::textsort::ItemList & c_items) {
JNIEnv * const jniEnv = djinni::jniGetThreadEnv(); JNIEnv * const jniEnv = djinni::jniGetThreadEnv();
......
...@@ -18,15 +18,15 @@ public: ...@@ -18,15 +18,15 @@ public:
const djinni::GlobalRef<jclass> clazz { djinni::jniFindClass("com/dropbox/textsort/TextboxListener") }; 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") }; 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: public:
JavaProxy(jobject obj); JavaProxy(jobject obj);
virtual void update(const ::textsort::ItemList & items) override; virtual void update(const ::textsort::ItemList & items) override;
private: private:
using djinni::JniWrapperCacheEntry::getGlobalRef; using djinni::JavaProxyCacheEntry::getGlobalRef;
friend class djinni::JniInterfaceJavaExt<::textsort::TextboxListener, NativeTextboxListener>; friend class djinni::JniInterfaceJavaExt<::textsort::TextboxListener, NativeTextboxListener>;
friend class djinni::JniWrapperCache<JavaProxy>; friend class djinni::JavaProxyCache<JavaProxy>;
}; };
private: private:
......
...@@ -210,7 +210,7 @@ class JNIGenerator(spec: Spec) extends Generator(spec) { ...@@ -210,7 +210,7 @@ class JNIGenerator(spec: Spec) extends Generator(spec) {
} }
if (i.ext.java) { if (i.ext.java) {
w.wl 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.wlOutdent(s"public:")
w.wl(s"JavaProxy(jobject obj);") w.wl(s"JavaProxy(jobject obj);")
for (m <- i.methods) { for (m <- i.methods) {
...@@ -220,9 +220,9 @@ class JNIGenerator(spec: Spec) extends Generator(spec) { ...@@ -220,9 +220,9 @@ class JNIGenerator(spec: Spec) extends Generator(spec) {
} }
w.wl w.wl
w.wlOutdent(s"private:") 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::JniInterfaceJavaExt<$selfQ, $jniClassName>;")
w.wl(s"friend class djinni::JniWrapperCache<JavaProxy>;") w.wl(s"friend class djinni::JavaProxyCache<JavaProxy>;")
} }
} }
w.wl w.wl
...@@ -239,7 +239,7 @@ class JNIGenerator(spec: Spec) extends Generator(spec) { ...@@ -239,7 +239,7 @@ class JNIGenerator(spec: Spec) extends Generator(spec) {
w.wl w.wl
if (i.ext.java) { if (i.ext.java) {
writeJniTypeParams(w, typeParams) 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) { for (m <- i.methods) {
w.wl w.wl
......
...@@ -417,33 +417,33 @@ void jniSetPendingFromCurrent(JNIEnv * env, const char * /*ctx*/) noexcept { ...@@ -417,33 +417,33 @@ void jniSetPendingFromCurrent(JNIEnv * env, const char * /*ctx*/) noexcept {
} }
} }
struct JniWrapperCacheState { struct JavaProxyCacheState {
std::mutex mtx; std::mutex mtx;
std::unordered_map<jobject, std::weak_ptr<void>, JavaIdentityHash, JavaIdentityEquals> m; std::unordered_map<jobject, std::weak_ptr<void>, JavaIdentityHash, JavaIdentityEquals> m;
int counter = 0; int counter = 0;
static JniWrapperCacheState & get() { static JavaProxyCacheState & get() {
static JniWrapperCacheState st; static JavaProxyCacheState st;
return st; return st;
} }
}; };
JniWrapperCacheEntry::JniWrapperCacheEntry(jobject localRef, JNIEnv * env) JavaProxyCacheEntry::JavaProxyCacheEntry(jobject localRef, JNIEnv * env)
: m_globalRef(env, localRef) { : m_globalRef(env, localRef) {
DJINNI_ASSERT(m_globalRef, env); DJINNI_ASSERT(m_globalRef, env);
} }
JniWrapperCacheEntry::JniWrapperCacheEntry(jobject localRef) JavaProxyCacheEntry::JavaProxyCacheEntry(jobject localRef)
: JniWrapperCacheEntry(localRef, jniGetThreadEnv()) {} : JavaProxyCacheEntry(localRef, jniGetThreadEnv()) {}
JniWrapperCacheEntry::~JniWrapperCacheEntry() noexcept { JavaProxyCacheEntry::~JavaProxyCacheEntry() noexcept {
JniWrapperCacheState & st = JniWrapperCacheState::get(); JavaProxyCacheState & st = JavaProxyCacheState::get();
const std::lock_guard<std::mutex> lock(st.mtx); const std::lock_guard<std::mutex> lock(st.mtx);
st.m.erase(m_globalRef.get()); st.m.erase(m_globalRef.get());
} }
std::shared_ptr<void> jniWrapperCacheLookup(jobject obj, std::pair<std::shared_ptr<void>, jobject>(*factory)(jobject)) { std::shared_ptr<void> javaProxyCacheLookup(jobject obj, std::pair<std::shared_ptr<void>, jobject>(*factory)(jobject)) {
JniWrapperCacheState & st = JniWrapperCacheState::get(); JavaProxyCacheState & st = JavaProxyCacheState::get();
const std::lock_guard<std::mutex> lock(st.mtx); const std::lock_guard<std::mutex> lock(st.mtx);
const auto it = st.m.find(obj); const auto it = st.m.find(obj);
......
...@@ -202,7 +202,7 @@ jfieldID jniGetFieldID(jclass clazz, const char * name, const char * sig); ...@@ -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 * 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 * 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 * 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 * 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. * 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); ...@@ -212,11 +212,11 @@ jfieldID jniGetFieldID(jclass clazz, const char * name, const char * sig);
* _____________ | | | | | * _____________ | | | | |
* | | | | JniImplFooListener | <=========== | Foo | * | | | | JniImplFooListener | <=========== | Foo |
* | FooListener | <============ | : public FooListener, | shared_ptr |___________| * | FooListener | <============ | : public FooListener, | shared_ptr |___________|
* |_____________| GlobalRef | JniWrapperCacheEntry | * |_____________| GlobalRef | JavaProxyCacheEntry |
* | |________________________| * | |________________________|
* | ^ ______________________ * | ^ ______________________
* | \ | | * | \ | |
* | - - - - - - | JniWrapperCache | * | - - - - - - | JavaProxyCache |
* | weak_ptr | <JniImplFooListener> | * | weak_ptr | <JniImplFooListener> |
* | |______________________| * | |______________________|
* *
...@@ -238,32 +238,32 @@ jfieldID jniGetFieldID(jclass clazz, const char * name, const char * sig); ...@@ -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 * a shared_ptr to the object (JniImplFooListener, in the diagram above), as well as the
* jobject *global* ref contained inside. * 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)); jobject>(*factory)(jobject));
class JniWrapperCacheEntry { class JavaProxyCacheEntry {
public: public:
jobject getGlobalRef() { jobject getGlobalRef() {
return m_globalRef.get(); return m_globalRef.get();
} }
protected: protected:
JniWrapperCacheEntry(jobject localRef, JNIEnv * env); // env used only for construction JavaProxyCacheEntry(jobject localRef, JNIEnv * env); // env used only for construction
JniWrapperCacheEntry(jobject localRef); JavaProxyCacheEntry(jobject localRef);
virtual ~JniWrapperCacheEntry() noexcept; virtual ~JavaProxyCacheEntry() noexcept;
JniWrapperCacheEntry(const JniWrapperCacheEntry & other) = delete; JavaProxyCacheEntry(const JavaProxyCacheEntry & other) = delete;
JniWrapperCacheEntry & operator=(const JniWrapperCacheEntry & other) = delete; JavaProxyCacheEntry & operator=(const JavaProxyCacheEntry & other) = delete;
private: private:
const GlobalRef<jobject> m_globalRef; const GlobalRef<jobject> m_globalRef;
}; };
template <class T> template <class T>
class JniWrapperCache { class JavaProxyCache {
public: public:
using Entry = JniWrapperCacheEntry; using Entry = JavaProxyCacheEntry;
static std::pair<std::shared_ptr<void>, jobject> factory(jobject obj) { static std::pair<std::shared_ptr<void>, jobject> factory(jobject obj) {
std::shared_ptr<T> ret = std::make_shared<T>(obj); std::shared_ptr<T> ret = std::make_shared<T>(obj);
...@@ -275,10 +275,10 @@ public: ...@@ -275,10 +275,10 @@ public:
* construct a new one with obj, save it, and return it. * construct a new one with obj, save it, and return it.
*/ */
static std::shared_ptr<T> get(jobject obj) { static std::shared_ptr<T> get(jobject obj) {
static_assert(std::is_base_of<JniWrapperCacheEntry, T>::value, static_assert(std::is_base_of<JavaProxyCacheEntry, T>::value,
"JniWrapperCache can only be used with T if T derives from Entry<T>"); "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: ...@@ -324,7 +324,7 @@ public:
if (j == 0) { if (j == 0) {
return nullptr; return nullptr;
} }
return JniWrapperCache<typename Self::JavaProxy>::get(j); return JavaProxyCache<typename Self::JavaProxy>::get(j);
} }
}; };
......
...@@ -9,7 +9,7 @@ namespace djinni_generated { ...@@ -9,7 +9,7 @@ namespace djinni_generated {
NativeClientInterface::NativeClientInterface() : djinni::JniInterfaceJavaExt<ClientInterface, NativeClientInterface>() {} 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) { ClientReturnedRecord NativeClientInterface::JavaProxy::JavaProxy::get_record(const std::string & c_utf8string) {
JNIEnv * const jniEnv = djinni::jniGetThreadEnv(); JNIEnv * const jniEnv = djinni::jniGetThreadEnv();
......
...@@ -18,15 +18,15 @@ public: ...@@ -18,15 +18,15 @@ public:
const djinni::GlobalRef<jclass> clazz { djinni::jniFindClass("com/dropbox/djinni/test/ClientInterface") }; 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;") }; 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: public:
JavaProxy(jobject obj); JavaProxy(jobject obj);
virtual ClientReturnedRecord get_record(const std::string & utf8string) override; virtual ClientReturnedRecord get_record(const std::string & utf8string) override;
private: private:
using djinni::JniWrapperCacheEntry::getGlobalRef; using djinni::JavaProxyCacheEntry::getGlobalRef;
friend class djinni::JniInterfaceJavaExt<ClientInterface, NativeClientInterface>; friend class djinni::JniInterfaceJavaExt<ClientInterface, NativeClientInterface>;
friend class djinni::JniWrapperCache<JavaProxy>; friend class djinni::JavaProxyCache<JavaProxy>;
}; };
private: 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