| 1 |
// SPDX-License-Identifier: LGPL-2.1-or-later |
| 2 |
|
| 3 |
/*************************************************************************** |
| 4 |
* Copyright (c) 2005 Imetric 3D GmbH * |
| 5 |
* * |
| 6 |
* This file is part of the FreeCAD CAx development system. * |
| 7 |
* * |
| 8 |
* This library is free software; you can redistribute it and/or * |
| 9 |
* modify it under the terms of the GNU Library General Public * |
| 10 |
* License as published by the Free Software Foundation; either * |
| 11 |
* version 2 of the License, or (at your option) any later version. * |
| 12 |
* * |
| 13 |
* This library is distributed in the hope that it will be useful, * |
| 14 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of * |
| 15 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * |
| 16 |
* GNU Library General Public License for more details. * |
| 17 |
* * |
| 18 |
* You should have received a copy of the GNU Library General Public * |
| 19 |
* License along with this library; see the file COPYING.LIB. If not, * |
| 20 |
* write to the Free Software Foundation, Inc., 59 Temple Place, * |
| 21 |
* Suite 330, Boston, MA 02111-1307, USA * |
| 22 |
* * |
| 23 |
***************************************************************************/ |
| 24 |
|
| 25 |
|
| 26 |
#ifndef BASE_SWAP_H |
| 27 |
#define BASE_SWAP_H |
| 28 |
|
| 29 |
#define LOW_ENDIAN (unsigned short)0x4949 |
| 30 |
#define HIGH_ENDIAN (unsigned short)0x4D4D |
| 31 |
|
| 32 |
|
| 33 |
namespace Base |
| 34 |
{ |
| 35 |
|
| 36 |
/** |
| 37 |
* \brief Definition of functions that allow swapping of data types |
| 38 |
* dependent on the architecture. |
| 39 |
*/ |
| 40 |
|
| 41 |
/** Returns machine type (low endian, high endian) */ |
| 42 |
unsigned short SwapOrder(); |
| 43 |
|
| 44 |
void SwapVar(char&); |
| 45 |
void SwapVar(unsigned char&); |
| 46 |
void SwapVar(short&); |
| 47 |
void SwapVar(unsigned short&); |
| 48 |
void SwapVar(long&); |
| 49 |
void SwapVar(unsigned long&); |
| 50 |
void SwapVar(float&); |
| 51 |
void SwapVar(double&); |
| 52 |
|
| 53 |
template<class T> |
| 54 |
void SwapEndian(T& v) |
| 55 |
{ |
| 56 |
T tmp = v; |
| 57 |
int i = 0; |
| 58 |
|
| 59 |
for (i = 0; i < (int)sizeof(T); i++) { |
| 60 |
*(((char*)&tmp) + i) = *(((char*)&v) + sizeof(T) - i - 1); |
| 61 |
} |
| 62 |
v = tmp; |
| 63 |
} |
| 64 |
|
| 65 |
} // namespace Base |
| 66 |
|
| 67 |
|
| 68 |
#endif // BASE_SWAP_H |