00001
00002 #ifndef LMSG_GRINDER_HH
00003 #define LMSG_GRINDER_HH
00004 #include "lmsg/MsgTypes.hh"
00005 #include <string.h>
00006
00007 namespace lmsg {
00008
00016 class Grinder {
00017 public:
00022 Grinder(void);
00023
00027 ~Grinder(void);
00028
00033 bool need_swap(void) const;
00034
00040 template<typename T> void SwapT(T* _data);
00041
00051 template<typename T> void SwapIn(const char* _in, T* _out, size_type N);
00052
00062 template<typename T> void SwapOut(const T* _in, char* out, size_type N);
00063 private:
00064 bool mBigEnd;
00065 };
00066
00067
00068 inline bool
00069 Grinder::need_swap(void) const {
00070 return !mBigEnd;
00071 }
00072
00073
00074 template<typename T>
00075 inline void
00076 Grinder::SwapT(T* _data) {
00077 char* fp = reinterpret_cast<char*>(_data);
00078 char* bp = fp + sizeof(T);
00079 while (fp < bp) {
00080 char t = *(--bp);
00081 *bp = *fp;
00082 *(fp++) = t;
00083 }
00084 }
00085
00086 template<typename T>
00087 inline void
00088 Grinder::SwapIn(const char* _in, T* _out, size_type N) {
00089 memcpy(_out, _in, N*sizeof(T));
00090 if (!mBigEnd) {
00091 for (size_type _i=0; _i<N; ++_i) SwapT(_out+_i);
00092 }
00093 }
00094
00095 template<typename T>
00096 inline void
00097 Grinder::SwapOut(const T* _in, char* _out, size_type N) {
00098 memcpy(_out, _in, N*sizeof(T));
00099 if (!mBigEnd) {
00100 T* _op = reinterpret_cast<T*>(_out);
00101 for (size_type _i=0; _i<N; ++_i) SwapT(_op+_i);
00102 }
00103 }
00104
00105 extern Grinder export_format_grinder;
00106
00107 }
00108
00109 #endif // LMSG_GRINDER_HH