RLMSwiftValueStorage.mm 5.3 KB

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