#ifndef cmw_MarshallingFunctions_hh #define cmw_MarshallingFunctions_hh #include #include #include #include #include #include template void boolMarshal (B& buf, bool const bl) { unsigned char boolRep = bl; buf.Receive(&boolRep, sizeof(boolRep)); } template void boolReceive (B& buf, bool& bl) { unsigned char boolRep; buf.Give(&boolRep, sizeof(boolRep)); (boolRep != 0) ? bl = true : bl = false; } inline void CstringCount (Counter& cntr, char const* cstr) { marshalling_integer slen(::strlen(cstr)); slen.CalculateMarshallingSize(cntr); cntr.Add(slen.value); } template void CstringMarshal (B& buf, char const* cstr) { marshalling_integer slen(::strlen(cstr)); slen.Marshal(buf); buf.Receive(cstr, slen.value); } template void CstringReceive (B& buf, char*& cstr) { marshalling_integer slen(buf); cstr = ::new char[slen.value + 1]; try { buf.Give(cstr, slen.value); *(cstr + slen.value) = '\0'; } catch (...) { ::delete [] cstr; throw; } } template void stringCount (Counter& cntr, T const& str) { marshalling_integer slen(str.length()); slen.CalculateMarshallingSize(cntr); cntr.Add(slen.value); } template void stringMarshal (B& buf, T const& str) { marshalling_integer slen(str.length()); slen.Marshal(buf); buf.Receive(str.c_str(), slen.value); } template void stringReceive (B& buf, T& str) { marshalling_integer slen(buf); ::std::string loc(slen.value, '\0'); buf.Give(&loc[0], slen.value); loc.swap(str); } template void blockCount (Counter& cntr, T const& grp) { cntr.MultiplyAndAdd(grp.size(), sizeof(typename T::value_type)); } template void blockMarshal (B& buf, T const& grp) { uint32_t headCount = grp.size(); buf.Receive(&headCount, sizeof(headCount)); if (headCount > 0) 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(), 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(), grpEnd = grp.end(); for (; grpIt != grpEnd; ++grpIt) { (*grpIt)->CalculateMarshallingSize(cntr); } } template void groupMarshal (B& buf, bool sendType, T const& grp) { uint32_t headCount = grp.size(); buf.Receive(&headCount, sizeof(headCount)); typename T::const_iterator grpIt = grp.begin(), grpEnd = grp.end(); for (; grpIt != grpEnd; ++grpIt) { grpIt->Marshal(buf, sendType); } } template void groupPointerMarshal (B& buf, bool sendType, T const& grp) { uint32_t headCount = grp.size(); buf.Receive(&headCount, sizeof(headCount)); typename T::const_iterator grpIt = grp.begin(), grpEnd = grp.end(); for (; grpIt != grpEnd; ++grpIt) { (*grpIt)->Marshal(buf, sendType); } } template void segmented_arrayMarshal (B& buf, ::neolib::segmented_array & grp) { uint32_t headCount = grp.size(); buf.Receive(&headCount, sizeof(headCount)); if (headCount > 0) { typename ::neolib::segmented_array::segment& seg = ::neolib::segmented_iterator(grp.begin()).segment(); size_t index = seg.size(); buf.Receive(&grp[0], sizeof(T) * index); int32_t const chunks = (headCount - index) / chunk_size; for (int32_t ii = 0; ii < chunks; ++ii) { buf.Receive(&grp[index], sizeof(T) * chunk_size); index += chunk_size; } if (index < headCount) { buf.Receive(&grp[index], sizeof(T) * (headCount - index)); } } } template void i_listReceive (B& buf, T& intrlst) { uint32_t headCount = 0; buf.Give(headCount); for (; headCount > 0; --headCount) { intrlst.push_back(*T::value_type::BuildPolyInstance(buf)); } } template void rbtreeReceive (B& buf, T& rbt) { uint32_t headCount = 0; buf.Give(headCount); typename T::iterator endIt = rbt.end(); for (; headCount > 0; --headCount) { rbt.insert_unique(endIt , *T::value_type::BuildPolyInstance(buf) ); } } template void complexMarshal (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 complexReceive (B& buf, ::std::complex cmplx) { T value; buf.Give(value); cmplx.real(value); buf.Give(value); cmplx.imag(value); } template void arrayEasyMarshal (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 valarrayMarshal (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