Loading...
Searching...
No Matches
ownership.hpp
Go to the documentation of this file.
#pragma once
#include <jni/types.hpp>
#include <jni/wrapping.hpp>
#include <jni/typed_methods.hpp>
namespace jni
{
struct LocalFrameDeleter
{
void operator()(JNIEnv* env) const
{
if (env)
{
env->PopLocalFrame(nullptr);
}
}
};
using UniqueLocalFrame = std::unique_ptr< JNIEnv, LocalFrameDeleter >;
using RefDeletionMethod = void (JNIEnv::*)(::jobject);
template < RefDeletionMethod DeleteRef >
class DefaultRefDeleter
{
private:
JNIEnv* env = nullptr;
public:
DefaultRefDeleter() = default;
DefaultRefDeleter(JNIEnv& e) : env(&e) {}
void operator()(jobject* p) const
{
if (p)
{
assert(env);
(env->*DeleteRef)(Unwrap(p));
}
}
};
template < class T, template < RefDeletionMethod > class Deleter = DefaultRefDeleter >
using UniqueGlobalRef = std::unique_ptr< T, Deleter<&JNIEnv::DeleteGlobalRef> >;
template < class T, template < RefDeletionMethod > class Deleter = DefaultRefDeleter >
using UniqueWeakGlobalRef = std::unique_ptr< T, Deleter<&JNIEnv::DeleteWeakGlobalRef> >;
// Not parameterized by Deleter because local references should be short-lived enough
// that DefaultRefDeleter suffices in all cases.
template < class T >
using UniqueLocalRef = std::unique_ptr< T, DefaultRefDeleter<&JNIEnv::DeleteLocalRef> >;
class StringCharsDeleter
{
private:
JNIEnv* env = nullptr;
jstring* string = nullptr;
public:
StringCharsDeleter() = default;
StringCharsDeleter(JNIEnv& e, jstring& s) : env(&e), string(&s) {}
void operator()(const char16_t* p) const
{
if (p)
{
assert(env);
assert(string);
env->ReleaseStringChars(Unwrap(string), Unwrap(p));
}
}
};
using UniqueStringChars = std::unique_ptr< const char16_t, StringCharsDeleter >;
class StringUTFCharsDeleter
{
private:
JNIEnv* env = nullptr;
jstring* string = nullptr;
public:
StringUTFCharsDeleter() = default;
StringUTFCharsDeleter(JNIEnv& e, jstring& s) : env(&e), string(&s) {}
void operator()(const char* p) const
{
if (p)
{
assert(env);
assert(string);
env->ReleaseStringUTFChars(Unwrap(string), p);
}
}
};
using UniqueStringUTFChars = std::unique_ptr< const char, StringUTFCharsDeleter >;
class StringCriticalDeleter
{
private:
JNIEnv* env = nullptr;
jstring* string = nullptr;
public:
StringCriticalDeleter() = default;
StringCriticalDeleter(JNIEnv& e, jstring& s) : env(&e), string(&s) {}
void operator()(const char16_t* p) const
{
if (p)
{
assert(env);
assert(string);
env->ReleaseStringCritical(Unwrap(string), Unwrap(p));
}
}
};
using UniqueStringCritical = std::unique_ptr< const char16_t, StringCriticalDeleter >;
template < class E >
class ArrayElementsDeleter
{
private:
JNIEnv* env = nullptr;
jarray<E>* array = nullptr;
public:
ArrayElementsDeleter() = default;
ArrayElementsDeleter(JNIEnv& e, jarray<E>& a) : env(&e), array(&a) {}
void operator()(E* p) const
{
if (p)
{
assert(env);
assert(array);
(env->*(TypedMethods<E>::ReleaseArrayElements))(Unwrap(array), p, JNI_ABORT);
}
}
};
template < class E >
using UniqueArrayElements = std::unique_ptr< E, ArrayElementsDeleter<E> >;
template < class E >
class PrimitiveArrayCriticalDeleter
{
private:
JNIEnv* env = nullptr;
jarray<E>* array = nullptr;
public:
PrimitiveArrayCriticalDeleter() = default;
PrimitiveArrayCriticalDeleter(JNIEnv& e, jarray<E>& a) : env(&e), array(&a) {}
void operator()(void* p) const
{
if (p)
{
assert(env);
assert(array);
env->ReleasePrimitiveArrayCritical(Unwrap(array), p, JNI_ABORT);
}
}
};
template < class E >
using UniquePrimitiveArrayCritical = std::unique_ptr< void, PrimitiveArrayCriticalDeleter<E> >;
class MonitorDeleter
{
private:
JNIEnv* env = nullptr;
public:
MonitorDeleter() = default;
MonitorDeleter(JNIEnv& e) : env(&e) {}
void operator()(jobject* p) const
{
if (p)
{
assert(env);
env->MonitorExit(Unwrap(p));
}
}
};
using UniqueMonitor = std::unique_ptr< jobject, MonitorDeleter >;
class JNIEnvDeleter
{
private:
JavaVM* vm = nullptr;
public:
JNIEnvDeleter() = default;
JNIEnvDeleter(JavaVM& v) : vm(&v) {}
void operator()(JNIEnv* p) const
{
if (p)
{
assert(vm);
vm->DetachCurrentThread();
}
}
};
using UniqueEnv = std::unique_ptr< JNIEnv, JNIEnvDeleter >;
}
jni::ArrayElementsDeleter::ArrayElementsDeleter
ArrayElementsDeleter(JNIEnv &e, jarray< E > &a)
Definition ownership.hpp:139
jni::ArrayElementsDeleter::operator()
void operator()(E *p) const
Definition ownership.hpp:141
jni::ArrayElementsDeleter::ArrayElementsDeleter
ArrayElementsDeleter()=default
jni::DefaultRefDeleter
Definition ownership.hpp:27
jni::DefaultRefDeleter::operator()
void operator()(jobject *p) const
Definition ownership.hpp:35
jni::DefaultRefDeleter::DefaultRefDeleter
DefaultRefDeleter(JNIEnv &e)
Definition ownership.hpp:33
jni::DefaultRefDeleter::DefaultRefDeleter
DefaultRefDeleter()=default
jni::JNIEnvDeleter::JNIEnvDeleter
JNIEnvDeleter()=default
jni::JNIEnvDeleter::operator()
void operator()(JNIEnv *p) const
Definition ownership.hpp:213
jni::JNIEnvDeleter::JNIEnvDeleter
JNIEnvDeleter(JavaVM &v)
Definition ownership.hpp:211
jni::MonitorDeleter::MonitorDeleter
MonitorDeleter(JNIEnv &e)
Definition ownership.hpp:189
jni::MonitorDeleter::MonitorDeleter
MonitorDeleter()=default
jni::MonitorDeleter::operator()
void operator()(jobject *p) const
Definition ownership.hpp:191
jni::PrimitiveArrayCriticalDeleter::operator()
void operator()(void *p) const
Definition ownership.hpp:167
jni::PrimitiveArrayCriticalDeleter::PrimitiveArrayCriticalDeleter
PrimitiveArrayCriticalDeleter()=default
jni::PrimitiveArrayCriticalDeleter::PrimitiveArrayCriticalDeleter
PrimitiveArrayCriticalDeleter(JNIEnv &e, jarray< E > &a)
Definition ownership.hpp:165
jni::StringCharsDeleter::operator()
void operator()(const char16_t *p) const
Definition ownership.hpp:68
jni::StringCharsDeleter::StringCharsDeleter
StringCharsDeleter(JNIEnv &e, jstring &s)
Definition ownership.hpp:66
jni::StringCharsDeleter::StringCharsDeleter
StringCharsDeleter()=default
jni::StringCriticalDeleter::StringCriticalDeleter
StringCriticalDeleter(JNIEnv &e, jstring &s)
Definition ownership.hpp:114
jni::StringCriticalDeleter::StringCriticalDeleter
StringCriticalDeleter()=default
jni::StringCriticalDeleter::operator()
void operator()(const char16_t *p) const
Definition ownership.hpp:116
jni::StringUTFCharsDeleter::StringUTFCharsDeleter
StringUTFCharsDeleter()=default
jni::StringUTFCharsDeleter::StringUTFCharsDeleter
StringUTFCharsDeleter(JNIEnv &e, jstring &s)
Definition ownership.hpp:90
jni::StringUTFCharsDeleter::operator()
void operator()(const char *p) const
Definition ownership.hpp:92
jni
Definition advanced_ownership.hpp:6
jni::UniqueStringUTFChars
std::unique_ptr< const char, StringUTFCharsDeleter > UniqueStringUTFChars
Definition ownership.hpp:103
jni::UniqueLocalRef
std::unique_ptr< T, DefaultRefDeleter<&JNIEnv::DeleteLocalRef > > UniqueLocalRef
Definition ownership.hpp:55
jni::RefDeletionMethod
void(JNIEnv::*)(::jobject) RefDeletionMethod
Definition ownership.hpp:23
jni::UniqueStringCritical
std::unique_ptr< const char16_t, StringCriticalDeleter > UniqueStringCritical
Definition ownership.hpp:127
jni::UniqueArrayElements
std::unique_ptr< E, ArrayElementsDeleter< E > > UniqueArrayElements
Definition ownership.hpp:153
jni::UniqueMonitor
std::unique_ptr< jobject, MonitorDeleter > UniqueMonitor
Definition ownership.hpp:201
jni::UniqueWeakGlobalRef
std::unique_ptr< T, Deleter<&JNIEnv::DeleteWeakGlobalRef > > UniqueWeakGlobalRef
Definition ownership.hpp:50
jni::Unwrap
auto Unwrap(W &&w)
Definition wrapping.hpp:22
jni::UniqueLocalFrame
std::unique_ptr< JNIEnv, LocalFrameDeleter > UniqueLocalFrame
Definition ownership.hpp:20
jni::UniqueStringChars
std::unique_ptr< const char16_t, StringCharsDeleter > UniqueStringChars
Definition ownership.hpp:79
jni::UniquePrimitiveArrayCritical
std::unique_ptr< void, PrimitiveArrayCriticalDeleter< E > > UniquePrimitiveArrayCritical
Definition ownership.hpp:179
jni::UniqueEnv
std::unique_ptr< JNIEnv, JNIEnvDeleter > UniqueEnv
Definition ownership.hpp:223
jni::UniqueGlobalRef
std::unique_ptr< T, Deleter<&JNIEnv::DeleteGlobalRef > > UniqueGlobalRef
Definition ownership.hpp:47
jni::LocalFrameDeleter
Definition ownership.hpp:10
jni::LocalFrameDeleter::operator()
void operator()(JNIEnv *env) const
Definition ownership.hpp:11
jni::TypedMethods
Definition typed_methods.hpp:7
jni::jarray
Definition types.hpp:43
jni::jobject
Definition types.hpp:31
jni::jstring
Definition types.hpp:39
typed_methods.hpp
types.hpp
wrapping.hpp