added iOS source code
[wl-app.git] / iOS / Pods / Realm / Realm / RLMThreadSafeReference.mm
1 ////////////////////////////////////////////////////////////////////////////
2 //
3 // Copyright 2016 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 "RLMThreadSafeReference_Private.hpp"
20 #import "RLMUtil.hpp"
21
22 template<typename Function>
23 static auto translateErrors(Function&& f) {
24     try {
25         return f();
26     }
27     catch (std::exception const& e) {
28         @throw RLMException(e);
29     }
30 }
31
32 @implementation RLMThreadSafeReference {
33     std::unique_ptr<realm::ThreadSafeReferenceBase> _reference;
34     id _metadata;
35     Class _type;
36 }
37
38 - (instancetype)initWithThreadConfined:(id<RLMThreadConfined>)threadConfined {
39     if (!(self = [super init])) {
40         return nil;
41     }
42
43     REALM_ASSERT_DEBUG([threadConfined conformsToProtocol:@protocol(RLMThreadConfined)]);
44     if (![threadConfined conformsToProtocol:@protocol(RLMThreadConfined_Private)]) {
45         @throw RLMException(@"Illegal custom conformance to `RLMThreadConfined` by `%@`", threadConfined.class);
46     } else if (threadConfined.invalidated) {
47         @throw RLMException(@"Cannot construct reference to invalidated object");
48     } else if (!threadConfined.realm) {
49         @throw RLMException(@"Cannot construct reference to unmanaged object, "
50                             "which can be passed across threads directly");
51     }
52
53     translateErrors([&] {
54         _reference = [(id<RLMThreadConfined_Private>)threadConfined makeThreadSafeReference];
55         _metadata = ((id<RLMThreadConfined_Private>)threadConfined).objectiveCMetadata;
56     });
57     _type = threadConfined.class;
58
59     return self;
60 }
61
62 + (instancetype)referenceWithThreadConfined:(id<RLMThreadConfined>)threadConfined {
63     return [[self alloc] initWithThreadConfined:threadConfined];
64 }
65
66 - (id<RLMThreadConfined>)resolveReferenceInRealm:(RLMRealm *)realm {
67     if (!_reference) {
68         @throw RLMException(@"Can only resolve a thread safe reference once.");
69     }
70     return translateErrors([&] {
71         return [_type objectWithThreadSafeReference:std::move(_reference) metadata:_metadata realm:realm];
72     });
73 }
74
75 - (BOOL)isInvalidated {
76     return !_reference;
77 }
78
79 @end