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.
5 #ifndef SYNC_SYNCABLE_SYNCABLE_ID_H_
6 #define SYNC_SYNCABLE_SYNCABLE_ID_H_
13 #include "base/containers/hash_tables.h"
14 #include "base/memory/scoped_ptr.h"
15 #include "sync/base/sync_export.h"
17 class MockConnectionManager
;
32 SYNC_EXPORT_PRIVATE
std::ostream
& operator<<(std::ostream
& out
, const Id
& id
);
34 // For historical reasons, 3 concepts got everloaded into the Id:
35 // 1. A unique, opaque identifier for the object.
36 // 2. Flag specifing whether server know about this object.
39 // We originally wrapped an integer for this information, but now we use a
40 // string. It will have one of three forms:
41 // 1. c<client only opaque id> for client items that have not been committed.
42 // 2. r for the root item.
43 // 3. s<server provided opaque id> for items that the server knows about.
44 class SYNC_EXPORT Id
{
47 inline Id(const Id
& that
) {
50 inline Id
& operator = (const Id
& that
) {
54 inline void Copy(const Id
& that
) {
57 inline bool IsRoot() const {
60 inline bool ServerKnows() const {
61 return !IsNull() && (s_
[0] == 's' || s_
== "r");
64 inline bool IsNull() const { return s_
.empty(); }
65 inline void Clear() { s_
.clear(); }
66 inline int compare(const Id
& that
) const {
67 return s_
.compare(that
.s_
);
69 inline bool operator == (const Id
& that
) const {
72 inline bool operator != (const Id
& that
) const {
75 inline bool operator < (const Id
& that
) const {
78 inline bool operator > (const Id
& that
) const {
82 const std::string
& value() const {
86 // Return the next highest ID in the lexicographic ordering. This is
87 // useful for computing upper bounds on std::sets that are ordered
89 Id
GetLexicographicSuccessor() const;
91 // Dumps the ID as a value and returns it. Transfers ownership of
92 // the StringValue to the caller.
93 base::StringValue
* ToValue() const;
95 // Three functions are used to work with our proto buffers.
96 std::string
GetServerId() const;
97 static Id
CreateFromServerId(const std::string
& server_id
);
98 // This should only be used if you get back a reference to a local
99 // id from the server. Returns a client only opaque id.
100 static Id
CreateFromClientString(const std::string
& local_id
);
102 // This method returns an ID that will compare less than any valid ID.
103 // The returned ID is not a valid ID itself. This is useful for
104 // computing lower bounds on std::sets that are ordered by operator<.
105 static Id
GetLeastIdForLexicographicComparison();
111 friend scoped_ptr
<EntryKernel
> UnpackEntry(sql::Statement
* statement
);
112 friend void BindFields(const EntryKernel
& entry
,
113 sql::Statement
* statement
);
114 SYNC_EXPORT_PRIVATE
friend std::ostream
& operator<<(std::ostream
& out
,
116 friend class MockConnectionManager
;
117 friend class SyncableIdTest
;
122 } // namespace syncable
123 } // namespace syncer
125 #endif // SYNC_SYNCABLE_SYNCABLE_ID_H_