]> git.vomp.tv Git - vompclient.git/blob - src/serialize.h
Type change: UCHAR -> u1
[vompclient.git] / src / serialize.h
1 /*
2     Copyright 2004-2005 Chris Tallon, Andreas Vogel
3
4     This file is part of VOMP.
5
6     VOMP is free software; you can redistribute it and/or modify
7     it under the terms of the GNU General Public License as published by
8     the Free Software Foundation; either version 2 of the License, or
9     (at your option) any later version.
10
11     VOMP is distributed in the hope that it will be useful,
12     but WITHOUT ANY WARRANTY; without even the implied warranty of
13     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14     GNU General Public License for more details.
15
16     You should have received a copy of the GNU General Public License
17     along with VOMP; if not, write to the Free Software
18     Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
19 */
20
21 #ifndef SERIALIZE_H
22 #define SERIALIZE_H
23
24 #include <stdio.h>
25 #include <string.h>
26 #include <vector>
27
28 #include "defines.h"
29
30 class SerializeBuffer {
31   public:
32     //constructor for send buffers
33     SerializeBuffer(u4 size,bool isMalloc=false,bool autoincrease=false);
34     //constructor for SerializeBuffers with external buffers
35     SerializeBuffer(u1 *buffer,u4 size,bool owning=false,bool isMalloc=true,bool autoincrease=false);
36     ~SerializeBuffer();
37     //access to bufferpointer
38     u1 * getStart(){ return start;}
39     u1 * getCurrent(){ return current;}
40     u1 * getEnd(){ return end;}
41     //get the buffer and take it away from this object
42     u1 * steelBuffer();
43     //seek relative to current
44     int seek(int amount);
45     void rewind();
46
47     //encode/decode functions
48     //always return != 0 on error
49     int encodeLong(u4 data);
50     int encodeLongLong(u8 data);
51     int encodeShort(u2 data);
52     int encodeString(const char *data);
53     int encodeByte(u1 data);
54
55     int decodeLong(u4 & data);
56     int decodeLong(int & data);
57     int decodeLongLong(u8 & data);
58     int decodeShort(u2 & data);
59     int decodeString(u4 &len,char * &data);
60     int decodeByte(u1 &data);
61
62   private:
63     u1* start;
64     u1* end;
65     u1* current;
66     u4 size;
67     bool useMalloc;
68     bool owning;
69     bool autoincrease;
70
71     //check buffer space and enlarge if allowed
72     int checkSpace(int size);
73
74 };
75
76 class Serializable {
77   public:
78     Serializable();
79     virtual ~Serializable();
80     //serialize functions
81     //get the #of bytes needed to serialize
82     int getSerializedLen();
83     //serialize
84     //advance buffer, check if >= end
85     //return 0 if OK
86     int serialize(SerializeBuffer *b);
87     //deserialize
88     //advance buffer, check if >= end
89     //return 0 if OK
90     int deserialize(SerializeBuffer *b);
91     //or the received version after deserialize
92     //can be queried with deserializeImpl
93     u2 getVersion();
94     //helper
95     static int getSerializedStringLen(const char *str);
96   protected:
97     //methods to be overwritten by impl
98     //those methods can use the version info
99     //the length and version is automatically encoded and decoded by the base class
100     virtual int getSerializedLenImpl()=0;
101     virtual int serializeImpl(SerializeBuffer *b)=0;
102     virtual int deserializeImpl(SerializeBuffer *b)=0;
103     u2 version;
104  };
105
106 /**
107   * a class for creating a list of serializable parameters
108   * by including a version handling this automatically maintains compatibility
109   * by correct usage.
110   * usage example:
111   * 1. version
112   * u2 myP1=0;
113   * u4  myP2=0;
114   * SerializableList myList();
115   * myList.addParam(&myP1);
116   * myList.addParam(&myP2);
117   * //now serialize/deserialize...
118   * //later - second version:
119   * u2 myP1=0;
120   * u4  myP2=0;
121   * char *myString=NULL;
122   * SerializableList myList();
123   * myList.addParam(&myP1);
124   * myList.addParam(&myP2);
125   * myList.addParam(&myString,2); //this parameter is new in version 2
126   * myList.deserialize(buffer);
127   * if (!myList.isDeserialized(&myString)) {
128   *   //we got a version 1 message
129   *   myString=strcpy(new char[22],"default-for-myString");
130   * }
131   *
132   */
133 class SerializableList : public Serializable{
134   public:
135     SerializableList();
136     virtual ~SerializableList();
137     /**
138       * addParam
139       * add a parameter to the list
140       * no life cycle handling included
141       * params should be initialized before!
142       * when adding new params after having released a version
143       * add them to the end with a new version - this will automatically maintain
144       * compatibility
145       * will return !=0 if adding is not possible (e.g. adding with a smaller version)
146       */
147     int addParam(Serializable *p,u2 version=1);
148     int addParam(u2 *p,u2 version=1);
149     int addParam(u4 *p,u2 version=1);
150     int addParam(u8 *p,u2 version=1);
151     int addParam(char **p,u2 version=1);
152
153     /**
154       * for lists only intended for encoding also
155       * const members will be added
156       * this is fully compatible to non const addParams on the  other side
157       * so on the sender side you can use the addParam(const ... ) methods, on the receiver 
158       * the non-const
159       * After having called one of this methods deserialize will always fail!
160       */
161     int addParam(const Serializable *p,u2 vs=1){
162       encodeOnly=true;
163       return addParam(const_cast<Serializable*>(p),vs);
164     }
165     int addParam(const u2 *p,u2 vs=1){
166       encodeOnly=true;
167       return addParam(const_cast<u2*>(p),vs);
168     }
169     int addParam(const u4 *p,u2 vs=1){
170       encodeOnly=true;
171       return addParam(const_cast<u4*>(p),vs);
172     }
173     int addParam(const int *p,u2 vs=1){
174       encodeOnly=true;
175       return addParam(reinterpret_cast<u4*>(const_cast<int*>(p)), vs);
176     }
177     int addParam(const u8 *p,u2 vs=1){
178       encodeOnly=true;
179       return addParam(const_cast<u8*>(p),vs);
180     }
181     int addParam(const char **p,u2 vs=1){
182       encodeOnly=true;
183       return addParam(const_cast<char**>(p),vs);
184     }
185
186
187     /**
188       * check functions to test if a certain parameter has been filled
189       * during deserialize
190       */
191     bool isDeserialized(Serializable *p);
192     bool isDeserialized(u2 *p);
193     bool isDeserialized(u4 *p);
194     bool isDeserialized(u8 *p);
195     bool isDeserialized(char **p);
196
197     //return the highest version after adding params
198
199
200   protected:
201     virtual int getSerializedLenImpl();
202     virtual int serializeImpl(SerializeBuffer *b);
203     virtual int deserializeImpl(SerializeBuffer *b);
204     bool encodeOnly; //if any addParam(const ...) is called this will be set
205
206   private:
207     typedef enum{
208       TUNKNOWN,
209       TSER,
210       Tu2,
211       TULONG,
212       TULLONG,
213       TCHAR } Ptypes;
214     struct Pentry {
215       Ptypes ptype;
216       bool isDeserialized;
217       u2 version;
218       union {
219         Serializable *pser;
220         u2 *pshort;
221         u4 *plong;
222         u8 *pllong;
223         char **pchar;
224       } ptr;
225       Pentry() {
226         ptype=TUNKNOWN;
227         version=1;
228         isDeserialized=false;
229         ptr.pser=NULL;
230       }
231       bool isEqual(void *p,Ptypes t);
232     } ;
233     std::vector<struct Pentry>list;
234     Pentry *findEntry(void *p,Ptypes t);
235 };
236
237
238
239 #endif