Commit 72bf719772117eead2e9a354f6c92cd926d58c72

Authored by m-holger
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 63 class IntConverter<From, To, false, false>
64 64 {
65 65 public:
66   - static To
  66 + inline static To
67 67 convert(From const& i)
68 68 {
69 69 // From and To are both unsigned.
70 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 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 88 template <typename From, typename To>
83 89 class IntConverter<From, To, true, true>
84 90 {
85 91 public:
86   - static To
  92 + inline static To
87 93 convert(From const& i)
88 94 {
89 95 // From and To are both signed.
90 96 if ((i < std::numeric_limits<To>::min()) ||
91 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 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 115 template <typename From, typename To>
104 116 class IntConverter<From, To, true, false>
105 117 {
106 118 public:
107   - static To
  119 + inline static To
108 120 convert(From const& i)
109 121 {
110 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 124 // compare with To's max.
113 125 auto ii = static_cast<typename to_u<From>::type>(i);
114 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 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 144 template <typename From, typename To>
127 145 class IntConverter<From, To, false, true>
128 146 {
129 147 public:
130   - static To
  148 + inline static To
131 149 convert(From const& i)
132 150 {
133 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 153 auto maxval = static_cast<typename to_u<To>::type>(
136 154 std::numeric_limits<To>::max());
137 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 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 173 // Specific converters. The return type of each function must match
150 174 // the second template parameter to IntConverter.
151 175 template <typename T>
152   - char
  176 + inline char
153 177 to_char(T const& i)
154 178 {
155 179 return IntConverter<T, char>::convert(i);
156 180 }
157 181  
158 182 template <typename T>
159   - unsigned char
  183 + inline unsigned char
160 184 to_uchar(T const& i)
161 185 {
162 186 return IntConverter<T, unsigned char>::convert(i);
163 187 }
164 188  
165 189 template <typename T>
166   - short
  190 + inline short
167 191 to_short(T const& i)
168 192 {
169 193 return IntConverter<T, short>::convert(i);
170 194 }
171 195  
172 196 template <typename T>
173   - unsigned short
  197 + inline unsigned short
174 198 to_ushort(T const& i)
175 199 {
176 200 return IntConverter<T, unsigned short>::convert(i);
177 201 }
178 202  
179 203 template <typename T>
180   - int
  204 + inline int
181 205 to_int(T const& i)
182 206 {
183 207 return IntConverter<T, int>::convert(i);
184 208 }
185 209  
186 210 template <typename T>
187   - unsigned int
  211 + inline unsigned int
188 212 to_uint(T const& i)
189 213 {
190 214 return IntConverter<T, unsigned int>::convert(i);
191 215 }
192 216  
193 217 template <typename T>
194   - size_t
  218 + inline size_t
195 219 to_size(T const& i)
196 220 {
197 221 return IntConverter<T, size_t>::convert(i);
198 222 }
199 223  
200 224 template <typename T>
201   - qpdf_offset_t
  225 + inline qpdf_offset_t
202 226 to_offset(T const& i)
203 227 {
204 228 return IntConverter<T, qpdf_offset_t>::convert(i);
205 229 }
206 230  
207 231 template <typename T>
208   - long
  232 + inline long
209 233 to_long(T const& i)
210 234 {
211 235 return IntConverter<T, long>::convert(i);
212 236 }
213 237  
214 238 template <typename T>
215   - unsigned long
  239 + inline unsigned long
216 240 to_ulong(T const& i)
217 241 {
218 242 return IntConverter<T, unsigned long>::convert(i);
219 243 }
220 244  
221 245 template <typename T>
222   - long long
  246 + inline long long
223 247 to_longlong(T const& i)
224 248 {
225 249 return IntConverter<T, long long>::convert(i);
226 250 }
227 251  
228 252 template <typename T>
229   - unsigned long long
  253 + inline unsigned long long
230 254 to_ulonglong(T const& i)
231 255 {
232 256 return IntConverter<T, unsigned long long>::convert(i);
... ... @@ -234,12 +258,8 @@ namespace QIntC // QIntC = qpdf Integer Conversion
234 258  
235 259 template <typename T>
236 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 263 if ((delta > 0) && ((std::numeric_limits<T>::max() - cur) < delta)) {
244 264 std::ostringstream msg;
245 265 msg.imbue(std::locale::classic());
... ... @@ -257,13 +277,19 @@ namespace QIntC // QIntC = qpdf Integer Conversion
257 277 }
258 278  
259 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 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 293 if ((delta > 0) && ((std::numeric_limits<T>::min() + delta) > cur)) {
268 294 std::ostringstream msg;
269 295 msg.imbue(std::locale::classic());
... ... @@ -279,6 +305,16 @@ namespace QIntC // QIntC = qpdf Integer Conversion
279 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 318 }; // namespace QIntC
283 319  
284 320 #endif // QINTC_HH
... ...