#ifndef cmw_ReceiveBuffer_hh #define cmw_ReceiveBuffer_hh #include #if CHAR_BIT != 8 #error Only 8 bit char supported #endif #include //#include #include #include #include #include #include template class ReceiveBuffer { protected: int32_t bufsize; int32_t index; int32_t packetLength; ::std::vector wrapper; unsigned char* buf; R reader; public: explicit ReceiveBuffer (int32_t sz) : bufsize(sz) , index(0) , packetLength(0) , wrapper(sz) { buf = &wrapper[0]; } void Give (void* address, int32_t len) { if (len > packetLength - index) { throw failure("ReceiveBuffer::Give -- len > bytes remaining"); } ::std::memcpy(address, buf + index, len); index += len; } unsigned char Give () { if (index >= packetLength) { throw failure("ReceiveBuffer::Give -- out of data"); } unsigned char byte = buf[index]; ++index; return byte; } void Give (uint8_t& val) { val = Give(); } void Give (uint16_t& val) { reader.Read(*this, val); } void Give (uint32_t& val) { reader.Read(*this, val); } void Give (uint64_t& val) { reader.Read(*this, val); } void Give (int8_t& value) { value = Give(); } void Give (int16_t& value) { uint16_t tmp; reader.Read(*this, tmp); value = tmp; } void Give (int32_t& value) { uint32_t tmp; reader.Read(*this, tmp); value = tmp; } void Give (int64_t& value) { uint64_t tmp; reader.Read(*this, tmp); value = tmp; } void Give (float& value) { reader.Read(*this, value); } void Give (double& value) { reader.Read(*this, value); } template void GiveBlock (T* data, unsigned int elements) { reader.ReadBlock(*this, data, elements); } void WriteFile (file_type fd) { uint32_t sz; Give(sz); while (sz > 0) { int rc = Write(fd, buf + index, sz); sz -= rc; index += rc; } } void Reset () { } private: ReceiveBuffer (ReceiveBuffer const&); ReceiveBuffer& operator= (ReceiveBuffer const&); }; #endif