Bug 1287946 - Improve usage of StringParam in PrefsHelper; r=me

PrefsHelper uses Maybe<> as a replacement for a ternary expression, in
order to work around jni::StringParam's lack of copy constructor.
However, we can add a move constructor to StringParam, which lets us use
it in a ternary expression, and avoid the awkwardness of Maybe<>.
This commit is contained in:
Jim Chen 2016-07-21 13:49:05 -04:00
Родитель ce45a595ab
Коммит a7794572ee
2 изменённых файлов: 20 добавлений и 28 удалений

Просмотреть файл

@ -12,10 +12,8 @@
#include "nsCOMPtr.h"
#include "nsVariant.h"
#include "mozilla/Maybe.h"
#include "mozilla/Preferences.h"
#include "mozilla/Services.h"
#include "mozilla/UniquePtr.h"
namespace mozilla {
@ -68,20 +66,17 @@ class PrefsHelper
return false;
}
Maybe<jni::StringParam> jstrVal;
jstrVal.emplace(nullptr);
if (type == java::PrefsHelper::PREF_STRING) {
jstrVal.reset();
jstrVal.emplace(strVal, aPrefName.Env());
}
jni::StringParam jstrVal(type == java::PrefsHelper::PREF_STRING ?
jni::StringParam(strVal, aPrefName.Env()) :
jni::StringParam(nullptr));
if (aPrefHandler) {
java::PrefsHelper::CallPrefHandler(
aPrefHandler, type, aPrefName,
boolVal, intVal, jstrVal.ref());
boolVal, intVal, jstrVal);
} else {
java::PrefsHelper::OnPrefChange(
aPrefName, type, boolVal, intVal, jstrVal.ref());
aPrefName, type, boolVal, intVal, jstrVal);
}
return true;
}
@ -194,16 +189,11 @@ public:
continue;
}
Maybe<jni::StringParam> jstrVal;
jstrVal.emplace(nullptr);
if (type == java::PrefsHelper::PREF_STRING) {
jstrVal.reset();
jstrVal.emplace(strVal, aCls.Env());
}
java::PrefsHelper::CallPrefHandler(
aPrefHandler, type, nameStr,
boolVal, intVal, jstrVal.ref());
aPrefHandler, type, nameStr, boolVal, intVal,
jni::StringParam(type == java::PrefsHelper::PREF_STRING ?
jni::StringParam(strVal, aCls.Env()) :
jni::StringParam(nullptr)));
}
java::PrefsHelper::CallPrefHandler(
@ -323,15 +313,10 @@ public:
return;
}
Maybe<jni::StringParam> jstrVal;
jstrVal.emplace(nullptr);
if (type == java::PrefsHelper::PREF_STRING) {
jstrVal.reset();
jstrVal.emplace(strVal);
}
java::PrefsHelper::OnPrefChange(
name, type, boolVal, intVal, jstrVal.ref());
name, type, boolVal, intVal,
jni::StringParam(type == java::PrefsHelper::PREF_STRING ?
jni::StringParam(strVal) : jni::StringParam(nullptr)));
}
};

Просмотреть файл

@ -680,9 +680,16 @@ public:
, mEnv(env)
{}
StringParam(StringParam&& other)
: Ref(other.Get())
, mEnv(other.mEnv)
{
other.mInstance = nullptr;
}
~StringParam()
{
if (mEnv) {
if (mEnv && Get()) {
mEnv->DeleteLocalRef(Get());
}
}