From 0ee0d3231c15c83601a737c4eb9160b5c1ad4863 Mon Sep 17 00:00:00 2001 From: q66 Date: Mon, 30 Jan 2017 19:19:09 +0100 Subject: [PATCH] use standard types --- examples/stream1.cc | 6 ++-- ostd/functional.hh | 6 ++-- ostd/platform.hh | 23 ++++++++------- ostd/string.hh | 2 +- ostd/type_traits.hh | 61 +++----------------------------------- ostd/types.hh | 72 +-------------------------------------------- 6 files changed, 24 insertions(+), 146 deletions(-) diff --git a/examples/stream1.cc b/examples/stream1.cc index 07e0329..7de4388 100644 --- a/examples/stream1.cc +++ b/examples/stream1.cc @@ -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() + wtest.iter() ); wtest.close(); FileStream rtest{"test.bin"}; writefln("stream size: %d", rtest.size()); - for (Uint32 x: map(rtest.iter(), FromBigEndian())) { + for (uint32_t x: map(rtest.iter(), FromBigEndian())) { print_result(x); } diff --git a/ostd/functional.hh b/ostd/functional.hh index 6e1d82f..86a07f8 100644 --- a/ostd/functional.hh +++ b/ostd/functional.hh @@ -147,7 +147,7 @@ struct EndianSwap { 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 { 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 { 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; diff --git a/ostd/platform.hh b/ostd/platform.hh index ddb9309..8a89b06 100644 --- a/ostd/platform.hh +++ b/ostd/platform.hh @@ -7,7 +7,7 @@ #define OSTD_PLATFORM_HH #include -#include +#include #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 ); } diff --git a/ostd/string.hh b/ostd/string.hh index aca74e5..431e46f 100644 --- a/ostd/string.hh +++ b/ostd/string.hh @@ -45,7 +45,7 @@ public: U beg, EnableIf && !IsArray, Nat> = Nat() ): p_beg(beg), p_end(static_cast(beg) + (beg ? strlen(beg) : 0)) {} - CharRangeBase(Nullptr): p_beg(nullptr), p_end(nullptr) {} + CharRangeBase(std::nullptr_t): p_beg(nullptr), p_end(nullptr) {} template CharRangeBase(U (&beg)[N], EnableIf, Nat> = Nat()): diff --git a/ostd/type_traits.hh b/ostd/type_traits.hh index b8b13a1..b80a5e5 100644 --- a/ostd/type_traits.hh +++ b/ostd/type_traits.hh @@ -95,70 +95,17 @@ constexpr bool IsVoid = IsSame, void>; /* is null pointer */ template -constexpr bool IsNullPointer = IsSame, Nullptr>; +constexpr bool IsNullPointer = IsSame, std::nullptr_t>; /* is integer */ -namespace detail { - template - constexpr bool IsIntegralBase = false; - - template<> - constexpr bool IsIntegralBase = true; - template<> - constexpr bool IsIntegralBase = true; - template<> - constexpr bool IsIntegralBase = true; - template<> - constexpr bool IsIntegralBase = true; - template<> - constexpr bool IsIntegralBase = true; - - template<> - constexpr bool IsIntegralBase = true; - template<> - constexpr bool IsIntegralBase = true; - template<> - constexpr bool IsIntegralBase = true; - template<> - constexpr bool IsIntegralBase = true; - template<> - constexpr bool IsIntegralBase = true; - template<> - constexpr bool IsIntegralBase = true; - template<> - constexpr bool IsIntegralBase = true; - -#ifndef OSTD_TYPES_CHAR_16_32_NO_BUILTINS - template<> - constexpr bool IsIntegralBase = true; - template<> - constexpr bool IsIntegralBase = true; -#endif - template<> - constexpr bool IsIntegralBase = true; -} - template -constexpr bool IsIntegral = detail::IsIntegralBase>; +constexpr bool IsIntegral = std::is_integral_v; /* is floating point */ -namespace detail { - template - constexpr bool IsFloatingPointBase = false; - - template<> - constexpr bool IsFloatingPointBase = true; - template<> - constexpr bool IsFloatingPointBase = true; - - template<> - constexpr bool IsFloatingPointBase = true; -} - template -constexpr bool IsFloatingPoint = detail::IsFloatingPointBase>; +constexpr bool IsFloatingPoint = std::is_floating_point_v; /* is array */ @@ -1609,7 +1556,7 @@ namespace detail { struct AlignedTest { union Type { byte data[N]; - MaxAlign align; + std::max_align_t align; }; }; diff --git a/ostd/types.hh b/ostd/types.hh index 36686d7..7df6366 100644 --- a/ostd/types.hh +++ b/ostd/types.hh @@ -7,7 +7,7 @@ #define OSTD_TYPES_HH #include -#include +#include 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 {