![]() |
pl-nk v0.4.5
Plonk|Plink|Plank are a set of cross-platform C/C++ frameworks for audio software development
|
00001 /* 00002 ------------------------------------------------------------------------------- 00003 This file is part of the Plink, Plonk, Plank libraries 00004 by Martin Robinson 00005 00006 http://code.google.com/p/pl-nk/ 00007 00008 Copyright University of the West of England, Bristol 2011-14 00009 All rights reserved. 00010 00011 Redistribution and use in source and binary forms, with or without 00012 modification, are permitted provided that the following conditions are met: 00013 00014 * Redistributions of source code must retain the above copyright 00015 notice, this list of conditions and the following disclaimer. 00016 * Redistributions in binary form must reproduce the above copyright 00017 notice, this list of conditions and the following disclaimer in the 00018 documentation and/or other materials provided with the distribution. 00019 * Neither the name of University of the West of England, Bristol nor 00020 the names of its contributors may be used to endorse or promote products 00021 derived from this software without specific prior written permission. 00022 00023 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 00024 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 00025 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 00026 DISCLAIMED. IN NO EVENT SHALL UNIVERSITY OF THE WEST OF ENGLAND, BRISTOL BE 00027 LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 00028 CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE 00029 GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 00030 HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 00031 LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT 00032 OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 00033 00034 This software makes use of third party libraries. For more information see: 00035 doc/license.txt included in the distribution. 00036 ------------------------------------------------------------------------------- 00037 */ 00038 00039 #ifndef PLONK_ENDIAN_H 00040 #define PLONK_ENDIAN_H 00041 00042 #include "../containers/plonk_ContainerForwardDeclarations.h" 00043 00044 template<UnsignedLong length> 00045 class EndianUtility 00046 { 00047 public: 00048 static inline void swap (void* bits) throw() 00049 { 00050 unsigned char* data = reinterpret_cast<unsigned char*> (bits); 00051 const unsigned int numSwap = length / 2; 00052 const unsigned int end = length - 1; 00053 00054 for (int i = 0; i < numSwap; ++i) 00055 { 00056 const unsigned char temp = data[i]; 00057 data[i] = data[end - i]; 00058 data[end - i] = temp; 00059 } 00060 } 00061 00062 static inline void swap (void* bits, 00063 const unsigned int itemSize, // hmm, isn't itemSize the same as the tparam length? 00064 const unsigned long numItems) throw() 00065 { 00066 unsigned char* data = reinterpret_cast<unsigned char*> (bits); 00067 00068 for (int i = 0; i < numItems; ++i, data += itemSize) 00069 swap (reinterpret_cast<void*> (data)); 00070 } 00071 }; 00072 00073 template<> 00074 class EndianUtility<1> 00075 { 00076 public: 00077 static inline void swap (void* bits) throw() 00078 { 00079 (void)bits; 00080 } 00081 00082 static inline void swap (void* bits, 00083 const unsigned int itemSize, 00084 const unsigned long numItems) throw() 00085 { 00086 (void)bits; 00087 (void)itemSize; 00088 (void)numItems; 00089 } 00090 }; 00091 00092 template<> 00093 class EndianUtility<2> 00094 { 00095 public: 00096 static inline void swap (void* bits) throw() 00097 { 00098 pl_SwapEndianUS (reinterpret_cast<PlankUS*> (bits)); 00099 } 00100 00101 static inline void swap (void* bits, 00102 const unsigned int itemSize, 00103 const unsigned long numItems) throw() 00104 { 00105 (void)itemSize; 00106 pl_VectorSwapEndianUS (reinterpret_cast<PlankUS*> (bits), numItems); 00107 } 00108 }; 00109 00110 template<> 00111 class EndianUtility<3> 00112 { 00113 public: 00114 static inline void swap (void* bits) throw() 00115 { 00116 pl_SwapEndianUI24 (reinterpret_cast<PlankUI24*> (bits)); 00117 } 00118 00119 static inline void swap (void* bits, 00120 const unsigned int itemSize, 00121 const unsigned long numItems) throw() 00122 { 00123 (void)itemSize; 00124 pl_VectorSwapEndianUI24 (reinterpret_cast<PlankUI24*> (bits), numItems); 00125 } 00126 }; 00127 00128 template<> 00129 class EndianUtility<4> 00130 { 00131 public: 00132 static inline void swap (void* bits) throw() 00133 { 00134 pl_SwapEndianUI (reinterpret_cast<PlankUI*> (bits)); 00135 } 00136 00137 static inline void swap (void* bits, 00138 const unsigned int itemSize, 00139 const unsigned long numItems) throw() 00140 { 00141 (void)itemSize; 00142 pl_VectorSwapEndianUI (reinterpret_cast<PlankUI*> (bits), numItems); 00143 } 00144 }; 00145 00146 template<> 00147 class EndianUtility<8> 00148 { 00149 public: 00150 static inline void swap (void* bits) throw() 00151 { 00152 pl_SwapEndianULL (reinterpret_cast<PlankULL*> (bits)); 00153 } 00154 00155 static inline void swap (void* bits, 00156 const unsigned int itemSize, 00157 const unsigned long numItems) throw() 00158 { 00159 (void)itemSize; 00160 pl_VectorSwapEndianULL (reinterpret_cast<PlankULL*> (bits), numItems); 00161 } 00162 }; 00163 00164 00167 class Endian 00168 { 00169 public: 00170 template<class Type> 00171 static inline void swap (Type& data) throw() 00172 { 00173 typedef EndianUtility<sizeof (Type)> EndianUtilityType; 00174 00175 EndianUtilityType::swap (reinterpret_cast<void*> (&data)); 00176 } 00177 00178 template<class Type> 00179 static inline void swap (Type* data) throw() 00180 { 00181 typedef EndianUtility<sizeof (Type)> EndianUtilityType; 00182 00183 EndianUtilityType::swap (reinterpret_cast<void*> (data)); 00184 } 00185 00186 template<class Type> 00187 static inline void swap (Type* data, const int numItems) throw() 00188 { 00189 typedef EndianUtility<sizeof (Type)> EndianUtilityType; 00190 00191 EndianUtilityType::swap (reinterpret_cast<void*> (data), 00192 sizeof (Type), 00193 numItems); 00194 } 00195 00196 template<class Type> 00197 static void swap (NumericalArray<Type>& array) throw() 00198 { 00199 typedef EndianUtility<sizeof (Type)> EndianUtilityType; 00200 00201 EndianUtilityType::swap (reinterpret_cast<void*> (array.getArray()), 00202 sizeof (Type), 00203 array.length()); 00204 } 00205 00206 }; 00207 00210 class EndianIfBig : public Endian 00211 { 00212 #if PLONK_LITTLEENDIAN 00213 public: 00214 template<class Type> 00215 static inline void swap (Type& /*data*/) throw() 00216 { 00217 } 00218 00219 template<class Type> 00220 static inline void swap (Type* /*data*/) throw() 00221 { 00222 } 00223 00224 template<class Type> 00225 static inline void swap (Type* /*data*/, const int /*numItems*/) throw() 00226 { 00227 } 00228 00229 template<class Type> 00230 static void swap (NumericalArray<Type>& /*array*/) throw() 00231 { 00232 } 00233 #endif 00234 }; 00235 00238 class EndianIfLittle : public Endian 00239 { 00240 #if PLONK_BIGENDIAN 00241 public: 00242 template<class Type> 00243 static inline void swap (Type& /*data*/) throw() 00244 { 00245 } 00246 00247 template<class Type> 00248 static inline void swap (Type* /*data*/) throw() 00249 { 00250 } 00251 00252 template<class Type> 00253 static inline void swap (Type* /*data*/, const int /*numItems*/) throw() 00254 { 00255 } 00256 00257 template<class Type> 00258 static void swap (NumericalArray<Type>& /*array*/) throw() 00259 { 00260 } 00261 #endif 00262 }; 00263 00264 00265 00266 #endif // PLONK_ENDIAN_H