From cc9af1e5891a331e55e5fc868657468dd7dccedc Mon Sep 17 00:00:00 2001 From: Ruud Overeem <overeem@astron.nl> Date: Thu, 1 Nov 2007 11:15:27 +0000 Subject: [PATCH] BugID: 1000 Added macros for packung and unpacking vectors of strings. --- .../include/APL/RTCCommon/Marshalling.h | 42 +++++++++++++++++++ MAC/APL/RTCCommon/test/tMarshalling.cc | 38 +++++++++++++++++ 2 files changed, 80 insertions(+) diff --git a/MAC/APL/RTCCommon/include/APL/RTCCommon/Marshalling.h b/MAC/APL/RTCCommon/include/APL/RTCCommon/Marshalling.h index 8050eb2501e..0ae525764fc 100644 --- a/MAC/APL/RTCCommon/include/APL/RTCCommon/Marshalling.h +++ b/MAC/APL/RTCCommon/include/APL/RTCCommon/Marshalling.h @@ -209,4 +209,46 @@ do { \ } \ } while (0) +// SIZE vector<string>> +#define MSH_SIZE_VECTOR_STRING(sizevar, thevector) \ +do { \ + sizevar = sizeof(int32); \ + vector<string>::iterator iter = thevector.begin(); \ + vector<string>::iterator end = thevector.end(); \ + while (iter != end) { \ + sizevar += MSH_STRING_SIZE(*iter); \ + iter++; \ + } \ +} while (0) + + +// PACK vector<string> +#define MSH_PACK_VECTOR_STRING(bufptr, offset, thevector) \ +do { \ + int32 nrElem = thevector.size(); \ + memcpy(((char*)(bufptr)) + (offset), &nrElem, sizeof(int32)); \ + offset += sizeof(int32); \ + \ + vector<string>::iterator iter = thevector.begin(); \ + vector<string>::iterator end = thevector.end(); \ + while (iter != end) { \ + MSH_PACK_STRING(bufptr, offset, *iter); \ + iter++; \ + } \ +} while (0) + +// UNPACK vector<string> +#define MSH_UNPACK_VECTOR_STRING(bufptr, offset, thevector) \ +do { \ + int32 nrElem = 0; \ + memcpy(&nrElem, ((char*)(bufptr)) + (offset), sizeof(nrElem)); \ + offset += sizeof(nrElem); \ + \ + for (int elem = 0; elem < nrElem; elem++) { \ + string elem1; \ + MSH_UNPACK_STRING(bufptr, offset, elem1); \ + thevector.push_back(elem1); \ + } \ +} while (0) + #endif /* MARSHALLING_H_ */ diff --git a/MAC/APL/RTCCommon/test/tMarshalling.cc b/MAC/APL/RTCCommon/test/tMarshalling.cc index ed84b1d5a79..ffd0b9eaf30 100644 --- a/MAC/APL/RTCCommon/test/tMarshalling.cc +++ b/MAC/APL/RTCCommon/test/tMarshalling.cc @@ -252,5 +252,43 @@ int main (int argc, char* argv[]) } + // vector<string> + vector<string> sv1; + sv1.push_back("piet hein"); + sv1.push_back("nelson"); + sv1.push_back("van Swieten"); + cout << "Testing vector<string>" << endl; + vector<string>::iterator itersv = sv1.begin(); + vector<string>::iterator endsv = sv1.end(); + int i = 0; + while (itersv != endsv) { + cout << "vector[" << i << "]:" << *itersv << endl; + i++; + itersv++; + } + + unsigned int svsize; + MSH_SIZE_VECTOR_STRING(svsize, sv1); + cout << "size = " << svsize << endl; + + bzero(buf, 4096); + offset = 0; + MSH_PACK_VECTOR_STRING(buf, offset, sv1); + cout << "packed:" << endl; + hexdump(buf, svsize); + + vector<string> sv2; + offset = 0; + MSH_UNPACK_VECTOR_STRING(buf, offset, sv2); + cout << "Unpacked vector<string>" << endl; + itersv = sv2.begin(); + endsv = sv2.end(); + i = 0; + while (itersv != endsv) { + cout << "vector[" << i << "]:" << *itersv << endl; + i++; + itersv++; + } + return (0); } -- GitLab