[Cppunit-cvs] cppunit2/include/opentest serializer.h,1.2,1.3
Brought to you by:
blep
From: Baptiste L. <bl...@us...> - 2005-06-26 19:41:24
|
Update of /cvsroot/cppunit/cppunit2/include/opentest In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv25409/include/opentest Modified Files: serializer.h Log Message: Extracted packet management to handle the dual packet cursor cleanly (cursor for serialization and transport). Index: serializer.h =================================================================== RCS file: /cvsroot/cppunit/cppunit2/include/opentest/serializer.h,v retrieving revision 1.2 retrieving revision 1.3 diff -C2 -d -r1.2 -r1.3 *** serializer.h 24 Jun 2005 19:41:50 -0000 1.2 --- serializer.h 26 Jun 2005 19:41:15 -0000 1.3 *************** *** 19,22 **** --- 19,169 ---- + class Packets + { + public: + typedef unsigned char Byte; + typedef unsigned int Pos; + + Packets(); + virtual ~Packets(); + + // for transport communication + void beginMessage(); + void received( void *data, Pos length ); + // Returns the length actual written at dest + unsigned int send( void *dest, Pos availableLength ); + void endMessage(); + + // for serialization + unsigned char serializationReadNextByte(); + void serializationUngetLastByte(); + bool serializationRead( void *buffer, Pos length ); + void serializationWrite( const void *buffer, Pos length ); + void serializationWrite( unsigned char byte ); + + private: + // Binary packet with intrusive double linked list. + struct Packet + { + Packet( Pos size, Packet *previous ) + : begin_( new Byte[size] ) + , endInitialized_( begin_ ) + , end_( begin_ + size ) + , previous_( previous ) + , next_( 0 ) + { + next_ = previous_->next_; + previous_->next_ = this; + } + + ~Packet() + { + if ( previous_ ) + previous_->next_ = next_; + if ( next_ ) + next_->previous_ = previous_; + delete[] begin_; + } + + Pos length() const + { + return end_ - begin_; + } + + Byte *offset( Pos offsetInByte ) + { + return begin_ + offsetInByte; + } + + Packet *previous_; + Packet *next_; + Byte *begin_; + Byte *end_; + Byte *endInitialized_; + }; + + struct PacketPos + { + PacketPos( Packet *packet = 0, Pos offsetPos = 0 ) + : packet_( packet ) + , currentData_( packet ? packet->offset(offsetPos) : 0 ) + { + } + + operator bool() const + { + return packet_ != 0; + } + + bool operator !() const + { + return packet_ == 0; + } + + Pos offset() const + { + CPPTL_ASSERT_MESSAGE( packet_ != 0, "No packet to get the offset of." ); + return currentData_ - packet_->begin_; + } + + Pos availableForWriting() const + { + CPPTL_ASSERT_MESSAGE( packet_ != 0, "No packet available" ); + return packet_->end_ - currentData_; + } + + Pos availableForReading() const + { + CPPTL_ASSERT_MESSAGE( packet_ != 0, "No packet available" ); + return packet_->endInitialized_ - packet_->begin_; + } + + void seekToNextPacketIfAtEnd() + { + if ( packet_ && currentData_ == packet_->end_ && packet_->next_ ) + seekToNextPacket(); + } + + void seekBeforeEndOfPreviousPacket() + { + CPPTL_ASSERT_MESSAGE( packet_ && packet_->previous_, "No previous packet." ); + packet_ = packet_->previous_; + currentData_ = packet_->end_ - 1; + } + + void seekToNextPacket() + { + packet_ = packet_->next_; + currentData_ = packet_->begin_; + } + + Packet *packet_; + Byte *currentData_; + }; + + // Describes data for a RemoteMessage + struct MessagePacket + { + PacketPos position_; // Position where the RemoteMessage can be found + Pos length_; // Length of the RemoteMessage in bytes. + }; + + void appendPacketIfFull( PacketPos &pos ); + Pos distance( const PacketPos &begin, + const PacketPos &end ) const; + void write( PacketPos &pos, const Byte *data, Pos length ); + /// Returns the length actually read + Pos read( PacketPos &pos, Byte *data, Pos length ); + + private: + std::deque<MessagePacket> messages_; + Packet *packetsHead_; + PacketPos serializePos_; // Position from where data are read or write for serialization + PacketPos externalPos_; // Position where data are read/write on transport medium + Pos defaultPacketSize_; + }; + + + class Stream { *************** *** 25,32 **** virtual ~Stream(); ! // Call this to add buffer to be "read". Reset read cursor on the first buffer. ! void addBuffer( unsigned int length, const void *data ); ! ! void startsWriteNewMessage(); Stream &operator <<( bool value ); --- 172,176 ---- virtual ~Stream(); ! Packets &packets(); Stream &operator <<( bool value ); *************** *** 80,89 **** }; ! typedef std::vector<BufferData> Buffers; ! Buffers buffers_; ! Buffers::iterator current_; ! unsigned char *currentData_; ! unsigned char *endData_; ! unsigned int writeBufferSize_; typedef std::map<String,unsigned int> IndexesByString; typedef std::deque<String> StringsByIndex; --- 224,228 ---- }; ! Packets packets_; typedef std::map<String,unsigned int> IndexesByString; typedef std::deque<String> StringsByIndex; |