• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2018 Google Inc.
3  *
4  * Use of this source code is governed by a BSD-style license that can be
5  * found in the LICENSE file.
6  */
7 #ifndef SkTo_DEFINED
8 #define SkTo_DEFINED
9 
10 #include "include/private/base/SkAssert.h"
11 #include "include/private/base/SkTFitsIn.h"
12 
13 #include <cstddef>
14 #include <cstdint>
15 
SkTo(S s)16 template <typename D, typename S> constexpr D SkTo(S s) {
17     return SkASSERT(SkTFitsIn<D>(s)),
18            static_cast<D>(s);
19 }
20 
SkToS8(S x)21 template <typename S> constexpr int8_t   SkToS8(S x)    { return SkTo<int8_t>(x);   }
SkToU8(S x)22 template <typename S> constexpr uint8_t  SkToU8(S x)    { return SkTo<uint8_t>(x);  }
SkToS16(S x)23 template <typename S> constexpr int16_t  SkToS16(S x)   { return SkTo<int16_t>(x);  }
SkToU16(S x)24 template <typename S> constexpr uint16_t SkToU16(S x)   { return SkTo<uint16_t>(x); }
SkToS32(S x)25 template <typename S> constexpr int32_t  SkToS32(S x)   { return SkTo<int32_t>(x);  }
SkToU32(S x)26 template <typename S> constexpr uint32_t SkToU32(S x)   { return SkTo<uint32_t>(x); }
SkToS64(S x)27 template <typename S> constexpr int64_t  SkToS64(S x)   { return SkTo<int64_t>(x);  }
SkToU64(S x)28 template <typename S> constexpr uint64_t SkToU64(S x)   { return SkTo<uint64_t>(x); }
SkToInt(S x)29 template <typename S> constexpr int      SkToInt(S x)   { return SkTo<int>(x);      }
SkToUInt(S x)30 template <typename S> constexpr unsigned SkToUInt(S x)  { return SkTo<unsigned>(x); }
SkToSizeT(S x)31 template <typename S> constexpr size_t   SkToSizeT(S x) { return SkTo<size_t>(x);   }
32 
33 /** @return false or true based on the condition
34 */
SkToBool(const T & x)35 template <typename T> static constexpr bool SkToBool(const T& x) {
36     return (bool)x;
37 }
38 
39 #endif  // SkTo_DEFINED
40