Skip to content

Delegate for NSCountingStream looks suspicious #1

Closed
sorced-jim opened this Issue Mar 1, 2012 · 3 comments

2 participants

@sorced-jim

Line 34 of HSCountingInputStream.m has

    [parentStream setDelegate:self];

However, setDelegate on line 60 has:

  • (void)setDelegate:(id)aDelegate { if (aDelegate == nil) { delegate = self; } else { delegate = aDelegate; } }

This seems to imply that setDelegate should call [parentStream setDelegate:{aDelegate,self}] and delegate should return [parentStream delegate].

If that's not the intention, then why check for nil in setDelegate?

@bjhomer
Owner
bjhomer commented Mar 1, 2012

Note that according to the NSStream Class Reference, any NSStream subclass is responsible for implementing -delegate and -setDelegate:. NSStream doesn't provide the storage for us; we have to do it ourselves. And the documentation also indicates that a stream should be its own delegate if another one is not provided. That's all the implementation of -delegate and -setDelegate: are doing; we would have to do this whether or not a parent stream was present. See the implementation of NSRandomDataInputStream, where we do the same thing.

The HSCountingInputStream is intended to be a "filtering" stream of sorts, one that encapsulates a parent stream. If -setDelegate just passed things through to the parentStream, then a client could change the parentStream's delegate, and the counting stream would no longer be notified of any input. This would make it difficult for it to do any counting. It would break the encapsulation.

Instead, the counting stream wraps the initial input stream. Thus, the counting stream wants to know about events on the parent stream, and a client of HSCountingInputStream would want to know about events on the counting stream.

@bjhomer bjhomer closed this Mar 1, 2012
@sorced-jim
@bjhomer
Owner
bjhomer commented Mar 1, 2012

Hm. That's probably true. I'll make sure that doesn't break anything and add that in.

Thanks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.