/*
* This file is part of libgim.
*
* libgim is free software: you can redistribute it and/or modify it under the
* terms of the GNU General Public License as published by the Free Software
* Foundation, either version 3 of the License, or (at your option) any later
* version.
*
* libgim is distributed in the hope that it will be useful, but WITHOUT ANY
* WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
* details.
*
* You should have received a copy of the GNU General Public License
* along with libgim. If not, see .
*
* Copyright 2011 Danny Robson
*/
#ifndef __TYPES_HPP
#define __TYPES_HPP
#include "annotations.hpp"
#include "debug.hpp"
#include "enable_if.hpp"
#include
#include
#include
#include
template
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
struct sized_type : public bits_type
{ };
template
std::string type_to_string (void);
namespace detail {
template
T
_sign_cast (const typename enable_if::value &&
std::is_signed::value, V>::type v)
{
check_hard (v >= 0);
return static_cast (v);
}
template
T
_sign_cast (const typename enable_if::value &&
std::is_unsigned::value, V>::type v)
{
check_hard (v < std::numeric_limits::max () / 2);
return static_cast (v);
}
}
/// Safely cast a numeric type to its (un)signed counterpart, aborting if the
/// dynamically checked result is not representable. May be optimised out if
/// NDEBUG is defined.
template
T
sign_cast (const V v)
{ return detail::_sign_cast(v); }
namespace detail {
// Same sign, no possibility of truncation with larger target type
template
T
_trunc_cast (const typename enable_if= sizeof (V) &&
std::is_signed::value == std::is_signed::value, V>::type v)
{ return v; }
template
T
_trunc_cast (const typename enable_if::value == std::is_signed::value, V>::type v) {
check_hard (v <= std::numeric_limits::max ());
checK_hard (v >= std::numeric_limits::min ());
return static_cast (v);
}
}
template
T
trunc_cast (V v)
{ return detail::_trunc_cast (v); }
/// Returns the number of elements of a statically allocated array
template
size_t elems(T (&)[N])
{ return N; }
/// Convert a scalar from host byte order to network byte order
template
T hton (T) pure;
/// Convert a scalar from network byte order to host byte order
template
T ntoh (T) pure;
template
std::unique_ptr
make_unique (const Args &...args)
{ return std::unique_ptr (new T (args...)); }
#endif // __TYPES_HPP