From e8a21e1120509bf1dcfedc88e82af5e136b98ce4 Mon Sep 17 00:00:00 2001 From: Constantin Michael Date: Thu, 26 May 2011 18:54:54 +0200 Subject: Correct endian-traits --- odb/pgsql/endian-traits.hxx | 169 ++++++++++++++++++++++++++------------------ 1 file changed, 99 insertions(+), 70 deletions(-) (limited to 'odb/pgsql/endian-traits.hxx') diff --git a/odb/pgsql/endian-traits.hxx b/odb/pgsql/endian-traits.hxx index 07dc023..2dc79f3 100644 --- a/odb/pgsql/endian-traits.hxx +++ b/odb/pgsql/endian-traits.hxx @@ -6,103 +6,132 @@ #ifndef ODB_PGSQL_ENDIAN_TRAITS_HXX #define ODB_PGSQL_ENDIAN_TRAITS_HXX +#include // std::size_t #include // std::reverse namespace odb { namespace pgsql { - template - struct swap_endian; - - template - struct swap_endian + namespace details { - static T - swap (T x) + template + struct swap_endian; + + template + struct swap_endian { - union u2 + static T + swap (T x) { - T t; - char c[2]; - }; + union u2 + { + T t; + char c[2]; + }; - u2 u; - u.t = x; - std::reverse (u.c, u.c + 2); + u2 u; + u.t = x; - return u.t; - } - }; + char tmp (u.c[0]); + u.c[0] = u.c[1]; + u.c[1] = tmp; - template - struct swap_endian - { - static T - swap (T x) + return u.t; + } + }; + + template + struct swap_endian { - union u4 + static T + swap (T x) { - T t; - char c[4]; - }; + union u4 + { + T t; + char c[4]; + }; - u4 u; - u.t = x; - std::reverse (u.c, u.c + 4); + u4 u; + u.t = x; - return u.t; - } - }; + char tmp (u.c[0]); + u.c[0] = u.c[3]; + u.c[3] = tmp; - template - struct swap_endian - { - static T - swap (T x) + tmp = u.c[1]; + u.c[1] = u.c[2]; + u.c[2] = tmp; + + return u.t; + } + }; + + template + struct swap_endian { - union u8 + static T + swap (T x) { - T t; - char c[8]; - }; + union u8 + { + T t; + char c[8]; + }; - u8 u; - u.t = x; - std::reverse (u.c, u.c + 8); + u8 u; + u.t = x; - return u.t; - } - }; + char tmp (u.c[0]); + u.c[0] = u.c[7]; + u.c[7] = tmp; - class endian_traits - { - public: - enum endian - { - big_endian, - little_endian - }; + tmp = u.c[1]; + u.c[1] = u.c[6]; + u.c[6] = tmp; - public: - static const endian host_endian; + tmp = u.c[2]; + u.c[2] = u.c[5]; + u.c[5] = tmp; - public: + tmp = u.c[3]; + u.c[3] = u.c[4]; + u.c[4] = tmp; - template - static T - hton (T x) - { - return host_endian == big_endian ? x : swap_endian::swap (x); - } + return u.t; + } + }; - template - static T - ntoh (T x) + class endian_traits { - return host_endian == big_endian ? x : swap_endian::swap (x); - } - }; + public: + enum endian + { + big, + little + }; + + public: + static const endian host_endian; + + public: + + template + static T + hton (T x) + { + return host_endian == big ? x : swap_endian::swap (x); + } + + template + static T + ntoh (T x) + { + return host_endian == big ? x : swap_endian::swap (x); + } + }; + } } } -- cgit v1.1