forked from mirrors/gecko-dev
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:
parent
1575347021
commit
55236f5ccb
2 changed files with 44 additions and 0 deletions
|
|
@ -50,6 +50,8 @@ class InitializedOnce final {
|
||||||
using MaybeType = Maybe<std::remove_const_t<T>>;
|
using MaybeType = Maybe<std::remove_const_t<T>>;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
|
using ValueType = T;
|
||||||
|
|
||||||
template <typename Dummy = void>
|
template <typename Dummy = void>
|
||||||
explicit constexpr InitializedOnce(
|
explicit constexpr InitializedOnce(
|
||||||
std::enable_if_t<InitWhenVal == InitWhen::LazyAllowed, Dummy>* =
|
std::enable_if_t<InitWhenVal == InitWhen::LazyAllowed, Dummy>* =
|
||||||
|
|
@ -135,6 +137,29 @@ class InitializedOnce final {
|
||||||
bool mWasReset = false;
|
bool mWasReset = false;
|
||||||
#endif
|
#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
|
} // namespace detail
|
||||||
|
|
||||||
// The following *InitializedOnce* template aliases allow to declare class
|
// The following *InitializedOnce* template aliases allow to declare class
|
||||||
|
|
@ -209,6 +234,14 @@ using LazyInitializedOnceNotNullEarlyDestructible =
|
||||||
detail::DestroyWhen::EarlyAllowed,
|
detail::DestroyWhen::EarlyAllowed,
|
||||||
detail::ValueCheckPolicies::ConvertsToTrue>;
|
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
|
} // namespace mozilla
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
|
||||||
|
|
@ -139,6 +139,17 @@ TEST(InitializedOnceAllowLazy, Init)
|
||||||
ASSERT_EQ(testValue, val.ref().mValue);
|
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)
|
TEST(InitializedOnceAllowLazyResettable, DefaultCtor)
|
||||||
{
|
{
|
||||||
LazyInitializedOnceEarlyDestructible<const MoveOnly> val;
|
LazyInitializedOnceEarlyDestructible<const MoveOnly> val;
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue