84 lines
3.5 KiB
C++
84 lines
3.5 KiB
C++
/* Copyright 2015, Michele Santullo
|
|
* This file is part of DoorKeeper.
|
|
*
|
|
* DoorKeeper 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.
|
|
*
|
|
* DoorKeeper 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 DoorKeeper. If not, see <http://www.gnu.org/licenses/>.
|
|
*/
|
|
|
|
#ifndef idA7B80E7E36484499AD624450C00B1DCA
|
|
#define idA7B80E7E36484499AD624450C00B1DCA
|
|
|
|
#include <climits>
|
|
|
|
namespace dk {
|
|
namespace implem {
|
|
class ERROR_inttype_not_available {
|
|
ERROR_inttype_not_available();
|
|
};
|
|
|
|
template<bool FIRST, typename A, typename B>
|
|
struct select {
|
|
typedef A type;
|
|
};
|
|
|
|
template<typename A, typename B>
|
|
struct select<false, A, B> {
|
|
typedef B type;
|
|
};
|
|
} // namespace implem
|
|
|
|
// Retrieves a signed/unsigned integer type with sizeof( T ) == BYTES
|
|
template<unsigned BYTES, bool SIGNED>
|
|
struct int_t {
|
|
typedef typename implem::select<sizeof(signed char) * CHAR_BIT == CHAR_BIT * BYTES, signed char,
|
|
typename implem::select<sizeof(signed short) * CHAR_BIT == CHAR_BIT * BYTES, signed short,
|
|
typename implem::select<sizeof(signed int) * CHAR_BIT == CHAR_BIT * BYTES, signed int,
|
|
typename implem::select<sizeof(signed long) * CHAR_BIT == CHAR_BIT * BYTES, signed long,
|
|
typename implem::select<sizeof(signed long long) * CHAR_BIT == CHAR_BIT * BYTES, signed long long,
|
|
implem::ERROR_inttype_not_available>::type>::type>::type>::type>::type type;
|
|
};
|
|
|
|
template<unsigned BYTES>
|
|
struct int_t<BYTES, false> {
|
|
typedef typename implem::select<sizeof(unsigned char) * CHAR_BIT == CHAR_BIT * BYTES, unsigned char,
|
|
typename implem::select<sizeof(unsigned short) * CHAR_BIT == CHAR_BIT * BYTES, unsigned short,
|
|
typename implem::select<sizeof(unsigned int) * CHAR_BIT == CHAR_BIT * BYTES, unsigned int,
|
|
typename implem::select<sizeof(unsigned long) * CHAR_BIT == CHAR_BIT * BYTES, unsigned long,
|
|
typename implem::select<sizeof(unsigned long long) * CHAR_BIT == CHAR_BIT * BYTES, unsigned long long,
|
|
implem::ERROR_inttype_not_available>::type>::type>::type>::type>::type type;
|
|
};
|
|
|
|
// Retrieves the smallest unsigned integer type with sizeof( T ) >= BYTES
|
|
template<unsigned BYTES>
|
|
struct uint_t_min {
|
|
typedef typename implem::select<sizeof(unsigned char) * CHAR_BIT >= CHAR_BIT * BYTES, unsigned char,
|
|
typename implem::select<sizeof(unsigned short) * CHAR_BIT >= CHAR_BIT * BYTES, unsigned short,
|
|
typename implem::select<sizeof(unsigned int) * CHAR_BIT >= CHAR_BIT * BYTES, unsigned int,
|
|
typename implem::select<sizeof(unsigned long) * CHAR_BIT >= CHAR_BIT * BYTES, unsigned long,
|
|
typename implem::select<sizeof(unsigned long long) * CHAR_BIT >= CHAR_BIT * BYTES, unsigned long long,
|
|
implem::ERROR_inttype_not_available >::type >::type >::type >::type >::type type;
|
|
};
|
|
|
|
// Machine independent definition of sized integer types
|
|
typedef int_t<1, true>::type i8;
|
|
typedef int_t<2, true>::type i16;
|
|
typedef int_t<4, true>::type i32;
|
|
typedef int_t<8, true>::type i64;
|
|
typedef int_t<1, false>::type u8;
|
|
typedef int_t<2, false>::type u16;
|
|
typedef int_t<4, false>::type u32;
|
|
typedef int_t<8, false>::type u64;
|
|
} //namespace dk
|
|
|
|
#endif
|