Bug 1861709 replace AudioCallbackDriver::ThreadRunning() assertions that mean to...
[gecko.git] / widget / windows / CheckInvariantWrapper.h
blobda6024ec212fe6bbbbda205262f3060f3ad0ed67
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
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
7 // A wrapper that uses RAII to ensure that a class invariant is checked
8 // before and after any public function is called
10 #ifndef CHECKINVARIANTWRAPPER_H_
11 #define CHECKINVARIANTWRAPPER_H_
13 #include "mozilla/Attributes.h"
14 #include <utility>
16 namespace mozilla {
19 // Wraps an object of type T and allows access to its public API by
20 // deferencing it using the pointer syntax "->".
22 // Using that operator will return a temporary RAII object that
23 // calls a method named "CheckInvariant" in its constructor, calls the
24 // requested method, and then calls "CheckInvariant" again in its
25 // destructor.
27 // The only thing your class requires is a method with the following signature:
29 // void CheckInvariant() const;
31 template <typename T>
32 class CheckInvariantWrapper {
33 public:
34 class Wrapper {
35 public:
36 explicit Wrapper(T& aObject) : mObject(aObject) {
37 mObject.CheckInvariant();
39 ~Wrapper() { mObject.CheckInvariant(); }
41 T* operator->() { return &mObject; }
43 private:
44 T& mObject;
47 class ConstWrapper {
48 public:
49 explicit ConstWrapper(const T& aObject) : mObject(aObject) {
50 mObject.CheckInvariant();
52 ~ConstWrapper() { mObject.CheckInvariant(); }
54 const T* operator->() const { return &mObject; }
56 private:
57 const T& mObject;
60 CheckInvariantWrapper() = default;
62 MOZ_IMPLICIT CheckInvariantWrapper(T aObject) : mObject(std::move(aObject)) {}
64 template <typename... Args>
65 explicit CheckInvariantWrapper(std::in_place_t, Args&&... args)
66 : mObject(std::forward<Args>(args)...) {}
68 const ConstWrapper operator->() const { return ConstWrapper(mObject); }
70 Wrapper operator->() { return Wrapper(mObject); }
72 private:
73 T mObject;
76 } // namespace mozilla
78 #endif // CHECKINVARIANTWRAPPER_H_