Generate all extension schema namespaces as "api" and instead vary the generated...
[chromium-blink-merge.git] / sync / syncable / syncable_id.h
blob932d2e6f859d1ab8baf60ecb9c6f85f97c65acb6
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_
8 #include <iosfwd>
9 #include <limits>
10 #include <sstream>
11 #include <string>
13 #include "base/containers/hash_tables.h"
14 #include "base/memory/scoped_ptr.h"
15 #include "sync/base/sync_export.h"
17 namespace base {
18 class StringValue;
21 namespace sql {
22 class Statement;
25 namespace syncer {
26 namespace syncable {
27 struct EntryKernel;
28 class Id;
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.
35 // 3. Flag for root.
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);
48 inline Id& operator = (const Id& that) {
49 Copy(that);
50 return *this;
52 inline void Copy(const Id& that) {
53 this->s_ = that.s_;
55 inline bool IsRoot() const {
56 return "r" == s_;
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 {
68 return s_ == that.s_;
70 inline bool operator != (const Id& that) const {
71 return s_ != that.s_;
73 inline bool operator < (const Id& that) const {
74 return s_ < that.s_;
76 inline bool operator > (const Id& that) const {
77 return s_ > that.s_;
80 const std::string& value() const {
81 return s_;
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;
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();
105 // Gets root ID.
106 static Id GetRoot();
108 private:
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,
113 const Id& id);
114 friend class SyncableIdTest;
116 std::string s_;
119 } // namespace syncable
120 } // namespace syncer
122 #endif // SYNC_SYNCABLE_SYNCABLE_ID_H_