FIRDocumentChange.mm 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. /*
  2. * Copyright 2017 Google
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #import "Firestore/Source/API/FIRDocumentChange+Internal.h"
  17. #import "Firestore/Source/API/FIRDocumentSnapshot+Internal.h"
  18. #include "Firestore/core/src/api/document_change.h"
  19. #include "Firestore/core/src/util/hard_assert.h"
  20. using firebase::firestore::api::DocumentChange;
  21. NS_ASSUME_NONNULL_BEGIN
  22. namespace {
  23. /**
  24. * Converts from C++ document change indexes to Objective-C document change
  25. * indexes. Objective-C's NSNotFound is signed NSIntegerMax, not unsigned -1.
  26. */
  27. constexpr NSUInteger MakeIndex(size_t index) {
  28. return index == DocumentChange::npos ? NSNotFound : index;
  29. }
  30. } // namespace
  31. @implementation FIRDocumentChange {
  32. DocumentChange _documentChange;
  33. }
  34. - (instancetype)initWithDocumentChange:(DocumentChange &&)documentChange {
  35. if (self = [super init]) {
  36. _documentChange = std::move(documentChange);
  37. }
  38. return self;
  39. }
  40. - (BOOL)isEqual:(nullable id)other {
  41. if (other == self) return YES;
  42. if (![other isKindOfClass:[FIRDocumentChange class]]) return NO;
  43. FIRDocumentChange *change = (FIRDocumentChange *)other;
  44. return _documentChange == change->_documentChange;
  45. }
  46. - (NSUInteger)hash {
  47. return _documentChange.Hash();
  48. }
  49. - (FIRDocumentChangeType)type {
  50. switch (_documentChange.type()) {
  51. case DocumentChange::Type::Added:
  52. return FIRDocumentChangeTypeAdded;
  53. case DocumentChange::Type::Modified:
  54. return FIRDocumentChangeTypeModified;
  55. case DocumentChange::Type::Removed:
  56. return FIRDocumentChangeTypeRemoved;
  57. }
  58. HARD_FAIL("Unknown DocumentChange::Type: %s", _documentChange.type());
  59. }
  60. - (FIRQueryDocumentSnapshot *)document {
  61. return [[FIRQueryDocumentSnapshot alloc] initWithSnapshot:_documentChange.document()];
  62. }
  63. - (NSUInteger)oldIndex {
  64. return MakeIndex(_documentChange.old_index());
  65. }
  66. - (NSUInteger)newIndex {
  67. return MakeIndex(_documentChange.new_index());
  68. }
  69. @end
  70. NS_ASSUME_NONNULL_END