Extract SIGPIPE ignoring code to a common place.
[chromium-blink-merge.git] / sync / syncable / syncable_id.h
blob64fc7f695fe3ec3ec884e25fa6c54e8a1b2523f9
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_
8 #include <iosfwd>
9 #include <limits>
10 #include <sstream>
11 #include <string>
13 #include "base/memory/scoped_ptr.h"
14 #include "base/hash_tables.h"
16 class MockConnectionManager;
18 namespace base {
19 class StringValue;
22 namespace sql {
23 class Statement;
26 namespace syncer {
27 namespace syncable {
28 struct EntryKernel;
29 class Id;
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.
36 // 3. Flag for root.
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.
43 class Id {
44 public:
45 // This constructor will be handy even when we move away from int64s, just
46 // for unit tests.
47 inline Id() : s_("r") { }
48 inline Id(const Id& that) {
49 Copy(that);
51 inline Id& operator = (const Id& that) {
52 Copy(that);
53 return *this;
55 inline void Copy(const Id& that) {
56 this->s_ = that.s_;
58 inline bool IsRoot() const {
59 return "r" == s_;
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 {
68 return IsRoot();
70 inline void Clear() {
71 s_ = "r";
73 inline int compare(const Id& that) const {
74 return s_.compare(that.s_);
76 inline bool operator == (const Id& that) const {
77 return s_ == that.s_;
79 inline bool operator != (const Id& that) const {
80 return s_ != that.s_;
82 inline bool operator < (const Id& that) const {
83 return s_ < that.s_;
85 inline bool operator > (const Id& that) const {
86 return s_ > that.s_;
89 const std::string& value() const {
90 return s_;
93 // Return the next highest ID in the lexicographic ordering. This is
94 // useful for computing upper bounds on std::sets that are ordered
95 // by operator<.
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();
114 private:
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;
122 std::string s_;
125 Id GetNullId();
127 } // namespace syncable
128 } // namespace syncer
130 #endif // SYNC_SYNCABLE_SYNCABLE_ID_H_