flatten 20260225
This commit is contained in:
72
tjp/core/endian/Endian.h
Executable file
72
tjp/core/endian/Endian.h
Executable file
@@ -0,0 +1,72 @@
|
||||
// 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
|
||||
Reference in New Issue
Block a user