Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Namespace Members | Class Members | File Members | Related Pages

Serializer.h

Go to the documentation of this file.
00001 /***
00002  * Copyright (c) 2005, SkeTo Project
00003  * All rights reserved.
00004  */
00011 #ifndef __SERIALIZER_H__
00012 #define __SERIALIZER_H__
00013 
00014 #include<utility>
00028 template <typename A>
00029 class Serializer {
00030 public:
00040     static unsigned char* construct(A *obj, unsigned char *buf){
00041         return obj->construct(buf);
00042     }
00052     static unsigned char* serialize(const A& obj, unsigned char *buf){
00053         return obj.serialize(buf);
00054     }
00063     static int getSerializedSize(const A& obj){
00064         return obj.getSerializedSize();
00065     }
00072     static bool needsSerializing(){
00073         return A::needsSerializing(); 
00074     }
00075 };
00079 template <>
00080 class Serializer<int> {
00081 public:
00085     static unsigned char* construct(int*obj, unsigned char *buf){
00086         *obj=*(int*)buf;        
00087         return buf+sizeof(int);
00088     }
00092     static unsigned char* serialize(const int& obj, unsigned char *buf){
00093         *((int*)buf) = obj;
00094         return buf+sizeof(int);
00095     }
00099     static int getSerializedSize(const int& obj){
00100         return sizeof(obj);
00101     }
00105     static bool needsSerializing(){
00106         return false; 
00107     }
00108 };
00112 template <>
00113 class Serializer<unsigned int> {
00114 public:
00118     static unsigned char* construct(unsigned int *obj, unsigned char *buf){
00119         *obj=*(unsigned int*)buf;       
00120         return buf+sizeof(unsigned int);
00121     }
00125     static unsigned char* serialize(const unsigned int& obj, unsigned char *buf){
00126         *((unsigned int*)buf) = obj;
00127         return buf+sizeof(unsigned int);
00128     }
00132     static int getSerializedSize(const unsigned int& obj){
00133         return sizeof(obj);
00134     }
00138     static bool needsSerializing(){
00139         return false; 
00140     }
00141 };
00145 template <>
00146 class Serializer<char> {
00147 public:
00151     static unsigned char* construct(char*obj, unsigned char *buf){
00152         *obj=*(char*)buf;       
00153         return buf+sizeof(char);
00154     }
00158     static unsigned char* serialize(const char& obj, unsigned char *buf){
00159         *((char*)buf) = obj;
00160         return buf+sizeof(char);
00161     }
00165     static int getSerializedSize(const char& obj){
00166         return sizeof(obj);
00167     }
00171     static bool needsSerializing(){
00172         return false; 
00173     }
00174 };
00178 template <>
00179 class Serializer<unsigned char> {
00180 public:
00184     static unsigned char* construct(unsigned char* obj, unsigned char *buf){
00185         *obj=*(unsigned char*)buf;      
00186         return buf+sizeof(unsigned char);
00187     }
00191     static unsigned char* serialize(const unsigned char& obj, unsigned char *buf){
00192         *((unsigned char*)buf) = obj;
00193         return buf+sizeof(unsigned char);
00194     }
00198     static int getSerializedSize(const unsigned char& obj){
00199         return sizeof(obj);
00200     }
00204     static bool needsSerializing(){
00205         return false; 
00206     }
00207 };
00211 template <>
00212 class Serializer<bool> {
00213 public:
00217     static unsigned char* construct(bool* obj, unsigned char *buf){
00218         *obj=*(bool*)buf;       
00219         return buf+sizeof(bool);
00220     }
00224     static unsigned char* serialize(const bool& obj, unsigned char *buf){
00225         *((bool*)buf) = obj;
00226         return buf+sizeof(bool);
00227     }
00231     static int getSerializedSize(const bool& obj){
00232         return sizeof(obj);
00233     }
00237     static bool needsSerializing(){
00238         return false; 
00239     }
00240 };
00241 
00245 template <>
00246 class Serializer<double> {
00247 public:
00251     static unsigned char* construct(double* obj, unsigned char *buf){
00252         *obj=*(double*)buf;     
00253         return buf+sizeof(double);
00254     }
00258     static unsigned char* serialize(const double& obj, unsigned char *buf){
00259         *((double*)buf) = obj;
00260         return buf+sizeof(double);
00261     }
00265     static int getSerializedSize(const double& obj){
00266         return sizeof(obj);
00267     }
00271     static bool needsSerializing(){
00272         return false; 
00273     }
00274 };
00275 
00279 template <>
00280 class Serializer<const double> {
00281 public:
00285     static unsigned char* construct(double* obj, unsigned char *buf){
00286         *obj=*(double*)buf;     
00287         return buf+sizeof(double);
00288     }
00292     static unsigned char* serialize(const double& obj, unsigned char *buf){
00293         *((double*)buf) = obj;
00294         return buf+sizeof(double);
00295     }
00299     static int getSerializedSize(const double& obj){
00300         return sizeof(obj);
00301     }
00305     static bool needsSerializing(){
00306         return false; 
00307     }
00308 };
00309 
00310 
00314 template <>
00315 class Serializer<float> {
00316 public:
00320     static unsigned char* construct(float* obj, unsigned char *buf){
00321         *obj=*(float*)buf;      
00322         return buf+sizeof(float);
00323     }
00327     static unsigned char* serialize(const float& obj, unsigned char *buf){
00328         *((float*)buf) = obj;
00329         return buf+sizeof(float);
00330     }
00334     static int getSerializedSize(const float& obj){
00335         return sizeof(obj);
00336     }
00340     static bool needsSerializing(){
00341         return false; 
00342     }
00343 };
00344 
00345 
00346 
00347 template <typename A, typename B>
00348 class Serializer<const std::pair<A,B> > {
00349 public:
00359  public:    
00360     static unsigned char* construct(std::pair<A,B>* obj, unsigned char *buf){
00361         buf=Serializer<A>::construct(&obj->first, buf);
00362         buf=Serializer<B>::construct(&obj->second, buf);
00363         return buf;
00364     }
00374  public:
00375     static unsigned char* serialize(const std::pair<A,B>& obj, unsigned char *buf){
00376         buf=Serializer<A>::serialize(obj.first, buf);
00377         buf=Serializer<B>::serialize(obj.second, buf);
00378         return buf;
00379     }
00387  public:
00388     static int getSerializedSize(const std::pair<A,B>& obj){
00389         int size = 0;
00390         size+=Serializer<A>::getSerializedSize(obj.first);
00391         size+=Serializer<B>::getSerializedSize(obj.second);
00392         return size;
00393     }
00397     static bool needsSerializing(){
00398         return true; 
00399     }
00400 };
00401 
00402 template <typename A, typename B>
00403 class Serializer<std::pair<A,B> > {
00404 public:
00414  public:    
00415     static unsigned char* construct(std::pair<A,B>* obj, unsigned char *buf){
00416         buf=Serializer<A>::construct(&obj->first, buf);
00417         buf=Serializer<B>::construct(&obj->second, buf);
00418         return buf;
00419     }
00429  public:
00430     static unsigned char* serialize(const std::pair<A,B>& obj, unsigned char *buf){
00431         buf=Serializer<A>::serialize(obj.first, buf);
00432         buf=Serializer<B>::serialize(obj.second, buf);
00433         return buf;
00434     }
00442  public:
00443     static int getSerializedSize(const std::pair<A,B>& obj){
00444         int size = 0;
00445         size+=Serializer<A>::getSerializedSize(obj.first);
00446         size+=Serializer<B>::getSerializedSize(obj.second);
00447         return size;
00448     }
00452     static bool needsSerializing(){
00453         return true; 
00454     }
00455 };
00456 
00457 
00458 
00459 #endif // __SERIALIZER_H__

Generated on Wed Jan 18 22:19:28 2006 for SkeTo -- Skeleton Library in Tokyo by  doxygen 1.4.4