/* * endian.hpp * * Copyright (C) 2005 Bastian Blank * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ #ifndef ENDIAN_HPP #define ENDIAN_HPP #include #include namespace endian { class little_endian { }; class big_endian { }; #if __BYTE_ORDER == __LITTLE_ENDIAN typedef little_endian host_endian; #elif __BYTE_ORDER == __BIG_ENDIAN typedef big_endian host_endian; #endif template struct convert_nop { inline type operator () (const type &in) const throw () { return in; } }; template struct convert_simple { }; template <> struct convert_simple : public convert_nop { }; template <> struct convert_simple { inline uint16_t operator () (const uint16_t &in) const throw () { return (in & 0x00ffU) << 8 | (in & 0xff00U) >> 8; } }; template <> struct convert_simple { inline uint32_t operator () (const uint32_t &in) const throw () { return (in & 0x000000ffU) << 24 | (in & 0xff000000U) >> 24 | (in & 0x0000ff00U) << 8 | (in & 0x00ff0000U) >> 8; } }; template <> struct convert_simple { inline uint64_t operator () (const uint64_t &in) const throw () { return (in & 0x00000000000000ffULL) << 56 | (in & 0xff00000000000000ULL) >> 56 | (in & 0x000000000000ff00ULL) << 40 | (in & 0x00ff000000000000ULL) >> 40 | (in & 0x0000000000ff0000ULL) << 24 | (in & 0x0000ff0000000000ULL) >> 24 | (in & 0x00000000ff000000ULL) << 8 | (in & 0x000000ff00000000ULL) >> 8; } }; template <> struct convert_simple : public convert_simple { }; template <> struct convert_simple : public convert_simple { }; template <> struct convert_simple : public convert_simple { }; template <> struct convert_simple : public convert_simple { }; template <> struct convert_simple : public convert_simple { }; template <> struct convert_simple : public convert_simple { }; template <> struct convert_simple : public convert_simple { }; template <> struct convert_simple : public convert_simple { }; template <> struct convert_simple : public convert_simple { }; template <> struct convert_simple : public convert_simple { }; template <> struct convert_simple : public convert_simple { }; template <> struct convert_simple : public convert_simple { }; template struct convert_complete { }; template struct convert_complete : public convert_nop { }; template struct convert_complete : public convert_simple { }; template struct convert_complete : public convert_nop { }; template struct convert_complete : public convert_simple { }; template struct convert { }; template struct convert : public convert_complete { }; template struct convert : public convert_complete { }; } #endif