document_change.h 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. /*
  2. * Copyright 2019 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. #ifndef FIRESTORE_CORE_SRC_API_DOCUMENT_CHANGE_H_
  17. #define FIRESTORE_CORE_SRC_API_DOCUMENT_CHANGE_H_
  18. #include <memory>
  19. #include <utility>
  20. #include "Firestore/core/src/api/document_snapshot.h"
  21. namespace firebase {
  22. namespace firestore {
  23. namespace api {
  24. class DocumentChange {
  25. public:
  26. enum class Type { Added, Modified, Removed };
  27. DocumentChange() = default;
  28. DocumentChange(Type type,
  29. DocumentSnapshot document,
  30. size_t old_index,
  31. size_t new_index)
  32. : type_(type),
  33. document_(std::move(document)),
  34. old_index_(old_index),
  35. new_index_(new_index) {
  36. }
  37. size_t Hash() const;
  38. Type type() const {
  39. return type_;
  40. }
  41. DocumentSnapshot document() const {
  42. return document_;
  43. }
  44. size_t old_index() const {
  45. return old_index_;
  46. }
  47. size_t new_index() const {
  48. return new_index_;
  49. }
  50. const std::shared_ptr<Firestore>& firestore() const {
  51. return document_.firestore();
  52. }
  53. /**
  54. * A sentinel return value for old_index() and new_index() indicating that
  55. * there's no relevant index to return because the document was newly added
  56. * or removed respectively.
  57. */
  58. static constexpr size_t npos = static_cast<size_t>(-1);
  59. private:
  60. Type type_;
  61. DocumentSnapshot document_;
  62. size_t old_index_;
  63. size_t new_index_;
  64. };
  65. bool operator==(const DocumentChange& lhs, const DocumentChange& rhs);
  66. } // namespace api
  67. } // namespace firestore
  68. } // namespace firebase
  69. #endif // FIRESTORE_CORE_SRC_API_DOCUMENT_CHANGE_H_