/usr/include/mapnik/safe_cast.hpp is in libmapnik-dev 3.0.12+ds-3.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 | /*****************************************************************************
*
* This file is part of Mapnik (c++ mapping toolkit)
*
* Copyright (C) 2015 Artem Pavlenko
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*
*****************************************************************************/
#ifndef MAPNIK_SAFE_CAST_HPP
#define MAPNIK_SAFE_CAST_HPP
#include <limits>
#include <type_traits>
#include <cstdint>
namespace mapnik {
namespace detail {
template<typename T, typename S, typename E = void>
struct numeric_compare;
template<typename T, typename S>
struct numeric_compare_same_sign
{
using sizeup = typename std::conditional<sizeof(T) >= sizeof(S), T, S>::type;
static inline bool less(T t, S s) {
return static_cast<sizeup>(t) < static_cast<sizeup>(s);
}
static inline bool greater(T t, S s) {
return static_cast<sizeup>(t) > static_cast<sizeup>(s);
}
};
template<typename T, typename S>
struct numeric_compare<T,S,typename std::enable_if<!std::is_floating_point<T>::value && !std::is_floating_point<S>::value &&
((std::is_unsigned<T>::value && std::is_unsigned<S>::value)
|| (std::is_signed<T>::value && std::is_signed<S>::value))>
::type> : numeric_compare_same_sign<T,S>
{};
template<typename T, typename S>
struct numeric_compare<T,S,typename std::enable_if<!std::is_floating_point<T>::value && !std::is_floating_point<S>::value &&
std::is_integral<T>::value && std::is_signed<T>::value && std::is_unsigned<S>::value>::type>
{
static inline bool less(T t, S s) {
return (t < static_cast<T>(0)) ? true : static_cast<std::uint64_t>(t) < static_cast<std::uint64_t>(s);
}
static inline bool greater(T t, S s) {
return (t < static_cast<T>(0)) ? false : static_cast<std::uint64_t>(t) > static_cast<std::uint64_t>(s);
}
};
template<typename T, typename S>
struct numeric_compare<T,S,typename std::enable_if<!std::is_floating_point<T>::value && !std::is_floating_point<S>::value &&
std::is_integral<T>::value && std::is_unsigned<T>::value && std::is_signed<S>::value>::type>
{
static inline bool less(T t, S s) {
return (s < static_cast<S>(0)) ? false : static_cast<std::uint64_t>(t) < static_cast<std::uint64_t>(s);
}
static inline bool greater(T t, S s) {
return (s < static_cast<S>(0)) ? true : static_cast<std::uint64_t>(t) > static_cast<std::uint64_t>(s);
}
};
template<typename T, typename S>
struct numeric_compare<T,S,typename std::enable_if<std::is_floating_point<T>::value && std::is_floating_point<S>::value>::type>
{
static inline bool less(T t, S s) {
return t < s;
}
static inline bool greater(T t, S s) {
return t > s;
}
};
template<typename T, typename S>
struct numeric_compare<T,S,typename std::enable_if<
(std::is_floating_point<T>::value && std::is_integral<S>::value) ||
(std::is_integral<T>::value && std::is_floating_point<S>::value)>::type>
{
static inline bool less(T t, S s) {
return numeric_compare<double,double>::less(static_cast<double>(t),static_cast<double>(s));
}
static inline bool greater(T t, S s) {
return numeric_compare<double,double>::greater(static_cast<double>(t),static_cast<double>(s));
}
};
// floats
template <typename T, typename Enable = void>
struct bounds
{
static const T lowest() { return static_cast<T>(-std::numeric_limits<T>::max());}
static const T highest() { return std::numeric_limits<T>::max();}
};
// integers
template <typename T>
struct bounds<T, typename std::enable_if<std::numeric_limits<T>::is_integer>::type >
{
static const T lowest() { return std::numeric_limits<T>::min();}
static const T highest() { return std::numeric_limits<T>::max();}
};
} // ns detail
template <typename T, typename S>
inline T safe_cast(S s)
{
static const auto max_val = detail::bounds<T>::highest();
static const auto min_val = detail::bounds<T>::lowest();
if (detail::numeric_compare<S,T>::greater(s,max_val))
{
return max_val;
}
else if (detail::numeric_compare<S,T>::less(s,min_val))
{
return min_val;
}
else
{
return static_cast<T>(s);
}
}
} // ns mapnik
#endif // MAPNIK_SAFE_CAST_HPP
|