Re: SourceComment: Removing code duplication in BinaryWriter

Dear Atul,

I looked into your patch and I see two issues with using the template function.

The major one is that the template function would allow one to write something like:

Ping packet;
Writer.write (ping);

which is obviously incorrect, as the memory image (actually only the pointer to the private data) of the packet will be written and not it’s content. Furthermore, we don’t want to allow a packet to be written like this. I guess this problem could be circumvented by declaring the template function private and declaring instantiations in the public section of the class but I’m not 100% sure that will work.

The second issue is that when one sees code like:

writer.write (payloadLength);

one does not know how many bytes are actually written. So if someone is inspecting the function writeHeader() it will not be obvious if the binary format is correct. One of the philosophies of the Python community ( http://en.wikipedia.org/wiki/Python_philosophy is “explicit is better than implicit” and I think that is especially true in our specific case.

IMHO for BinaryWriter it would be more suitable to apply the same kind of refactoring that you already performed in BinaryReader.

Best regards,

Peter

atul wrote:Dear all

BinaryWriter has a number of methods that differ only in type.

writeUInt32, writeInt32, writeUInt16 etc..

One C++ idiom is to let the compiler write these versions with template member functions….

Here is the patch that does this and as a result a lot of repetitive functions get eliminated…

The advantage is if we change some class member from char to int, this change is automatically propogated…. So we do not have to go manually and change things..

Let me know….

— cheers atul

Would you like to post a relpy?


This post is a reply to:
SourceComment: Removing code duplication in BinaryWriter
Dear all BinaryWriter has a number of methods that differ only in type. writeUInt32, writeInt32, writeUInt16 etc.. One C++ idiom is to let the compiler write these versions with template member functions....           Here (more...)

Follow-ups:
Re: Re: SourceComment: Removing code duplication in BinaryWriter
Dear Peter In bold ;-) Peter Dimov wrote: Dear Atul, I looked into your patch and I see two issues with using the template function. The major one is that the template function would allow (more...)