java/Ref: allow LocalRef to be nullable

Makes using the Java glue classes simpler to use, at the cost of very
little overhead.
This commit is contained in:
Max Kellermann 2019-04-11 11:32:07 +02:00
parent 791245dec2
commit 1d49f1108f

View File

@ -47,13 +47,13 @@ namespace Java {
public: public:
/** /**
* The local reference is obtained by the caller. * The local reference is obtained by the caller. May
* be nullptr.
*/ */
LocalRef(JNIEnv *_env, T _value) noexcept LocalRef(JNIEnv *_env, T _value) noexcept
:env(_env), value(_value) :env(_env), value(_value)
{ {
assert(env != nullptr); assert(env != nullptr);
assert(value != nullptr);
} }
~LocalRef() noexcept { ~LocalRef() noexcept {
@ -67,6 +67,10 @@ namespace Java {
return env; return env;
} }
operator bool() const noexcept {
return value != nullptr;
}
T Get() const noexcept { T Get() const noexcept {
return value; return value;
} }