added iOS source code
[wl-app.git] / iOS / Pods / RealmSwift / RealmSwift / Util.swift
1 ////////////////////////////////////////////////////////////////////////////
2 //
3 // Copyright 2015 Realm Inc.
4 //
5 // Licensed under the Apache License, Version 2.0 (the "License");
6 // you may not use this file except in compliance with the License.
7 // You may obtain a copy of the License at
8 //
9 // http://www.apache.org/licenses/LICENSE-2.0
10 //
11 // Unless required by applicable law or agreed to in writing, software
12 // distributed under the License is distributed on an "AS IS" BASIS,
13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 // See the License for the specific language governing permissions and
15 // limitations under the License.
16 //
17 ////////////////////////////////////////////////////////////////////////////
18
19 import Foundation
20 import Realm
21
22 #if BUILDING_REALM_SWIFT_TESTS
23 import RealmSwift
24 #endif
25
26 // MARK: Internal Helpers
27
28 // Swift 3.1 provides fixits for some of our uses of unsafeBitCast
29 // to use unsafeDowncast instead, but the bitcast is required.
30 internal func noWarnUnsafeBitCast<T, U>(_ x: T, to type: U.Type) -> U {
31     return unsafeBitCast(x, to: type)
32 }
33
34 /// Given a list of `Any`-typed varargs, unwrap any optionals and
35 /// replace them with the underlying value or NSNull.
36 internal func unwrapOptionals(in varargs: [Any]) -> [Any] {
37     return varargs.map { arg in
38 #if swift(>=3.1)
39         if let someArg = arg as Any? {
40             return someArg
41         }
42         return NSNull()
43 #else
44         if let optionalArg = arg as? RealmAnyOptionalUnboxingWorkaround {
45             return optionalArg.rlm_unwrappedValue()
46         } else {
47             return arg
48         }
49 #endif
50     }
51 }
52
53 // FIXME: Kill this when we drop Xcode 8.0 support.
54 #if swift(>=3.1)
55 #else
56 private protocol RealmAnyOptionalUnboxingWorkaround {
57     func rlm_unwrappedValue() -> Any
58 }
59
60 extension Optional: RealmAnyOptionalUnboxingWorkaround {
61     func rlm_unwrappedValue() -> Any {
62         switch self {
63         case .none: return NSNull()
64         case let .some(underlying): return underlying
65         }
66     }
67 }
68 #endif
69
70 internal func notFoundToNil(index: UInt) -> Int? {
71     if index == UInt(NSNotFound) {
72         return nil
73     }
74     return Int(index)
75 }
76
77 internal func throwRealmException(_ message: String, userInfo: [AnyHashable: Any]? = nil) {
78     NSException(name: NSExceptionName(rawValue: RLMExceptionName), reason: message, userInfo: userInfo).raise()
79 }
80
81 internal func throwForNegativeIndex(_ int: Int, parameterName: String = "index") {
82     if int < 0 {
83         throwRealmException("Cannot pass a negative value for '\(parameterName)'.")
84     }
85 }
86
87 internal func gsub(pattern: String, template: String, string: String, error: NSErrorPointer = nil) -> String? {
88     let regex = try? NSRegularExpression(pattern: pattern, options: [])
89     return regex?.stringByReplacingMatches(in: string, options: [],
90                                            range: NSRange(location: 0, length: string.utf16.count),
91                                            withTemplate: template)
92 }
93
94 internal func cast<U, V>(_ value: U, to: V.Type) -> V {
95     if let v = value as? V {
96         return v
97     }
98     return unsafeBitCast(value, to: to)
99 }
100
101 extension Object {
102     // Must *only* be used to call Realm Objective-C APIs that are exposed on `RLMObject`
103     // but actually operate on `RLMObjectBase`. Do not expose cast value to user.
104     internal func unsafeCastToRLMObject() -> RLMObject {
105         return unsafeBitCast(self, to: RLMObject.self)
106     }
107 }
108
109 // MARK: CustomObjectiveCBridgeable
110
111 internal func dynamicBridgeCast<T>(fromObjectiveC x: Any) -> T {
112     if T.self == DynamicObject.self {
113         return unsafeBitCast(x as AnyObject, to: T.self)
114     } else if let BridgeableType = T.self as? CustomObjectiveCBridgeable.Type {
115         return BridgeableType.bridging(objCValue: x) as! T
116     } else {
117         return x as! T
118     }
119 }
120
121 internal func dynamicBridgeCast<T>(fromSwift x: T) -> Any {
122     if let x = x as? CustomObjectiveCBridgeable {
123         return x.objCValue
124     } else {
125         return x
126     }
127 }
128
129 // Used for conversion from Objective-C types to Swift types
130 internal protocol CustomObjectiveCBridgeable {
131     static func bridging(objCValue: Any) -> Self
132     var objCValue: Any { get }
133 }
134
135 // FIXME: needed with swift 3.2
136 // Double isn't though?
137 extension Float: CustomObjectiveCBridgeable {
138     static func bridging(objCValue: Any) -> Float {
139         return (objCValue as! NSNumber).floatValue
140     }
141     var objCValue: Any {
142         return NSNumber(value: self)
143     }
144 }
145
146 extension Int8: CustomObjectiveCBridgeable {
147     static func bridging(objCValue: Any) -> Int8 {
148         return (objCValue as! NSNumber).int8Value
149     }
150     var objCValue: Any {
151         return NSNumber(value: self)
152     }
153 }
154 extension Int16: CustomObjectiveCBridgeable {
155     static func bridging(objCValue: Any) -> Int16 {
156         return (objCValue as! NSNumber).int16Value
157     }
158     var objCValue: Any {
159         return NSNumber(value: self)
160     }
161 }
162 extension Int32: CustomObjectiveCBridgeable {
163     static func bridging(objCValue: Any) -> Int32 {
164         return (objCValue as! NSNumber).int32Value
165     }
166     var objCValue: Any {
167         return NSNumber(value: self)
168     }
169 }
170 extension Int64: CustomObjectiveCBridgeable {
171     static func bridging(objCValue: Any) -> Int64 {
172         return (objCValue as! NSNumber).int64Value
173     }
174     var objCValue: Any {
175         return NSNumber(value: self)
176     }
177 }
178 extension Optional: CustomObjectiveCBridgeable {
179     static func bridging(objCValue: Any) -> Optional {
180         if objCValue is NSNull {
181             return nil
182         } else {
183             return .some(dynamicBridgeCast(fromObjectiveC: objCValue))
184         }
185     }
186     var objCValue: Any {
187         if let value = self {
188             return dynamicBridgeCast(fromSwift: value)
189         } else {
190             return NSNull()
191         }
192     }
193 }
194
195 // MARK: AssistedObjectiveCBridgeable
196
197 internal protocol AssistedObjectiveCBridgeable {
198     static func bridging(from objectiveCValue: Any, with metadata: Any?) -> Self
199     var bridged: (objectiveCValue: Any, metadata: Any?) { get }
200 }