Libosmium  2.16.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 (https://osmcode.org/libosmium).
7 
8 Copyright 2013-2021 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 
37 
38 #ifndef assert
39 # include <cassert>
40 #endif
41 
42 #include <cstdint>
43 #include <limits>
44 #include <type_traits>
45 
46 namespace osmium {
47 
48  // These functions are wrappers around static_cast<>() that call assert()
49  // to check that there is no integer overflow happening before doing the
50  // cast. There are several versions of this templated function here
51  // depending on the types of the input and output. In any case, both input
52  // and output have to be integral types. If the cast can't overflow, no
53  // check is done.
54 
55  template <typename A, typename B>
57  std::integral_constant<bool,
58  std::is_integral<A>::value &&
59  std::is_integral<B>::value &&
60  !std::is_same<A, bool>::value &&
61  !std::is_same<B, bool>::value> {
62  };
63 
64  template <typename T, typename F, typename std::enable_if<are_real_integers<T, F>::value && std::is_same<T, F>::value, int>::type = 0>
65  OSMIUM_DEPRECATED inline T static_cast_with_assert(const F value) {
66  return value;
67  }
68 
69  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>
70  OSMIUM_DEPRECATED inline T static_cast_with_assert(const F value) {
71  return static_cast<T>(value);
72  }
73 
74  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>
75  OSMIUM_DEPRECATED inline T static_cast_with_assert(const F value) {
76  return static_cast<T>(value);
77  }
78 
79  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>
80  OSMIUM_DEPRECATED inline T static_cast_with_assert(const F value) {
81  assert(value >= std::numeric_limits<T>::min() && value <= std::numeric_limits<T>::max());
82  return static_cast<T>(value);
83  }
84 
85  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>
86  OSMIUM_DEPRECATED inline T static_cast_with_assert(const F value) {
87  assert(value >= 0 && static_cast<typename std::make_unsigned<F>::type>(value) <= std::numeric_limits<T>::max());
88  return static_cast<T>(value);
89  }
90 
91  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>
92  OSMIUM_DEPRECATED inline T static_cast_with_assert(const F value) {
93  assert(value <= std::numeric_limits<T>::max());
94  return static_cast<T>(value);
95  }
96 
97  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>
98  OSMIUM_DEPRECATED inline T static_cast_with_assert(const F value) {
99  assert(static_cast<int64_t>(value) <= static_cast<int64_t>(std::numeric_limits<T>::max()));
100  return static_cast<T>(value);
101  }
102 
103 } // namespace osmium
104 
105 #endif // OSMIUM_UTIL_CAST_HPP
osmium::static_cast_with_assert
OSMIUM_DEPRECATED T static_cast_with_assert(const F value)
Definition: cast.hpp:65
OSMIUM_DEPRECATED
#define OSMIUM_DEPRECATED
Definition: compatibility.hpp:51
compatibility.hpp
osmium
Namespace for everything in the Osmium library.
Definition: assembler.hpp:53
osmium::are_real_integers
Definition: cast.hpp:56
osmium::osm_entity_bits::type
type
Definition: entity_bits.hpp:63