| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "sync/syncable/syncable_id.h" | 5 #include "sync/syncable/syncable_id.h" |
| 6 | 6 |
| 7 #include <iosfwd> | 7 #include <iosfwd> |
| 8 | 8 |
| 9 #include "base/logging.h" |
| 9 #include "base/values.h" | 10 #include "base/values.h" |
| 10 | 11 |
| 11 using std::ostream; | 12 using std::ostream; |
| 12 using std::string; | 13 using std::string; |
| 13 | 14 |
| 14 namespace syncer { | 15 namespace syncer { |
| 15 namespace syncable { | 16 namespace syncable { |
| 16 | 17 |
| 17 ostream& operator<<(ostream& out, const Id& id) { | 18 ostream& operator<<(ostream& out, const Id& id) { |
| 18 out << id.s_; | 19 out << id.s_; |
| 19 return out; | 20 return out; |
| 20 } | 21 } |
| 21 | 22 |
| 22 base::StringValue* Id::ToValue() const { | 23 base::StringValue* Id::ToValue() const { |
| 23 return new base::StringValue(s_); | 24 return new base::StringValue(s_); |
| 24 } | 25 } |
| 25 | 26 |
| 26 string Id::GetServerId() const { | 27 string Id::GetServerId() const { |
| 27 // Currently root is the string "0". We need to decide on a true value. | 28 // Currently root is the string "0". We need to decide on a true value. |
| 28 // "" would be convenient here, as the IsRoot call would not be needed. | 29 // "" would be convenient here, as the IsRoot call would not be needed. |
| 30 DCHECK(!IsNull()); |
| 29 if (IsRoot()) | 31 if (IsRoot()) |
| 30 return "0"; | 32 return "0"; |
| 31 return s_.substr(1); | 33 return s_.substr(1); |
| 32 } | 34 } |
| 33 | 35 |
| 34 Id Id::CreateFromServerId(const string& server_id) { | 36 Id Id::CreateFromServerId(const string& server_id) { |
| 35 Id id; | 37 Id id; |
| 36 if (server_id == "0") | 38 if (server_id == "0") |
| 37 id.s_ = "r"; | 39 id.s_ = "r"; |
| 38 else | 40 else |
| 39 id.s_ = string("s") + server_id; | 41 id.s_ = string("s") + server_id; |
| 40 return id; | 42 return id; |
| 41 } | 43 } |
| 42 | 44 |
| 43 Id Id::CreateFromClientString(const string& local_id) { | 45 Id Id::CreateFromClientString(const string& local_id) { |
| 44 Id id; | 46 Id id; |
| 45 if (local_id == "0") | 47 if (local_id == "0") |
| 46 id.s_ = "r"; | 48 id.s_ = "r"; |
| 47 else | 49 else |
| 48 id.s_ = string("c") + local_id; | 50 id.s_ = string("c") + local_id; |
| 49 return id; | 51 return id; |
| 50 } | 52 } |
| 51 | 53 |
| 54 Id Id::GetRoot() { |
| 55 Id id; |
| 56 id.s_ = "r"; |
| 57 return id; |
| 58 } |
| 59 |
| 52 Id Id::GetLexicographicSuccessor() const { | 60 Id Id::GetLexicographicSuccessor() const { |
| 53 // The successor of a string is given by appending the least | 61 // The successor of a string is given by appending the least |
| 54 // character in the alphabet. | 62 // character in the alphabet. |
| 55 Id id = *this; | 63 Id id = *this; |
| 56 id.s_.push_back(0); | 64 id.s_.push_back(0); |
| 57 return id; | 65 return id; |
| 58 } | 66 } |
| 59 | 67 |
| 60 // static | 68 // static |
| 61 Id Id::GetLeastIdForLexicographicComparison() { | 69 Id Id::GetLeastIdForLexicographicComparison() { |
| 62 Id id; | 70 Id id; |
| 63 id.s_.clear(); | 71 id.s_.clear(); |
| 64 return id; | 72 return id; |
| 65 } | 73 } |
| 66 | 74 |
| 67 Id GetNullId() { | |
| 68 return Id(); // Currently == root. | |
| 69 } | |
| 70 | |
| 71 } // namespace syncable | 75 } // namespace syncable |
| 72 } // namespace syncer | 76 } // namespace syncer |
| OLD | NEW |