Commit 72bf719772117eead2e9a354f6c92cd926d58c72
Committed by
Jay Berkenbilt
1 parent
deb1c330
Inline QIntC functions
Showing
1 changed file
with
84 additions
and
48 deletions
include/qpdf/QIntC.hh
| @@ -63,48 +63,60 @@ namespace QIntC // QIntC = qpdf Integer Conversion | @@ -63,48 +63,60 @@ namespace QIntC // QIntC = qpdf Integer Conversion | ||
| 63 | class IntConverter<From, To, false, false> | 63 | class IntConverter<From, To, false, false> |
| 64 | { | 64 | { |
| 65 | public: | 65 | public: |
| 66 | - static To | 66 | + inline static To |
| 67 | convert(From const& i) | 67 | convert(From const& i) |
| 68 | { | 68 | { |
| 69 | // From and To are both unsigned. | 69 | // From and To are both unsigned. |
| 70 | if (i > std::numeric_limits<To>::max()) { | 70 | if (i > std::numeric_limits<To>::max()) { |
| 71 | - std::ostringstream msg; | ||
| 72 | - msg.imbue(std::locale::classic()); | ||
| 73 | - msg << "integer out of range converting " << i << " from a " | ||
| 74 | - << sizeof(From) << "-byte unsigned type to a " << sizeof(To) | ||
| 75 | - << "-byte unsigned type"; | ||
| 76 | - throw std::range_error(msg.str()); | 71 | + error(i); |
| 77 | } | 72 | } |
| 78 | return static_cast<To>(i); | 73 | return static_cast<To>(i); |
| 79 | } | 74 | } |
| 75 | + | ||
| 76 | + static void | ||
| 77 | + error(From i) | ||
| 78 | + { | ||
| 79 | + std::ostringstream msg; | ||
| 80 | + msg.imbue(std::locale::classic()); | ||
| 81 | + msg << "integer out of range converting " << i << " from a " | ||
| 82 | + << sizeof(From) << "-byte unsigned type to a " << sizeof(To) | ||
| 83 | + << "-byte unsigned type"; | ||
| 84 | + throw std::range_error(msg.str()); | ||
| 85 | + } | ||
| 80 | }; | 86 | }; |
| 81 | 87 | ||
| 82 | template <typename From, typename To> | 88 | template <typename From, typename To> |
| 83 | class IntConverter<From, To, true, true> | 89 | class IntConverter<From, To, true, true> |
| 84 | { | 90 | { |
| 85 | public: | 91 | public: |
| 86 | - static To | 92 | + inline static To |
| 87 | convert(From const& i) | 93 | convert(From const& i) |
| 88 | { | 94 | { |
| 89 | // From and To are both signed. | 95 | // From and To are both signed. |
| 90 | if ((i < std::numeric_limits<To>::min()) || | 96 | if ((i < std::numeric_limits<To>::min()) || |
| 91 | (i > std::numeric_limits<To>::max())) { | 97 | (i > std::numeric_limits<To>::max())) { |
| 92 | - std::ostringstream msg; | ||
| 93 | - msg.imbue(std::locale::classic()); | ||
| 94 | - msg << "integer out of range converting " << i << " from a " | ||
| 95 | - << sizeof(From) << "-byte signed type to a " << sizeof(To) | ||
| 96 | - << "-byte signed type"; | ||
| 97 | - throw std::range_error(msg.str()); | 98 | + error(i); |
| 98 | } | 99 | } |
| 99 | return static_cast<To>(i); | 100 | return static_cast<To>(i); |
| 100 | } | 101 | } |
| 102 | + | ||
| 103 | + static void | ||
| 104 | + error(From i) | ||
| 105 | + { | ||
| 106 | + std::ostringstream msg; | ||
| 107 | + msg.imbue(std::locale::classic()); | ||
| 108 | + msg << "integer out of range converting " << i << " from a " | ||
| 109 | + << sizeof(From) << "-byte signed type to a " << sizeof(To) | ||
| 110 | + << "-byte signed type"; | ||
| 111 | + throw std::range_error(msg.str()); | ||
| 112 | + } | ||
| 101 | }; | 113 | }; |
| 102 | 114 | ||
| 103 | template <typename From, typename To> | 115 | template <typename From, typename To> |
| 104 | class IntConverter<From, To, true, false> | 116 | class IntConverter<From, To, true, false> |
| 105 | { | 117 | { |
| 106 | public: | 118 | public: |
| 107 | - static To | 119 | + inline static To |
| 108 | convert(From const& i) | 120 | convert(From const& i) |
| 109 | { | 121 | { |
| 110 | // From is signed, and To is unsigned. If i > 0, it's safe to | 122 | // From is signed, and To is unsigned. If i > 0, it's safe to |
| @@ -112,22 +124,28 @@ namespace QIntC // QIntC = qpdf Integer Conversion | @@ -112,22 +124,28 @@ namespace QIntC // QIntC = qpdf Integer Conversion | ||
| 112 | // compare with To's max. | 124 | // compare with To's max. |
| 113 | auto ii = static_cast<typename to_u<From>::type>(i); | 125 | auto ii = static_cast<typename to_u<From>::type>(i); |
| 114 | if ((i < 0) || (ii > std::numeric_limits<To>::max())) { | 126 | if ((i < 0) || (ii > std::numeric_limits<To>::max())) { |
| 115 | - std::ostringstream msg; | ||
| 116 | - msg.imbue(std::locale::classic()); | ||
| 117 | - msg << "integer out of range converting " << i << " from a " | ||
| 118 | - << sizeof(From) << "-byte signed type to a " << sizeof(To) | ||
| 119 | - << "-byte unsigned type"; | ||
| 120 | - throw std::range_error(msg.str()); | 127 | + error(i); |
| 121 | } | 128 | } |
| 122 | return static_cast<To>(i); | 129 | return static_cast<To>(i); |
| 123 | } | 130 | } |
| 131 | + | ||
| 132 | + static void | ||
| 133 | + error(From i) | ||
| 134 | + { | ||
| 135 | + std::ostringstream msg; | ||
| 136 | + msg.imbue(std::locale::classic()); | ||
| 137 | + msg << "integer out of range converting " << i << " from a " | ||
| 138 | + << sizeof(From) << "-byte signed type to a " << sizeof(To) | ||
| 139 | + << "-byte unsigned type"; | ||
| 140 | + throw std::range_error(msg.str()); | ||
| 141 | + } | ||
| 124 | }; | 142 | }; |
| 125 | 143 | ||
| 126 | template <typename From, typename To> | 144 | template <typename From, typename To> |
| 127 | class IntConverter<From, To, false, true> | 145 | class IntConverter<From, To, false, true> |
| 128 | { | 146 | { |
| 129 | public: | 147 | public: |
| 130 | - static To | 148 | + inline static To |
| 131 | convert(From const& i) | 149 | convert(From const& i) |
| 132 | { | 150 | { |
| 133 | // From is unsigned, and to is signed. Convert To's max to the | 151 | // From is unsigned, and to is signed. Convert To's max to the |
| @@ -135,98 +153,104 @@ namespace QIntC // QIntC = qpdf Integer Conversion | @@ -135,98 +153,104 @@ namespace QIntC // QIntC = qpdf Integer Conversion | ||
| 135 | auto maxval = static_cast<typename to_u<To>::type>( | 153 | auto maxval = static_cast<typename to_u<To>::type>( |
| 136 | std::numeric_limits<To>::max()); | 154 | std::numeric_limits<To>::max()); |
| 137 | if (i > maxval) { | 155 | if (i > maxval) { |
| 138 | - std::ostringstream msg; | ||
| 139 | - msg.imbue(std::locale::classic()); | ||
| 140 | - msg << "integer out of range converting " << i << " from a " | ||
| 141 | - << sizeof(From) << "-byte unsigned type to a " << sizeof(To) | ||
| 142 | - << "-byte signed type"; | ||
| 143 | - throw std::range_error(msg.str()); | 156 | + error(i); |
| 144 | } | 157 | } |
| 145 | return static_cast<To>(i); | 158 | return static_cast<To>(i); |
| 146 | } | 159 | } |
| 160 | + | ||
| 161 | + static void | ||
| 162 | + error(From i) | ||
| 163 | + { | ||
| 164 | + std::ostringstream msg; | ||
| 165 | + msg.imbue(std::locale::classic()); | ||
| 166 | + msg << "integer out of range converting " << i << " from a " | ||
| 167 | + << sizeof(From) << "-byte unsigned type to a " << sizeof(To) | ||
| 168 | + << "-byte signed type"; | ||
| 169 | + throw std::range_error(msg.str()); | ||
| 170 | + } | ||
| 147 | }; | 171 | }; |
| 148 | 172 | ||
| 149 | // Specific converters. The return type of each function must match | 173 | // Specific converters. The return type of each function must match |
| 150 | // the second template parameter to IntConverter. | 174 | // the second template parameter to IntConverter. |
| 151 | template <typename T> | 175 | template <typename T> |
| 152 | - char | 176 | + inline char |
| 153 | to_char(T const& i) | 177 | to_char(T const& i) |
| 154 | { | 178 | { |
| 155 | return IntConverter<T, char>::convert(i); | 179 | return IntConverter<T, char>::convert(i); |
| 156 | } | 180 | } |
| 157 | 181 | ||
| 158 | template <typename T> | 182 | template <typename T> |
| 159 | - unsigned char | 183 | + inline unsigned char |
| 160 | to_uchar(T const& i) | 184 | to_uchar(T const& i) |
| 161 | { | 185 | { |
| 162 | return IntConverter<T, unsigned char>::convert(i); | 186 | return IntConverter<T, unsigned char>::convert(i); |
| 163 | } | 187 | } |
| 164 | 188 | ||
| 165 | template <typename T> | 189 | template <typename T> |
| 166 | - short | 190 | + inline short |
| 167 | to_short(T const& i) | 191 | to_short(T const& i) |
| 168 | { | 192 | { |
| 169 | return IntConverter<T, short>::convert(i); | 193 | return IntConverter<T, short>::convert(i); |
| 170 | } | 194 | } |
| 171 | 195 | ||
| 172 | template <typename T> | 196 | template <typename T> |
| 173 | - unsigned short | 197 | + inline unsigned short |
| 174 | to_ushort(T const& i) | 198 | to_ushort(T const& i) |
| 175 | { | 199 | { |
| 176 | return IntConverter<T, unsigned short>::convert(i); | 200 | return IntConverter<T, unsigned short>::convert(i); |
| 177 | } | 201 | } |
| 178 | 202 | ||
| 179 | template <typename T> | 203 | template <typename T> |
| 180 | - int | 204 | + inline int |
| 181 | to_int(T const& i) | 205 | to_int(T const& i) |
| 182 | { | 206 | { |
| 183 | return IntConverter<T, int>::convert(i); | 207 | return IntConverter<T, int>::convert(i); |
| 184 | } | 208 | } |
| 185 | 209 | ||
| 186 | template <typename T> | 210 | template <typename T> |
| 187 | - unsigned int | 211 | + inline unsigned int |
| 188 | to_uint(T const& i) | 212 | to_uint(T const& i) |
| 189 | { | 213 | { |
| 190 | return IntConverter<T, unsigned int>::convert(i); | 214 | return IntConverter<T, unsigned int>::convert(i); |
| 191 | } | 215 | } |
| 192 | 216 | ||
| 193 | template <typename T> | 217 | template <typename T> |
| 194 | - size_t | 218 | + inline size_t |
| 195 | to_size(T const& i) | 219 | to_size(T const& i) |
| 196 | { | 220 | { |
| 197 | return IntConverter<T, size_t>::convert(i); | 221 | return IntConverter<T, size_t>::convert(i); |
| 198 | } | 222 | } |
| 199 | 223 | ||
| 200 | template <typename T> | 224 | template <typename T> |
| 201 | - qpdf_offset_t | 225 | + inline qpdf_offset_t |
| 202 | to_offset(T const& i) | 226 | to_offset(T const& i) |
| 203 | { | 227 | { |
| 204 | return IntConverter<T, qpdf_offset_t>::convert(i); | 228 | return IntConverter<T, qpdf_offset_t>::convert(i); |
| 205 | } | 229 | } |
| 206 | 230 | ||
| 207 | template <typename T> | 231 | template <typename T> |
| 208 | - long | 232 | + inline long |
| 209 | to_long(T const& i) | 233 | to_long(T const& i) |
| 210 | { | 234 | { |
| 211 | return IntConverter<T, long>::convert(i); | 235 | return IntConverter<T, long>::convert(i); |
| 212 | } | 236 | } |
| 213 | 237 | ||
| 214 | template <typename T> | 238 | template <typename T> |
| 215 | - unsigned long | 239 | + inline unsigned long |
| 216 | to_ulong(T const& i) | 240 | to_ulong(T const& i) |
| 217 | { | 241 | { |
| 218 | return IntConverter<T, unsigned long>::convert(i); | 242 | return IntConverter<T, unsigned long>::convert(i); |
| 219 | } | 243 | } |
| 220 | 244 | ||
| 221 | template <typename T> | 245 | template <typename T> |
| 222 | - long long | 246 | + inline long long |
| 223 | to_longlong(T const& i) | 247 | to_longlong(T const& i) |
| 224 | { | 248 | { |
| 225 | return IntConverter<T, long long>::convert(i); | 249 | return IntConverter<T, long long>::convert(i); |
| 226 | } | 250 | } |
| 227 | 251 | ||
| 228 | template <typename T> | 252 | template <typename T> |
| 229 | - unsigned long long | 253 | + inline unsigned long long |
| 230 | to_ulonglong(T const& i) | 254 | to_ulonglong(T const& i) |
| 231 | { | 255 | { |
| 232 | return IntConverter<T, unsigned long long>::convert(i); | 256 | return IntConverter<T, unsigned long long>::convert(i); |
| @@ -234,12 +258,8 @@ namespace QIntC // QIntC = qpdf Integer Conversion | @@ -234,12 +258,8 @@ namespace QIntC // QIntC = qpdf Integer Conversion | ||
| 234 | 258 | ||
| 235 | template <typename T> | 259 | template <typename T> |
| 236 | void | 260 | void |
| 237 | - range_check(T const& cur, T const& delta) | 261 | + range_check_error(T const& cur, T const& delta) |
| 238 | { | 262 | { |
| 239 | - if ((delta > 0) != (cur > 0)) { | ||
| 240 | - return; | ||
| 241 | - } | ||
| 242 | - | ||
| 243 | if ((delta > 0) && ((std::numeric_limits<T>::max() - cur) < delta)) { | 263 | if ((delta > 0) && ((std::numeric_limits<T>::max() - cur) < delta)) { |
| 244 | std::ostringstream msg; | 264 | std::ostringstream msg; |
| 245 | msg.imbue(std::locale::classic()); | 265 | msg.imbue(std::locale::classic()); |
| @@ -257,13 +277,19 @@ namespace QIntC // QIntC = qpdf Integer Conversion | @@ -257,13 +277,19 @@ namespace QIntC // QIntC = qpdf Integer Conversion | ||
| 257 | } | 277 | } |
| 258 | 278 | ||
| 259 | template <typename T> | 279 | template <typename T> |
| 260 | - void | ||
| 261 | - range_check_substract(T const& cur, T const& delta) | 280 | + inline void |
| 281 | + range_check(T const& cur, T const& delta) | ||
| 262 | { | 282 | { |
| 263 | - if ((delta >= 0) == (cur >= 0)) { | 283 | + if ((delta > 0) != (cur > 0)) { |
| 264 | return; | 284 | return; |
| 265 | } | 285 | } |
| 286 | + QIntC::range_check_error<T>(cur, delta); | ||
| 287 | + } | ||
| 266 | 288 | ||
| 289 | + template <typename T> | ||
| 290 | + void | ||
| 291 | + range_check_substract_error(T const& cur, T const& delta) | ||
| 292 | + { | ||
| 267 | if ((delta > 0) && ((std::numeric_limits<T>::min() + delta) > cur)) { | 293 | if ((delta > 0) && ((std::numeric_limits<T>::min() + delta) > cur)) { |
| 268 | std::ostringstream msg; | 294 | std::ostringstream msg; |
| 269 | msg.imbue(std::locale::classic()); | 295 | msg.imbue(std::locale::classic()); |
| @@ -279,6 +305,16 @@ namespace QIntC // QIntC = qpdf Integer Conversion | @@ -279,6 +305,16 @@ namespace QIntC // QIntC = qpdf Integer Conversion | ||
| 279 | throw std::range_error(msg.str()); | 305 | throw std::range_error(msg.str()); |
| 280 | } | 306 | } |
| 281 | } | 307 | } |
| 308 | + | ||
| 309 | + template <typename T> | ||
| 310 | + inline void | ||
| 311 | + range_check_substract(T const& cur, T const& delta) | ||
| 312 | + { | ||
| 313 | + if ((delta >= 0) == (cur >= 0)) { | ||
| 314 | + return; | ||
| 315 | + } | ||
| 316 | + QIntC::range_check_substract_error<T>(cur, delta); | ||
| 317 | + } | ||
| 282 | }; // namespace QIntC | 318 | }; // namespace QIntC |
| 283 | 319 | ||
| 284 | #endif // QINTC_HH | 320 | #endif // QINTC_HH |