Homepage Demos Overview Downloads Tutorials Reference
Credits

Serializer.h

Go to the documentation of this file.
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   // SYNC with Listener.java in TekkotsuMon
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 //!provides a default serializer base class for simple objects
00025 class Serializer {
00026 public:
00027   //! writes @a value to @a dst and advances @a dst
00028   template<class T>
00029   inline static void encode(char **dst, T value) {
00030     memcpy(*dst, (char *)&value, sizeof(T));
00031 //    hostToNetwork(*dst, (char *)&value, sizeof(T));
00032 //    it'd be nice to use network byte order, but we'll save the aibo
00033 //    extra work
00034     (*dst) += sizeof(T);
00035   }
00036 
00037   //! converts to network byte order (big endian - aibo is little endian)
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   //! writes @a length bytes from @a src to @a dst
00044   inline static void encode(char **dst, const char *src, int length) {
00045     memcpy(*dst, src, length);
00046     (*dst) +=length;
00047   }
00048 
00049   //! writes packet type and timestamp
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   //! a simple form of compression - calls encode(@a dst,float(@a src[i])) for i=0..@a length
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 /*! @file
00064  * @brief Defines the Serializer base class, which provides a default serializer for simple objects
00065  * @author alokl (Creator)
00066  *
00067  * $Author: alokl $
00068  * $Name: tekkotsu-1_5 $
00069  * $Revision: 1.5 $
00070  * $State: Rel $
00071  * $Date: 2003/07/28 06:43:52 $
00072  */
00073 
00074 #endif

Tekkotsu v1.5
Generated Fri Oct 10 15:51:59 2003 by Doxygen 1.3.4