1. move flipBytes(float/double) to ByteOrder.

2. remove redundant code from BinaryWriter and BinaryReader.
This commit is contained in:
aaron0x
2015-09-20 05:21:36 +08:00
parent ee9794f087
commit 3e7f722ca6
6 changed files with 165 additions and 241 deletions

View File

@@ -24,7 +24,6 @@
#include <stdlib.h> // builtins
#endif
namespace Poco {
@@ -38,6 +37,8 @@ public:
static UInt16 flipBytes(UInt16 value);
static Int32 flipBytes(Int32 value);
static UInt32 flipBytes(UInt32 value);
static float flipBytes(float value);
static double flipBytes(double value);
#if defined(POCO_HAVE_INT64)
static Int64 flipBytes(Int64 value);
static UInt64 flipBytes(UInt64 value);
@@ -96,6 +97,21 @@ public:
static Int64 fromNetwork(Int64 value);
static UInt64 fromNetwork (UInt64 value);
#endif
private:
template<typename T>
static T flip(T value)
{
T flip = value;
std::size_t halfSize = sizeof(T) / 2;
char* flipP = reinterpret_cast<char*>(&flip);
for (std::size_t i = 0; i < halfSize; i++)
{
std::swap(flipP[i], flipP[sizeof(T) - i - 1]);
}
return flip;
}
};
@@ -152,6 +168,18 @@ inline Int32 ByteOrder::flipBytes(Int32 value)
}
inline float ByteOrder::flipBytes(float value)
{
return flip(value);
}
inline double ByteOrder::flipBytes(double value)
{
return flip(value);
}
#if defined(POCO_HAVE_INT64)
inline UInt64 ByteOrder::flipBytes(UInt64 value)
{