Bug 1598164 - Added FlippedOnce class template to help reducing statefulness of boolean flags. r=dom-workers-and-storage-reviewers,ytausky
authorSimon Giesecke <sgiesecke@mozilla.com>
Tue, 03 Dec 2019 15:25:49 +0000
changeset 505287 9c56dd45e9f5a997e8e7790f144c6191f8a09e21
parent 505286 e5875d21e9b50ee7b255f1e006f4437cfc481ade
child 505288 2c2c3b33ea1bee0b65074400d5dbca0c916d0d22
push id102248
push usersgiesecke@mozilla.com
push dateWed, 04 Dec 2019 08:05:34 +0000
treeherderautoland@2c2c3b33ea1b [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersdom-workers-and-storage-reviewers, ytausky
bugs1598164
milestone73.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 1598164 - Added FlippedOnce class template to help reducing statefulness of boolean flags. r=dom-workers-and-storage-reviewers,ytausky Differential Revision: https://phabricator.services.mozilla.com/D55080
dom/indexedDB/FlippedOnce.h
dom/indexedDB/moz.build
new file mode 100644
--- /dev/null
+++ b/dom/indexedDB/FlippedOnce.h
@@ -0,0 +1,39 @@
+/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
+/* vim: set ts=8 sts=2 et sw=2 tw=80: */
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this file,
+ * You can obtain one at http://mozilla.org/MPL/2.0/. */
+
+#ifndef mozilla_dom_indexeddb_flippedonce_h__
+#define mozilla_dom_indexeddb_flippedonce_h__
+
+#include "mozilla/Assertions.h"
+#include "mozilla/Attributes.h"
+
+namespace mozilla {
+
+// A state-restricted bool, which can only be flipped once. It isn't required to
+// be flipped during its lifetime.
+template <bool Initial>
+class FlippedOnce {
+ public:
+  FlippedOnce(const FlippedOnce&) = delete;
+  FlippedOnce& operator=(const FlippedOnce&) = delete;
+
+  FlippedOnce() = default;
+
+  MOZ_IMPLICIT operator bool() const { return mValue; };
+
+  void Flip() {
+    MOZ_ASSERT(mValue == Initial);
+    EnsureFlipped();
+  }
+
+  void EnsureFlipped() { mValue = !Initial; }
+
+ private:
+  bool mValue = Initial;
+};
+}  // namespace mozilla
+
+#endif
--- a/dom/indexedDB/moz.build
+++ b/dom/indexedDB/moz.build
@@ -19,16 +19,17 @@ MOCHITEST_CHROME_MANIFESTS += ['test/chr
 XPCSHELL_TESTS_MANIFESTS += [
     'test/unit/xpcshell-child-process.ini',
     'test/unit/xpcshell-parent-process.ini'
 ]
 
 TEST_DIRS += ['test/gtest']
 
 EXPORTS.mozilla.dom += [
+    'FlippedOnce.h',
     'IDBCursor.h',
     'IDBDatabase.h',
     'IDBEvents.h',
     'IDBFactory.h',
     'IDBFileHandle.h',
     'IDBFileRequest.h',
     'IDBIndex.h',
     'IDBKeyRange.h',