Core/Endianness: Add SwapBytes shortcut

Former-commit-id: c345121453f38c9eb8c05e224db91f068440f729
This commit is contained in:
Lynix 2016-04-21 13:20:46 +02:00
parent 0d75f8ab2e
commit 3caeb91a32
2 changed files with 12 additions and 6 deletions

View File

@ -11,7 +11,7 @@
#include <Nazara/Core/Enums.hpp> #include <Nazara/Core/Enums.hpp>
#if !defined(NAZARA_BIG_ENDIAN) && !defined(NAZARA_LITTLE_ENDIAN) #if !defined(NAZARA_BIG_ENDIAN) && !defined(NAZARA_LITTLE_ENDIAN)
// Automatic detection following macroes of compiler // Automatic detection following macros of compiler
#if defined(__m68k__) || defined(mc68000) || defined(_M_M68K) || (defined(__MIPS__) && defined(__MISPEB__)) || \ #if defined(__m68k__) || defined(mc68000) || defined(_M_M68K) || (defined(__MIPS__) && defined(__MISPEB__)) || \
defined(__ppc__) || defined(__POWERPC__) || defined(_M_PPC) || defined(__sparc__) || defined(__hppa__) defined(__ppc__) || defined(__POWERPC__) || defined(_M_PPC) || defined(__sparc__) || defined(__hppa__)
#define NAZARA_BIG_ENDIAN #define NAZARA_BIG_ENDIAN
@ -28,7 +28,8 @@
namespace Nz namespace Nz
{ {
inline constexpr Endianness GetPlatformEndianness(); inline constexpr Endianness GetPlatformEndianness();
inline void SwapBytes(void* buffer, unsigned int size); inline void SwapBytes(void* buffer, std::size_t size);
template<typename T> T SwapBytes(T value);
} }
#include <Nazara/Core/Endianness.inl> #include <Nazara/Core/Endianness.inl>

View File

@ -12,7 +12,6 @@ namespace Nz
* \brief Gets the platform endianness * \brief Gets the platform endianness
* \return Type of the endianness * \return Type of the endianness
*/ */
inline constexpr Endianness GetPlatformEndianness() inline constexpr Endianness GetPlatformEndianness()
{ {
#if defined(NAZARA_BIG_ENDIAN) #if defined(NAZARA_BIG_ENDIAN)
@ -29,10 +28,9 @@ namespace Nz
* \param buffer Raw memory * \param buffer Raw memory
* \param size Size to change endianness * \param size Size to change endianness
* *
* \remark If size is greather than the preallocated buffer, the behaviour is undefined * \remark If size is greater than the preallocated buffer, the behavior is undefined
*/ */
inline void SwapBytes(void* buffer, std::size_t size)
inline void SwapBytes(void* buffer, unsigned int size)
{ {
UInt8* bytes = static_cast<UInt8*>(buffer); UInt8* bytes = static_cast<UInt8*>(buffer);
unsigned int i = 0; unsigned int i = 0;
@ -41,6 +39,13 @@ namespace Nz
while (i < j) while (i < j)
std::swap(bytes[i++], bytes[j--]); std::swap(bytes[i++], bytes[j--]);
} }
template<typename T>
T SwapBytes(T value)
{
SwapBytes(&value, sizeof(T));
return value;
}
} }
#include <Nazara/Core/DebugOff.hpp> #include <Nazara/Core/DebugOff.hpp>