Opaque.h 1.1 KB

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