Bug 860222: use our own isascii() for sanitizing TURN passwords r=glandium
[gecko.git] / mfbt / DebugOnly.h
blob1f78ed79895b84754203e922bc1d2eeb1c100cb7
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/. */
6 /*
7 * Provides DebugOnly, a type for variables used only in debug builds (i.e. by
8 * assertions).
9 */
11 #ifndef mozilla_DebugOnly_h_
12 #define mozilla_DebugOnly_h_
14 namespace mozilla {
16 /**
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();
22 * MOZ_ASSERT(check);
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.
30 template<typename T>
31 class DebugOnly
33 public:
34 #ifdef DEBUG
35 T value;
37 DebugOnly() { }
38 DebugOnly(const T& other) : value(other) { }
39 DebugOnly(const DebugOnly& other) : value(other.value) { }
40 DebugOnly& operator=(const T& rhs) {
41 value = rhs;
42 return *this;
44 void operator++(int) {
45 value++;
47 void operator--(int) {
48 value--;
51 T* operator&() { return &value; }
53 operator T&() { return value; }
54 operator const T&() const { return value; }
56 T& operator->() { return value; }
58 #else
59 DebugOnly() { }
60 DebugOnly(const T&) { }
61 DebugOnly(const DebugOnly&) { }
62 DebugOnly& operator=(const T&) { return *this; }
63 void operator++(int) { }
64 void operator--(int) { }
65 #endif
68 * DebugOnly must always have a destructor or else it will
69 * generate "unused variable" warnings, exactly what it's intended
70 * to avoid!
72 ~DebugOnly() {}
77 #endif /* mozilla_DebugOnly_h_ */