client_tag_hash.h 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. // Copyright 2019 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_BASE_CLIENT_TAG_HASH_H_
  5. #define COMPONENTS_SYNC_BASE_CLIENT_TAG_HASH_H_
  6. #include <iosfwd>
  7. #include <string>
  8. #include "base/hash/hash.h"
  9. #include "components/sync/base/model_type.h"
  10. namespace syncer {
  11. // Represents a client defined unique hash for sync entities. Hash is derived
  12. // from client tag, and should be used as |client_defined_unique_tag| for
  13. // SyncEntity at least for CommitMessages. For convenience it supports storing
  14. // in ordered stl containers, logging and equality comparisons. It also supports
  15. // unordered stl containers using ClientTagHash::Hash.
  16. class ClientTagHash {
  17. public:
  18. // For use in std::unordered_map.
  19. struct Hash {
  20. size_t operator()(const ClientTagHash& client_tag_hash) const {
  21. return base::FastHash(client_tag_hash.value());
  22. }
  23. };
  24. // Creates ClientTagHash based on |client_tag|.
  25. static ClientTagHash FromUnhashed(ModelType type,
  26. const std::string& client_tag);
  27. // Creates ClientTagHash from already hashed client tag.
  28. static ClientTagHash FromHashed(std::string hash_value);
  29. ClientTagHash();
  30. ClientTagHash(const ClientTagHash& other);
  31. ClientTagHash(ClientTagHash&& other);
  32. ~ClientTagHash();
  33. ClientTagHash& operator=(const ClientTagHash& other);
  34. ClientTagHash& operator=(ClientTagHash&& other);
  35. const std::string& value() const { return value_; }
  36. size_t EstimateMemoryUsage() const;
  37. private:
  38. explicit ClientTagHash(std::string value);
  39. std::string value_;
  40. };
  41. bool operator<(const ClientTagHash& lhs, const ClientTagHash& rhs);
  42. bool operator==(const ClientTagHash& lhs, const ClientTagHash& rhs);
  43. bool operator!=(const ClientTagHash& lhs, const ClientTagHash& rhs);
  44. std::ostream& operator<<(std::ostream& os,
  45. const ClientTagHash& client_tag_hash);
  46. } // namespace syncer
  47. #endif // COMPONENTS_SYNC_BASE_CLIENT_TAG_HASH_H_