Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Namespace Members | Class Members | File Members | Related Pages

byteswap.hpp

00001 /************************************************************************************
00002  *    This file is part of the MynahSA streaming and archiving toolkit              *
00003  *    Copyright (C) 2006 Mynah-Software Ltd. All Rights Reserved.                   *
00004  *                                                                                  *
00005  *    This program is free software; you can redistribute it and/or modify          *
00006  *    it under the terms of the GNU General Public License, version 2               *
00007  *    as published by the Free Software Foundation.                                 *
00008  *                                                                                  *
00009  *    This program is distributed in the hope that it will be useful,               *
00010  *    but WITHOUT ANY WARRANTY; without even the implied warranty of                *
00011  *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the                 *
00012  *    GNU General Public License for more details.                                  *
00013  *                                                                                  *
00014  *    You should have received a copy of the GNU General Public License along       *
00015  *    with this program; if not, write to the Free Software Foundation, Inc.,       *
00016  *    51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.                   *
00017  *                                                                                  *
00018  ************************************************************************************/
00019 
00020 #ifndef __byte_swap_hpp
00021 #define __byte_swap_hpp
00022 
00023 // simple recursive template declaration for swapping byte order
00024 
00025 
00026 namespace MynahSA {
00027   
00028   template<int N>
00029   inline void swapit_helper(char* a, char* b) { 
00030     std::swap(*a, *b);
00031     swapit_helper<N-1>(a+1, b-1);
00032   }
00033   
00034   template<>
00035   inline void swapit_helper<0>(char* a, char* b) {
00036   }
00037   
00038   
00039   template<class T>
00040   inline void byteswap(T& i) {
00041     swapit_helper< sizeof(T)/2 >( ((char*) &i), ((char*)&i)+sizeof(T)-1);
00042   }
00043 
00044 }
00045 #endif