RLMOptionalBase.mm 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  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. #import "RLMOptionalBase.h"
  19. #import "RLMAccessor.hpp"
  20. #import "RLMObject_Private.hpp"
  21. #import "RLMProperty.h"
  22. #import "RLMUtil.hpp"
  23. #import "object.hpp"
  24. namespace {
  25. struct OptionalBase {
  26. virtual id get() = 0;
  27. virtual void set(id) = 0;
  28. virtual ~OptionalBase() = default;
  29. };
  30. class UnmanagedOptional : public OptionalBase {
  31. public:
  32. id get() override {
  33. return _value;
  34. }
  35. void set(__unsafe_unretained const id newValue) override {
  36. @autoreleasepool {
  37. RLMObjectBase *object = _parent;
  38. [object willChangeValueForKey:_property];
  39. _value = newValue;
  40. [object didChangeValueForKey:_property];
  41. }
  42. }
  43. void attach(__unsafe_unretained RLMObjectBase *const obj, NSString *property) {
  44. if (!_property) {
  45. _property = property;
  46. _parent = obj;
  47. }
  48. }
  49. private:
  50. id _value;
  51. NSString *_property;
  52. __weak RLMObjectBase *_parent;
  53. };
  54. class ManagedOptional : public OptionalBase {
  55. public:
  56. ManagedOptional(RLMObjectBase *obj, RLMProperty *prop)
  57. : _realm(obj->_realm)
  58. , _object(obj->_realm->_realm, *obj->_info->objectSchema, obj->_row)
  59. , _propertyName(prop.name.UTF8String)
  60. , _ctx(obj->_realm, *obj->_info)
  61. {
  62. }
  63. id get() override {
  64. return _object.get_property_value<id>(_ctx, _propertyName);
  65. }
  66. void set(__unsafe_unretained id const value) override {
  67. _object.set_property_value(_ctx, _propertyName, value ?: NSNull.null, false);
  68. }
  69. private:
  70. // We have to hold onto a strong reference to the Realm as
  71. // RLMAccessorContext holds a non-retaining one.
  72. __unused RLMRealm *_realm;
  73. realm::Object _object;
  74. std::string _propertyName;
  75. RLMAccessorContext _ctx;
  76. };
  77. } // anonymous namespace
  78. @interface RLMOptionalBase () {
  79. std::unique_ptr<OptionalBase> _impl;
  80. }
  81. @end
  82. @implementation RLMOptionalBase
  83. - (instancetype)init {
  84. return self;
  85. }
  86. - (BOOL)isKindOfClass:(Class)aClass {
  87. return [RLMGetOptional(self) isKindOfClass:aClass] || RLMIsKindOfClass(object_getClass(self), aClass);
  88. }
  89. - (NSMethodSignature *)methodSignatureForSelector:(SEL)sel {
  90. return [RLMGetOptional(self) methodSignatureForSelector:sel];
  91. }
  92. - (void)forwardInvocation:(NSInvocation *)invocation {
  93. [invocation invokeWithTarget:RLMGetOptional(self)];
  94. }
  95. - (id)forwardingTargetForSelector:(__unused SEL)sel {
  96. return RLMGetOptional(self);
  97. }
  98. - (BOOL)respondsToSelector:(SEL)aSelector {
  99. return [RLMGetOptional(self) respondsToSelector:aSelector];
  100. }
  101. - (void)doesNotRecognizeSelector:(SEL)aSelector {
  102. [RLMGetOptional(self) doesNotRecognizeSelector:aSelector];
  103. }
  104. id RLMGetOptional(__unsafe_unretained RLMOptionalBase *const self) {
  105. try {
  106. return self->_impl ? RLMCoerceToNil(self->_impl->get()) : nil;
  107. }
  108. catch (std::exception const& err) {
  109. @throw RLMException(err);
  110. }
  111. }
  112. void RLMSetOptional(__unsafe_unretained RLMOptionalBase *const self, __unsafe_unretained const id value) {
  113. try {
  114. if (!self->_impl && value) {
  115. self->_impl.reset(new UnmanagedOptional);
  116. }
  117. if (self->_impl) {
  118. self->_impl->set(value);
  119. }
  120. }
  121. catch (std::exception const& err) {
  122. @throw RLMException(err);
  123. }
  124. }
  125. void RLMInitializeManagedOptional(__unsafe_unretained RLMOptionalBase *const self,
  126. __unsafe_unretained RLMObjectBase *const parent,
  127. __unsafe_unretained RLMProperty *const prop) {
  128. REALM_ASSERT(parent->_realm);
  129. self->_impl.reset(new ManagedOptional(parent, prop));
  130. }
  131. void RLMInitializeUnmanagedOptional(__unsafe_unretained RLMOptionalBase *const self,
  132. __unsafe_unretained RLMObjectBase *const parent,
  133. __unsafe_unretained RLMProperty *const prop) {
  134. if (!self->_impl) {
  135. self->_impl.reset(new UnmanagedOptional);
  136. }
  137. static_cast<UnmanagedOptional&>(*self->_impl).attach(parent, prop.name);
  138. }
  139. @end