Click
Loading...
Searching...
No Matches
type_traits.hh
Go to the documentation of this file.
1#ifndef CLICK_TYPE_TRAITS_HH
2#define CLICK_TYPE_TRAITS_HH
3CLICK_DECLS
4
21template <typename T, T V>
23 typedef integral_constant<T, V> type;
24 typedef T value_type;
25 static constexpr T value = V;
26};
27template <typename T, T V> constexpr T integral_constant<T, V>::value;
28
32
36
37
43template <bool B, typename T, typename F> struct conditional {};
44
45template <typename T, typename F>
46struct conditional<true, T, F> {
47 typedef T type;
48};
49
50template <typename T, typename F>
51struct conditional<false, T, F> {
52 typedef F type;
53};
54
55
61#if HAVE___IS_TRIVIALLY_COPYABLE
62template <typename T> struct has_trivial_copy : public integral_constant<bool, __is_trivially_copyable(T)> {};
63#elif HAVE___HAS_TRIVIAL_COPY
64template <typename T> struct has_trivial_copy : public integral_constant<bool, __has_trivial_copy(T)> {};
65#else
66template <typename T> struct has_trivial_copy : public false_type {};
67template <> struct has_trivial_copy<unsigned char> : public true_type {};
68template <> struct has_trivial_copy<signed char> : public true_type {};
69template <> struct has_trivial_copy<char> : public true_type {};
70template <> struct has_trivial_copy<unsigned short> : public true_type {};
71template <> struct has_trivial_copy<short> : public true_type {};
72template <> struct has_trivial_copy<unsigned> : public true_type {};
73template <> struct has_trivial_copy<int> : public true_type {};
74template <> struct has_trivial_copy<unsigned long> : public true_type {};
75template <> struct has_trivial_copy<long> : public true_type {};
76# if HAVE_LONG_LONG
77template <> struct has_trivial_copy<unsigned long long> : public true_type {};
78template <> struct has_trivial_copy<long long> : public true_type {};
79# endif
80template <typename T> struct has_trivial_copy<T*> : public true_type {};
81#endif
82class IPAddress;
83template <> struct has_trivial_copy<IPAddress> : public true_type {};
84
90template <typename T> struct is_reference : public false_type {};
91template <typename T> struct is_reference<T&> : public true_type {};
92#if HAVE_CXX_RVALUE_REFERENCES
93template <typename T> struct is_reference<T&&> : public true_type {};
94#endif
95
102template <typename T> struct remove_reference {
103 typedef T type;
104};
105template <typename T> struct remove_reference<T&> {
106 typedef T type;
107};
108#if HAVE_CXX_RVALUE_REFERENCES
109template <typename T> struct remove_reference<T&&> {
110 typedef T type;
111};
112template <typename T>
113inline typename remove_reference<T>::type&& click_move(T&& x) {
114 return static_cast<typename remove_reference<T>::type&&>(x);
115}
116#endif
117
118template <typename T> struct remove_cv {
119 typedef T type;
120};
121template <typename T> struct remove_cv<const T> {
122 typedef T type;
123};
124template <typename T> struct remove_cv<volatile T> {
125 typedef T type;
126};
127template <typename T> struct remove_cv<const volatile T> {
128 typedef T type;
129};
130
131template <typename T, typename U> struct same_type : public false_type {};
132template <typename T> struct same_type<T, T> : public true_type {};
133
134template <typename T, typename U> struct types_compatible
135 : public same_type<typename remove_cv<T>::type, typename remove_cv<U>::type> {};
136
145template <typename T, bool use_reference = (!is_reference<T>::value
146 && (!has_trivial_copy<T>::value
147 || sizeof(T) > sizeof(void *)))>
149
150template <typename T> struct fast_argument<T, true> {
151 static constexpr bool is_reference = true;
152 typedef const T &type;
153#if HAVE_CXX_RVALUE_REFERENCES
154 typedef void enable_rvalue_reference;
155#endif
156};
157template <typename T> struct fast_argument<T, false> {
158 static constexpr bool is_reference = false;
159 typedef T type;
160};
161template <typename T> constexpr bool fast_argument<T, true>::is_reference;
162template <typename T> constexpr bool fast_argument<T, false>::is_reference;
163
164
167template <size_t S> struct char_array {
168 char x[S];
169} CLICK_SIZE_PACKED_ATTRIBUTE;
170template <size_t S> struct has_trivial_copy<char_array<S> > : public true_type {};
171
172
173#if HAVE_INT64_TYPES && (!HAVE_LONG_LONG || SIZEOF_LONG_LONG < 8)
174typedef int64_t click_intmax_t;
175typedef uint64_t click_uintmax_t;
176# define SIZEOF_CLICK_INTMAX_T 8
177# define CLICK_ERRHdMAX "^64d"
178# define CLICK_ERRHuMAX "^64u"
179# define CLICK_ERRHoMAX "^64o"
180# define CLICK_ERRHxMAX "^64x"
181# define CLICK_ERRHXMAX "^64X"
182#elif HAVE_LONG_LONG
183typedef long long click_intmax_t;
184typedef unsigned long long click_uintmax_t;
185# define SIZEOF_CLICK_INTMAX_T SIZEOF_LONG_LONG
186# define CLICK_ERRHdMAX "lld"
187# define CLICK_ERRHuMAX "llu"
188# define CLICK_ERRHoMAX "llo"
189# define CLICK_ERRHxMAX "llx"
190# define CLICK_ERRHXMAX "llX"
191#else
192typedef long click_intmax_t;
193typedef unsigned long click_uintmax_t;
194# define SIZEOF_CLICK_INTMAX_T SIZEOF_LONG
195# define CLICK_ERRHdMAX "ld"
196# define CLICK_ERRHuMAX "lu"
197# define CLICK_ERRHoMAX "lo"
198# define CLICK_ERRHxMAX "lx"
199# define CLICK_ERRHXMAX "lX"
200#endif
201
202typedef click_intmax_t click_int_large_t;
203typedef click_uintmax_t click_uint_large_t;
204#define SIZEOF_CLICK_INT_LARGE_T SIZEOF_CLICK_INTMAX_T
205#define CLICK_ERRHdLARGE CLICK_ERRHdMAX
206#define CLICK_ERRHuLARGE CLICK_ERRHuMAX
207#define CLICK_ERRHoLARGE CLICK_ERRHoMAX
208#define CLICK_ERRHxLARGE CLICK_ERRHxMAX
209#define CLICK_ERRHXLARGE CLICK_ERRHXMAX
210
211
243template <typename T>
245 static constexpr bool is_numeric = false;
246 static constexpr bool is_integral = false;
247};
248template <typename T> constexpr bool integer_traits<T>::is_numeric;
249template <typename T> constexpr bool integer_traits<T>::is_integral;
250
251template <>
252struct integer_traits<unsigned char> {
253 static constexpr bool is_numeric = true;
254 static constexpr bool is_integral = true;
255 static constexpr unsigned char const_min = 0;
256 static constexpr unsigned char const_max = ~const_min;
257 static constexpr bool is_signed = false;
258 typedef signed char signed_type;
259 typedef unsigned char unsigned_type;
260 typedef unsigned char type;
261 typedef click_uintmax_t max_type;
262 static bool negative(type) { return false; }
263};
264
265template <>
266struct integer_traits<signed char> {
267 static constexpr bool is_numeric = true;
268 static constexpr bool is_integral = true;
269 static constexpr signed char const_min = -128;
270 static constexpr signed char const_max = 127;
271 static constexpr bool is_signed = true;
272 typedef signed char signed_type;
273 typedef unsigned char unsigned_type;
274 typedef signed char type;
275 typedef click_intmax_t max_type;
276 static bool negative(type x) { return x < 0; }
277};
278
279#if __CHAR_UNSIGNED__
280template <>
281struct integer_traits<char> : public integer_traits<unsigned char> {
282 static constexpr char const_min = 0;
283 static constexpr char const_max = ~const_min;
284 typedef char type;
285 static bool negative(type) { return false; }
286};
287#else
288template <>
289struct integer_traits<char> : public integer_traits<signed char> {
290 static constexpr char const_min = -128;
291 static constexpr char const_max = 127;
292 typedef char type;
293 static bool negative(type x) { return x < 0; }
294};
295#endif
296
297template <>
298struct integer_traits<unsigned short> {
299 static constexpr bool is_numeric = true;
300 static constexpr bool is_integral = true;
301 static constexpr unsigned short const_min = 0;
302 static constexpr unsigned short const_max = ~const_min;
303 static constexpr bool is_signed = false;
304 typedef short signed_type;
305 typedef unsigned short unsigned_type;
306 typedef unsigned short type;
307 typedef click_uintmax_t max_type;
308 static bool negative(type) { return false; }
309};
310
311template <>
312struct integer_traits<short> {
313 static constexpr bool is_numeric = true;
314 static constexpr bool is_integral = true;
315 static constexpr short const_min = -32768;
316 static constexpr short const_max = 32767;
317 static constexpr bool is_signed = true;
318 typedef short signed_type;
319 typedef unsigned short unsigned_type;
320 typedef short type;
321 typedef click_intmax_t max_type;
322 static bool negative(type x) { return x < 0; }
323};
324
325template <>
326struct integer_traits<unsigned int> {
327 static constexpr bool is_numeric = true;
328 static constexpr bool is_integral = true;
329 static constexpr unsigned int const_min = 0;
330 static constexpr unsigned int const_max = ~const_min;
331 static constexpr bool is_signed = false;
332 typedef int signed_type;
333 typedef unsigned int unsigned_type;
334 typedef unsigned int type;
335 typedef click_uintmax_t max_type;
336 static bool negative(type) { return false; }
337};
338
339template <>
340struct integer_traits<int> {
341 static constexpr bool is_numeric = true;
342 static constexpr bool is_integral = true;
343 static constexpr int const_min = 1 << (8*SIZEOF_INT - 1);
344 static constexpr int const_max = (unsigned) const_min - 1;
345 static constexpr bool is_signed = true;
346 typedef int signed_type;
347 typedef unsigned int unsigned_type;
348 typedef int type;
349 typedef click_intmax_t max_type;
350 static bool negative(type x) { return x < 0; }
351};
352
353template <>
354struct integer_traits<unsigned long> {
355 static constexpr bool is_numeric = true;
356 static constexpr bool is_integral = true;
357 static constexpr unsigned long const_min = 0;
358 static constexpr unsigned long const_max = ~const_min;
359 static constexpr bool is_signed = false;
360 typedef long signed_type;
361 typedef unsigned long unsigned_type;
362 typedef unsigned long type;
363 typedef click_uintmax_t max_type;
364 static bool negative(type) { return false; }
365};
366
367template <>
368struct integer_traits<long> {
369 static constexpr bool is_numeric = true;
370 static constexpr bool is_integral = true;
371 static constexpr long const_min = (long) 1 << (8*SIZEOF_LONG - 1);
372 static constexpr long const_max = (unsigned long) const_min - 1;
373 static constexpr bool is_signed = true;
374 typedef long signed_type;
375 typedef unsigned long unsigned_type;
376 typedef long type;
377 typedef click_intmax_t max_type;
378 static bool negative(type x) { return x < 0; }
379};
380
381#if HAVE_LONG_LONG
382template <>
383struct integer_traits<unsigned long long> {
384 static constexpr bool is_numeric = true;
385 static constexpr bool is_integral = true;
386 static constexpr unsigned long long const_min = 0;
387 static constexpr unsigned long long const_max = ~const_min;
388 static constexpr bool is_signed = false;
389 typedef long long signed_type;
390 typedef unsigned long long unsigned_type;
391 typedef unsigned long long type;
392 typedef click_uintmax_t max_type;
393 static bool negative(type) { return false; }
394};
395
396template <>
397struct integer_traits<long long> {
398 static constexpr bool is_numeric = true;
399 static constexpr bool is_integral = true;
400 static constexpr long long const_min = (long long) 1 << (8*SIZEOF_LONG_LONG - 1);
401 static constexpr long long const_max = (unsigned long long) const_min - 1;
402 static constexpr bool is_signed = true;
403 typedef long long signed_type;
404 typedef unsigned long long unsigned_type;
405 typedef long long type;
406 typedef click_intmax_t max_type;
407 static bool negative(type x) { return x < 0; }
408};
409#endif
410
411#if HAVE_INT64_TYPES && !HAVE_INT64_IS_LONG && !HAVE_INT64_IS_LONG_LONG
412template <>
413struct integer_traits<uint64_t> {
414 static constexpr bool is_numeric = true;
415 static constexpr bool is_integral = true;
416 static constexpr uint64_t const_min = 0;
417 static constexpr uint64_t const_max = ~const_min;
418 static constexpr bool is_signed = false;
419 typedef int64_t signed_type;
420 typedef uint64_t unsigned_type;
421 typedef uint64_t type;
422 typedef click_uintmax_t max_type;
423 static bool negative(type) { return false; }
424};
425
426template <>
427struct integer_traits<int64_t> {
428 static constexpr bool is_numeric = true;
429 static constexpr bool is_integral = true;
430 static constexpr int64_t const_min = (int64_t) 1 << 63;
431 static constexpr int64_t const_max = (uint64_t) const_min - 1;
432 static constexpr bool is_signed = true;
433 typedef int64_t signed_type;
434 typedef uint64_t unsigned_type;
435 typedef int64_t type;
436 typedef click_intmax_t max_type;
437 static bool negative(type x) { return x < 0; }
438};
439#endif
440
441
449template <typename T>
451 typedef typename integer_traits<T>::signed_type type;
452};
453
461template <typename T>
463 typedef typename integer_traits<T>::unsigned_type type;
464};
465
466
468template <typename T, typename Thalf>
469struct make_fast_half_integer {
470 typedef T type;
471 typedef Thalf half_type;
472 static constexpr int half_bits = int(sizeof(type) * 4);
473 static constexpr type half_value = type(1) << half_bits;
474 static half_type low(type x) {
475 return x & (half_value - 1);
476 }
477 static half_type high(type x) {
478 return x >> (sizeof(type) * 4);
479 }
480};
485template <typename T> struct fast_half_integer : public make_fast_half_integer<T, T> {};
486
487#if SIZEOF_LONG >= 8 && SIZEOF_LONG <= 2 * SIZEOF_INT
488template <> struct fast_half_integer<unsigned long> : public make_fast_half_integer<unsigned long, unsigned int> {};
489#endif
490
491#if HAVE_LONG_LONG && SIZEOF_LONG_LONG <= 2 * SIZEOF_INT
492template <> struct fast_half_integer<unsigned long long> : public make_fast_half_integer<unsigned long long, unsigned int> {};
493#endif
494
495#if HAVE_INT64_TYPES && !HAVE_INT64_IS_LONG_LONG && !HAVE_INT64_IS_LONG && SIZEOF_INT >= 4
496template <> struct fast_half_integer<uint64_t> : public make_fast_half_integer<uint64_t, unsigned int> {};
497#endif
498
499
500template <int n, typename Limb, typename V>
501struct extract_integer_helper {
502 static void extract(const Limb *x, V &value) {
503 extract_integer_helper<n - 1, Limb, V>::extract(x + 1, value);
504 value = (value << (sizeof(Limb) * 8)) | *x;
505 }
506};
507
508template <typename Limb, typename V>
509struct extract_integer_helper<1, Limb, V> {
510 static void extract(const Limb *x, V &value) {
511 value = x[0];
512 }
513};
514
516template <typename Limb, typename V>
517inline void extract_integer(const Limb *x, V &value) {
518 extract_integer_helper<
519 int((sizeof(V) + sizeof(Limb) - 1) / sizeof(Limb)), Limb, V
520 >::extract(x, value);
521}
522
523CLICK_ENDDECLS
524#endif
An IPv4 address.
Definition ipaddress.hh:16
Type wrapper for the constant false.
Type wrapper for the constant true.
Template defining a fixed-size character array.
Definition type_traits.hh:167
Conditional type transformation.
Definition type_traits.hh:43
Template defining a fast argument type for objects of type T.
Definition type_traits.hh:148
Type transformation for big integers.
Definition type_traits.hh:485
Template determining whether T may be copied by memcpy.
Definition type_traits.hh:66
Numeric traits template.
Definition type_traits.hh:244
Type wrapper for an integral constant V.
Definition type_traits.hh:22
Template determining whether T is a reference type.
Definition type_traits.hh:90
Signed integer type transformation.
Definition type_traits.hh:450
Unsigned integer type transformation.
Definition type_traits.hh:462
Template removing any reference layer from T.
Definition type_traits.hh:102
void extract_integer(const Limb *x, V &value)
Extract an integral type from a multi-limb integer.
Definition type_traits.hh:517