sync_change.h 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. // Copyright 2012 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #ifndef COMPONENTS_SYNC_MODEL_SYNC_CHANGE_H_
  5. #define COMPONENTS_SYNC_MODEL_SYNC_CHANGE_H_
  6. #include <iosfwd>
  7. #include <string>
  8. #include <vector>
  9. #include "base/location.h"
  10. #include "components/sync/model/sync_data.h"
  11. namespace syncer {
  12. // A SyncChange object reflects a change to a piece of synced data. The change
  13. // can be either a delete, add, or an update. All data relevant to the change
  14. // is encapsulated within the SyncChange, which, once created, is immutable.
  15. // Note: it is safe and cheap to pass these by value or make copies, as they do
  16. // not create deep copies of their internal data.
  17. class SyncChange {
  18. public:
  19. enum SyncChangeType {
  20. ACTION_ADD,
  21. ACTION_UPDATE,
  22. ACTION_DELETE,
  23. };
  24. // Create a new change with the specified sync data.
  25. SyncChange(const base::Location& from_here,
  26. SyncChangeType change_type,
  27. const SyncData& sync_data);
  28. // Copy constructor and assignment operator welcome.
  29. SyncChange(const SyncChange&) = default;
  30. SyncChange& operator=(const SyncChange&) = default;
  31. // Move constructor and assignment operator allowed (although questionable).
  32. // TODO(crbug.com/1152824): Avoid move semantics if that leads invalid state.
  33. SyncChange(SyncChange&&) = default;
  34. SyncChange& operator=(SyncChange&&) = default;
  35. ~SyncChange();
  36. // Whether this change is valid. This must be true before attempting to access
  37. // the data. It may only return false for moved-away instances (unspecified
  38. // behavior). Otherwise it's guaranteed to return true.
  39. // TODO(crbug.com/1152824): Remove this API once move semantics are removed.
  40. bool IsValid() const;
  41. // Getters.
  42. SyncChangeType change_type() const;
  43. SyncData sync_data() const;
  44. base::Location location() const;
  45. // Returns a string representation of |change_type|.
  46. static std::string ChangeTypeToString(SyncChangeType change_type);
  47. // Returns a string representation of the entire object. Used for gmock
  48. // printing method, PrintTo.
  49. std::string ToString() const;
  50. private:
  51. base::Location location_;
  52. SyncChangeType change_type_;
  53. // An immutable container for the data of this SyncChange. Whenever
  54. // SyncChanges are copied, they copy references to this data.
  55. SyncData sync_data_;
  56. };
  57. // gmock printer helper.
  58. void PrintTo(const SyncChange& sync_change, std::ostream* os);
  59. using SyncChangeList = std::vector<SyncChange>;
  60. } // namespace syncer
  61. #endif // COMPONENTS_SYNC_MODEL_SYNC_CHANGE_H_