2 Copyright 2004-2005 Chris Tallon, Andreas Vogel
4 This file is part of VOMP.
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.
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.
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.
30 class SerializeBuffer {
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);
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
43 //seek relative to current
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);
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);
71 //check buffer space and enlarge if allowed
72 int checkSpace(int size);
79 virtual ~Serializable();
81 //get the #of bytes needed to serialize
82 int getSerializedLen();
84 //advance buffer, check if >= end
86 int serialize(SerializeBuffer *b);
88 //advance buffer, check if >= end
90 int deserialize(SerializeBuffer *b);
91 //or the received version after deserialize
92 //can be queried with deserializeImpl
95 static int getSerializedStringLen(const char *str);
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;
107 * a class for creating a list of serializable parameters
108 * by including a version handling this automatically maintains compatibility
114 * SerializableList myList();
115 * myList.addParam(&myP1);
116 * myList.addParam(&myP2);
117 * //now serialize/deserialize...
118 * //later - second version:
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");
133 class SerializableList : public Serializable{
136 virtual ~SerializableList();
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
145 * will return !=0 if adding is not possible (e.g. adding with a smaller version)
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);
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
159 * After having called one of this methods deserialize will always fail!
161 int addParam(const Serializable *p,u2 vs=1){
163 return addParam(const_cast<Serializable*>(p),vs);
165 int addParam(const u2 *p,u2 vs=1){
167 return addParam(const_cast<u2*>(p),vs);
169 int addParam(const u4 *p,u2 vs=1){
171 return addParam(const_cast<u4*>(p),vs);
173 int addParam(const int *p,u2 vs=1){
175 return addParam(reinterpret_cast<u4*>(const_cast<int*>(p)), vs);
177 int addParam(const u8 *p,u2 vs=1){
179 return addParam(const_cast<u8*>(p),vs);
181 int addParam(const char **p,u2 vs=1){
183 return addParam(const_cast<char**>(p),vs);
188 * check functions to test if a certain parameter has been filled
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);
197 //return the highest version after adding params
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
228 isDeserialized=false;
231 bool isEqual(void *p,Ptypes t);
233 std::vector<struct Pentry>list;
234 Pentry *findEntry(void *p,Ptypes t);