Home | History | Annotate | Download | only in bindings
      1 // Copyright 2014 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 MOJO_PUBLIC_CPP_BINDINGS_STRING_H_
      6 #define MOJO_PUBLIC_CPP_BINDINGS_STRING_H_
      7 
      8 #include <assert.h>
      9 
     10 #include <string>
     11 
     12 #include "mojo/public/cpp/bindings/lib/array_internal.h"
     13 #include "mojo/public/cpp/bindings/type_converter.h"
     14 #include "mojo/public/cpp/system/macros.h"
     15 
     16 namespace mojo {
     17 
     18 class String {
     19  public:
     20   typedef internal::String_Data Data_;
     21 
     22   String() : is_null_(true) {}
     23   String(const std::string& str) : value_(str), is_null_(false) {}
     24   String(const char* chars) : is_null_(!chars) {
     25     if (chars)
     26       value_ = chars;
     27   }
     28   String(const char* chars, size_t num_chars)
     29       : value_(chars, num_chars),
     30         is_null_(false) {
     31   }
     32   template <size_t N>
     33   String(const char chars[N]) : value_(chars, N-1), is_null_(false) {}
     34 
     35   template <typename U>
     36   static String From(const U& other) {
     37     return TypeConverter<String, U>::ConvertFrom(other);
     38   }
     39 
     40   template <typename U>
     41   U To() const {
     42     return TypeConverter<String, U>::ConvertTo(*this);
     43   }
     44 
     45   String& operator=(const std::string& str) {
     46     value_ = str;
     47     is_null_ = false;
     48     return *this;
     49   }
     50   String& operator=(const char* chars) {
     51     is_null_ = !chars;
     52     if (chars) {
     53       value_ = chars;
     54     } else {
     55       value_.clear();
     56     }
     57     return *this;
     58   }
     59 
     60   void reset() {
     61     value_.clear();
     62     is_null_ = true;
     63   }
     64 
     65   bool is_null() const { return is_null_; }
     66 
     67   size_t size() const { return value_.size(); }
     68 
     69   const char* data() const { return value_.data(); }
     70 
     71   const char& at(size_t offset) const { return value_.at(offset); }
     72   const char& operator[](size_t offset) const { return value_[offset]; }
     73 
     74   const std::string& get() const { return value_; }
     75   operator const std::string&() const { return value_; }
     76 
     77   void Swap(String* other) {
     78     std::swap(is_null_, other->is_null_);
     79     value_.swap(other->value_);
     80   }
     81 
     82   void Swap(std::string* other) {
     83     is_null_ = false;
     84     value_.swap(*other);
     85   }
     86 
     87  private:
     88   typedef std::string String::*Testable;
     89 
     90  public:
     91   operator Testable() const { return is_null_ ? 0 : &String::value_; }
     92 
     93  private:
     94   std::string value_;
     95   bool is_null_;
     96 };
     97 
     98 inline bool operator==(const String& a, const String& b) {
     99   return a.is_null() == b.is_null() && a.get() == b.get();
    100 }
    101 inline bool operator==(const char* a, const String& b) {
    102   return !b.is_null() && a == b.get();
    103 }
    104 inline bool operator==(const String& a, const char* b) {
    105   return !a.is_null() && a.get() == b;
    106 }
    107 inline bool operator!=(const String& a, const String& b) { return !(a == b); }
    108 inline bool operator!=(const char* a, const String& b) { return !(a == b); }
    109 inline bool operator!=(const String& a, const char* b) { return !(a == b); }
    110 
    111 // TODO(darin): Add similar variants of operator<,<=,>,>=
    112 
    113 template <>
    114 class TypeConverter<String, std::string> {
    115  public:
    116   static String ConvertFrom(const std::string& input) {
    117     return String(input);
    118   }
    119   static std::string ConvertTo(const String& input) {
    120     return input;
    121   }
    122 };
    123 
    124 template <size_t N>
    125 class TypeConverter<String, char[N]> {
    126  public:
    127   static String ConvertFrom(const char input[N]) {
    128     assert(input);
    129     return String(input, N-1);
    130   }
    131 };
    132 
    133 // Appease MSVC.
    134 template <size_t N>
    135 class TypeConverter<String, const char[N]> {
    136  public:
    137   static String ConvertFrom(const char input[N]) {
    138     assert(input);
    139     return String(input, N-1);
    140   }
    141 };
    142 
    143 template <>
    144 class TypeConverter<String, const char*> {
    145  public:
    146   // |input| may be null, in which case a null String will be returned.
    147   static String ConvertFrom(const char* input) {
    148     return String(input);
    149   }
    150 };
    151 
    152 }  // namespace mojo
    153 
    154 #endif  // MOJO_PUBLIC_CPP_BINDINGS_STRING_H_
    155