From 2df00647d599a4af75bf0727418a59e9601c51ec Mon Sep 17 00:00:00 2001 From: Peter Johnson Date: Wed, 14 Dec 2016 23:26:41 -0800 Subject: [PATCH] Make JLocal and JArrayRef movable but non-copyable. --- wpiutil/include/support/jni_util.h | 32 ++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/wpiutil/include/support/jni_util.h b/wpiutil/include/support/jni_util.h index a330f7abd9..a7c99bf4c7 100644 --- a/wpiutil/include/support/jni_util.h +++ b/wpiutil/include/support/jni_util.h @@ -60,6 +60,17 @@ template class JLocal { public: JLocal(JNIEnv *env, T obj) : m_env(env), m_obj(obj) {} + JLocal(const JLocal&) = delete; + JLocal(JLocal&& oth) : m_env(oth.m_env), m_obj(oth.m_obj) { + oth.m_obj = nullptr; + } + JLocal& operator=(const JLocal&) = delete; + JLocal& operator=(JLocal&& oth) { + m_env = oth.m_env; + m_obj = oth.m_obj; + oth.m_obj = nullptr; + return *this; + } ~JLocal() { if (m_obj) m_env->DeleteLocalRef(m_obj); } @@ -131,6 +142,27 @@ class JArrayRefBase : public JArrayRefInner, T> { return llvm::ArrayRef{this->m_elements, this->m_size}; } + JArrayRefBase(const JArrayRefBase&) = delete; + JArrayRefBase& operator=(const JArrayRefBase&) = delete; + + JArrayRefBase(JArrayRefBase&& oth) + : m_env(oth.m_env), + m_jarr(oth.m_jarr), + m_size(oth.m_size), + m_elements(oth.m_elements) { + oth.m_jarr = nullptr; + oth.m_elements = nullptr; + } + + JArrayRefBase& operator=(JArrayRefBase&& oth) { + this->m_env = oth.m_env; + this->m_jarr = oth.m_jarr; + this->m_size = oth.m_size; + this->m_elements = oth.m_elements; + oth.m_jarr = nullptr; + oth.m_elements = nullptr; + } + protected: JArrayRefBase(JNIEnv *env, T* elements, size_t size) { this->m_env = env;