use standard types

master
Daniel Kolesa 2017-01-30 19:19:09 +01:00
parent 2884f4b47b
commit 0ee0d3231c
6 changed files with 24 additions and 146 deletions

View File

@ -3,7 +3,7 @@
using namespace ostd;
void print_result(Uint32 x) {
void print_result(uint32_t x) {
writefln("got x: 0x%X", x);
}
@ -11,14 +11,14 @@ int main() {
FileStream wtest{"test.bin", StreamMode::write};
copy(
iter({ 0xABCD1214, 0xBADC3264, 0xDEADBEEF, 0xBEEFDEAD }),
wtest.iter<Uint32>()
wtest.iter<uint32_t>()
);
wtest.close();
FileStream rtest{"test.bin"};
writefln("stream size: %d", rtest.size());
for (Uint32 x: map(rtest.iter<Uint32>(), FromBigEndian<Uint32>())) {
for (uint32_t x: map(rtest.iter<uint32_t>(), FromBigEndian<uint32_t>())) {
print_result(x);
}

View File

@ -147,7 +147,7 @@ struct EndianSwap<T, 2, true> {
using Argument = T;
using Result = T;
T operator()(T v) const {
union { T iv; uint16_t sv; } u;
union { T iv; std::uint16_t sv; } u;
u.iv = v;
u.sv = endian_swap16(u.sv);
return u.iv;
@ -159,7 +159,7 @@ struct EndianSwap<T, 4, true> {
using Argument = T;
using Result = T;
T operator()(T v) const {
union { T iv; uint32_t sv; } u;
union { T iv; std::uint32_t sv; } u;
u.iv = v;
u.sv = endian_swap32(u.sv);
return u.iv;
@ -171,7 +171,7 @@ struct EndianSwap<T, 8, true> {
using Argument = T;
using Result = T;
T operator()(T v) const {
union { T iv; uint64_t sv; } u;
union { T iv; std::uint64_t sv; } u;
u.iv = v;
u.sv = endian_swap64(u.sv);
return u.iv;

View File

@ -7,7 +7,7 @@
#define OSTD_PLATFORM_HH
#include <stdlib.h>
#include <stdint.h>
#include <cstdint>
#if defined(WIN32) || defined(_WIN32) || (defined(__WIN32) && !defined(__CYGWIN__))
# define OSTD_PLATFORM_WIN32 1
@ -102,42 +102,43 @@ namespace ostd {
#if defined(OSTD_TOOLCHAIN_GNU)
/* using gcc/clang builtins */
inline uint16_t endian_swap16(uint16_t x) noexcept {
inline std::uint16_t endian_swap16(std::uint16_t x) noexcept {
return __builtin_bswap16(x);
}
inline uint32_t endian_swap32(uint32_t x) noexcept {
inline std::uint32_t endian_swap32(std::uint32_t x) noexcept {
return __builtin_bswap32(x);
}
inline uint64_t endian_swap64(uint64_t x) noexcept {
inline std::uint64_t endian_swap64(std::uint64_t x) noexcept {
return __builtin_bswap64(x);
}
#elif defined(OSTD_TOOLCHAIN_MSVC)
/* using msvc builtins */
inline uint16_t endian_swap16(uint16_t x) noexcept {
inline std::uint16_t endian_swap16(std::uint16_t x) noexcept {
return _byteswap_ushort(x);
}
inline uint32_t endian_swap32(uint32_t x) noexcept {
inline std::uint32_t endian_swap32(std::uint32_t x) noexcept {
/* win64 is llp64 */
return _byteswap_ulong(x);
}
inline uint64_t endian_swap64(uint64_t x) noexcept {
inline std::uint64_t endian_swap64(std::uint64_t x) noexcept {
return _byteswap_uint64(x);
}
#else
/* fallback */
inline uint16_t endian_swap16(uint16_t x) noexcept {
inline std::uint16_t endian_swap16(std::uint16_t x) noexcept {
return (x << 8) | (x >> 8);
}
inline uint32_t endian_swap32(uint32_t x) noexcept {
inline std::uint32_t endian_swap32(std::uint32_t x) noexcept {
return (x << 24) | (x >> 24) | ((x >> 8) & 0xFF00) | ((x << 8) & 0xFF0000);
}
inline uint64_t endian_swap64(uint64_t x) noexcept {
inline std::uint64_t endian_swap64(std::uint64_t x) noexcept {
return endian_swap32(
uint32_t(x >> 32)) | (uint64_t(endian_swap32(uint32_t(x))) << 32
std::uint32_t(x >> 32)) |
(std::uint64_t(endian_swap32(std::uint32_t(x))) << 32
);
}

View File

@ -45,7 +45,7 @@ public:
U beg, EnableIf<IsConvertible<U, T *> && !IsArray<U>, Nat> = Nat()
): p_beg(beg), p_end(static_cast<T *>(beg) + (beg ? strlen(beg) : 0)) {}
CharRangeBase(Nullptr): p_beg(nullptr), p_end(nullptr) {}
CharRangeBase(std::nullptr_t): p_beg(nullptr), p_end(nullptr) {}
template<typename U, size_t N>
CharRangeBase(U (&beg)[N], EnableIf<IsConvertible<U *, T *>, Nat> = Nat()):

View File

@ -95,70 +95,17 @@ constexpr bool IsVoid = IsSame<RemoveCv<T>, void>;
/* is null pointer */
template<typename T>
constexpr bool IsNullPointer = IsSame<RemoveCv<T>, Nullptr>;
constexpr bool IsNullPointer = IsSame<RemoveCv<T>, std::nullptr_t>;
/* is integer */
namespace detail {
template<typename>
constexpr bool IsIntegralBase = false;
template<>
constexpr bool IsIntegralBase<bool> = true;
template<>
constexpr bool IsIntegralBase<char> = true;
template<>
constexpr bool IsIntegralBase<short> = true;
template<>
constexpr bool IsIntegralBase<int> = true;
template<>
constexpr bool IsIntegralBase<long> = true;
template<>
constexpr bool IsIntegralBase<sbyte> = true;
template<>
constexpr bool IsIntegralBase<byte> = true;
template<>
constexpr bool IsIntegralBase<ushort> = true;
template<>
constexpr bool IsIntegralBase<uint> = true;
template<>
constexpr bool IsIntegralBase<ulong> = true;
template<>
constexpr bool IsIntegralBase<llong> = true;
template<>
constexpr bool IsIntegralBase<ullong> = true;
#ifndef OSTD_TYPES_CHAR_16_32_NO_BUILTINS
template<>
constexpr bool IsIntegralBase<Char16> = true;
template<>
constexpr bool IsIntegralBase<Char32> = true;
#endif
template<>
constexpr bool IsIntegralBase<Wchar> = true;
}
template<typename T>
constexpr bool IsIntegral = detail::IsIntegralBase<RemoveCv<T>>;
constexpr bool IsIntegral = std::is_integral_v<T>;
/* is floating point */
namespace detail {
template<typename>
constexpr bool IsFloatingPointBase = false;
template<>
constexpr bool IsFloatingPointBase<float> = true;
template<>
constexpr bool IsFloatingPointBase<double> = true;
template<>
constexpr bool IsFloatingPointBase<ldouble> = true;
}
template<typename T>
constexpr bool IsFloatingPoint = detail::IsFloatingPointBase<RemoveCv<T>>;
constexpr bool IsFloatingPoint = std::is_floating_point_v<T>;
/* is array */
@ -1609,7 +1556,7 @@ namespace detail {
struct AlignedTest {
union Type {
byte data[N];
MaxAlign align;
std::max_align_t align;
};
};

View File

@ -7,7 +7,7 @@
#define OSTD_TYPES_HH
#include <stdint.h>
#include <stddef.h>
#include <cstddef>
namespace ostd {
@ -23,76 +23,6 @@ using llong = long long;
using ldouble = long double;
/* keywords in c++, but aliased */
using Wchar = wchar_t;
/* while we do not support Visual Studio 2013, Clang on Windows by default
* pretends to be VS (with the default version being 2013). In this case,
* it also tries to be compatible (in order to not conflict with MS include
* headers) with said version, so it doesn't provide the builtins; so we work
* around this (same typedefs/behavior as VS 2013, of course not standards
* compliant but no way around it)
*/
#if defined(_MSC_VER) && (_MSC_VER < 1900)
using Char16 = uint16_t;
using Char32 = uint32_t;
#define OSTD_TYPES_CHAR_16_32_NO_BUILTINS 1
#else
using Char16 = char16_t;
using Char32 = char32_t;
#endif
/* nullptr type */
using Nullptr = decltype(nullptr);
/* max align */
#if defined(__CLANG_MAX_ALIGN_T_DEFINED) || defined(_GCC_MAX_ALIGN_T)
using MaxAlign = ::max_align_t;
#else
using MaxAlign = long double;
#endif
/* stdint */
using Intmax = intmax_t;
using Uintmax = uintmax_t;
using Intptr = intptr_t;
using Uintptr = uintptr_t;
using Int8 = int8_t;
using Int16 = int16_t;
using Int32 = int32_t;
using Int64 = int64_t;
using Uint8 = uint8_t;
using Uint16 = uint16_t;
using Uint32 = uint32_t;
using Uint64 = uint64_t;
using IntLeast8 = int_least8_t;
using IntLeast16 = int_least16_t;
using IntLeast32 = int_least32_t;
using IntLeast64 = int_least64_t;
using UintLeast8 = uint_least8_t;
using UintLeast16 = uint_least16_t;
using UintLeast32 = uint_least32_t;
using UintLeast64 = uint_least64_t;
using IntFast8 = int_fast8_t;
using IntFast16 = int_fast16_t;
using IntFast32 = int_fast32_t;
using IntFast64 = int_fast64_t;
using UintFast8 = uint_fast8_t;
using UintFast16 = uint_fast16_t;
using UintFast32 = uint_fast32_t;
using UintFast64 = uint_fast64_t;
/* used occasionally for template variables */
namespace detail {