Opaque.h 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  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. /* An opaque integral type supporting only comparison operators. */
  7. #ifndef mozilla_Opaque_h
  8. #define mozilla_Opaque_h
  9. #include "mozilla/TypeTraits.h"
  10. namespace mozilla {
  11. /**
  12. * Opaque<T> is a replacement for integral T in cases where only comparisons
  13. * must be supported, and it's desirable to prevent accidental dependency on
  14. * exact values.
  15. */
  16. template<typename T>
  17. class Opaque final
  18. {
  19. static_assert(mozilla::IsIntegral<T>::value,
  20. "mozilla::Opaque only supports integral types");
  21. T mValue;
  22. public:
  23. Opaque() {}
  24. explicit Opaque(T aValue) : mValue(aValue) {}
  25. bool operator==(const Opaque& aOther) const {
  26. return mValue == aOther.mValue;
  27. }
  28. bool operator!=(const Opaque& aOther) const {
  29. return !(*this == aOther);
  30. }
  31. };
  32. } // namespace mozilla
  33. #endif /* mozilla_Opaque_h */