DebugOnly.h 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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/. */
  6. /*
  7. * Provides DebugOnly, a type for variables used only in debug builds (i.e. by
  8. * assertions).
  9. */
  10. #ifndef mozilla_DebugOnly_h
  11. #define mozilla_DebugOnly_h
  12. #include "mozilla/Attributes.h"
  13. namespace mozilla {
  14. /**
  15. * DebugOnly contains a value of type T, but only in debug builds. In release
  16. * builds, it does not contain a value. This helper is intended to be used with
  17. * MOZ_ASSERT()-style macros, allowing one to write:
  18. *
  19. * DebugOnly<bool> check = func();
  20. * MOZ_ASSERT(check);
  21. *
  22. * more concisely than declaring |check| conditional on #ifdef DEBUG.
  23. *
  24. * DebugOnly instances can only be coerced to T in debug builds. In release
  25. * builds they don't have a value, so type coercion is not well defined.
  26. *
  27. * NOTE: DebugOnly instances still take up one byte of space, plus padding, even
  28. * in optimized, non-DEBUG builds (see bug 1253094 comment 37 for more info).
  29. * For this reason the class is MOZ_STACK_CLASS to prevent consumers using
  30. * DebugOnly for struct/class members and unwittingly inflating the size of
  31. * their objects in release builds.
  32. */
  33. template<typename T>
  34. class MOZ_STACK_CLASS DebugOnly
  35. {
  36. public:
  37. #ifdef DEBUG
  38. T value;
  39. DebugOnly() { }
  40. MOZ_IMPLICIT DebugOnly(const T& aOther) : value(aOther) { }
  41. DebugOnly(const DebugOnly& aOther) : value(aOther.value) { }
  42. DebugOnly& operator=(const T& aRhs) {
  43. value = aRhs;
  44. return *this;
  45. }
  46. void operator++(int) { value++; }
  47. void operator--(int) { value--; }
  48. // Do not define operator+=(), etc. here. These will coerce via the
  49. // implicit cast and built-in operators. Defining explicit methods here
  50. // will create ambiguity the compiler can't deal with.
  51. T* operator&() { return &value; }
  52. operator T&() { return value; }
  53. operator const T&() const { return value; }
  54. T& operator->() { return value; }
  55. const T& operator->() const { return value; }
  56. #else
  57. DebugOnly() { }
  58. MOZ_IMPLICIT DebugOnly(const T&) { }
  59. DebugOnly(const DebugOnly&) { }
  60. DebugOnly& operator=(const T&) { return *this; }
  61. void operator++(int) { }
  62. void operator--(int) { }
  63. DebugOnly& operator+=(const T&) { return *this; }
  64. DebugOnly& operator-=(const T&) { return *this; }
  65. DebugOnly& operator&=(const T&) { return *this; }
  66. DebugOnly& operator|=(const T&) { return *this; }
  67. DebugOnly& operator^=(const T&) { return *this; }
  68. #endif
  69. /*
  70. * DebugOnly must always have a destructor or else it will
  71. * generate "unused variable" warnings, exactly what it's intended
  72. * to avoid!
  73. */
  74. ~DebugOnly() {}
  75. };
  76. } // namespace mozilla
  77. #endif /* mozilla_DebugOnly_h */