1+ /*
2+ * Copyright 2016 Robert Cottrell
3+ *
4+ * Licensed under the Apache License, Version 2.0 (the "License");
5+ * you may not use this file except in compliance with the License.
6+ * You may obtain a copy of the License at
7+ *
8+ * http://www.apache.org/licenses/LICENSE-2.0
9+ *
10+ * Unless required by applicable law or agreed to in writing, software
11+ * distributed under the License is distributed on an "AS IS" BASIS,
12+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+ * See the License for the specific language governing permissions and
14+ * limitations under the License.
15+ */
16+
17+ import Foundation
18+
19+ /// A type-erased `Processor` type.
20+ ///
21+ /// Forwards operations to an arbitrary underlying processor with the same
22+ /// `SubscribeTrype` and `PublishType` types, hiding the specifics of the
23+ /// underlying processor.
24+ public struct AnyProcessor < Subscribe, Publish> : Processor {
25+ /// The type of elements to be received.
26+ public typealias SubscribeType = Subscribe
27+
28+ /// The type of elements to be published.
29+ public typealias PublishType = Publish
30+
31+ /// The boxed processor which will receive forwarded calls.
32+ private let box : _AnyProcessorBoxBase < SubscribeType , PublishType >
33+
34+ /// Create a type erased wrapper around a processor.
35+ ///
36+ /// - parameter box: The processor to receive operations.
37+ public init < P: Processor where P. SubscribeType == SubscribeType , P. PublishType == PublishType > ( _ box: P ) {
38+ self . box = _AnyProcessorBox ( box)
39+ }
40+
41+ /// Forward `onSubscribe(subscription:)` to the boxed processor.
42+ public func onSubscribe( subscription: Subscription ) {
43+ box. onSubscribe ( subscription: subscription)
44+ }
45+
46+ /// Forward `onNext` to the boxed processor.
47+ public func onNext( element: SubscribeType ) {
48+ box. onNext ( element: element)
49+ }
50+
51+ /// Forward `onError(error:)` to the boxed processor.
52+ public func onError( error: ErrorProtocol ) {
53+ box. onError ( error: error)
54+ }
55+
56+ /// Forward `onComplete()` to the boxed processor.
57+ public func onComplete( ) {
58+ box. onComplete ( )
59+ }
60+
61+ /// Forward `subscribe(subscriber:)` to the boxed processor.
62+ public func subscribe< S: Subscriber where S. SubscribeType == PublishType > ( subscriber: S ) {
63+ box. subscribe ( subscriber: subscriber)
64+ }
65+
66+ /// Erases type of the processor and returns the canonical processor.
67+ ///
68+ /// - returns: type erased processor.
69+ public func asProcessor( ) -> AnyProcessor < SubscribeType , PublishType > {
70+ return self
71+ }
72+ }
73+
74+ public extension Processor {
75+ /// Erases type of the processor and returns the canonical processor.
76+ ///
77+ /// - returns: type erased processor.
78+ public func asProcessor( ) -> AnyProcessor < SubscribeType , PublishType > {
79+ return AnyProcessor ( self )
80+ }
81+ }
82+
83+ private class _AnyProcessorBox < P: Processor > : _AnyProcessorBoxBase < P . SubscribeType , P . PublishType > {
84+ let box : P
85+
86+ init ( _ box: P ) {
87+ self . box = box
88+ }
89+
90+ override func onSubscribe( subscription: Subscription ) {
91+ box. onSubscribe ( subscription: subscription)
92+ }
93+
94+ override func onNext( element: SubscribeType ) {
95+ box. onNext ( element: element)
96+ }
97+
98+ override func onError( error: ErrorProtocol ) {
99+ box. onError ( error: error)
100+ }
101+
102+ override func onComplete( ) {
103+ box. onComplete ( )
104+ }
105+
106+ override func subscribe< S: Subscriber where S. SubscribeType == PublishType > ( subscriber: S ) {
107+ box. subscribe ( subscriber: subscriber)
108+ }
109+ }
110+
111+ private class _AnyProcessorBoxBase < Subscribe, Publish> : Processor {
112+ typealias SubscribeType = Subscribe
113+ typealias PublishType = Publish
114+
115+ func onSubscribe( subscription: Subscription ) {
116+ fatalError ( )
117+ }
118+
119+ func onNext( element: SubscribeType ) {
120+ fatalError ( )
121+ }
122+
123+ func onError( error: ErrorProtocol ) {
124+ fatalError ( )
125+ }
126+
127+ func onComplete( ) {
128+ fatalError ( )
129+ }
130+
131+ func subscribe< S: Subscriber where S. SubscribeType == PublishType > ( subscriber: S ) {
132+ fatalError ( )
133+ }
134+ }
0 commit comments