diff --git a/MAC/APL/RTCCommon/include/APL/RTCCommon/Marshalling.h b/MAC/APL/RTCCommon/include/APL/RTCCommon/Marshalling.h index 8050eb2501eec09df9d68a05d1fef34b9f92c8b5..0ae525764fc75857c04495f449267a8a48ce43f8 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 ed84b1d5a79adf4efc843f3b246e055b9d77a1e7..ffd0b9eaf3070e6b5174cab1298560f2a31ba2e6 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); }