diff options
author | Jay Berkenbilt <ejb@ql.org> | 2020-04-03 18:05:50 +0200 |
---|---|---|
committer | Jay Berkenbilt <ejb@ql.org> | 2020-04-03 18:17:57 +0200 |
commit | 8a87a2fbeed0d42d6a32e20c9e144672627b6b71 (patch) | |
tree | d855c76e8ad549f52d5925e7f0e01802735b765c | |
parent | a6f1f829dbad51ef0a6a5d9262e5c49e32dba45a (diff) | |
download | qpdf-8a87a2fbeed0d42d6a32e20c9e144672627b6b71.tar.zst |
Use C++11 features to simplify QIntC
-rw-r--r-- | include/qpdf/QIntC.hh | 56 |
1 files changed, 7 insertions, 49 deletions
diff --git a/include/qpdf/QIntC.hh b/include/qpdf/QIntC.hh index fed8e781..906eff77 100644 --- a/include/qpdf/QIntC.hh +++ b/include/qpdf/QIntC.hh @@ -29,60 +29,20 @@ #include <limits> #include <sstream> #include <cassert> +#include <type_traits> // This namespace provides safe integer conversion that detects // overflows. It uses short, cryptic names for brevity. namespace QIntC // QIntC = qpdf Integer Conversion { - // Create templates to get the unsigned version of integer types. - // With C++11, we could use std::make_unsigned, but qpdf, at least - // for now, supports pre-c++11 compilers. + // to_u is here for backward-compatibility from before we required + // C++-11. template <typename T> class to_u { - }; - - template <> - class to_u<char> - { - public: - typedef unsigned char type; - }; - - template <> - class to_u<signed char> - { - public: - typedef unsigned char type; - }; - - template <> - class to_u<short> - { - public: - typedef unsigned short type; - }; - - template <> - class to_u<int> - { - public: - typedef unsigned int type; - }; - - template <> - class to_u<long> - { - public: - typedef unsigned long type; - }; - - template <> - class to_u<long long> - { public: - typedef unsigned long long type; + typedef typename std::make_unsigned<T>::type type; }; // Basic IntConverter class, which converts an integer from the @@ -147,8 +107,7 @@ namespace QIntC // QIntC = qpdf Integer Conversion // From is signed, and To is unsigned. If i > 0, it's safe to // convert it to the corresponding unsigned type and to // compare with To's max. - typename to_u<From>::type ii = - static_cast<typename to_u<From>::type>(i); + auto ii = static_cast<typename to_u<From>::type>(i); if ((i < 0) || (ii > std::numeric_limits<To>::max())) { std::ostringstream msg; @@ -170,9 +129,8 @@ namespace QIntC // QIntC = qpdf Integer Conversion { // From is unsigned, and to is signed. Convert To's max to the // unsigned version of To and compare i against that. - typename to_u<To>::type maxval = - static_cast<typename to_u<To>::type>( - std::numeric_limits<To>::max()); + auto maxval = static_cast<typename to_u<To>::type>( + std::numeric_limits<To>::max()); if (i > maxval) { std::ostringstream msg; |