Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //==============================================================================
- //==============================================================================
- namespace {
- template <int S> struct swapHelper {
- static void swap (unsigned char *v)
- {
- const unsigned int HALF_SIZE = S/2;
- for (unsigned int i = 0; i < HALF_SIZE; ++i) {
- unsigned char *a = v+i;
- unsigned char *b = v+S-i-1;
- unsigned char temp = *a;
- *a = *b;
- *b = temp;
- }
- }
- };
- template <> struct swapHelper<1> {
- static void swap (unsigned char *v)
- {
- // Do nothing
- }
- };
- template <> struct swapHelper<2> {
- static void swap (unsigned char *v)
- {
- unsigned char temp = v[0];
- v[0] = v[1];
- v[1] = temp;
- }
- };
- template <> struct swapHelper<4> {
- static void swap (unsigned char *v)
- {
- unsigned char temp = v[0];
- v[0] = v[3];
- v[3] = temp;
- temp = v[1];
- v[1] = v[2];
- v[2] = temp;
- }
- };
- template <> struct swapHelper<8> {
- static void swap (unsigned char *v)
- {
- unsigned char temp = v[0];
- v[0] = v[7];
- v[7] = temp;
- temp = v[1];
- v[1] = v[6];
- v[6] = temp;
- temp = v[2];
- v[2] = v[5];
- v[5] = temp;
- temp = v[3];
- v[3] = v[4];
- v[4] = temp;
- }
- };
- }
- //==============================================================================
- //==============================================================================
- class Endian {
- private:
- Endian (void);
- Endian (const Endian &rhs);
- Endian & operator = (const Endian &rhs);
- ~Endian (void);
- public:
- /// Swaps endianness of input data
- /// \param v input data to swap
- template <typename T>
- static void swap (T &v)
- {
- swapHelper<sizeof(T)>::swap((unsigned char*)&v);
- }
- };
- //==============================================================================
- //==============================================================================
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement