RLMObjectId.mm 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. ////////////////////////////////////////////////////////////////////////////
  2. //
  3. // Copyright 2020 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 "RLMObjectId_Private.hpp"
  19. #import "RLMError_Private.hpp"
  20. #import "RLMUtil.hpp"
  21. #import <realm/object_id.hpp>
  22. // Swift's obj-c bridging does not support making an obj-c defined class conform
  23. // to Decodable, so we need a Swift-defined subclass for that. This means that
  24. // when Realm Swift is being used, we need to produce objects of that type rather
  25. // than our obj-c defined type. objc_runtime_visible marks the type as being
  26. // visbile only to the obj-c runtime and not the linker, which means that it'll
  27. // be `nil` at runtime rather than being a linker error if it's not defined, and
  28. // valid if it happens to be defined by some other library (i.e. Realm Swift).
  29. //
  30. // At the point where the objects are being allocated we generally don't have
  31. // any good way of knowing whether or not it's going to end up being used by
  32. // Swift, so we just switch to the subclass unconditionally if the subclass
  33. // exists. This shouldn't have any impact on obj-c code other than a small
  34. // performance hit.
  35. [[clang::objc_runtime_visible]]
  36. @interface RealmSwiftObjectId : RLMObjectId
  37. @end
  38. @implementation RLMObjectId
  39. - (instancetype)init {
  40. if ((self = [super init])) {
  41. if (auto cls = [RealmSwiftObjectId class]; cls && cls != self.class) {
  42. object_setClass(self, cls);
  43. }
  44. }
  45. return self;
  46. }
  47. - (instancetype)initWithString:(NSString *)string error:(NSError **)error {
  48. if ((self = [self init])) {
  49. const char *str = string.UTF8String;
  50. if (!realm::ObjectId::is_valid_str(str)) {
  51. if (error) {
  52. NSString *msg = [NSString stringWithFormat:@"Invalid Object ID string '%@': must be 24 hex digits", string];
  53. *error = [NSError errorWithDomain:RLMErrorDomain
  54. code:RLMErrorInvalidInput
  55. userInfo:@{NSLocalizedDescriptionKey: msg}];
  56. }
  57. return nil;
  58. }
  59. _value = realm::ObjectId(str);
  60. }
  61. return self;
  62. }
  63. - (instancetype)initWithTimestamp:(NSDate *)timestamp
  64. machineIdentifier:(int)machineIdentifier
  65. processIdentifier:(int)processIdentifier {
  66. if ((self = [self init])) {
  67. _value = realm::ObjectId(RLMTimestampForNSDate(timestamp), machineIdentifier, processIdentifier);
  68. }
  69. return self;
  70. }
  71. - (instancetype)initWithValue:(realm::ObjectId)value {
  72. if ((self = [self init])) {
  73. _value = value;
  74. }
  75. return self;
  76. }
  77. - (id)copyWithZone:(NSZone *)zone {
  78. // RLMObjectID is immutable so we don't have to actually copy
  79. return self;
  80. }
  81. + (instancetype)objectId {
  82. return [[RLMObjectId alloc] initWithValue:realm::ObjectId::gen()];
  83. }
  84. - (BOOL)isEqual:(id)object {
  85. if (RLMObjectId *objectId = RLMDynamicCast<RLMObjectId>(object)) {
  86. return objectId->_value == _value;
  87. }
  88. return NO;
  89. }
  90. - (BOOL)isGreaterThan:(nullable RLMObjectId *)objectId {
  91. return _value > objectId.value;
  92. }
  93. - (BOOL)isGreaterThanOrEqualTo:(nullable RLMObjectId *)objectId {
  94. return _value >= objectId.value;
  95. }
  96. - (BOOL)isLessThan:(nullable RLMObjectId *)objectId {
  97. return _value < objectId.value;
  98. }
  99. - (BOOL)isLessThanOrEqualTo:(nullable RLMObjectId *)objectId {
  100. return _value <= objectId.value;
  101. }
  102. - (NSUInteger)hash {
  103. return _value.hash();
  104. }
  105. - (NSString *)description {
  106. return self.stringValue;
  107. }
  108. - (NSString *)stringValue {
  109. return @(_value.to_string().c_str());
  110. }
  111. - (NSDate *)timestamp {
  112. return RLMTimestampToNSDate(_value.get_timestamp());
  113. }
  114. @end