1 ////////////////////////////////////////////////////////////////////////////
3 // Copyright 2016 Realm Inc.
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
9 // http://www.apache.org/licenses/LICENSE-2.0
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.
17 ////////////////////////////////////////////////////////////////////////////
19 #import "RLMThreadSafeReference_Private.hpp"
22 template<typename Function>
23 static auto translateErrors(Function&& f) {
27 catch (std::exception const& e) {
28 @throw RLMException(e);
32 @implementation RLMThreadSafeReference {
33 std::unique_ptr<realm::ThreadSafeReferenceBase> _reference;
38 - (instancetype)initWithThreadConfined:(id<RLMThreadConfined>)threadConfined {
39 if (!(self = [super init])) {
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");
54 _reference = [(id<RLMThreadConfined_Private>)threadConfined makeThreadSafeReference];
55 _metadata = ((id<RLMThreadConfined_Private>)threadConfined).objectiveCMetadata;
57 _type = threadConfined.class;
62 + (instancetype)referenceWithThreadConfined:(id<RLMThreadConfined>)threadConfined {
63 return [[self alloc] initWithThreadConfined:threadConfined];
66 - (id<RLMThreadConfined>)resolveReferenceInRealm:(RLMRealm *)realm {
68 @throw RLMException(@"Can only resolve a thread safe reference once.");
70 return translateErrors([&] {
71 return [_type objectWithThreadSafeReference:std::move(_reference) metadata:_metadata realm:realm];
75 - (BOOL)isInvalidated {