#ifndef MarshallingFunctions_hh #define MarshallingFunctions_hh #include #include #include template void Receive_Precursor(B* buf, uint32_t& message_length) { buf->SetMsgLength(sizeof(message_length)); buf->Give(message_length); buf->SetMsgLength(message_length); } template inline void boolSend(B* buf, bool const bl) { unsigned char boolRep = bl; buf->Receive(&boolRep, sizeof(boolRep)); } template inline void boolReceive(B* buf, bool& bl) { unsigned char boolRep; buf->Give(&boolRep, sizeof(boolRep)); (boolRep != 0) ? bl = true : bl = false; } template void stringGroupCount(Counter& cntr, T const& grp) { cntr.MultiplyAndAdd(grp.size(), sizeof(uint32_t) ); typename T::const_iterator It = grp.begin(); typename T::const_iterator End = grp.end(); for (; It != End; ++It) { cntr.Add((*It).length()); } } template void stringGroupPointerCount(Counter& cntr, T const& grp) { cntr.MultiplyAndAdd(grp.size(), sizeof(uint32_t) ); typename T::const_iterator It = grp.begin(); typename T::const_iterator End = grp.end(); for (; It != End; ++It) { cntr.Add((**It).length()); } } template void stringCount(Counter& cntr, T const& str) { cntr.Add(sizeof(uint32_t)); cntr.Add(str.length()); } template void stringSend(B* buf, T const& str) { uint32_t slen = str.length(); buf->Receive(&slen, sizeof(slen)); buf->Receive(str.c_str(), slen); } template void stringGroupSend(B* buf, T const& grp) { uint32_t headCount = grp.size(); buf->Receive(&headCount, sizeof(headCount)); typename T::const_iterator It = grp.begin(); typename T::const_iterator End = grp.end(); for (; It != End; ++It) { stringSend(buf, *It); } } template void stringGroupPointerSend(B* buf, T const& grp) { uint32_t headCount = grp.size(); buf->Receive(&headCount, sizeof(headCount)); typename T::const_iterator It = grp.begin(); typename T::const_iterator End = grp.end(); for (; It != End; ++It) { stringSend(buf, **It); } } template void stringReceive(B* buf, T& str) { uint32_t slen = 0; buf->Give(slen); str.resize(slen); buf->Give(&str[0], slen); } inline void CstringCount(Counter& cntr, char const* cstr) { cntr.Add(sizeof(uint32_t)); cntr.Add(strlen(cstr)); } template inline void CstringSend(B* buf, char const* cstr) { uint32_t slen = strlen(cstr); buf->Receive(&slen, sizeof(slen)); buf->Receive(cstr, slen); } template inline void CstringReceive(B* buf, char*& cstr) { uint32_t slen = 0; buf->Give(slen); cstr = new char[slen + 1]; buf->Give(cstr, slen); *(cstr + slen) = '\0'; } template void blockCount(Counter& cntr, T const& grp) { cntr.MultiplyAndAdd(grp.size(), sizeof(typename T::value_type)); } template void sendAsBlock(B* buf, T const& grp) { uint32_t headCount = grp.size(); buf->Receive(&headCount, sizeof(headCount)); buf->Receive(&(*grp.begin()), headCount * sizeof(typename T::value_type)); } template void groupCount(Counter& cntr, bool sendType, int constantsSize, T const& grp) { if (sendType) { cntr.MultiplyAndAdd(grp.size(), constantsSize); } typename T::const_iterator grpIt = grp.begin(); typename T::const_iterator grpEnd = grp.end(); for (; grpIt != grpEnd; ++grpIt) { (*grpIt).CalculateMarshallingSize(cntr); } } template void groupPointerCount(Counter& cntr, bool sendType, int constantsSize, T const& grp) { if (sendType) { cntr.MultiplyAndAdd(grp.size(), constantsSize); } typename T::const_iterator grpIt = grp.begin(); typename T::const_iterator grpEnd = grp.end(); for (; grpIt != grpEnd; ++grpIt) { (*grpIt)->CalculateMarshallingSize(cntr); } } template void groupSend(B* buf, bool sendType, T const& grp) { uint32_t headCount = grp.size(); buf->Receive(&headCount, sizeof(headCount)); typename T::const_iterator grpIt = grp.begin(); typename T::const_iterator grpEnd = grp.end(); for (; grpIt != grpEnd; ++grpIt) { (*grpIt).Send(buf, sendType); } } template void groupPointerSend(B* buf, bool sendType, T const& grp) { uint32_t headCount = grp.size(); buf->Receive(&headCount, sizeof(headCount)); typename T::const_iterator grpIt = grp.begin(); typename T::const_iterator grpEnd = grp.end(); for (; grpIt != grpEnd; ++grpIt) { (*grpIt)->Send(buf, sendType); } } template void i_listReceive(B* buf, T& intrlst) { uint32_t headCount = 0; buf->Give(headCount); for (; headCount > 0; --headCount) { typename T::value_type* rep = T::value_type::BuildPolyInstance(buf); intrlst.push_back(*rep); } } template void rbtreeReceive(B* buf, T& rbt) { uint32_t headCount = 0; buf->Give(headCount); typename T::iterator endIt = rbt.end(); for (; headCount > 0; --headCount) { typename T::value_type* rep = T::value_type::BuildPolyInstance(buf); rbt.insert_unique(endIt, *rep); } } template void complexSend(B* buf, std::complex const& cmplx) { T value = cmplx.real(); buf->Receive(&value, sizeof(T)); value = cmplx.imag(); buf->Receive(&value, sizeof(T)); } template void arrayEasySend(B* buf, T const& arr) { buf->Receive(&(*arr.begin()), arr.size() * sizeof(typename T::value_type)); } #ifdef VALARRAY_MARSHALLING #include template void valarrayCount(Counter& cntr, std::valarray const& valarr) { cntr.Add(sizeof(uint32_t)); cntr.MultiplyAndAdd(valarr.size(), sizeof(T)); } template void valarraySend(B* buf, std::valarray const& valarr) { uint32_t headCount = valarr.size(); buf->Receive(&headCount, sizeof(headCount)); buf->Receive(&valarr[0], headCount * sizeof(T) ); } template void valarrayReceive(B* buf, std::valarray& valarr) { uint32_t headCount = 0; buf->Give(&headCount, sizeof(headCount)); valarr.resize(headCount); buf->Give(&valarr, headCount * sizeof(T) ); } #endif // VALARRAY_MARSHALLING #endif