nuclear@0: /* nuclear@0: Open Asset Import Library (assimp) nuclear@0: ---------------------------------------------------------------------- nuclear@0: nuclear@0: Copyright (c) 2006-2012, assimp team nuclear@0: All rights reserved. nuclear@0: nuclear@0: Redistribution and use of this software in source and binary forms, nuclear@0: with or without modification, are permitted provided that the nuclear@0: following conditions are met: nuclear@0: nuclear@0: * Redistributions of source code must retain the above nuclear@0: copyright notice, this list of conditions and the nuclear@0: following disclaimer. nuclear@0: nuclear@0: * Redistributions in binary form must reproduce the above nuclear@0: copyright notice, this list of conditions and the nuclear@0: following disclaimer in the documentation and/or other nuclear@0: materials provided with the distribution. nuclear@0: nuclear@0: * Neither the name of the assimp team, nor the names of its nuclear@0: contributors may be used to endorse or promote products nuclear@0: derived from this software without specific prior nuclear@0: written permission of the assimp team. nuclear@0: nuclear@0: THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS nuclear@0: "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT nuclear@0: LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR nuclear@0: A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT nuclear@0: OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, nuclear@0: SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT nuclear@0: LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, nuclear@0: DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY nuclear@0: THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT nuclear@0: (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE nuclear@0: OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. nuclear@0: nuclear@0: ---------------------------------------------------------------------- nuclear@0: */ nuclear@0: nuclear@0: /** @file Helper class tp perform various byte oder swappings nuclear@0: (e.g. little to big endian) */ nuclear@0: #ifndef AI_BYTESWAP_H_INC nuclear@0: #define AI_BYTESWAP_H_INC nuclear@0: nuclear@0: #include "assimp/ai_assert.h" nuclear@0: #include "assimp/types.h" nuclear@0: nuclear@0: #if _MSC_VER >= 1400 nuclear@0: #include nuclear@0: #endif nuclear@0: nuclear@0: namespace Assimp { nuclear@0: // -------------------------------------------------------------------------------------- nuclear@0: /** Defines some useful byte order swap routines. nuclear@0: * nuclear@0: * This is required to read big-endian model formats on little-endian machines, nuclear@0: * and vice versa. Direct use of this class is DEPRECATED. Use #StreamReader instead. */ nuclear@0: // -------------------------------------------------------------------------------------- nuclear@0: class ByteSwap nuclear@0: { nuclear@0: ByteSwap() {} nuclear@0: nuclear@0: public: nuclear@0: nuclear@0: // ---------------------------------------------------------------------- nuclear@0: /** Swap two bytes of data nuclear@0: * @param[inout] _szOut A void* to save the reintcasts for the caller. */ nuclear@0: static inline void Swap2(void* _szOut) nuclear@0: { nuclear@0: ai_assert(_szOut); nuclear@0: nuclear@0: #if _MSC_VER >= 1400 nuclear@0: uint16_t* const szOut = reinterpret_cast(_szOut); nuclear@0: *szOut = _byteswap_ushort(*szOut); nuclear@0: #else nuclear@0: uint8_t* const szOut = reinterpret_cast(_szOut); nuclear@0: std::swap(szOut[0],szOut[1]); nuclear@0: #endif nuclear@0: } nuclear@0: nuclear@0: // ---------------------------------------------------------------------- nuclear@0: /** Swap four bytes of data nuclear@0: * @param[inout] _szOut A void* to save the reintcasts for the caller. */ nuclear@0: static inline void Swap4(void* _szOut) nuclear@0: { nuclear@0: ai_assert(_szOut); nuclear@0: nuclear@0: #if _MSC_VER >= 1400 nuclear@0: uint32_t* const szOut = reinterpret_cast(_szOut); nuclear@0: *szOut = _byteswap_ulong(*szOut); nuclear@0: #else nuclear@0: uint8_t* const szOut = reinterpret_cast(_szOut); nuclear@0: std::swap(szOut[0],szOut[3]); nuclear@0: std::swap(szOut[1],szOut[2]); nuclear@0: #endif nuclear@0: } nuclear@0: nuclear@0: // ---------------------------------------------------------------------- nuclear@0: /** Swap eight bytes of data nuclear@0: * @param[inout] _szOut A void* to save the reintcasts for the caller. */ nuclear@0: static inline void Swap8(void* _szOut) nuclear@0: { nuclear@0: ai_assert(_szOut); nuclear@0: nuclear@0: #if _MSC_VER >= 1400 nuclear@0: uint64_t* const szOut = reinterpret_cast(_szOut); nuclear@0: *szOut = _byteswap_uint64(*szOut); nuclear@0: #else nuclear@0: uint8_t* const szOut = reinterpret_cast(_szOut); nuclear@0: std::swap(szOut[0],szOut[7]); nuclear@0: std::swap(szOut[1],szOut[6]); nuclear@0: std::swap(szOut[2],szOut[5]); nuclear@0: std::swap(szOut[3],szOut[4]); nuclear@0: #endif nuclear@0: } nuclear@0: nuclear@0: // ---------------------------------------------------------------------- nuclear@0: /** ByteSwap a float. Not a joke. nuclear@0: * @param[inout] fOut ehm. .. */ nuclear@0: static inline void Swap(float* fOut) { nuclear@0: Swap4(fOut); nuclear@0: } nuclear@0: nuclear@0: // ---------------------------------------------------------------------- nuclear@0: /** ByteSwap a double. Not a joke. nuclear@0: * @param[inout] fOut ehm. .. */ nuclear@0: static inline void Swap(double* fOut) { nuclear@0: Swap8(fOut); nuclear@0: } nuclear@0: nuclear@0: nuclear@0: // ---------------------------------------------------------------------- nuclear@0: /** ByteSwap an int16t. Not a joke. nuclear@0: * @param[inout] fOut ehm. .. */ nuclear@0: static inline void Swap(int16_t* fOut) { nuclear@0: Swap2(fOut); nuclear@0: } nuclear@0: nuclear@0: static inline void Swap(uint16_t* fOut) { nuclear@0: Swap2(fOut); nuclear@0: } nuclear@0: nuclear@0: // ---------------------------------------------------------------------- nuclear@0: /** ByteSwap an int32t. Not a joke. nuclear@0: * @param[inout] fOut ehm. .. */ nuclear@0: static inline void Swap(int32_t* fOut){ nuclear@0: Swap4(fOut); nuclear@0: } nuclear@0: nuclear@0: static inline void Swap(uint32_t* fOut){ nuclear@0: Swap4(fOut); nuclear@0: } nuclear@0: nuclear@0: // ---------------------------------------------------------------------- nuclear@0: /** ByteSwap an int64t. Not a joke. nuclear@0: * @param[inout] fOut ehm. .. */ nuclear@0: static inline void Swap(int64_t* fOut) { nuclear@0: Swap8(fOut); nuclear@0: } nuclear@0: nuclear@0: static inline void Swap(uint64_t* fOut) { nuclear@0: Swap8(fOut); nuclear@0: } nuclear@0: nuclear@0: // ---------------------------------------------------------------------- nuclear@0: //! Templatized ByteSwap nuclear@0: //! \returns param tOut as swapped nuclear@0: template nuclear@0: static inline Type Swapped(Type tOut) nuclear@0: { nuclear@0: return _swapper()(tOut); nuclear@0: } nuclear@0: nuclear@0: private: nuclear@0: nuclear@0: template struct _swapper; nuclear@0: }; nuclear@0: nuclear@0: template struct ByteSwap::_swapper { nuclear@0: T operator() (T tOut) { nuclear@0: Swap2(&tOut); nuclear@0: return tOut; nuclear@0: } nuclear@0: }; nuclear@0: nuclear@0: template struct ByteSwap::_swapper { nuclear@0: T operator() (T tOut) { nuclear@0: Swap4(&tOut); nuclear@0: return tOut; nuclear@0: } nuclear@0: }; nuclear@0: nuclear@0: template struct ByteSwap::_swapper { nuclear@0: T operator() (T tOut) { nuclear@0: Swap8(&tOut); nuclear@0: return tOut; nuclear@0: } nuclear@0: }; nuclear@0: nuclear@0: nuclear@0: // -------------------------------------------------------------------------------------- nuclear@0: // ByteSwap macros for BigEndian/LittleEndian support nuclear@0: // -------------------------------------------------------------------------------------- nuclear@0: #if (defined AI_BUILD_BIG_ENDIAN) nuclear@0: # define AI_LE(t) (t) nuclear@0: # define AI_BE(t) ByteSwap::Swapped(t) nuclear@0: # define AI_LSWAP2(p) nuclear@0: # define AI_LSWAP4(p) nuclear@0: # define AI_LSWAP8(p) nuclear@0: # define AI_LSWAP2P(p) nuclear@0: # define AI_LSWAP4P(p) nuclear@0: # define AI_LSWAP8P(p) nuclear@0: # define LE_NCONST const nuclear@0: # define AI_SWAP2(p) ByteSwap::Swap2(&(p)) nuclear@0: # define AI_SWAP4(p) ByteSwap::Swap4(&(p)) nuclear@0: # define AI_SWAP8(p) ByteSwap::Swap8(&(p)) nuclear@0: # define AI_SWAP2P(p) ByteSwap::Swap2((p)) nuclear@0: # define AI_SWAP4P(p) ByteSwap::Swap4((p)) nuclear@0: # define AI_SWAP8P(p) ByteSwap::Swap8((p)) nuclear@0: # define BE_NCONST nuclear@0: #else nuclear@0: # define AI_BE(t) (t) nuclear@0: # define AI_LE(t) ByteSwap::Swapped(t) nuclear@0: # define AI_SWAP2(p) nuclear@0: # define AI_SWAP4(p) nuclear@0: # define AI_SWAP8(p) nuclear@0: # define AI_SWAP2P(p) nuclear@0: # define AI_SWAP4P(p) nuclear@0: # define AI_SWAP8P(p) nuclear@0: # define BE_NCONST const nuclear@0: # define AI_LSWAP2(p) ByteSwap::Swap2(&(p)) nuclear@0: # define AI_LSWAP4(p) ByteSwap::Swap4(&(p)) nuclear@0: # define AI_LSWAP8(p) ByteSwap::Swap8(&(p)) nuclear@0: # define AI_LSWAP2P(p) ByteSwap::Swap2((p)) nuclear@0: # define AI_LSWAP4P(p) ByteSwap::Swap4((p)) nuclear@0: # define AI_LSWAP8P(p) ByteSwap::Swap8((p)) nuclear@0: # define LE_NCONST nuclear@0: #endif nuclear@0: nuclear@0: nuclear@0: namespace Intern { nuclear@0: nuclear@0: // -------------------------------------------------------------------------------------------- nuclear@0: template nuclear@0: struct ByteSwapper { nuclear@0: void operator() (T* inout) { nuclear@0: ByteSwap::Swap(inout); nuclear@0: } nuclear@0: }; nuclear@0: nuclear@0: template nuclear@0: struct ByteSwapper { nuclear@0: void operator() (T*) { nuclear@0: } nuclear@0: }; nuclear@0: nuclear@0: // -------------------------------------------------------------------------------------------- nuclear@0: template nuclear@0: struct Getter { nuclear@0: void operator() (T* inout, bool le) { nuclear@0: #ifdef AI_BUILD_BIG_ENDIAN nuclear@0: le = le; nuclear@0: #else nuclear@0: le = !le; nuclear@0: #endif nuclear@0: if (le) { nuclear@0: ByteSwapper1?true:false)> () (inout); nuclear@0: } nuclear@0: else ByteSwapper () (inout); nuclear@0: } nuclear@0: }; nuclear@0: nuclear@0: template nuclear@0: struct Getter { nuclear@0: nuclear@0: void operator() (T* inout, bool /*le*/) { nuclear@0: // static branch nuclear@0: ByteSwapper1)> () (inout); nuclear@0: } nuclear@0: }; nuclear@0: } // end Intern nuclear@0: } // end Assimp nuclear@0: nuclear@0: #endif //!! AI_BYTESWAP_H_INC