added iOS source code
[wl-app.git] / iOS / Pods / Realm / Realm / RLMOptionalBase.mm
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 "RLMAccessor.h"
20 #import "RLMOptionalBase.h"
21 #import "RLMObject_Private.h"
22 #import "RLMObjectStore.h"
23 #import "RLMProperty_Private.h"
24 #import "RLMUtil.hpp"
25
26 #import <objc/runtime.h>
27
28 @interface RLMOptionalBase ()
29 @property (nonatomic) id unmanagedValue;
30 @end
31
32 @implementation RLMOptionalBase
33
34 - (instancetype)init {
35     return self;
36 }
37
38 - (id)underlyingValue {
39     if ((_object && _object->_realm) || _object.isInvalidated) {
40         return RLMDynamicGet(_object, _property);
41     }
42     else {
43         return _unmanagedValue;
44     }
45 }
46
47 - (void)setUnderlyingValue:(id)underlyingValue {
48     if ((_object && _object->_realm) || _object.isInvalidated) {
49         if (_property.isPrimary) {
50             @throw RLMException(@"Primary key can't be changed after an object is inserted.");
51         }
52         RLMDynamicSet(_object, _property, underlyingValue);
53     }
54     else {
55         NSString *propertyName = _property.name;
56         [_object willChangeValueForKey:propertyName];
57         _unmanagedValue = underlyingValue;
58         [_object didChangeValueForKey:propertyName];
59     }
60 }
61
62 - (BOOL)isKindOfClass:(Class)aClass {
63     return [self.underlyingValue isKindOfClass:aClass] || RLMIsKindOfClass(object_getClass(self), aClass);
64 }
65
66 - (NSMethodSignature *)methodSignatureForSelector:(SEL)sel {
67     return [self.underlyingValue methodSignatureForSelector:sel];
68 }
69
70 - (void)forwardInvocation:(NSInvocation *)invocation {
71     [invocation invokeWithTarget:self.underlyingValue];
72 }
73
74 - (id)forwardingTargetForSelector:(__unused SEL)sel {
75     return self.underlyingValue;
76 }
77
78 - (BOOL)respondsToSelector:(SEL)aSelector {
79     if (id val = self.underlyingValue) {
80         return [val respondsToSelector:aSelector];
81     }
82     return NO;
83 }
84
85 - (void)doesNotRecognizeSelector:(SEL)aSelector {
86     [self.underlyingValue doesNotRecognizeSelector:aSelector];
87 }
88
89 @end