Commit 8a87a2fbeed0d42d6a32e20c9e144672627b6b71
1 parent
a6f1f829
Use C++11 features to simplify QIntC
Showing
1 changed file
with
7 additions
and
49 deletions
include/qpdf/QIntC.hh
| ... | ... | @@ -29,60 +29,20 @@ |
| 29 | 29 | #include <limits> |
| 30 | 30 | #include <sstream> |
| 31 | 31 | #include <cassert> |
| 32 | +#include <type_traits> | |
| 32 | 33 | |
| 33 | 34 | // This namespace provides safe integer conversion that detects |
| 34 | 35 | // overflows. It uses short, cryptic names for brevity. |
| 35 | 36 | |
| 36 | 37 | namespace QIntC // QIntC = qpdf Integer Conversion |
| 37 | 38 | { |
| 38 | - // Create templates to get the unsigned version of integer types. | |
| 39 | - // With C++11, we could use std::make_unsigned, but qpdf, at least | |
| 40 | - // for now, supports pre-c++11 compilers. | |
| 39 | + // to_u is here for backward-compatibility from before we required | |
| 40 | + // C++-11. | |
| 41 | 41 | template <typename T> |
| 42 | 42 | class to_u |
| 43 | 43 | { |
| 44 | - }; | |
| 45 | - | |
| 46 | - template <> | |
| 47 | - class to_u<char> | |
| 48 | - { | |
| 49 | - public: | |
| 50 | - typedef unsigned char type; | |
| 51 | - }; | |
| 52 | - | |
| 53 | - template <> | |
| 54 | - class to_u<signed char> | |
| 55 | - { | |
| 56 | - public: | |
| 57 | - typedef unsigned char type; | |
| 58 | - }; | |
| 59 | - | |
| 60 | - template <> | |
| 61 | - class to_u<short> | |
| 62 | - { | |
| 63 | - public: | |
| 64 | - typedef unsigned short type; | |
| 65 | - }; | |
| 66 | - | |
| 67 | - template <> | |
| 68 | - class to_u<int> | |
| 69 | - { | |
| 70 | - public: | |
| 71 | - typedef unsigned int type; | |
| 72 | - }; | |
| 73 | - | |
| 74 | - template <> | |
| 75 | - class to_u<long> | |
| 76 | - { | |
| 77 | - public: | |
| 78 | - typedef unsigned long type; | |
| 79 | - }; | |
| 80 | - | |
| 81 | - template <> | |
| 82 | - class to_u<long long> | |
| 83 | - { | |
| 84 | 44 | public: |
| 85 | - typedef unsigned long long type; | |
| 45 | + typedef typename std::make_unsigned<T>::type type; | |
| 86 | 46 | }; |
| 87 | 47 | |
| 88 | 48 | // Basic IntConverter class, which converts an integer from the |
| ... | ... | @@ -147,8 +107,7 @@ namespace QIntC // QIntC = qpdf Integer Conversion |
| 147 | 107 | // From is signed, and To is unsigned. If i > 0, it's safe to |
| 148 | 108 | // convert it to the corresponding unsigned type and to |
| 149 | 109 | // compare with To's max. |
| 150 | - typename to_u<From>::type ii = | |
| 151 | - static_cast<typename to_u<From>::type>(i); | |
| 110 | + auto ii = static_cast<typename to_u<From>::type>(i); | |
| 152 | 111 | if ((i < 0) || (ii > std::numeric_limits<To>::max())) |
| 153 | 112 | { |
| 154 | 113 | std::ostringstream msg; |
| ... | ... | @@ -170,9 +129,8 @@ namespace QIntC // QIntC = qpdf Integer Conversion |
| 170 | 129 | { |
| 171 | 130 | // From is unsigned, and to is signed. Convert To's max to the |
| 172 | 131 | // unsigned version of To and compare i against that. |
| 173 | - typename to_u<To>::type maxval = | |
| 174 | - static_cast<typename to_u<To>::type>( | |
| 175 | - std::numeric_limits<To>::max()); | |
| 132 | + auto maxval = static_cast<typename to_u<To>::type>( | |
| 133 | + std::numeric_limits<To>::max()); | |
| 176 | 134 | if (i > maxval) |
| 177 | 135 | { |
| 178 | 136 | std::ostringstream msg; | ... | ... |