Commit 8a87a2fbeed0d42d6a32e20c9e144672627b6b71

Authored by Jay Berkenbilt
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,60 +29,20 @@
29 #include <limits> 29 #include <limits>
30 #include <sstream> 30 #include <sstream>
31 #include <cassert> 31 #include <cassert>
  32 +#include <type_traits>
32 33
33 // This namespace provides safe integer conversion that detects 34 // This namespace provides safe integer conversion that detects
34 // overflows. It uses short, cryptic names for brevity. 35 // overflows. It uses short, cryptic names for brevity.
35 36
36 namespace QIntC // QIntC = qpdf Integer Conversion 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 template <typename T> 41 template <typename T>
42 class to_u 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 public: 44 public:
85 - typedef unsigned long long type; 45 + typedef typename std::make_unsigned<T>::type type;
86 }; 46 };
87 47
88 // Basic IntConverter class, which converts an integer from the 48 // Basic IntConverter class, which converts an integer from the
@@ -147,8 +107,7 @@ namespace QIntC // QIntC = qpdf Integer Conversion @@ -147,8 +107,7 @@ namespace QIntC // QIntC = qpdf Integer Conversion
147 // From is signed, and To is unsigned. If i > 0, it's safe to 107 // From is signed, and To is unsigned. If i > 0, it's safe to
148 // convert it to the corresponding unsigned type and to 108 // convert it to the corresponding unsigned type and to
149 // compare with To's max. 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 if ((i < 0) || (ii > std::numeric_limits<To>::max())) 111 if ((i < 0) || (ii > std::numeric_limits<To>::max()))
153 { 112 {
154 std::ostringstream msg; 113 std::ostringstream msg;
@@ -170,9 +129,8 @@ namespace QIntC // QIntC = qpdf Integer Conversion @@ -170,9 +129,8 @@ namespace QIntC // QIntC = qpdf Integer Conversion
170 { 129 {
171 // From is unsigned, and to is signed. Convert To's max to the 130 // From is unsigned, and to is signed. Convert To's max to the
172 // unsigned version of To and compare i against that. 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 if (i > maxval) 134 if (i > maxval)
177 { 135 {
178 std::ostringstream msg; 136 std::ostringstream msg;