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