OLD | NEW |
(Empty) | |
| 1 /* |
| 2 * |
| 3 * Copyright 2015, Google Inc. |
| 4 * All rights reserved. |
| 5 * |
| 6 * Redistribution and use in source and binary forms, with or without |
| 7 * modification, are permitted provided that the following conditions are |
| 8 * met: |
| 9 * |
| 10 * * Redistributions of source code must retain the above copyright |
| 11 * notice, this list of conditions and the following disclaimer. |
| 12 * * Redistributions in binary form must reproduce the above |
| 13 * copyright notice, this list of conditions and the following disclaimer |
| 14 * in the documentation and/or other materials provided with the |
| 15 * distribution. |
| 16 * * Neither the name of Google Inc. nor the names of its |
| 17 * contributors may be used to endorse or promote products derived from |
| 18 * this software without specific prior written permission. |
| 19 * |
| 20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 21 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 22 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 23 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 24 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 25 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 26 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 27 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 28 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 29 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 30 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 31 * |
| 32 */ |
| 33 |
| 34 #import "GRXForwardingWriter.h" |
| 35 |
| 36 @interface GRXForwardingWriter () <GRXWriteable> |
| 37 @end |
| 38 |
| 39 @implementation GRXForwardingWriter { |
| 40 GRXWriter *_writer; |
| 41 id<GRXWriteable> _writeable; |
| 42 } |
| 43 |
| 44 - (instancetype)init { |
| 45 return [self initWithWriter:nil]; |
| 46 } |
| 47 |
| 48 // Designated initializer |
| 49 - (instancetype)initWithWriter:(GRXWriter *)writer { |
| 50 if (!writer) { |
| 51 return nil; |
| 52 } |
| 53 if (writer.state != GRXWriterStateNotStarted) { |
| 54 [NSException raise:NSInvalidArgumentException |
| 55 format:@"The writer argument must not have already started."]; |
| 56 } |
| 57 if ((self = [super init])) { |
| 58 _writer = writer; |
| 59 } |
| 60 return self; |
| 61 } |
| 62 |
| 63 // This is used to send a completion or an error to the writeable. It nillifies |
| 64 // our reference to it in order to guarantee no more messages are sent to it, |
| 65 // and to release it. |
| 66 - (void)finishOutputWithError:(NSError *)errorOrNil { |
| 67 id<GRXWriteable> writeable = _writeable; |
| 68 _writeable = nil; |
| 69 [writeable writesFinishedWithError:errorOrNil]; |
| 70 } |
| 71 |
| 72 // This is used to stop the input writer. It nillifies our reference to it |
| 73 // to release it. |
| 74 - (void)finishInput { |
| 75 GRXWriter *writer = _writer; |
| 76 _writer = nil; |
| 77 writer.state = GRXWriterStateFinished; |
| 78 } |
| 79 |
| 80 #pragma mark GRXWriteable implementation |
| 81 |
| 82 - (void)writeValue:(id)value { |
| 83 [_writeable writeValue:value]; |
| 84 } |
| 85 |
| 86 - (void)writesFinishedWithError:(NSError *)errorOrNil { |
| 87 _writer = nil; |
| 88 [self finishOutputWithError:errorOrNil]; |
| 89 } |
| 90 |
| 91 #pragma mark GRXWriter implementation |
| 92 |
| 93 - (GRXWriterState)state { |
| 94 return _writer ? _writer.state : GRXWriterStateFinished; |
| 95 } |
| 96 |
| 97 - (void)setState:(GRXWriterState)state { |
| 98 if (state == GRXWriterStateFinished) { |
| 99 _writeable = nil; |
| 100 [self finishInput]; |
| 101 } else { |
| 102 _writer.state = state; |
| 103 } |
| 104 } |
| 105 |
| 106 - (void)startWithWriteable:(id<GRXWriteable>)writeable { |
| 107 _writeable = writeable; |
| 108 [_writer startWithWriteable:self]; |
| 109 } |
| 110 |
| 111 - (void)finishWithError:(NSError *)errorOrNil { |
| 112 [self finishOutputWithError:errorOrNil]; |
| 113 [self finishInput]; |
| 114 } |
| 115 |
| 116 @end |
OLD | NEW |