mirror of
https://github.com/mozilla/gecko-dev.git
synced 2025-03-03 07:01:19 +00:00
Bug 1625871 - InitializedOnce<const T> should be a literal type for literal types T. r=froydnj
Differential Revision: https://phabricator.services.mozilla.com/D68759 --HG-- extra : moz-landing-system : lando
This commit is contained in:
parent
3eeb9d7e2d
commit
237efbb270
@ -47,10 +47,11 @@ template <typename T, InitWhen InitWhenVal, DestroyWhen DestroyWhenVal,
|
||||
ValueCheckPolicies::AllowAnyValue>
|
||||
class InitializedOnce final {
|
||||
static_assert(std::is_const_v<T>);
|
||||
using MaybeType = Maybe<std::remove_const_t<T>>;
|
||||
|
||||
public:
|
||||
template <typename Dummy = void>
|
||||
explicit InitializedOnce(
|
||||
explicit constexpr InitializedOnce(
|
||||
std::enable_if_t<InitWhenVal == InitWhen::LazyAllowed, Dummy>* =
|
||||
nullptr) {}
|
||||
|
||||
@ -58,7 +59,7 @@ class InitializedOnce final {
|
||||
// arguments. The default constructor should only be available conditionally
|
||||
// and is declared above.
|
||||
template <typename Arg0, typename... Args>
|
||||
explicit InitializedOnce(Arg0&& aArg0, Args&&... aArgs)
|
||||
explicit constexpr InitializedOnce(Arg0&& aArg0, Args&&... aArgs)
|
||||
: mMaybe{Some(std::remove_const_t<T>{std::forward<Arg0>(aArg0),
|
||||
std::forward<Args>(aArgs)...})} {
|
||||
MOZ_ASSERT(ValueCheckPolicy<T>::Check(*mMaybe));
|
||||
@ -77,8 +78,8 @@ class InitializedOnce final {
|
||||
DestroyWhenVal == DestroyWhen::EarlyAllowed);
|
||||
MOZ_ASSERT(!mWasReset);
|
||||
MOZ_ASSERT(!mMaybe);
|
||||
mMaybe.~Maybe<std::remove_const_t<T>>();
|
||||
new (&mMaybe) Maybe<T>{std::move(aOther.mMaybe)};
|
||||
mMaybe.~MaybeType();
|
||||
new (&mMaybe) MaybeType{std::move(aOther.mMaybe)};
|
||||
#ifdef DEBUG
|
||||
aOther.mWasReset = true;
|
||||
#endif
|
||||
@ -86,7 +87,7 @@ class InitializedOnce final {
|
||||
}
|
||||
|
||||
template <typename... Args, typename Dummy = void>
|
||||
std::enable_if_t<InitWhenVal == InitWhen::LazyAllowed, Dummy> init(
|
||||
constexpr std::enable_if_t<InitWhenVal == InitWhen::LazyAllowed, Dummy> init(
|
||||
Args&&... aArgs) {
|
||||
MOZ_ASSERT(mMaybe.isNothing());
|
||||
MOZ_ASSERT(!mWasReset);
|
||||
@ -94,12 +95,12 @@ class InitializedOnce final {
|
||||
MOZ_ASSERT(ValueCheckPolicy<T>::Check(*mMaybe));
|
||||
}
|
||||
|
||||
explicit operator bool() const { return isSome(); }
|
||||
bool isSome() const { return mMaybe.isSome(); }
|
||||
bool isNothing() const { return mMaybe.isNothing(); }
|
||||
constexpr explicit operator bool() const { return isSome(); }
|
||||
constexpr bool isSome() const { return mMaybe.isSome(); }
|
||||
constexpr bool isNothing() const { return mMaybe.isNothing(); }
|
||||
|
||||
T& operator*() const { return *mMaybe; }
|
||||
T* operator->() const { return mMaybe.operator->(); }
|
||||
constexpr T& operator*() const { return *mMaybe; }
|
||||
constexpr T* operator->() const { return mMaybe.operator->(); }
|
||||
|
||||
template <typename Dummy = void>
|
||||
std::enable_if_t<DestroyWhenVal == DestroyWhen::EarlyAllowed, Dummy>
|
||||
@ -127,7 +128,7 @@ class InitializedOnce final {
|
||||
}
|
||||
|
||||
private:
|
||||
Maybe<std::remove_const_t<T>> mMaybe;
|
||||
MaybeType mMaybe;
|
||||
#ifdef DEBUG
|
||||
bool mWasReset = false;
|
||||
#endif
|
||||
|
@ -27,10 +27,8 @@ void AssertIsNothing(const T& aVal) {
|
||||
ASSERT_TRUE(aVal.isNothing());
|
||||
}
|
||||
|
||||
// XXX TODO Should be true once Bug ... ensures this for Maybe.
|
||||
// static_assert(std::is_trivially_destructible_v<InitializedOnce<const int>>);
|
||||
// static_assert(std::is_trivially_default_constructible_v<InitializedOnce<const
|
||||
// int, LazyInit::Allow>>);
|
||||
static_assert(std::is_trivially_destructible_v<InitializedOnce<const int>>);
|
||||
static_assert(std::is_trivially_destructible_v<LazyInitializedOnce<const int>>);
|
||||
|
||||
static_assert(!std::is_copy_constructible_v<InitializedOnce<const int>>);
|
||||
static_assert(!std::is_copy_assignable_v<InitializedOnce<const int>>);
|
||||
@ -75,7 +73,7 @@ static_assert(
|
||||
test_has_init_method(kPtrInitializedOnceIntLazyInitAllowResettable));
|
||||
|
||||
struct MoveOnly {
|
||||
explicit MoveOnly(int aValue) : mValue{aValue} {}
|
||||
explicit constexpr MoveOnly(int aValue) : mValue{aValue} {}
|
||||
|
||||
MoveOnly(MoveOnly&&) = default;
|
||||
MoveOnly& operator=(MoveOnly&&) = default;
|
||||
@ -89,8 +87,16 @@ constexpr int testValue = 32;
|
||||
|
||||
TEST(InitializedOnce, ImmediateInit)
|
||||
{
|
||||
const InitializedOnce<const MoveOnly> val{testValue};
|
||||
constexpr InitializedOnce<const MoveOnly> val{testValue};
|
||||
|
||||
// compile-time assertions
|
||||
static_assert(val);
|
||||
static_assert(val.isSome());
|
||||
static_assert(!val.isNothing());
|
||||
static_assert(testValue == (*val).mValue);
|
||||
static_assert(testValue == val->mValue);
|
||||
|
||||
// run-time assertions
|
||||
AssertIsSome(val);
|
||||
ASSERT_EQ(testValue, (*val).mValue);
|
||||
ASSERT_EQ(testValue, val->mValue);
|
||||
|
Loading…
x
Reference in New Issue
Block a user