Bug 1685679 - Add do_Init function to allow assignment to InitializedOnce variables. r=janv

Differential Revision: https://phabricator.services.mozilla.com/D101143
This commit is contained in:
Simon Giesecke 2021-01-15 11:23:41 +00:00
Родитель 1575347021
Коммит 55236f5ccb
2 изменённых файлов: 44 добавлений и 0 удалений

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

@ -50,6 +50,8 @@ class InitializedOnce final {
using MaybeType = Maybe<std::remove_const_t<T>>;
public:
using ValueType = T;
template <typename Dummy = void>
explicit constexpr InitializedOnce(
std::enable_if_t<InitWhenVal == InitWhen::LazyAllowed, Dummy>* =
@ -135,6 +137,29 @@ class InitializedOnce final {
bool mWasReset = false;
#endif
};
template <typename T, InitWhen InitWhenVal, DestroyWhen DestroyWhenVal,
template <typename> class ValueCheckPolicy>
class LazyInitializer {
public:
explicit LazyInitializer(InitializedOnce<T, InitWhenVal, DestroyWhenVal,
ValueCheckPolicy>& aLazyInitialized)
: mLazyInitialized{aLazyInitialized} {}
template <typename U>
LazyInitializer& operator=(U&& aValue) {
mLazyInitialized.init(std::forward<U>(aValue));
return *this;
}
LazyInitializer(const LazyInitializer&) = delete;
LazyInitializer& operator=(const LazyInitializer&) = delete;
private:
InitializedOnce<T, InitWhenVal, DestroyWhenVal, ValueCheckPolicy>&
mLazyInitialized;
};
} // namespace detail
// The following *InitializedOnce* template aliases allow to declare class
@ -209,6 +234,14 @@ using LazyInitializedOnceNotNullEarlyDestructible =
detail::DestroyWhen::EarlyAllowed,
detail::ValueCheckPolicies::ConvertsToTrue>;
template <typename T, detail::InitWhen InitWhenVal,
detail::DestroyWhen DestroyWhenVal,
template <typename> class ValueCheckPolicy>
auto do_Init(detail::InitializedOnce<T, InitWhenVal, DestroyWhenVal,
ValueCheckPolicy>& aLazyInitialized) {
return detail::LazyInitializer(aLazyInitialized);
}
} // namespace mozilla
#endif

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

@ -139,6 +139,17 @@ TEST(InitializedOnceAllowLazy, Init)
ASSERT_EQ(testValue, val.ref().mValue);
}
TEST(InitializedOnceAllowLazy, do_Init)
{
LazyInitializedOnce<const MoveOnly> val;
do_Init(val) = MoveOnly{testValue};
AssertIsSome(val);
ASSERT_EQ(testValue, (*val).mValue);
ASSERT_EQ(testValue, val->mValue);
ASSERT_EQ(testValue, val.ref().mValue);
}
TEST(InitializedOnceAllowLazyResettable, DefaultCtor)
{
LazyInitializedOnceEarlyDestructible<const MoveOnly> val;