stlab.adobe.com Adobe Systems Incorporated

endian.hpp

Go to the documentation of this file.
00001 /*
00002     Copyright 2005-2007 Adobe Systems Incorporated
00003     Distributed under the MIT License (see accompanying file LICENSE_1_0_0.txt
00004     or a copy at http://stlab.adobe.com/licenses.html)
00005 */
00006 
00007 /****************************************************************************************************/
00008 
00009 #ifndef ADOBE_ENDIAN_HPP
00010 #define ADOBE_ENDIAN_HPP
00011 
00012 /****************************************************************************************************/
00013 
00014 #include <adobe/config.hpp>
00015 
00016 #include <boost/detail/endian.hpp>
00017 
00018 /*************************************************************************************************/
00019 
00020 namespace adobe {
00021 
00022 /*************************************************************************************************/
00023 
00024 struct endian
00025 {
00026     enum type
00027     {
00028         big,
00029         little,
00030 #if defined(BOOST_BIG_ENDIAN) || defined(__BIG_ENDIAN__)
00031         platform = big,
00032 #elif defined(BOOST_LITTLE_ENDIAN) || defined(__LITTLE_ENDIAN__)
00033         platform = little,
00034 #endif
00035     };
00036 
00037     enum platform_test
00038     {
00039         is_big    = platform == big,
00040         is_little = platform == little
00041     };
00042 };
00043 
00044 /*************************************************************************************************/
00045 
00046 namespace implementation {
00047 
00048 /*************************************************************************************************/
00049 
00050 template <bool NeedsIt>
00051 struct byteswap
00052 {
00053     template <typename T>
00054     void operator()(T& x) const
00055     {
00056         enum
00057         {
00058             size = sizeof(T),
00059             end = size >> 1
00060         };
00061 
00062         for (std::size_t i(0); i < end; ++i)
00063             std::swap(reinterpret_cast<char*>(&x)[i],
00064                       reinterpret_cast<char*>(&x)[size - i - 1]);
00065     }
00066 };
00067 
00068 template <>
00069 struct byteswap<false>
00070 {
00071     template <typename T>
00072     void operator()(T&) const
00073     { }
00074 };
00075 
00076 /*************************************************************************************************/
00077 
00078 } // namespace implementation
00079 
00080 /*************************************************************************************************/
00081 
00082 template <typename T>
00083 inline void byteswap(T& x)
00084 { implementation::byteswap<true>()(x); }
00085 
00086 /*************************************************************************************************/
00087 
00088 template <endian::type SourceEndian>
00089 struct endian_swap
00090 {
00091     enum { need_swap = SourceEndian != endian::platform };
00092 
00093     template <typename T>
00094     void operator()(T& x) const
00095     { implementation::byteswap<need_swap>()(x); }
00096 };
00097 
00098 /****************************************************************************************************/
00099 
00100 } // namespace adobe
00101 
00102 /****************************************************************************************************/
00103 
00104 #endif
00105 
00106 /****************************************************************************************************/

Copyright © 2006-2007 Adobe Systems Incorporated.

Use of this website signifies your agreement to the Terms of Use and Online Privacy Policy.

Search powered by Google