tor-browser

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

FlippedOnce.h (1186B)


      1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
      2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
      3 /* This Source Code Form is subject to the terms of the Mozilla Public
      4 * License, v. 2.0. If a copy of the MPL was not distributed with this file,
      5 * You can obtain one at http://mozilla.org/MPL/2.0/. */
      6 
      7 #ifndef mozilla_dom_indexeddb_flippedonce_h__
      8 #define mozilla_dom_indexeddb_flippedonce_h__
      9 
     10 #include "mozilla/Assertions.h"
     11 #include "mozilla/Attributes.h"
     12 
     13 namespace mozilla {
     14 
     15 // A state-restricted bool, which can only be flipped once. It isn't required to
     16 // be flipped during its lifetime.
     17 template <bool Initial>
     18 class FlippedOnce {
     19 public:
     20  FlippedOnce(const FlippedOnce&) = delete;
     21  FlippedOnce& operator=(const FlippedOnce&) = delete;
     22  FlippedOnce(FlippedOnce&&) = default;
     23  FlippedOnce& operator=(FlippedOnce&&) = default;
     24 
     25  constexpr FlippedOnce() = default;
     26 
     27  MOZ_IMPLICIT constexpr operator bool() const { return mValue; };
     28 
     29  constexpr void Flip() {
     30    MOZ_ASSERT(mValue == Initial);
     31    EnsureFlipped();
     32  }
     33 
     34  constexpr void EnsureFlipped() { mValue = !Initial; }
     35 
     36 private:
     37  bool mValue = Initial;
     38 };
     39 }  // namespace mozilla
     40 
     41 #endif