Categorise functionality from types.hpp
This commit is contained in:
parent
3d61e01f62
commit
f9cc4926f2
@ -21,7 +21,6 @@ UTIL_FILES = \
|
||||
crc.hpp \
|
||||
debug.cpp \
|
||||
debug.hpp \
|
||||
enable_if.hpp \
|
||||
endian.cpp \
|
||||
endian.hpp \
|
||||
except.cpp \
|
||||
@ -99,8 +98,12 @@ UTIL_FILES = \
|
||||
string.hpp \
|
||||
time.cpp \
|
||||
time.hpp \
|
||||
types.cpp \
|
||||
types.hpp \
|
||||
types/bits.hpp \
|
||||
types/casts.hpp \
|
||||
types/string.cpp \
|
||||
types/string.hpp \
|
||||
types/traits.hpp \
|
||||
vector.cpp \
|
||||
vector.hpp \
|
||||
version.cpp \
|
||||
|
85
endian.cpp
85
endian.cpp
@ -18,3 +18,88 @@
|
||||
*/
|
||||
|
||||
#include "endian.hpp"
|
||||
|
||||
#include <cstdio>
|
||||
#include <type_traits>
|
||||
|
||||
using std::is_fundamental;
|
||||
using std::is_unsigned;
|
||||
|
||||
#ifdef WORDS_BIGENDIAN
|
||||
/* Big endian doesn't need swapping */
|
||||
template <typename T>
|
||||
T hton (T val) {
|
||||
static_assert (is_fundamental <T>(), "hton implementation assumes fundamental types");
|
||||
static_assert (is_unsigned <T>(), "hton implementation does not handle signed");
|
||||
return val;
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
T ntoh (T val) {
|
||||
static_assert (is_fundamental <T>(), "ntoh implementation assumes fundamental types");
|
||||
static_assert (is_unsigned <T>(), "ntoh implementation does not handle signed");
|
||||
return val;
|
||||
}
|
||||
|
||||
template <> uint8_t hton ( uint8_t);
|
||||
template <> uint16_t hton (uint16_t);
|
||||
template <> uint32_t hton (uint32_t);
|
||||
template <> uint64_t hton (uint64_t);
|
||||
|
||||
template <> uint8_t ntoh ( uint8_t);
|
||||
template <> uint16_t ntoh (uint16_t);
|
||||
template <> uint32_t ntoh (uint32_t);
|
||||
template <> uint64_t ntoh (uint64_t);
|
||||
|
||||
#else
|
||||
|
||||
static void
|
||||
byte_swap (uint8_t *restrict dst, uint8_t *restrict src, size_t len) {
|
||||
for (unsigned int i = 0; i < len; ++i)
|
||||
dst[len - i - 1] = src[i];
|
||||
}
|
||||
|
||||
|
||||
template <typename T>
|
||||
T
|
||||
hton (T i) {
|
||||
// Unsure if this will really be sensible for non-intrinsic types, or types larger than 8 bytes.
|
||||
static_assert (is_fundamental <T>::value, "hton implementation assumes fundamental types");
|
||||
static_assert (is_unsigned <T>::value, "hton implementation does not handle signed");
|
||||
|
||||
T swapped;
|
||||
byte_swap (reinterpret_cast <uint8_t*>(&swapped),
|
||||
reinterpret_cast <uint8_t*>(&i),
|
||||
sizeof (T));
|
||||
return swapped;
|
||||
}
|
||||
|
||||
|
||||
template <typename T>
|
||||
T
|
||||
ntoh (T i) {
|
||||
// Unsure if this will really be sensible for non-intrinsic types, or types larger than 8 bytes.
|
||||
static_assert (is_fundamental <T>::value, "ntoh implementation assumes fundamental types");
|
||||
static_assert (is_unsigned <T>::value, "ntoh implementation does not handle signed");
|
||||
|
||||
T swapped;
|
||||
byte_swap (reinterpret_cast <uint8_t*>(&swapped),
|
||||
reinterpret_cast <uint8_t*>(&i),
|
||||
sizeof (T));
|
||||
return swapped;
|
||||
}
|
||||
|
||||
template <> uint8_t hton (uint8_t i) { return i; }
|
||||
template <> uint8_t ntoh (uint8_t i) { return i; }
|
||||
|
||||
template uint16_t ntoh<uint16_t> (uint16_t);
|
||||
template uint32_t ntoh<uint32_t> (uint32_t);
|
||||
template uint64_t ntoh<uint64_t> (uint64_t);
|
||||
|
||||
template uint16_t hton<uint16_t> (uint16_t);
|
||||
template uint32_t hton<uint32_t> (uint32_t);
|
||||
template uint64_t hton<uint64_t> (uint64_t);
|
||||
|
||||
#endif
|
||||
|
||||
|
||||
|
2
exe.cpp
2
exe.cpp
@ -24,8 +24,8 @@
|
||||
|
||||
#if defined(PLATFORM_LINUX)
|
||||
|
||||
#include "types.hpp"
|
||||
#include "except.hpp"
|
||||
#include "types/casts.hpp"
|
||||
|
||||
#include <sys/stat.h>
|
||||
#include <sys/types.h>
|
||||
|
@ -20,7 +20,7 @@
|
||||
#ifndef __UTIL_FIXED_HPP
|
||||
#define __UTIL_FIXED_HPP
|
||||
|
||||
#include "types.hpp"
|
||||
#include "types/bits.hpp"
|
||||
|
||||
#include <cstdint>
|
||||
|
||||
|
@ -20,11 +20,12 @@
|
||||
#ifndef __UTIL_FLETCHER_HPP
|
||||
#define __UTIL_FLETCHER_HPP
|
||||
|
||||
#include "types.hpp"
|
||||
#include "types/bits.hpp"
|
||||
|
||||
#include <cstdint>
|
||||
#include <cstdlib>
|
||||
|
||||
|
||||
template <unsigned BITS, unsigned MODULUS, unsigned INITIAL_A, unsigned INITIAL_B>
|
||||
typename bits_type<BITS>::uint
|
||||
fletcher (const void *restrict _data, size_t size) {
|
||||
|
@ -1,7 +1,7 @@
|
||||
#ifndef __FLOAT_HPP
|
||||
#define __FLOAT_HPP
|
||||
|
||||
#include "types.hpp"
|
||||
#include "types/bits.hpp"
|
||||
#include "debug.hpp"
|
||||
|
||||
|
||||
|
1
io.cpp
1
io.cpp
@ -22,6 +22,7 @@
|
||||
#include "debug.hpp"
|
||||
#include "except.hpp"
|
||||
#include "platform.hpp"
|
||||
#include "types/casts.hpp"
|
||||
|
||||
#include <cstdio>
|
||||
#include <sys/types.h>
|
||||
|
@ -21,7 +21,7 @@
|
||||
#ifndef __UTIL_ITERATOR_HPP
|
||||
#define __UTIL_ITERATOR_HPP
|
||||
|
||||
#include "types.hpp"
|
||||
#include "types/traits.hpp"
|
||||
|
||||
|
||||
template <typename Base>
|
||||
|
@ -20,6 +20,7 @@
|
||||
|
||||
#include "../json.hpp"
|
||||
|
||||
#include "../debug.hpp"
|
||||
#include "../maths.hpp"
|
||||
|
||||
#include <cmath>
|
||||
|
@ -19,7 +19,6 @@
|
||||
|
||||
#include "maths.hpp"
|
||||
|
||||
#include "enable_if.hpp"
|
||||
#include "float.hpp"
|
||||
|
||||
#include <cmath>
|
||||
@ -38,7 +37,7 @@ template int pow2( int);
|
||||
template <typename T>
|
||||
bool
|
||||
is_pow2 (T value) {
|
||||
typedef typename enable_if<std::is_integral<T>::value, bool>::type return_type;
|
||||
typedef typename std::enable_if<std::is_integral<T>::value, bool>::type return_type;
|
||||
return (return_type)(value && !(value & (value - 1)));
|
||||
}
|
||||
|
||||
@ -82,7 +81,7 @@ almost_equal (const double &a, const double &b)
|
||||
|
||||
|
||||
template <typename T>
|
||||
typename enable_if<std::is_integral<T>::value, T>::type
|
||||
typename std::enable_if<std::is_integral<T>::value, T>::type
|
||||
round_up (T value, T align) {
|
||||
CHECK_HARD (align > 1);
|
||||
return (value + align - 1) / align;
|
||||
@ -92,7 +91,7 @@ round_up (T value, T align) {
|
||||
template <typename T>
|
||||
T
|
||||
round_pow2 (T value) {
|
||||
typedef typename enable_if<std::is_integral<T>::value, T>::type return_type;
|
||||
typedef typename std::enable_if<std::is_integral<T>::value, T>::type return_type;
|
||||
|
||||
--value;
|
||||
|
||||
|
@ -20,9 +20,10 @@
|
||||
#include "noise/basis.hpp"
|
||||
#include "noise/lut.hpp"
|
||||
|
||||
#include "../vector.hpp"
|
||||
#include "../debug.hpp"
|
||||
#include "../point.hpp"
|
||||
#include "../random.hpp"
|
||||
#include "../vector.hpp"
|
||||
|
||||
#include <algorithm>
|
||||
|
||||
|
@ -51,6 +51,16 @@ util::noise::fractal::eval (double, double) const
|
||||
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
template <typename B>
|
||||
util::noise::fbm<B>::fbm (unsigned _octaves,
|
||||
double _frequency,
|
||||
double _lacunarity,
|
||||
basis::seed_t _seed):
|
||||
fractal (_octaves, _frequency, _lacunarity),
|
||||
basis (_seed)
|
||||
{ ; }
|
||||
|
||||
|
||||
template <typename B>
|
||||
util::noise::fbm<B>::fbm ()
|
||||
{ ; }
|
||||
@ -85,6 +95,16 @@ template struct util::noise::fbm<util::noise::value<lerp::quintic>>;
|
||||
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
template <typename B>
|
||||
util::noise::musgrave<B>::musgrave (unsigned _octaves,
|
||||
double _frequency,
|
||||
double _lacunarity,
|
||||
basis::seed_t _seed):
|
||||
fractal (_octaves, _frequency, _lacunarity),
|
||||
basis (_seed)
|
||||
{ ; }
|
||||
|
||||
|
||||
template <typename B>
|
||||
util::noise::musgrave<B>::musgrave ()
|
||||
{ ; }
|
||||
|
@ -20,7 +20,6 @@
|
||||
|
||||
#include "region.hpp"
|
||||
|
||||
#include "enable_if.hpp"
|
||||
#include "debug.hpp"
|
||||
|
||||
#include <cmath>
|
||||
|
2
time.cpp
2
time.cpp
@ -21,7 +21,7 @@
|
||||
|
||||
#include "debug.hpp"
|
||||
#include "platform.hpp"
|
||||
#include "types.hpp"
|
||||
#include "types/casts.hpp"
|
||||
|
||||
|
||||
using namespace util;
|
||||
|
170
types.cpp
170
types.cpp
@ -1,170 +0,0 @@
|
||||
/*
|
||||
* 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 <http://www.gnu.org/licenses/>.
|
||||
*
|
||||
* Copyright 2011 Danny Robson <danny@nerdcruft.net>
|
||||
*/
|
||||
|
||||
#include "types.hpp"
|
||||
|
||||
#include <type_traits>
|
||||
|
||||
using namespace std;
|
||||
|
||||
|
||||
#define do_type_to_string(T) \
|
||||
template <> std::string type_to_string <T> (void) { return #T; } \
|
||||
template <> std::string type_to_string <const T> (void) { return "const " #T; }
|
||||
|
||||
do_type_to_string (float)
|
||||
do_type_to_string (double)
|
||||
|
||||
do_type_to_string ( int8_t)
|
||||
do_type_to_string ( int16_t)
|
||||
do_type_to_string ( int32_t)
|
||||
do_type_to_string ( int64_t)
|
||||
|
||||
do_type_to_string ( uint8_t)
|
||||
do_type_to_string (uint16_t)
|
||||
do_type_to_string (uint32_t)
|
||||
do_type_to_string (uint64_t)
|
||||
|
||||
#ifdef WORDS_BIGENDIAN
|
||||
/* Big endian doesn't need swapping */
|
||||
template <typename T>
|
||||
T hton (T val) {
|
||||
static_assert (is_fundamental <T>(), "hton implementation assumes fundamental types");
|
||||
static_assert (is_unsigned <T>(), "hton implementation does not handle signed");
|
||||
return val;
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
T ntoh (T val) {
|
||||
static_assert (is_fundamental <T>(), "ntoh implementation assumes fundamental types");
|
||||
static_assert (is_unsigned <T>(), "ntoh implementation does not handle signed");
|
||||
return val;
|
||||
}
|
||||
|
||||
template <> uint8_t hton ( uint8_t);
|
||||
template <> uint16_t hton (uint16_t);
|
||||
template <> uint32_t hton (uint32_t);
|
||||
template <> uint64_t hton (uint64_t);
|
||||
|
||||
template <> uint8_t ntoh ( uint8_t);
|
||||
template <> uint16_t ntoh (uint16_t);
|
||||
template <> uint32_t ntoh (uint32_t);
|
||||
template <> uint64_t ntoh (uint64_t);
|
||||
|
||||
#else
|
||||
|
||||
static void
|
||||
byte_swap (uint8_t *restrict dst, uint8_t *restrict src, size_t len) {
|
||||
for (unsigned int i = 0; i < len; ++i)
|
||||
dst[len - i - 1] = src[i];
|
||||
}
|
||||
|
||||
|
||||
template <typename T>
|
||||
T
|
||||
hton (T i) {
|
||||
// Unsure if this will really be sensible for non-intrinsic types, or types larger than 8 bytes.
|
||||
static_assert (is_fundamental <T>::value, "hton implementation assumes fundamental types");
|
||||
static_assert (is_unsigned <T>::value, "hton implementation does not handle signed");
|
||||
|
||||
T swapped;
|
||||
byte_swap (reinterpret_cast <uint8_t*>(&swapped),
|
||||
reinterpret_cast <uint8_t*>(&i),
|
||||
sizeof (T));
|
||||
return swapped;
|
||||
}
|
||||
|
||||
|
||||
template <typename T>
|
||||
T
|
||||
ntoh (T i) {
|
||||
// Unsure if this will really be sensible for non-intrinsic types, or types larger than 8 bytes.
|
||||
static_assert (is_fundamental <T>::value, "ntoh implementation assumes fundamental types");
|
||||
static_assert (is_unsigned <T>::value, "ntoh implementation does not handle signed");
|
||||
|
||||
T swapped;
|
||||
byte_swap (reinterpret_cast <uint8_t*>(&swapped),
|
||||
reinterpret_cast <uint8_t*>(&i),
|
||||
sizeof (T));
|
||||
return swapped;
|
||||
}
|
||||
|
||||
template <> uint8_t hton (uint8_t i) { return i; }
|
||||
template <> uint8_t ntoh (uint8_t i) { return i; }
|
||||
|
||||
template uint16_t ntoh<uint16_t> (uint16_t);
|
||||
template uint32_t ntoh<uint32_t> (uint32_t);
|
||||
template uint64_t ntoh<uint64_t> (uint64_t);
|
||||
|
||||
template uint16_t hton<uint16_t> (uint16_t);
|
||||
template uint32_t hton<uint32_t> (uint32_t);
|
||||
template uint64_t hton<uint64_t> (uint64_t);
|
||||
|
||||
#endif
|
||||
|
||||
|
||||
fourcc
|
||||
fourcc::from_chars (uint8_t a, uint8_t b, uint8_t c, uint8_t d) {
|
||||
fourcc lhs;
|
||||
|
||||
lhs.data[0] = a;
|
||||
lhs.data[1] = b;
|
||||
lhs.data[2] = c;
|
||||
lhs.data[3] = d;
|
||||
|
||||
return lhs;
|
||||
}
|
||||
|
||||
|
||||
fourcc
|
||||
fourcc::from_str (const char data[4]) {
|
||||
fourcc lhs;
|
||||
|
||||
lhs.data[0] = (uint8_t)data[0];
|
||||
lhs.data[1] = (uint8_t)data[1];
|
||||
lhs.data[2] = (uint8_t)data[2];
|
||||
lhs.data[3] = (uint8_t)data[3];
|
||||
|
||||
return lhs;
|
||||
}
|
||||
|
||||
|
||||
bool
|
||||
fourcc::operator== (const char rhs[4]) const {
|
||||
return data[0] == rhs[0] &&
|
||||
data[1] == rhs[1] &&
|
||||
data[2] == rhs[2] &&
|
||||
data[3] == rhs[3];
|
||||
}
|
||||
|
||||
|
||||
fourcc::operator uint32_t (void) const {
|
||||
return (uint32_t)(data[0] << 24U |
|
||||
data[1] << 16U |
|
||||
data[2] << 8U |
|
||||
data[3]);
|
||||
}
|
||||
|
||||
|
||||
ostream&
|
||||
operator<< (ostream &os, fourcc f) {
|
||||
os << f.data[0] << f.data[1] << f.data[2] << f.data[3];
|
||||
return os;
|
||||
}
|
||||
|
153
types.hpp
153
types.hpp
@ -20,124 +20,10 @@
|
||||
#ifndef __TYPES_HPP
|
||||
#define __TYPES_HPP
|
||||
|
||||
#include "annotations.hpp"
|
||||
#include "debug.hpp"
|
||||
#include "enable_if.hpp"
|
||||
|
||||
#include <cstdint>
|
||||
#include <string>
|
||||
#include <limits>
|
||||
#include <cstdlib>
|
||||
#include <memory>
|
||||
|
||||
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);
|
||||
|
||||
|
||||
namespace detail {
|
||||
template <typename T, typename V>
|
||||
T
|
||||
_sign_cast (const typename enable_if<sizeof(T) == sizeof(V) &&
|
||||
std::is_unsigned<T>::value &&
|
||||
std::is_signed<V>::value, V>::type v)
|
||||
{
|
||||
CHECK_HARD (v >= 0);
|
||||
return static_cast<T> (v);
|
||||
}
|
||||
|
||||
|
||||
template <typename T, typename V>
|
||||
T
|
||||
_sign_cast (const typename enable_if<sizeof(T) == sizeof(V) &&
|
||||
std::is_signed<T>::value &&
|
||||
std::is_unsigned<V>::value, V>::type v)
|
||||
{
|
||||
CHECK_HARD (v < std::numeric_limits<V>::max () / 2);
|
||||
return static_cast<T> (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 <typename T, typename V>
|
||||
T
|
||||
sign_cast (const V v)
|
||||
{ return detail::_sign_cast<T,V>(v); }
|
||||
|
||||
|
||||
namespace detail {
|
||||
// Same sign, no possibility of truncation with larger target type
|
||||
template <typename T, typename V>
|
||||
T
|
||||
_trunc_cast (const typename enable_if<sizeof (T) >= sizeof (V) &&
|
||||
std::is_signed<T>::value == std::is_signed<V>::value, V>::type v)
|
||||
{ return v; }
|
||||
|
||||
|
||||
template <typename T, typename V>
|
||||
T
|
||||
_trunc_cast (const typename enable_if<sizeof (T) < sizeof (V) &&
|
||||
std::is_signed<T>::value == std::is_signed<V>::value, V>::type v) {
|
||||
CHECK_HARD (v <= std::numeric_limits<T>::max ());
|
||||
checK_hard (v >= std::numeric_limits<T>::min ());
|
||||
|
||||
return static_cast<T> (v);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
template <typename T, typename V>
|
||||
T
|
||||
trunc_cast (V v)
|
||||
{ return detail::_trunc_cast<T, V> (v); }
|
||||
|
||||
|
||||
/// Returns the number of elements of a statically allocated array
|
||||
template <typename T, size_t N>
|
||||
size_t elems(T (&)[N])
|
||||
@ -145,43 +31,10 @@ size_t elems(T (&)[N])
|
||||
|
||||
|
||||
template<class T, class...Args>
|
||||
std::unique_ptr<T> make_unique(Args&&... args) {
|
||||
std::unique_ptr<T>
|
||||
make_unique(Args&&... args) {
|
||||
return std::unique_ptr<T> (new T(std::forward<Args>(args)...));
|
||||
}
|
||||
|
||||
|
||||
struct fourcc {
|
||||
uint8_t data[4];
|
||||
|
||||
static fourcc from_str(const char[4]);
|
||||
static fourcc from_chars(uint8_t, uint8_t, uint8_t, uint8_t);
|
||||
|
||||
bool operator== (const char[4]) const;
|
||||
operator uint32_t (void) const;
|
||||
};
|
||||
|
||||
std::ostream& operator<< (std::ostream&, fourcc);
|
||||
|
||||
|
||||
template <typename T> struct is_dereferencable : std::false_type { };
|
||||
template <typename T> struct is_dereferencable<T*> : std::true_type { };
|
||||
template <typename T> struct is_dereferencable<std::unique_ptr<T>> : std::true_type { };
|
||||
template <typename T> struct is_dereferencable<std::shared_ptr<T>> : std::true_type { };
|
||||
template <typename T> struct is_dereferencable<std::weak_ptr<T>> : std::true_type { };
|
||||
template <typename T> struct is_dereferencable<std::auto_ptr<T>> : std::true_type { };
|
||||
|
||||
|
||||
template <typename T> struct dereferenced_type {
|
||||
typedef typename std::enable_if<
|
||||
std::is_pointer<T>::value,
|
||||
std::remove_pointer<T>
|
||||
>::type type;
|
||||
};
|
||||
|
||||
template <typename T> struct dereferenced_type<std::unique_ptr<T>> { typedef T type; };
|
||||
template <typename T> struct dereferenced_type<std::shared_ptr<T>> { typedef T type; };
|
||||
template <typename T> struct dereferenced_type<std::weak_ptr<T>> { typedef T type; };
|
||||
template <typename T> struct dereferenced_type<std::auto_ptr<T>> { typedef T type; };
|
||||
|
||||
|
||||
#endif // __TYPES_HPP
|
||||
|
69
types/bits.hpp
Normal file
69
types/bits.hpp
Normal file
@ -0,0 +1,69 @@
|
||||
/*
|
||||
* 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 <http://www.gnu.org/licenses/>.
|
||||
*
|
||||
* Copyright 2011 Danny Robson <danny@nerdcruft.net>
|
||||
*/
|
||||
|
||||
#ifndef __UTIL_TYPES_BITS_HPP
|
||||
#define __UTIL_TYPES_BITS_HPP
|
||||
|
||||
#include <cstdint>
|
||||
|
||||
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>
|
||||
{ };
|
||||
|
||||
#endif
|
87
types/casts.hpp
Normal file
87
types/casts.hpp
Normal file
@ -0,0 +1,87 @@
|
||||
/*
|
||||
* 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 <http://www.gnu.org/licenses/>.
|
||||
*
|
||||
* Copyright 2011 Danny Robson <danny@nerdcruft.net>
|
||||
*/
|
||||
|
||||
#ifndef __UTIL_TYPES_CASTS_HPP
|
||||
#define __UTIL_TYPES_CASTS_HPP
|
||||
|
||||
#include "../debug.hpp"
|
||||
|
||||
#include <type_traits>
|
||||
#include <limits>
|
||||
|
||||
|
||||
namespace detail {
|
||||
template <typename T, typename V>
|
||||
T
|
||||
_sign_cast (const typename std::enable_if<sizeof(T) == sizeof(V) &&
|
||||
std::is_unsigned<T>::value &&
|
||||
std::is_signed<V>::value, V>::type v)
|
||||
{
|
||||
CHECK_HARD (v >= 0);
|
||||
return static_cast<T> (v);
|
||||
}
|
||||
|
||||
|
||||
template <typename T, typename V>
|
||||
T
|
||||
_sign_cast (const typename std::enable_if<sizeof(T) == sizeof(V) &&
|
||||
std::is_signed<T>::value &&
|
||||
std::is_unsigned<V>::value, V>::type v)
|
||||
{
|
||||
CHECK_HARD (v < std::numeric_limits<V>::max () / 2);
|
||||
return static_cast<T> (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 <typename T, typename V>
|
||||
T
|
||||
sign_cast (const V v)
|
||||
{ return detail::_sign_cast<T,V>(v); }
|
||||
|
||||
|
||||
namespace detail {
|
||||
// Same sign, no possibility of truncation with larger target type
|
||||
template <typename T, typename V>
|
||||
T
|
||||
_trunc_cast (const typename std::enable_if<sizeof (T) >= sizeof (V) &&
|
||||
std::is_signed<T>::value == std::is_signed<V>::value, V>::type v)
|
||||
{ return v; }
|
||||
|
||||
|
||||
template <typename T, typename V>
|
||||
T
|
||||
_trunc_cast (const typename std::enable_if<sizeof (T) < sizeof (V) &&
|
||||
std::is_signed<T>::value == std::is_signed<V>::value, V>::type v) {
|
||||
CHECK_HARD (v <= std::numeric_limits<T>::max ());
|
||||
checK_hard (v >= std::numeric_limits<T>::min ());
|
||||
|
||||
return static_cast<T> (v);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
template <typename T, typename V>
|
||||
T
|
||||
trunc_cast (V v)
|
||||
{ return detail::_trunc_cast<T, V> (v); }
|
||||
|
||||
#endif
|
38
types/string.cpp
Normal file
38
types/string.cpp
Normal file
@ -0,0 +1,38 @@
|
||||
/*
|
||||
* 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 <http://www.gnu.org/licenses/>.
|
||||
*
|
||||
* Copyright 2011 Danny Robson <danny@nerdcruft.net>
|
||||
*/
|
||||
|
||||
#include "string.hpp"
|
||||
|
||||
#define do_type_to_string(T) \
|
||||
template <> std::string type_to_string <T> (void) { return #T; } \
|
||||
template <> std::string type_to_string <const T> (void) { return "const " #T; }
|
||||
|
||||
do_type_to_string (float)
|
||||
do_type_to_string (double)
|
||||
|
||||
do_type_to_string ( int8_t)
|
||||
do_type_to_string ( int16_t)
|
||||
do_type_to_string ( int32_t)
|
||||
do_type_to_string ( int64_t)
|
||||
|
||||
do_type_to_string ( uint8_t)
|
||||
do_type_to_string (uint16_t)
|
||||
do_type_to_string (uint32_t)
|
||||
do_type_to_string (uint64_t)
|
||||
|
@ -14,20 +14,16 @@
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with libgim. If not, see <http://www.gnu.org/licenses/>.
|
||||
*
|
||||
* Copyright 2010 Danny Robson <danny@nerdcruft.net>
|
||||
* Copyright 2011 Danny Robson <danny@nerdcruft.net>
|
||||
*/
|
||||
|
||||
#ifndef __ENABLE_IF_HPP
|
||||
#define __ENABLE_IF_HPP
|
||||
|
||||
template <bool B, typename T>
|
||||
struct enable_if {
|
||||
typedef T type;
|
||||
};
|
||||
#ifndef __UTIL_TYPES_STRING_HPP
|
||||
#define __UTIL_TYPES_STRING_HPP
|
||||
|
||||
#include <string>
|
||||
|
||||
template <typename T>
|
||||
struct enable_if<false, T> { };
|
||||
std::string
|
||||
type_to_string (void);
|
||||
|
||||
|
||||
#endif // __ENABLE_IF_HPP
|
||||
#endif
|
43
types/traits.hpp
Normal file
43
types/traits.hpp
Normal file
@ -0,0 +1,43 @@
|
||||
/*
|
||||
* 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 <http://www.gnu.org/licenses/>.
|
||||
*
|
||||
* Copyright 2012 Danny Robson <danny@nerdcruft.net>
|
||||
*/
|
||||
|
||||
#ifndef __UTIL_TYPES_TRAITS_HPP
|
||||
#define __UTIL_TYPES_TRAITS_HPP
|
||||
|
||||
template <typename T> struct is_dereferencable : std::false_type { };
|
||||
template <typename T> struct is_dereferencable<T*> : std::true_type { };
|
||||
template <typename T> struct is_dereferencable<std::unique_ptr<T>> : std::true_type { };
|
||||
template <typename T> struct is_dereferencable<std::shared_ptr<T>> : std::true_type { };
|
||||
template <typename T> struct is_dereferencable<std::weak_ptr<T>> : std::true_type { };
|
||||
template <typename T> struct is_dereferencable<std::auto_ptr<T>> : std::true_type { };
|
||||
|
||||
|
||||
template <typename T> struct dereferenced_type {
|
||||
typedef typename std::enable_if<
|
||||
std::is_pointer<T>::value,
|
||||
std::remove_pointer<T>
|
||||
>::type type;
|
||||
};
|
||||
|
||||
template <typename T> struct dereferenced_type<std::unique_ptr<T>> { typedef T type; };
|
||||
template <typename T> struct dereferenced_type<std::shared_ptr<T>> { typedef T type; };
|
||||
template <typename T> struct dereferenced_type<std::weak_ptr<T>> { typedef T type; };
|
||||
template <typename T> struct dereferenced_type<std::auto_ptr<T>> { typedef T type; };
|
||||
|
||||
#endif
|
Loading…
Reference in New Issue
Block a user