source: trunk/src/mesh/byte_order.h @ 349

Last change on this file since 349 was 270, checked in by cxsong, 18 years ago

added copy array functions

File size: 2.2 KB
Line 
1#ifndef __RP_BYTEORDER_H__
2#define __RP_BYTEORDER_H__
3
4//#include <stdio.h>
5#include <string.h>
6
7template<class ValType> class ByteOrder {
8public:
9        ByteOrder() { };
10        ~ByteOrder() { };
11
12        // returns 1 if byte order is big endian
13        static int IsBigEndian()
14        {
15                unsigned x = 1;
16                return !(*(char *)(&x));
17        };
18
19        // copy value from src to dst adjusting for endianess
20        // If the machine calling this function uses big endian byte order,
21        // bytes are flipped.
22        static void OrderCopy(const ValType* src_val, ValType* dst_val)
23        {
24                unsigned char* src = (unsigned char *)src_val;
25                unsigned char* dst = (unsigned char *)dst_val;
26       
27                // our software will use little endian
28                // swap bytes if reading on a big endian machine
29                //
30                if (IsBigEndian()) {
31                        switch(sizeof(ValType)) {
32                                case 1:
33                                        dst[0] = src[0];
34                                        break;
35                                case 2:
36                                        dst[1] = src[0];
37                                        dst[0] = src[1];
38                                        break;
39                                case 4:
40                                        dst[3] = src[0];
41                                        dst[2] = src[1];
42                                        dst[1] = src[2];
43                                        dst[0] = src[3];
44                                default:
45                                        dst += sizeof(ValType)-1;
46                                        for(int i=sizeof(ValType); i>0; i-- )
47                                                *(dst--) = *(src++);
48                        }
49                }
50                else {
51                        // LE: straight copy
52                        unsigned int i;
53                        for (i=0; i<sizeof(ValType); i++)
54                                memcpy((void*)dst, (void*)src, sizeof(ValType));
55                }
56        };
57
58        // copy an array of ValType elements
59        static void OrderCopyArray(const ValType* src_val, ValType* dst_val, int nitems)
60        {
61                for (int i=0; i < nitems; i++)
62                        OrderCopy(&(src_val[i]), &(dst_val[i]));
63        };
64
65        // swap bytes in place, src altered for BE machine.
66        static void Flip(ValType* src_val)
67        {
68                ValType val;
69                Copy(src_val, &val);
70                memcpy((void*)src_val, (void*)&val, sizeof(ValType));
71        };
72
73        static void FlipArray(ValType* src_val, int nitems)
74        {
75                for (int i=0; i < nitems; i++)
76                        Flip(&(src_val[i]));
77        }
78
79        operator ValType () const
80        {
81                ValType l_value;
82                OrderCopy(m_val, l_value);
83                return l_value;
84        };
85
86        ValType operator=(ValType in_val)
87        {
88                OrderCopy(in_val, m_val);
89                return in_val;
90        };
91
92private:
93        ValType m_val;
94};
95
96#endif
Note: See TracBrowser for help on using the repository browser.