73 lines
1.1 KiB
C++
Executable File
73 lines
1.1 KiB
C++
Executable File
// TJP COPYRIGHT HEADER
|
|
|
|
#ifndef __Utilities_Endian_h__
|
|
#define __Utilities_Endian_h__
|
|
|
|
namespace tjp {
|
|
namespace core {
|
|
namespace endian {
|
|
|
|
/**
|
|
* Swaps a buffer that represents one variable
|
|
*/
|
|
void swap (char *, int size);
|
|
|
|
/**
|
|
* Swaps the specified buffer if the processor is big endian.
|
|
*/
|
|
inline void toLittleEndian (char *buffer, int size)
|
|
{
|
|
#ifdef __BIG_ENDIAN__
|
|
swap(buffer, size);
|
|
#endif
|
|
}
|
|
|
|
template<typename T>
|
|
inline void toLittleEndian (T &t)
|
|
{
|
|
#ifdef __BIG_ENDIAN__
|
|
swap((char*)&t, sizeof(T));
|
|
#endif
|
|
|
|
return t;
|
|
}
|
|
|
|
/**
|
|
* Swaps the specified buffer if the processor is little endian.
|
|
*/
|
|
inline void toBigEndian (char *buffer, int size)
|
|
{
|
|
#ifndef __BIG_ENDIAN__
|
|
swap(buffer, size);
|
|
#endif
|
|
}
|
|
|
|
template<typename T>
|
|
inline void toBigEndian (T &t)
|
|
{
|
|
#ifndef __BIG_ENDIAN__
|
|
swap((char*)&t, sizeof(T));
|
|
#endif
|
|
|
|
return t;
|
|
}
|
|
|
|
/**
|
|
* Swaps a single variable.
|
|
*/
|
|
template<typename T>
|
|
inline T &swap (T &t)
|
|
{
|
|
#ifdef __BIG_ENDIAN__
|
|
swap((char*)&t, sizeof(T));
|
|
#endif
|
|
|
|
return t;
|
|
}
|
|
|
|
} // namespace endian
|
|
} // namespace core
|
|
} // namespace
|
|
|
|
#endif
|