Home | History | Annotate | Download | only in syncable
      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 #include "sync/syncable/syncable_id.h"
      6 
      7 #include <iosfwd>
      8 
      9 #include "base/values.h"
     10 
     11 using std::ostream;
     12 using std::string;
     13 
     14 namespace syncer {
     15 namespace syncable {
     16 
     17 ostream& operator<<(ostream& out, const Id& id) {
     18   out << id.s_;
     19   return out;
     20 }
     21 
     22 base::StringValue* Id::ToValue() const {
     23   return new base::StringValue(s_);
     24 }
     25 
     26 string Id::GetServerId() const {
     27   // 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   if (IsRoot())
     30     return "0";
     31   return s_.substr(1);
     32 }
     33 
     34 Id Id::CreateFromServerId(const string& server_id) {
     35   Id id;
     36   if (server_id == "0")
     37     id.s_ = "r";
     38   else
     39     id.s_ = string("s") + server_id;
     40   return id;
     41 }
     42 
     43 Id Id::CreateFromClientString(const string& local_id) {
     44   Id id;
     45   if (local_id == "0")
     46     id.s_ = "r";
     47   else
     48     id.s_ = string("c") + local_id;
     49   return id;
     50 }
     51 
     52 Id Id::GetLexicographicSuccessor() const {
     53   // The successor of a string is given by appending the least
     54   // character in the alphabet.
     55   Id id = *this;
     56   id.s_.push_back(0);
     57   return id;
     58 }
     59 
     60 // static
     61 Id Id::GetLeastIdForLexicographicComparison() {
     62   Id id;
     63   id.s_.clear();
     64   return id;
     65 }
     66 
     67 Id GetNullId() {
     68   return Id();  // Currently == root.
     69 }
     70 
     71 }  // namespace syncable
     72 }  // namespace syncer
     73