OLD | NEW |
| (Empty) |
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. | |
4 | |
5 #ifndef SYNC_SYNCABLE_SYNCABLE_ID_H_ | |
6 #define SYNC_SYNCABLE_SYNCABLE_ID_H_ | |
7 | |
8 #include <iosfwd> | |
9 #include <limits> | |
10 #include <memory> | |
11 #include <sstream> | |
12 #include <string> | |
13 | |
14 #include "base/containers/hash_tables.h" | |
15 #include "sync/base/sync_export.h" | |
16 | |
17 namespace base { | |
18 class StringValue; | |
19 } | |
20 | |
21 namespace sql { | |
22 class Statement; | |
23 } | |
24 | |
25 namespace syncer { | |
26 namespace syncable { | |
27 struct EntryKernel; | |
28 class Id; | |
29 | |
30 SYNC_EXPORT std::ostream& operator<<(std::ostream& out, const Id& id); | |
31 | |
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. | |
35 // 3. Flag for root. | |
36 // | |
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 { | |
43 public: | |
44 inline Id() {} | |
45 inline Id(const Id& that) { | |
46 Copy(that); | |
47 } | |
48 inline Id& operator = (const Id& that) { | |
49 Copy(that); | |
50 return *this; | |
51 } | |
52 inline void Copy(const Id& that) { | |
53 this->s_ = that.s_; | |
54 } | |
55 inline bool IsRoot() const { | |
56 return "r" == s_; | |
57 } | |
58 inline bool ServerKnows() const { | |
59 return !IsNull() && (s_[0] == 's' || s_ == "r"); | |
60 } | |
61 | |
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_); | |
66 } | |
67 inline bool operator == (const Id& that) const { | |
68 return s_ == that.s_; | |
69 } | |
70 inline bool operator != (const Id& that) const { | |
71 return s_ != that.s_; | |
72 } | |
73 inline bool operator < (const Id& that) const { | |
74 return s_ < that.s_; | |
75 } | |
76 inline bool operator > (const Id& that) const { | |
77 return s_ > that.s_; | |
78 } | |
79 | |
80 const std::string& value() const { | |
81 return s_; | |
82 } | |
83 | |
84 // Return the next highest ID in the lexicographic ordering. This is | |
85 // useful for computing upper bounds on std::sets that are ordered | |
86 // by operator<. | |
87 Id GetLexicographicSuccessor() const; | |
88 | |
89 // Dumps the ID as a value and returns it. Transfers ownership of | |
90 // the StringValue to the caller. | |
91 base::StringValue* ToValue() const; | |
92 | |
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); | |
99 | |
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(); | |
104 | |
105 // Gets root ID. | |
106 static Id GetRoot(); | |
107 | |
108 private: | |
109 friend std::unique_ptr<EntryKernel> UnpackEntry(sql::Statement* statement, | |
110 int* total_created_entries); | |
111 friend void BindFields(const EntryKernel& entry, | |
112 sql::Statement* statement); | |
113 SYNC_EXPORT friend std::ostream& operator<<(std::ostream& out, const Id& id); | |
114 friend class SyncableIdTest; | |
115 | |
116 std::string s_; | |
117 }; | |
118 | |
119 } // namespace syncable | |
120 } // namespace syncer | |
121 | |
122 #endif // SYNC_SYNCABLE_SYNCABLE_ID_H_ | |
OLD | NEW |