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
{
46 // This constructor will be handy even when we move away from int64s, just
48 inline Id() : s_("r") { }
49 inline Id(const Id
& that
) {
52 inline Id
& operator = (const Id
& that
) {
56 inline void Copy(const Id
& that
) {
59 inline bool IsRoot() const {
62 inline bool ServerKnows() const {
63 return s_
[0] == 's' || s_
== "r";
66 // TODO(sync): We could use null here, but to ease conversion we use "r".
67 // fix this, this is madness :)
68 inline bool IsNull() const {
74 inline int compare(const Id
& that
) const {
75 return s_
.compare(that
.s_
);
77 inline bool operator == (const Id
& that
) const {
80 inline bool operator != (const Id
& that
) const {
83 inline bool operator < (const Id
& that
) const {
86 inline bool operator > (const Id
& that
) const {
90 const std::string
& value() const {
94 // Return the next highest ID in the lexicographic ordering. This is
95 // useful for computing upper bounds on std::sets that are ordered
97 Id
GetLexicographicSuccessor() const;
99 // Dumps the ID as a value and returns it. Transfers ownership of
100 // the StringValue to the caller.
101 base::StringValue
* ToValue() const;
103 // Three functions are used to work with our proto buffers.
104 std::string
GetServerId() const;
105 static Id
CreateFromServerId(const std::string
& server_id
);
106 // This should only be used if you get back a reference to a local
107 // id from the server. Returns a client only opaque id.
108 static Id
CreateFromClientString(const std::string
& local_id
);
110 // This method returns an ID that will compare less than any valid ID.
111 // The returned ID is not a valid ID itself. This is useful for
112 // computing lower bounds on std::sets that are ordered by operator<.
113 static Id
GetLeastIdForLexicographicComparison();
116 friend scoped_ptr
<EntryKernel
> UnpackEntry(sql::Statement
* statement
);
117 friend void BindFields(const EntryKernel
& entry
,
118 sql::Statement
* statement
);
119 SYNC_EXPORT_PRIVATE
friend std::ostream
& operator<<(std::ostream
& out
,
121 friend class MockConnectionManager
;
122 friend class SyncableIdTest
;
127 SYNC_EXPORT_PRIVATE Id
GetNullId();
129 } // namespace syncable
130 } // namespace syncer
132 #endif // SYNC_SYNCABLE_SYNCABLE_ID_H_