RLMObjectId.mm 4.0 KB

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