1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* This Source Code Form is subject to the terms of the Mozilla Public
3 * License, v. 2.0. If a copy of the MPL was not distributed with this
4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
7 * Provides DebugOnly, a type for variables used only in debug builds (i.e. by
11 #ifndef mozilla_DebugOnly_h_
12 #define mozilla_DebugOnly_h_
17 * DebugOnly contains a value of type T, but only in debug builds. In release
18 * builds, it does not contain a value. This helper is intended to be used with
19 * MOZ_ASSERT()-style macros, allowing one to write:
21 * DebugOnly<bool> check = func();
24 * more concisely than declaring |check| conditional on #ifdef DEBUG, but also
25 * without allocating storage space for |check| in release builds.
27 * DebugOnly instances can only be coerced to T in debug builds. In release
28 * builds they don't have a value, so type coercion is not well defined.
38 DebugOnly(const T
& other
) : value(other
) { }
39 DebugOnly(const DebugOnly
& other
) : value(other
.value
) { }
40 DebugOnly
& operator=(const T
& rhs
) {
44 void operator++(int) {
47 void operator--(int) {
51 T
* operator&() { return &value
; }
53 operator T
&() { return value
; }
54 operator const T
&() const { return value
; }
56 T
& operator->() { return value
; }
60 DebugOnly(const T
&) { }
61 DebugOnly(const DebugOnly
&) { }
62 DebugOnly
& operator=(const T
&) { return *this; }
63 void operator++(int) { }
64 void operator--(int) { }
68 * DebugOnly must always have a destructor or else it will
69 * generate "unused variable" warnings, exactly what it's intended
77 #endif /* mozilla_DebugOnly_h_ */