#ifndef __BEELIB_ENDIANNESS_H__ #define __BEELIB_ENDIANNESS_H__ /* Copyright (c) 2007 Andrew Molloy Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ #include "BeeLib.h" #include #include #include namespace BeeLib { enum Endianness { BigEndian, LittleEndian, #if defined(BOOST_BIG_ENDIAN) HostEndianness = BigEndian #elif defined(BOOST_LITTLE_ENDIAN) HostEndianness = LittleEndian #else #error BeeLib only supports big and little endian systems. Sorry, PDP! #endif }; template T swapBytes2(T in); template T swapBytes4(T in); template T swapBytes8(T in); template class swapBytes_impl { template static T swapBytes_(T in) { return in; } template friend T swapBytes(T); }; // specialize for the cases where a swap is needed template<> class swapBytes_impl { template static T swapBytes_(T in) { return swapBytes2(in); } template friend T swapBytes(T); }; template<> class swapBytes_impl { template static T swapBytes_(T in) { return swapBytes4(in); } template friend T swapBytes(T); }; template<> class swapBytes_impl { template static T swapBytes_(T in) { return swapBytes8(in); } template friend T swapBytes(T); }; // this is what you call. template T swapBytes(T in) { // make sure we can actually handle this sized data BOOST_STATIC_ASSERT(sizeof(T) == 2 || sizeof(T) == 4 || sizeof(T) == 8); // Don't try to swap anything except built-in arithmetic types (integers & floats) // If you, for some reason, you think your data should be swapped as-is, then you'll // need to cast it to the appropriately sized type yourself. You should also reconsider // your design, because it's very possibly wrong. Keep in mind, also, that swapping a // pointer makes very little sense. BOOST_STATIC_ASSERT(boost::is_arithmetic::value); return swapBytes_impl::swapBytes_(in); } // some convenience functions template T hostToLittle(T in) { return swapBytes(in); } template T hostToBig(T in) { return swapBytes(in); } template T littleToHost(T in) { return swapBytes(in); } template T bigToHost(T in) { return swapBytes(in); } } #if defined(BEELIB_NO_INLINE_ASM) #include "Endianness.tcc" #else // defined(BEELIB_NO_INLINE_ASM) #if defined(__i386__) #include "Endianness_i686.tcc" #else #include "Endianness.tcc" #endif #endif // defined(BEELIB_NO_INLINE_ASM) #endif