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.cxx | 25 ++++--- odb/pgsql/endian-traits.hxx | 169 ++++++++++++++++++++++++++------------------ 2 files changed, 113 insertions(+), 81 deletions(-) (limited to 'odb') diff --git a/odb/pgsql/endian-traits.cxx b/odb/pgsql/endian-traits.cxx index 6730912..00420d0 100644 --- a/odb/pgsql/endian-traits.cxx +++ b/odb/pgsql/endian-traits.cxx @@ -9,21 +9,24 @@ namespace odb { namespace pgsql { - namespace + namespace details { - endian_traits::endian - infer_host_endian () + namespace { - short s (1); - char* c (reinterpret_cast (&s)); + endian_traits::endian + infer_host_endian () + { + short s (1); + char* c (reinterpret_cast (&s)); - return *c == 0 ? - endian_traits::big_endian : - endian_traits::little_endian; + return *c == 0 ? + endian_traits::big : + endian_traits::little; + } } - } - const endian_traits::endian endian_traits::host_endian ( - infer_host_endian ()); + const endian_traits::endian endian_traits::host_endian ( + infer_host_endian ()); + } } } 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