Line data Source code
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 "mozilla/TypeTraits.h"
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 : {
24 : static_assert(mozilla::IsIntegral<T>::value,
25 : "mozilla::Opaque only supports integral types");
26 :
27 : T mValue;
28 :
29 : public:
30 : Opaque() {}
31 16609743 : explicit Opaque(T aValue) : mValue(aValue) {}
32 :
33 12069285 : bool operator==(const Opaque& aOther) const {
34 12069285 : return mValue == aOther.mValue;
35 : }
36 :
37 0 : bool operator!=(const Opaque& aOther) const {
38 0 : return !(*this == aOther);
39 : }
40 : };
41 :
42 : } // namespace mozilla
43 :
44 : #endif /* mozilla_Opaque_h */
|