Libosmium  2.10.0
Fast and flexible C++ library for working with OpenStreetMap data
cast.hpp
Go to the documentation of this file.
1 #ifndef OSMIUM_UTIL_CAST_HPP
2 #define OSMIUM_UTIL_CAST_HPP
3 
4 /*
5 
6 This file is part of Osmium (http://osmcode.org/libosmium).
7 
8 Copyright 2013-2016 Jochen Topf <jochen@topf.org> and others (see README).
9 
10 Boost Software License - Version 1.0 - August 17th, 2003
11 
12 Permission is hereby granted, free of charge, to any person or organization
13 obtaining a copy of the software and accompanying documentation covered by
14 this license (the "Software") to use, reproduce, display, distribute,
15 execute, and transmit the Software, and to prepare derivative works of the
16 Software, and to permit third-parties to whom the Software is furnished to
17 do so, all subject to the following:
18 
19 The copyright notices in the Software and this entire statement, including
20 the above license grant, this restriction and the following disclaimer,
21 must be included in all copies of the Software, in whole or in part, and
22 all derivative works of the Software, unless such copies or derivative
23 works are solely in the form of machine-executable object code generated by
24 a source language processor.
25 
26 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
27 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
28 FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
29 SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
30 FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
31 ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
32 DEALINGS IN THE SOFTWARE.
33 
34 */
35 
36 #ifndef assert
37 # include <cassert>
38 #endif
39 
40 #include <cstdint>
41 #include <limits>
42 #include <type_traits>
43 
44 namespace osmium {
45 
46  // These functions are wrappers around static_cast<>() that call assert()
47  // to check that there is no integer overflow happening before doing the
48  // cast. There are several versions of this templated function here
49  // depending on the types of the input and output. In any case, both input
50  // and output have to be integral types. If the cast can't overflow, no
51  // check is done.
52 
53  template <typename A, typename B>
55  std::integral_constant<bool,
56  std::is_integral<A>::value &&
57  std::is_integral<B>::value &&
58  !std::is_same<A, bool>::value &&
59  !std::is_same<B, bool>::value> {
60  };
61 
62  template <typename T, typename F, typename std::enable_if<are_real_integers<T, F>::value && std::is_same<T, F>::value, int>::type = 0>
63  inline T static_cast_with_assert(const F value) {
64  return value;
65  }
66 
67  template <typename T, typename F, typename std::enable_if<are_real_integers<T, F>::value && !std::is_same<T, F>::value && (sizeof(T) > sizeof(F)), int>::type = 0>
68  inline T static_cast_with_assert(const F value) {
69  return static_cast<T>(value);
70  }
71 
72  template <typename T, typename F, typename std::enable_if<are_real_integers<T, F>::value && !std::is_same<T, F>::value && std::is_signed<T>::value == std::is_signed<F>::value && (sizeof(T) == sizeof(F)), int>::type = 0>
73  inline T static_cast_with_assert(const F value) {
74  return static_cast<T>(value);
75  }
76 
77  template <typename T, typename F, typename std::enable_if<are_real_integers<T, F>::value && !std::is_same<T, F>::value && (sizeof(T) < sizeof(F)) && std::is_signed<T>::value && std::is_signed<F>::value, int>::type = 0>
78  inline T static_cast_with_assert(const F value) {
79  assert(value >= std::numeric_limits<T>::min() && value <= std::numeric_limits<T>::max());
80  return static_cast<T>(value);
81  }
82 
83  template <typename T, typename F, typename std::enable_if<are_real_integers<T, F>::value && !std::is_same<T, F>::value && (sizeof(T) <= sizeof(F)) && std::is_unsigned<T>::value && std::is_signed<F>::value, int>::type = 0>
84  inline T static_cast_with_assert(const F value) {
85  assert(value >= 0 && static_cast<typename std::make_unsigned<F>::type>(value) <= std::numeric_limits<T>::max());
86  return static_cast<T>(value);
87  }
88 
89  template <typename T, typename F, typename std::enable_if<are_real_integers<T, F>::value && !std::is_same<T, F>::value && (sizeof(T) < sizeof(F)) && std::is_unsigned<T>::value && std::is_unsigned<F>::value, int>::type = 0>
90  inline T static_cast_with_assert(const F value) {
91  assert(value <= std::numeric_limits<T>::max());
92  return static_cast<T>(value);
93  }
94 
95  template <typename T, typename F, typename std::enable_if<are_real_integers<T, F>::value && !std::is_same<T, F>::value && (sizeof(T) <= sizeof(F)) && std::is_signed<T>::value && std::is_unsigned<F>::value, int>::type = 0>
96  inline T static_cast_with_assert(const F value) {
97  assert(static_cast<int64_t>(value) <= static_cast<int64_t>(std::numeric_limits<T>::max()));
98  return static_cast<T>(value);
99  }
100 
101 } // namespace osmium
102 
103 #endif // OSMIUM_UTIL_CAST_HPP
type
Definition: entity_bits.hpp:63
Definition: cast.hpp:54
Namespace for everything in the Osmium library.
Definition: assembler.hpp:73
T static_cast_with_assert(const F value)
Definition: cast.hpp:63