OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2009 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 |
| 8 #include <iosfwd> |
| 9 #include <limits> |
| 10 #include <sstream> |
| 11 #include <string> |
| 12 |
| 13 #include "base/hash_tables.h" |
| 14 #include "chrome/browser/sync/util/fast_dump.h" |
| 15 #include "chrome/browser/sync/util/sync_types.h" |
| 16 |
| 17 extern "C" { |
| 18 struct sqlite3; |
| 19 struct sqlite3_stmt; |
| 20 } |
| 21 |
| 22 namespace syncable { |
| 23 class Id; |
| 24 } // namespace syncable |
| 25 |
| 26 class MockConnectionManager; |
| 27 |
| 28 sqlite3_stmt* BindArg(sqlite3_stmt*, const syncable::Id&, int index); |
| 29 void GetColumn(sqlite3_stmt*, int index, syncable::Id* value); |
| 30 std::ostream& operator << (std::ostream& out, const syncable::Id& id); |
| 31 browser_sync::FastDump& operator << |
| 32 (browser_sync::FastDump& out, const syncable::Id& id); |
| 33 |
| 34 namespace syncable { |
| 35 |
| 36 // For historical reasons, 3 concepts got everloaded into the Id: |
| 37 // 1. A unique, opaque identifier for the object. |
| 38 // 2. Flag specifing whether server know about this object. |
| 39 // 3. Flag for root. |
| 40 // |
| 41 // We originally wrapped an integer for this information, but now we use a |
| 42 // string. It will have one of three forms: |
| 43 // 1. c<client only opaque id> for client items that have not been committed. |
| 44 // 2. r for the root item. |
| 45 // 3. s<server provided opaque id> for items that the server knows about. |
| 46 class Id { |
| 47 friend sqlite3_stmt* ::BindArg(sqlite3_stmt*, const syncable::Id&, int index); |
| 48 friend void ::GetColumn(sqlite3_stmt*, int index, syncable::Id* value); |
| 49 friend std::ostream& ::operator << (std::ostream& out, |
| 50 const syncable::Id& id); |
| 51 friend browser_sync::FastDump& ::operator << |
| 52 (browser_sync::FastDump& out, const syncable::Id& id); |
| 53 friend class MockConnectionManager; |
| 54 friend class SyncableIdTest; |
| 55 public: |
| 56 // This constructor will be handy even when we move away from |
| 57 // int64s, just for unit tests. |
| 58 inline Id() : s_("r") { } |
| 59 inline Id(const Id& that) { |
| 60 Copy(that); |
| 61 } |
| 62 inline Id& operator = (const Id& that) { |
| 63 Copy(that); |
| 64 return *this; |
| 65 } |
| 66 inline void Copy(const Id& that) { |
| 67 this->s_ = that.s_; |
| 68 } |
| 69 inline bool IsRoot() const { |
| 70 return "r" == s_; |
| 71 } |
| 72 inline bool ServerKnows() const { |
| 73 return s_[0] == 's' || s_ == "r"; |
| 74 } |
| 75 |
| 76 // TODO(sync): We could use null here, but to ease conversion we use "r". |
| 77 // fix this, this is madness :) |
| 78 inline bool IsNull() const { |
| 79 return IsRoot(); |
| 80 } |
| 81 inline void Clear() { |
| 82 s_ = "r"; |
| 83 } |
| 84 std::string AsQueryParam() const; |
| 85 // Must never allow id == 0 or id < 0 to compile. |
| 86 inline bool operator == (const Id& that) const { |
| 87 return s_ == that.s_; |
| 88 } |
| 89 inline bool operator != (const Id& that) const { |
| 90 return s_ != that.s_; |
| 91 } |
| 92 inline bool operator < (const Id& that) const { |
| 93 return s_ < that.s_; |
| 94 } |
| 95 inline bool operator > (const Id& that) const { |
| 96 return s_ > that.s_; |
| 97 } |
| 98 |
| 99 public: |
| 100 // Three functions used to work with our proto buffers. |
| 101 std::string GetServerId() const; |
| 102 static Id CreateFromServerId(const std::string& server_id); |
| 103 // This should only be used if you get back a reference to a local |
| 104 // id from the server. Returns a client only opaque id. |
| 105 static Id CreateFromClientString(const std::string& local_id); |
| 106 protected: |
| 107 std::string s_; |
| 108 }; |
| 109 |
| 110 extern const Id kNullId; |
| 111 |
| 112 } // namespace syncable |
| 113 |
| 114 #endif // CHROME_BROWSER_SYNC_SYNCABLE_SYNCABLE_ID_H_ |
OLD | NEW |