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