blob: 3fdfd41b070314f6e12aab14a5c3456f22728271 [file] [log] [blame]
Jens Geyer56e5b9b2015-10-09 22:01:55 +02001/*
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing,
13 * software distributed under the License is distributed on an
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 * KIND, either express or implied. See the License for the
16 * specific language governing permissions and limitations
17 * under the License.
18 */
19
20import Foundation
21
22
23public protocol TSerializable : Hashable {
24
25 static var thriftType : TType { get }
26
27 init()
28
29 static func readValueFromProtocol(proto: TProtocol) throws -> Self
30
31 static func writeValue(value: Self, toProtocol proto: TProtocol) throws
32
33}
34
35
36
37infix operator ?== {}
38
39public func ?==<T: TSerializable>(lhs: T?, rhs: T?) -> Bool {
40 if let l = lhs, r = rhs {
41 return l == r
42 }
43 return lhs == rhs
44}
45
46public func ?==<T: TSerializable>(lhs: T, rhs: T) -> Bool {
47 return lhs == rhs
48}
49
50
51
52extension Bool : TSerializable {
53
54 public static let thriftType = TType.BOOL
55
56 public static func readValueFromProtocol(proto: TProtocol) throws -> Bool {
57 var value : ObjCBool = false
58 try proto.readBool(&value)
59 return value.boolValue
60 }
61
62 public static func writeValue(value: Bool, toProtocol proto: TProtocol) throws {
63 try proto.writeBool(value)
64 }
65
66}
67
68extension Int8 : TSerializable {
69
70 public static let thriftType = TType.BYTE
71
72 public static func readValueFromProtocol(proto: TProtocol) throws -> Int8 {
73 var value = UInt8()
74 try proto.readByte(&value)
75 return Int8(value)
76 }
77
78 public static func writeValue(value: Int8, toProtocol proto: TProtocol) throws {
79 try proto.writeByte(UInt8(value))
80 }
81
82}
83
84extension Int16 : TSerializable {
85
86 public static let thriftType = TType.I16
87
88 public static func readValueFromProtocol(proto: TProtocol) throws -> Int16 {
89 var value = Int16()
90 try proto.readI16(&value)
91 return value
92 }
93
94 public static func writeValue(value: Int16, toProtocol proto: TProtocol) throws {
95 try proto.writeI16(value)
96 }
97
98}
99
100extension Int : TSerializable {
101
102 public static let thriftType = TType.I32
103
104 public static func readValueFromProtocol(proto: TProtocol) throws -> Int {
105 var value = Int32()
106 try proto.readI32(&value)
107 return Int(value)
108 }
109
110 public static func writeValue(value: Int, toProtocol proto: TProtocol) throws {
111 try proto.writeI32(Int32(value))
112 }
113
114}
115
116extension Int32 : TSerializable {
117
118 public static let thriftType = TType.I32
119
120 public static func readValueFromProtocol(proto: TProtocol) throws -> Int32 {
121 var value = Int32()
122 try proto.readI32(&value)
123 return value
124 }
125
126 public static func writeValue(value: Int32, toProtocol proto: TProtocol) throws {
127 try proto.writeI32(value)
128 }
129
130}
131
132extension Int64 : TSerializable {
133
134 public static let thriftType = TType.I64
135
136 public static func readValueFromProtocol(proto: TProtocol) throws -> Int64 {
137 var value = Int64()
138 try proto.readI64(&value)
139 return value
140 }
141
142 public static func writeValue(value: Int64, toProtocol proto: TProtocol) throws {
143 try proto.writeI64(value)
144 }
145
146}
147
148extension Double : TSerializable {
149
150 public static let thriftType = TType.DOUBLE
151
152 public static func readValueFromProtocol(proto: TProtocol) throws -> Double {
153 var value = Double()
154 try proto.readDouble(&value)
155 return value
156 }
157
158 public static func writeValue(value: Double, toProtocol proto: TProtocol) throws {
159 try proto.writeDouble(value)
160 }
161
162}
163
164extension String : TSerializable {
165
166 public static let thriftType = TType.STRING
167
168 public static func readValueFromProtocol(proto: TProtocol) throws -> String {
169 var value : NSString?
170 try proto.readString(&value)
171 return value as! String
172 }
173
174 public static func writeValue(value: String, toProtocol proto: TProtocol) throws {
175 try proto.writeString(value)
176 }
177
178}