Commit 3670b290 authored by Eddie Kohler's avatar Eddie Kohler

NumericTraits: Add new template class defining interesting integer facts.

NumericTraits<T> (where T is integral) defines:
- const bool is_numeric = true
- const bool is_integer = true
- const T const_min (minimum value of the type)
- const T const_max (maximum value of the type)
- const bool is_signed (true if type is signed)
- typedef signed_type (signed version of the type)
- typedef unsigned_type (unsigned version of the type)

NumericTraits<T> (where T is not integral) defines:
- const bool is_numeric = false
- const bool is_integer = false
Signed-off-by: default avatarEddie Kohler <ekohler@gmail.com>
parent 4365e758
......@@ -2,6 +2,11 @@
#ifndef CLICK_CONFIG_H
#define CLICK_CONFIG_H
/* Define to 1 if type `char' is unsigned and you are not using gcc. */
#ifndef __CHAR_UNSIGNED__
# undef __CHAR_UNSIGNED__
#endif
/* Define to byte order of target machine. */
#undef CLICK_BYTE_ORDER
#define CLICK_BIG_ENDIAN 4321
......
......@@ -6648,6 +6648,38 @@ _ACEOF
{ $as_echo "$as_me:${as_lineno-$LINENO}: checking whether char is unsigned" >&5
$as_echo_n "checking whether char is unsigned... " >&6; }
if test "${ac_cv_c_char_unsigned+set}" = set; then :
$as_echo_n "(cached) " >&6
else
cat confdefs.h - <<_ACEOF >conftest.$ac_ext
/* end confdefs.h. */
$ac_includes_default
int
main ()
{
static int test_array [1 - 2 * !(((char) -1) < 0)];
test_array [0] = 0
;
return 0;
}
_ACEOF
if ac_fn_cxx_try_compile "$LINENO"; then :
ac_cv_c_char_unsigned=no
else
ac_cv_c_char_unsigned=yes
fi
rm -f core conftest.err conftest.$ac_objext conftest.$ac_ext
fi
{ $as_echo "$as_me:${as_lineno-$LINENO}: result: $ac_cv_c_char_unsigned" >&5
$as_echo "$ac_cv_c_char_unsigned" >&6; }
if test $ac_cv_c_char_unsigned = yes && test "$GCC" != yes; then
$as_echo "#define __CHAR_UNSIGNED__ 1" >>confdefs.h
fi
for ac_header in inttypes.h
do :
ac_fn_cxx_check_header_mongrel "$LINENO" "inttypes.h" "ac_cv_header_inttypes_h" "$ac_includes_default"
......@@ -6981,7 +7013,7 @@ else
ac_cv_endian=0
cat > conftest.$ac_ext <<EOF
#line 6984 "configure"
#line 7016 "configure"
#include "confdefs.h"
#include <$endian_hdr>
#ifdef __BYTE_ORDER
......
......@@ -8,7 +8,7 @@
CLICK_DECLS
/** @file <click/integers.hh>
* @brief Functions for manipulating integers.
* @brief Functions and types for manipulating integers.
*/
#if HAVE_INT64_TYPES && (!HAVE_LONG_LONG || SIZEOF_LONG_LONG <= 8)
......@@ -22,6 +22,170 @@ typedef long click_int_large_t;
typedef unsigned long click_uint_large_t;
#endif
/** @class NumericTraits
@brief Numeric traits template.
The NumericTraits template defines constants and type definitions related
to integers. Where T is an integer, NumericTraits<T> defines:
<dl>
<dt>const T const_min</dt>
<dd>The minimum value defined for the type.</dd>
<dt>const T const_max</dt>
<dd>The maximum value available for the type.</dd>
<dt>const bool is_numeric</dt>
<dd>True.</dd>
<dt>const bool is_integer</dt>
<dd>True.</dd>
<dt>const bool is_signed</dt>
<dd>True iff the type is signed.</dd>
<dt>signed_type (typedef)</dt>
<dd>Signed version of the type.</dd>
<dt>unsigned_type (typedef)</dt>
<dd>Unsigned version of the type.</dd>
</dl>
If T is <em>not</em> an integer, NumericTraits<T> defines:
<dl>
<dt>const bool is_numeric</dt>
<dd>False.</dd>
<dt>const bool is_integer</dt>
<dd>False.</dd>
</dl> */
template <typename T>
struct NumericTraits {
static const bool is_numeric = false;
static const bool is_integer = false;
};
template <>
struct NumericTraits<unsigned char> {
static const bool is_numeric = true;
static const bool is_integer = true;
static const unsigned char const_min = 0;
static const unsigned char const_max = ~const_min;
static const bool is_signed = false;
typedef signed char signed_type;
typedef unsigned char unsigned_type;
};
template <>
struct NumericTraits<signed char> {
static const bool is_numeric = true;
static const bool is_integer = true;
static const signed char const_min = -128;
static const signed char const_max = 127;
static const bool is_signed = true;
typedef signed char signed_type;
typedef unsigned char unsigned_type;
};
#if __CHAR_UNSIGNED__
template <>
struct NumericTraits<char> : public NumericTraits<unsigned char> {
static const char const_min = 0;
static const char const_max = ~const_min;
};
#else
template <>
struct NumericTraits<char> : public NumericTraits<signed char> {
static const char const_min = -128;
static const char const_max = 127;
};
#endif
template <>
struct NumericTraits<unsigned short> {
static const bool is_numeric = true;
static const bool is_integer = true;
static const unsigned short const_min = 0;
static const unsigned short const_max = ~const_min;
static const bool is_signed = false;
typedef short signed_type;
typedef unsigned short unsigned_type;
};
template <>
struct NumericTraits<short> {
static const bool is_numeric = true;
static const bool is_integer = true;
static const short const_min = -32768;
static const short const_max = 32767;
static const bool is_signed = true;
typedef short signed_type;
typedef unsigned short unsigned_type;
};
template <>
struct NumericTraits<unsigned int> {
static const bool is_numeric = true;
static const bool is_integer = true;
static const unsigned int const_min = 0;
static const unsigned int const_max = ~const_min;
static const bool is_signed = false;
typedef int signed_type;
typedef unsigned int unsigned_type;
};
template <>
struct NumericTraits<int> {
static const bool is_numeric = true;
static const bool is_integer = true;
static const int const_min = 1 << (8*SIZEOF_INT - 1);
static const int const_max = (unsigned) const_min - 1;
static const bool is_signed = true;
typedef int signed_type;
typedef unsigned int unsigned_type;
};
template <>
struct NumericTraits<unsigned long> {
static const bool is_numeric = true;
static const bool is_integer = true;
static const unsigned long const_min = 0;
static const unsigned long const_max = ~const_min;
static const bool is_signed = false;
typedef long signed_type;
typedef unsigned long unsigned_type;
};
template <>
struct NumericTraits<long> {
static const bool is_numeric = true;
static const bool is_integer = true;
static const long const_min = (long) 1 << (8*SIZEOF_LONG - 1);
static const long const_max = (unsigned long) const_min - 1;
static const bool is_signed = true;
typedef long signed_type;
typedef unsigned long unsigned_type;
};
#if HAVE_LONG_LONG
template <>
struct NumericTraits<unsigned long long> {
static const bool is_numeric = true;
static const bool is_integer = true;
static const unsigned long long const_min = 0;
static const unsigned long long const_max = ~const_min;
static const bool is_signed = false;
typedef long long signed_type;
typedef unsigned long long unsigned_type;
};
template <>
struct NumericTraits<long long> {
static const bool is_numeric = true;
static const bool is_integer = true;
static const long long const_min = (long long) 1 << (8*SIZEOF_LONG_LONG - 1);
static const long long const_max = (unsigned long long) const_min - 1;
static const bool is_signed = true;
typedef long long signed_type;
typedef unsigned long long unsigned_type;
};
#endif
#if HAVE_INT64_TYPES
/** @brief Return @a x translated from host to network byte order. */
......
......@@ -496,10 +496,12 @@ dnl
dnl CLICK_CHECK_INTEGER_TYPES
dnl Finds definitions for 'int8_t' ... 'int32_t' and 'uint8_t' ... 'uint32_t'.
dnl Also defines shell variable 'have_inttypes_h' to 'yes' iff the header
dnl file <inttypes.h> exists. If 'uintXX_t' doesn't exist, try 'u_intXX_t'.
dnl file <inttypes.h> exists. If 'uintXX_t' does not exist, try 'u_intXX_t'.
dnl Also defines __CHAR_UNSIGNED__ if 'char' is unsigned.
dnl
AC_DEFUN([CLICK_CHECK_INTEGER_TYPES], [
AC_C_CHAR_UNSIGNED
AC_CHECK_HEADERS(inttypes.h, have_inttypes_h=yes, have_inttypes_h=no)
if test $have_inttypes_h = no; then
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment