Bug 1541509 [wpt PR 16042] - Replace generateOffer by generateAudioReceiveOnlyOffer...
[gecko.git] / mfbt / IntegerTypeTraits.h
blob851afeeac32c8d996875ce1b38590cde2bc7fa3f
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/. */
7 /* Helpers to manipulate integer types that don't fit in TypeTraits.h */
9 #ifndef mozilla_IntegerTypeTraits_h
10 #define mozilla_IntegerTypeTraits_h
12 #include "mozilla/TypeTraits.h"
13 #include <stdint.h>
15 namespace mozilla {
17 namespace detail {
19 /**
20 * StdintTypeForSizeAndSignedness returns the stdint integer type
21 * of given size (can be 1, 2, 4 or 8) and given signedness
22 * (false means unsigned, true means signed).
24 template <size_t Size, bool Signedness>
25 struct StdintTypeForSizeAndSignedness;
27 template <>
28 struct StdintTypeForSizeAndSignedness<1, true> {
29 typedef int8_t Type;
32 template <>
33 struct StdintTypeForSizeAndSignedness<1, false> {
34 typedef uint8_t Type;
37 template <>
38 struct StdintTypeForSizeAndSignedness<2, true> {
39 typedef int16_t Type;
42 template <>
43 struct StdintTypeForSizeAndSignedness<2, false> {
44 typedef uint16_t Type;
47 template <>
48 struct StdintTypeForSizeAndSignedness<4, true> {
49 typedef int32_t Type;
52 template <>
53 struct StdintTypeForSizeAndSignedness<4, false> {
54 typedef uint32_t Type;
57 template <>
58 struct StdintTypeForSizeAndSignedness<8, true> {
59 typedef int64_t Type;
62 template <>
63 struct StdintTypeForSizeAndSignedness<8, false> {
64 typedef uint64_t Type;
67 } // namespace detail
69 template <size_t Size>
70 struct UnsignedStdintTypeForSize
71 : detail::StdintTypeForSizeAndSignedness<Size, false> {};
73 template <size_t Size>
74 struct SignedStdintTypeForSize
75 : detail::StdintTypeForSizeAndSignedness<Size, true> {};
77 template <typename IntegerType>
78 struct PositionOfSignBit {
79 static_assert(IsIntegral<IntegerType>::value,
80 "PositionOfSignBit is only for integral types");
81 // 8 here should be CHAR_BIT from limits.h, but the world has moved on.
82 static const size_t value = 8 * sizeof(IntegerType) - 1;
85 /**
86 * MinValue returns the minimum value of the given integer type as a
87 * compile-time constant, which std::numeric_limits<IntegerType>::min()
88 * cannot do in c++98.
90 template <typename IntegerType>
91 struct MinValue {
92 private:
93 static_assert(IsIntegral<IntegerType>::value,
94 "MinValue is only for integral types");
96 typedef typename MakeUnsigned<IntegerType>::Type UnsignedIntegerType;
97 static const size_t PosOfSignBit = PositionOfSignBit<IntegerType>::value;
99 public:
100 // Bitwise ops may return a larger type, that's why we cast explicitly.
101 // In C++, left bit shifts on signed values is undefined by the standard
102 // unless the shifted value is representable.
103 // Notice that signed-to-unsigned conversions are always well-defined in
104 // the standard as the value congruent to 2**n, as expected. By contrast,
105 // unsigned-to-signed is only well-defined if the value is representable.
106 static const IntegerType value =
107 IsSigned<IntegerType>::value
108 ? IntegerType(UnsignedIntegerType(1) << PosOfSignBit)
109 : IntegerType(0);
113 * MaxValue returns the maximum value of the given integer type as a
114 * compile-time constant, which std::numeric_limits<IntegerType>::max()
115 * cannot do in c++98.
117 template <typename IntegerType>
118 struct MaxValue {
119 static_assert(IsIntegral<IntegerType>::value,
120 "MaxValue is only for integral types");
122 // Tricksy, but covered by the CheckedInt unit test.
123 // Relies on the type of MinValue<IntegerType>::value
124 // being IntegerType.
125 static const IntegerType value = ~MinValue<IntegerType>::value;
128 } // namespace mozilla
130 #endif // mozilla_IntegerTypeTraits_h