2011-05-23 17:18:52 +10:00
|
|
|
#ifndef __TYPES_HPP
|
|
|
|
#define __TYPES_HPP
|
|
|
|
|
|
|
|
#include <cstdint>
|
|
|
|
#include <string>
|
2011-06-15 22:38:58 +10:00
|
|
|
#include <limits>
|
2011-05-23 17:18:52 +10:00
|
|
|
|
|
|
|
template <int BITS>
|
|
|
|
struct bits_type;
|
|
|
|
|
|
|
|
|
|
|
|
template <> struct bits_type< 8> {
|
|
|
|
static const bool has_floating = false;
|
|
|
|
|
|
|
|
typedef uint8_t uint;
|
|
|
|
typedef int8_t sint;
|
|
|
|
typedef uint8_t floating;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
template <> struct bits_type<16> {
|
|
|
|
static const bool has_floating = false;
|
|
|
|
|
|
|
|
typedef uint16_t uint;
|
|
|
|
typedef int16_t sint;
|
|
|
|
typedef uint16_t floating;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
template <> struct bits_type<32> {
|
|
|
|
static const bool has_floating = true;
|
|
|
|
|
|
|
|
typedef uint32_t uint;
|
|
|
|
typedef int32_t sint;
|
|
|
|
typedef float floating;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
template <> struct bits_type<64> {
|
|
|
|
static const bool has_floating = true;
|
|
|
|
|
|
|
|
typedef uint64_t uint;
|
|
|
|
typedef int64_t sint;
|
|
|
|
typedef double floating;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
struct sized_type : public bits_type<sizeof(T) * 8>
|
|
|
|
{ };
|
|
|
|
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
std::string type_to_string (void);
|
|
|
|
|
|
|
|
|
2011-06-15 22:38:58 +10:00
|
|
|
template <typename T>
|
|
|
|
struct sign_types;
|
|
|
|
|
|
|
|
|
|
|
|
template <> struct sign_types <signed int> {
|
|
|
|
typedef signed int with_sign;
|
|
|
|
typedef unsigned int without_sign;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
template <> struct sign_types <unsigned int> {
|
|
|
|
typedef signed int with_sign;
|
|
|
|
typedef unsigned int without_sign;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
/// Safely cast a numeric type to its signed comparison, aborting if the
|
|
|
|
/// result is not representable.
|
|
|
|
template <typename T>
|
|
|
|
typename sign_types<T>::with_sign
|
|
|
|
sign_cast (const typename sign_types<T>::without_sign v)
|
|
|
|
{
|
|
|
|
check_hard (v <= std::numeric_limits<typename sign_types<T>::without_sign>::max () >> 1);
|
|
|
|
return static_cast <typename sign_types<T>::with_sign> (v);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// Safely cast a numeric type to its unsigned comparison, aborting if the
|
|
|
|
/// result is not representable.
|
|
|
|
template <typename T>
|
|
|
|
typename sign_types<T>::without_sign
|
|
|
|
sign_cast (const typename sign_types<T>::with_sign v)
|
|
|
|
{
|
|
|
|
check_hard (v >= 0);
|
|
|
|
return static_cast <typename sign_types<T>::without_sign> (v);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-06-21 20:16:39 +10:00
|
|
|
/// Returns the number of elements of a statically allocated array
|
|
|
|
template <typename T, size_t N>
|
|
|
|
size_t elems(T (&)[N])
|
|
|
|
{ return N; }
|
2011-06-15 22:38:58 +10:00
|
|
|
|
2011-06-21 23:36:51 +10:00
|
|
|
|
2011-06-23 22:06:28 +10:00
|
|
|
/// Convert a scalar from host byte order to network byte order
|
2011-06-21 23:36:51 +10:00
|
|
|
template <typename T>
|
|
|
|
T hton (T);
|
|
|
|
|
|
|
|
|
2011-06-23 22:06:28 +10:00
|
|
|
/// Convert a scalar from network byte order to host byte order
|
2011-06-21 23:36:51 +10:00
|
|
|
template <typename T>
|
|
|
|
T ntoh (T);
|
|
|
|
|
2011-05-23 17:18:52 +10:00
|
|
|
#endif // __TYPES_HPP
|