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"
30 SYNC_EXPORT_PRIVATE
std::ostream
& operator<<(std::ostream
& out
, const Id
& id
);
32 // For historical reasons, 3 concepts got everloaded into the Id:
33 // 1. A unique, opaque identifier for the object.
34 // 2. Flag specifing whether server know about this object.
37 // We originally wrapped an integer for this information, but now we use a
38 // string. It will have one of three forms:
39 // 1. c<client only opaque id> for client items that have not been committed.
40 // 2. r for the root item.
41 // 3. s<server provided opaque id> for items that the server knows about.
42 class SYNC_EXPORT Id
{
45 inline Id(const Id
& that
) {
48 inline Id
& operator = (const Id
& that
) {
52 inline void Copy(const Id
& that
) {
55 inline bool IsRoot() const {
58 inline bool ServerKnows() const {
59 return !IsNull() && (s_
[0] == 's' || s_
== "r");
62 inline bool IsNull() const { return s_
.empty(); }
63 inline void Clear() { s_
.clear(); }
64 inline int compare(const Id
& that
) const {
65 return s_
.compare(that
.s_
);
67 inline bool operator == (const Id
& that
) const {
70 inline bool operator != (const Id
& that
) const {
73 inline bool operator < (const Id
& that
) const {
76 inline bool operator > (const Id
& that
) const {
80 const std::string
& value() const {
84 // Return the next highest ID in the lexicographic ordering. This is
85 // useful for computing upper bounds on std::sets that are ordered
87 Id
GetLexicographicSuccessor() const;
89 // Dumps the ID as a value and returns it. Transfers ownership of
90 // the StringValue to the caller.
91 base::StringValue
* ToValue() const;
93 // Three functions are used to work with our proto buffers.
94 std::string
GetServerId() const;
95 static Id
CreateFromServerId(const std::string
& server_id
);
96 // This should only be used if you get back a reference to a local
97 // id from the server. Returns a client only opaque id.
98 static Id
CreateFromClientString(const std::string
& local_id
);
100 // This method returns an ID that will compare less than any valid ID.
101 // The returned ID is not a valid ID itself. This is useful for
102 // computing lower bounds on std::sets that are ordered by operator<.
103 static Id
GetLeastIdForLexicographicComparison();
109 friend scoped_ptr
<EntryKernel
> UnpackEntry(sql::Statement
* statement
);
110 friend void BindFields(const EntryKernel
& entry
,
111 sql::Statement
* statement
);
112 SYNC_EXPORT_PRIVATE
friend std::ostream
& operator<<(std::ostream
& out
,
114 friend class SyncableIdTest
;
119 } // namespace syncable
120 } // namespace syncer
122 #endif // SYNC_SYNCABLE_SYNCABLE_ID_H_