michael@0: /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ michael@0: /* vim: set ts=8 sts=2 et sw=2 tw=80: */ michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: /* michael@0: * Provides DebugOnly, a type for variables used only in debug builds (i.e. by michael@0: * assertions). michael@0: */ michael@0: michael@0: #ifndef mozilla_DebugOnly_h michael@0: #define mozilla_DebugOnly_h michael@0: michael@0: namespace mozilla { michael@0: michael@0: /** michael@0: * DebugOnly contains a value of type T, but only in debug builds. In release michael@0: * builds, it does not contain a value. This helper is intended to be used with michael@0: * MOZ_ASSERT()-style macros, allowing one to write: michael@0: * michael@0: * DebugOnly check = func(); michael@0: * MOZ_ASSERT(check); michael@0: * michael@0: * more concisely than declaring |check| conditional on #ifdef DEBUG, but also michael@0: * without allocating storage space for |check| in release builds. michael@0: * michael@0: * DebugOnly instances can only be coerced to T in debug builds. In release michael@0: * builds they don't have a value, so type coercion is not well defined. michael@0: * michael@0: * Note that DebugOnly instances still take up one byte of space, plus padding, michael@0: * when used as members of structs. michael@0: */ michael@0: template michael@0: class DebugOnly michael@0: { michael@0: public: michael@0: #ifdef DEBUG michael@0: T value; michael@0: michael@0: DebugOnly() { } michael@0: DebugOnly(const T& other) : value(other) { } michael@0: DebugOnly(const DebugOnly& other) : value(other.value) { } michael@0: DebugOnly& operator=(const T& rhs) { michael@0: value = rhs; michael@0: return *this; michael@0: } michael@0: void operator++(int) { michael@0: value++; michael@0: } michael@0: void operator--(int) { michael@0: value--; michael@0: } michael@0: michael@0: T* operator&() { return &value; } michael@0: michael@0: operator T&() { return value; } michael@0: operator const T&() const { return value; } michael@0: michael@0: T& operator->() { return value; } michael@0: const T& operator->() const { return value; } michael@0: michael@0: #else michael@0: DebugOnly() { } michael@0: DebugOnly(const T&) { } michael@0: DebugOnly(const DebugOnly&) { } michael@0: DebugOnly& operator=(const T&) { return *this; } michael@0: void operator++(int) { } michael@0: void operator--(int) { } michael@0: #endif michael@0: michael@0: /* michael@0: * DebugOnly must always have a destructor or else it will michael@0: * generate "unused variable" warnings, exactly what it's intended michael@0: * to avoid! michael@0: */ michael@0: ~DebugOnly() {} michael@0: }; michael@0: michael@0: } michael@0: michael@0: #endif /* mozilla_DebugOnly_h */