00001 #ifndef INCLUDED_Serializer_h
00002 #define INCLUDED_Serializer_h
00003
00004 #include "Shared/get_time.h"
00005 #include <iostream>
00006
00007 namespace SerializerNS {
00008
00009 enum PacketFormat_t {
00010 packet_text=0,
00011 packet_visionraw_half,
00012 packet_visionraw_full,
00013 packet_visionraw_yfull_uvhalf,
00014 packet_visionrle_full,
00015 packet_worldstatejoints,
00016 packet_worldstatepids,
00017 packet_worldstatebuttons,
00018 packet_wmclass
00019 };
00020 };
00021
00022 using namespace SerializerNS;
00023
00024
00025 class Serializer {
00026 public:
00027
00028 template<class T>
00029 inline static void encode(char **dst, T value) {
00030 memcpy(*dst, (char *)&value, sizeof(T));
00031
00032
00033
00034 (*dst) += sizeof(T);
00035 }
00036
00037
00038 inline static void hostToNetwork(char *dst, const char *src, int length) {
00039 for (int i=0; i<length; i++)
00040 dst[length-1-i]=src[i];
00041 }
00042
00043
00044 inline static void encode(char **dst, const char *src, int length) {
00045 memcpy(*dst, src, length);
00046 (*dst) +=length;
00047 }
00048
00049
00050 inline static void encodeHeader(char **dst, PacketFormat_t pformat) {
00051 int time=get_time();
00052 encode(dst, pformat);
00053 encode(dst, time);
00054 }
00055
00056
00057 inline static void encodeDoublesAsFloats(char **dst, const double *src, int length) {
00058 for (int i=0; i<length; i++)
00059 encode(dst, float(src[i]));
00060 }
00061 };
00062
00063
00064
00065
00066
00067
00068
00069
00070
00071
00072
00073
00074 #endif