1 // Copyright (c) 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/memory/scoped_ptr.h"
14 #include "base/hash_tables.h"
16 class MockConnectionManager
;
31 std::ostream
& operator<<(std::ostream
& out
, const Id
& id
);
33 // For historical reasons, 3 concepts got everloaded into the Id:
34 // 1. A unique, opaque identifier for the object.
35 // 2. Flag specifing whether server know about this object.
38 // We originally wrapped an integer for this information, but now we use a
39 // string. It will have one of three forms:
40 // 1. c<client only opaque id> for client items that have not been committed.
41 // 2. r for the root item.
42 // 3. s<server provided opaque id> for items that the server knows about.
45 // This constructor will be handy even when we move away from int64s, just
47 inline Id() : s_("r") { }
48 inline Id(const Id
& that
) {
51 inline Id
& operator = (const Id
& that
) {
55 inline void Copy(const Id
& that
) {
58 inline bool IsRoot() const {
61 inline bool ServerKnows() const {
62 return s_
[0] == 's' || s_
== "r";
65 // TODO(sync): We could use null here, but to ease conversion we use "r".
66 // fix this, this is madness :)
67 inline bool IsNull() const {
73 inline int compare(const Id
& that
) const {
74 return s_
.compare(that
.s_
);
76 inline bool operator == (const Id
& that
) const {
79 inline bool operator != (const Id
& that
) const {
82 inline bool operator < (const Id
& that
) const {
85 inline bool operator > (const Id
& that
) const {
89 const std::string
& value() const {
93 // Return the next highest ID in the lexicographic ordering. This is
94 // useful for computing upper bounds on std::sets that are ordered
96 Id
GetLexicographicSuccessor() const;
98 // Dumps the ID as a value and returns it. Transfers ownership of
99 // the StringValue to the caller.
100 base::StringValue
* ToValue() const;
102 // Three functions are used to work with our proto buffers.
103 std::string
GetServerId() const;
104 static Id
CreateFromServerId(const std::string
& server_id
);
105 // This should only be used if you get back a reference to a local
106 // id from the server. Returns a client only opaque id.
107 static Id
CreateFromClientString(const std::string
& local_id
);
109 // This method returns an ID that will compare less than any valid ID.
110 // The returned ID is not a valid ID itself. This is useful for
111 // computing lower bounds on std::sets that are ordered by operator<.
112 static Id
GetLeastIdForLexicographicComparison();
115 friend scoped_ptr
<EntryKernel
> UnpackEntry(sql::Statement
* statement
);
116 friend void BindFields(const EntryKernel
& entry
,
117 sql::Statement
* statement
);
118 friend std::ostream
& operator<<(std::ostream
& out
, const Id
& id
);
119 friend class MockConnectionManager
;
120 friend class SyncableIdTest
;
127 } // namespace syncable
128 } // namespace syncer
130 #endif // SYNC_SYNCABLE_SYNCABLE_ID_H_