SkTo.h 1.1 KB

12345678910111213141516171819202122232425262728
  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. #include "SkTypes.h"
  10. #include "SkTFitsIn.h"
  11. template <typename D, typename S> constexpr D SkTo(S s) {
  12. return SkASSERT(SkTFitsIn<D>(s)),
  13. static_cast<D>(s);
  14. }
  15. template <typename S> constexpr int8_t SkToS8(S x) { return SkTo<int8_t>(x); }
  16. template <typename S> constexpr uint8_t SkToU8(S x) { return SkTo<uint8_t>(x); }
  17. template <typename S> constexpr int16_t SkToS16(S x) { return SkTo<int16_t>(x); }
  18. template <typename S> constexpr uint16_t SkToU16(S x) { return SkTo<uint16_t>(x); }
  19. template <typename S> constexpr int32_t SkToS32(S x) { return SkTo<int32_t>(x); }
  20. template <typename S> constexpr uint32_t SkToU32(S x) { return SkTo<uint32_t>(x); }
  21. template <typename S> constexpr int SkToInt(S x) { return SkTo<int>(x); }
  22. template <typename S> constexpr unsigned SkToUInt(S x) { return SkTo<unsigned>(x); }
  23. template <typename S> constexpr size_t SkToSizeT(S x) { return SkTo<size_t>(x); }
  24. #endif // SkTo_DEFINED