OLD | NEW |
| (Empty) |
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. | |
4 | |
5 #ifndef CHROME_BROWSER_SYNC_SYNCABLE_SYNCABLE_ID_H_ | |
6 #define CHROME_BROWSER_SYNC_SYNCABLE_SYNCABLE_ID_H_ | |
7 #pragma once | |
8 | |
9 #include <iosfwd> | |
10 #include <limits> | |
11 #include <sstream> | |
12 #include <string> | |
13 | |
14 #include "base/hash_tables.h" | |
15 | |
16 class MockConnectionManager; | |
17 | |
18 namespace base { | |
19 class StringValue; | |
20 } | |
21 | |
22 namespace sql { | |
23 class Statement; | |
24 } | |
25 | |
26 namespace syncable { | |
27 struct EntryKernel; | |
28 class Id; | |
29 } | |
30 | |
31 namespace syncable { | |
32 | |
33 std::ostream& operator<<(std::ostream& out, const Id& id); | |
34 | |
35 // For historical reasons, 3 concepts got everloaded into the Id: | |
36 // 1. A unique, opaque identifier for the object. | |
37 // 2. Flag specifing whether server know about this object. | |
38 // 3. Flag for root. | |
39 // | |
40 // We originally wrapped an integer for this information, but now we use a | |
41 // string. It will have one of three forms: | |
42 // 1. c<client only opaque id> for client items that have not been committed. | |
43 // 2. r for the root item. | |
44 // 3. s<server provided opaque id> for items that the server knows about. | |
45 class Id { | |
46 public: | |
47 // This constructor will be handy even when we move away from int64s, just | |
48 // for unit tests. | |
49 inline Id() : s_("r") { } | |
50 inline Id(const Id& that) { | |
51 Copy(that); | |
52 } | |
53 inline Id& operator = (const Id& that) { | |
54 Copy(that); | |
55 return *this; | |
56 } | |
57 inline void Copy(const Id& that) { | |
58 this->s_ = that.s_; | |
59 } | |
60 inline bool IsRoot() const { | |
61 return "r" == s_; | |
62 } | |
63 inline bool ServerKnows() const { | |
64 return s_[0] == 's' || s_ == "r"; | |
65 } | |
66 | |
67 // TODO(sync): We could use null here, but to ease conversion we use "r". | |
68 // fix this, this is madness :) | |
69 inline bool IsNull() const { | |
70 return IsRoot(); | |
71 } | |
72 inline void Clear() { | |
73 s_ = "r"; | |
74 } | |
75 inline int compare(const Id& that) const { | |
76 return s_.compare(that.s_); | |
77 } | |
78 inline bool operator == (const Id& that) const { | |
79 return s_ == that.s_; | |
80 } | |
81 inline bool operator != (const Id& that) const { | |
82 return s_ != that.s_; | |
83 } | |
84 inline bool operator < (const Id& that) const { | |
85 return s_ < that.s_; | |
86 } | |
87 inline bool operator > (const Id& that) const { | |
88 return s_ > that.s_; | |
89 } | |
90 | |
91 const std::string& value() const { | |
92 return s_; | |
93 } | |
94 | |
95 // Return the next highest ID in the lexicographic ordering. This is | |
96 // useful for computing upper bounds on std::sets that are ordered | |
97 // by operator<. | |
98 Id GetLexicographicSuccessor() const; | |
99 | |
100 // Note: |lowercase_query| should be passed in as lower case. | |
101 bool ContainsStringCaseInsensitive(const std::string& lowercase_query) const; | |
102 | |
103 // Dumps the ID as a value and returns it. Transfers ownership of | |
104 // the StringValue to the caller. | |
105 base::StringValue* ToValue() const; | |
106 | |
107 // Three functions are used to work with our proto buffers. | |
108 std::string GetServerId() const; | |
109 static Id CreateFromServerId(const std::string& server_id); | |
110 // This should only be used if you get back a reference to a local | |
111 // id from the server. Returns a client only opaque id. | |
112 static Id CreateFromClientString(const std::string& local_id); | |
113 | |
114 // This method returns an ID that will compare less than any valid ID. | |
115 // The returned ID is not a valid ID itself. This is useful for | |
116 // computing lower bounds on std::sets that are ordered by operator<. | |
117 static Id GetLeastIdForLexicographicComparison(); | |
118 | |
119 private: | |
120 friend EntryKernel* UnpackEntry(sql::Statement* statement); | |
121 friend void BindFields(const EntryKernel& entry, | |
122 sql::Statement* statement); | |
123 friend std::ostream& operator<<(std::ostream& out, const Id& id); | |
124 friend class MockConnectionManager; | |
125 friend class SyncableIdTest; | |
126 | |
127 std::string s_; | |
128 }; | |
129 | |
130 Id GetNullId(); | |
131 | |
132 } // namespace syncable | |
133 | |
134 #endif // CHROME_BROWSER_SYNC_SYNCABLE_SYNCABLE_ID_H_ | |
OLD | NEW |