tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

IntegerTypeTraits.h (2116B)


      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 #ifndef mozilla_IntegerTypeTraits_h
      8 #define mozilla_IntegerTypeTraits_h
      9 
     10 #include <stddef.h>
     11 #include <stdint.h>
     12 #include <type_traits>
     13 
     14 namespace mozilla {
     15 
     16 namespace detail {
     17 
     18 /**
     19 * StdintTypeForSizeAndSignedness returns the stdint integer type
     20 * of given size (can be 1, 2, 4 or 8) and given signedness
     21 * (false means unsigned, true means signed).
     22 */
     23 template <size_t Size, bool Signedness>
     24 struct StdintTypeForSizeAndSignedness;
     25 
     26 template <>
     27 struct StdintTypeForSizeAndSignedness<1, true> {
     28  typedef int8_t Type;
     29 };
     30 
     31 template <>
     32 struct StdintTypeForSizeAndSignedness<1, false> {
     33  typedef uint8_t Type;
     34 };
     35 
     36 template <>
     37 struct StdintTypeForSizeAndSignedness<2, true> {
     38  typedef int16_t Type;
     39 };
     40 
     41 template <>
     42 struct StdintTypeForSizeAndSignedness<2, false> {
     43  typedef uint16_t Type;
     44 };
     45 
     46 template <>
     47 struct StdintTypeForSizeAndSignedness<4, true> {
     48  typedef int32_t Type;
     49 };
     50 
     51 template <>
     52 struct StdintTypeForSizeAndSignedness<4, false> {
     53  typedef uint32_t Type;
     54 };
     55 
     56 template <>
     57 struct StdintTypeForSizeAndSignedness<8, true> {
     58  typedef int64_t Type;
     59 };
     60 
     61 template <>
     62 struct StdintTypeForSizeAndSignedness<8, false> {
     63  typedef uint64_t Type;
     64 };
     65 
     66 }  // namespace detail
     67 
     68 template <size_t Size>
     69 struct UnsignedStdintTypeForSize
     70    : detail::StdintTypeForSizeAndSignedness<Size, false> {};
     71 
     72 template <size_t Size>
     73 struct SignedStdintTypeForSize
     74    : detail::StdintTypeForSizeAndSignedness<Size, true> {};
     75 
     76 template <typename IntegerType>
     77 struct PositionOfSignBit {
     78  static_assert(std::is_integral_v<IntegerType>,
     79                "PositionOfSignBit is only for integral types");
     80  // 8 here should be CHAR_BIT from limits.h, but the world has moved on.
     81  static const size_t value = 8 * sizeof(IntegerType) - 1;
     82 };
     83 
     84 }  // namespace mozilla
     85 
     86 #endif  // mozilla_IntegerTypeTraits_h