Turi Create  4.0
basic_types.hpp
1 /* Copyright © 2017 Apple Inc. All rights reserved.
2  *
3  * Use of this source code is governed by a BSD-3-clause license that can
4  * be found in the LICENSE.txt file or at https://opensource.org/licenses/BSD-3-Clause
5  */
6 
7 #ifndef TURI_BASIC_TYPES_H_
8 #define TURI_BASIC_TYPES_H_
9 
10 #include <core/logging/assertions.hpp>
11 #include <cstdint>
12 
13 
14 template<typename Target, typename Source, int UnusedParam> struct truncate_check_impl {
15  Target operator()(Source x);
16 };
17 
18 template<typename Target, typename Source> Target truncate_check(Source x) {
19  return truncate_check_impl<Target, Source, 0>()(x);
20 }
21 
22 template<typename Target, typename Source> struct truncate_check_impl<Target, Source, 0> {
23  Target operator()(Source x) {
24  static_assert(!std::is_same<Source, Source>::value, "Unknown instantiation of truncate_check");
25  return 0;
26  }
27 };
28 
29 template<> struct truncate_check_impl<int64_t, size_t, 0> {
30  int64_t operator()(size_t x) {
31  ASSERT_LT(x, (1ULL << 63));
32  return static_cast<int64_t>(x);
33  }
34 };
35 
36 // For integer values, returns ceil(n / m);
37 template<typename T> T ceil_divide(T n, T m) {
38  return (n + (m-1)) / m;
39 }
40 
41 #endif