tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

commit 3cd0b5f187c7e8c017bcc6dd0b1b2103ac0994bd
parent 63beb0dd5950c0a2ee0f8afdcc7a4bd756298ff7
Author: serge-sans-paille <sguelton@mozilla.com>
Date:   Wed,  8 Oct 2025 15:10:23 +0000

Bug 1993112 - Make InitializedOnce implementation more readable r=emilio,dom-worker-reviewers,asuth

Use a concept-like approach to make requirements easier to read.

Also remove an unused header in the process.

Differential Revision: https://phabricator.services.mozilla.com/D267871

Diffstat:
Mdom/cache/AutoUtils.h | 1-
Mmfbt/InitializedOnce.h | 38++++++++++++++++++++++----------------
2 files changed, 22 insertions(+), 17 deletions(-)

diff --git a/dom/cache/AutoUtils.h b/dom/cache/AutoUtils.h @@ -8,7 +8,6 @@ #define mozilla_dom_cache_AutoUtils_h #include "mozilla/Attributes.h" -#include "mozilla/InitializedOnce.h" #include "mozilla/dom/SafeRefPtr.h" #include "mozilla/dom/cache/CacheTypes.h" #include "mozilla/dom/cache/TypeUtils.h" diff --git a/mfbt/InitializedOnce.h b/mfbt/InitializedOnce.h @@ -49,13 +49,19 @@ class InitializedOnce final { static_assert(std::is_const_v<T>); using MaybeType = Maybe<std::remove_const_t<T>>; + template <typename Dummy> + using requires_lazy_init_allowed = + std::enable_if_t<InitWhenVal == InitWhen::LazyAllowed, Dummy>; + + template <typename Dummy> + using requires_early_destroy_allowed = + std::enable_if_t<DestroyWhenVal == DestroyWhen::EarlyAllowed, Dummy>; + public: using ValueType = T; - template <typename Dummy = void> - explicit constexpr InitializedOnce( - std::enable_if_t<InitWhenVal == InitWhen::LazyAllowed, Dummy>* = - nullptr) {} + template <typename Dummy = void, typename = requires_lazy_init_allowed<Dummy>> + explicit constexpr InitializedOnce() {} // note: aArg0 is named separately here to disallow calling this with no // arguments. The default constructor should only be available conditionally @@ -88,9 +94,9 @@ class InitializedOnce final { return *this; } - template <typename... Args, typename Dummy = void> - constexpr std::enable_if_t<InitWhenVal == InitWhen::LazyAllowed, Dummy> init( - Args&&... aArgs) { + template <typename... Args, typename Dummy = void, + typename = requires_lazy_init_allowed<Dummy>> + constexpr void init(Args&&... aArgs) { MOZ_ASSERT(mMaybe.isNothing()); MOZ_ASSERT(!mWasReset); mMaybe.emplace(std::remove_const_t<T>{std::forward<Args>(aArgs)...}); @@ -106,25 +112,25 @@ class InitializedOnce final { constexpr T& ref() const { return mMaybe.ref(); } - template <typename Dummy = void> - std::enable_if_t<DestroyWhenVal == DestroyWhen::EarlyAllowed, Dummy> - destroy() { + template <typename Dummy = void, + typename = requires_early_destroy_allowed<Dummy>> + void destroy() { MOZ_ASSERT(mMaybe.isSome()); maybeDestroy(); } - template <typename Dummy = void> - std::enable_if_t<DestroyWhenVal == DestroyWhen::EarlyAllowed, Dummy> - maybeDestroy() { + template <typename Dummy = void, + typename = requires_early_destroy_allowed<Dummy>> + void maybeDestroy() { mMaybe.reset(); #ifdef DEBUG mWasReset = true; #endif } - template <typename Dummy = T> - std::enable_if_t<DestroyWhenVal == DestroyWhen::EarlyAllowed, Dummy> - release() { + template <typename Dummy = void, + typename = requires_early_destroy_allowed<Dummy>> + T release() { MOZ_ASSERT(mMaybe.isSome()); auto res = std::move(mMaybe.ref()); destroy();