Temporarily re-enabling SizeAfterPrefChange test with traces.
[chromium-blink-merge.git] / mojo / public / cpp / bindings / string.h
bloba242784497531e8e6310fe175dcd1a7ef2e23d17
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.
5 #ifndef MOJO_PUBLIC_CPP_BINDINGS_STRING_H_
6 #define MOJO_PUBLIC_CPP_BINDINGS_STRING_H_
8 #include <assert.h>
10 #include <string>
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"
16 namespace mojo {
18 class String {
19 public:
20 typedef internal::String_Data Data_;
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;
28 String(const char* chars, size_t num_chars)
29 : value_(chars, num_chars),
30 is_null_(false) {
32 template <size_t N>
33 String(const char chars[N]) : value_(chars, N-1), is_null_(false) {}
35 template <typename U>
36 static String From(const U& other) {
37 return TypeConverter<String, U>::ConvertFrom(other);
40 template <typename U>
41 U To() const {
42 return TypeConverter<String, U>::ConvertTo(*this);
45 String& operator=(const std::string& str) {
46 value_ = str;
47 is_null_ = false;
48 return *this;
50 String& operator=(const char* chars) {
51 is_null_ = !chars;
52 if (chars) {
53 value_ = chars;
54 } else {
55 value_.clear();
57 return *this;
60 void reset() {
61 value_.clear();
62 is_null_ = true;
65 bool is_null() const { return is_null_; }
67 size_t size() const { return value_.size(); }
69 const char* data() const { return value_.data(); }
71 const char& at(size_t offset) const { return value_.at(offset); }
72 const char& operator[](size_t offset) const { return value_[offset]; }
74 const std::string& get() const { return value_; }
75 operator const std::string&() const { return value_; }
77 void Swap(String* other) {
78 std::swap(is_null_, other->is_null_);
79 value_.swap(other->value_);
82 void Swap(std::string* other) {
83 is_null_ = false;
84 value_.swap(*other);
87 private:
88 typedef std::string String::*Testable;
90 public:
91 operator Testable() const { return is_null_ ? 0 : &String::value_; }
93 private:
94 std::string value_;
95 bool is_null_;
98 inline bool operator==(const String& a, const String& b) {
99 return a.is_null() == b.is_null() && a.get() == b.get();
101 inline bool operator==(const char* a, const String& b) {
102 return !b.is_null() && a == b.get();
104 inline bool operator==(const String& a, const char* b) {
105 return !a.is_null() && a.get() == b;
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); }
111 // TODO(darin): Add similar variants of operator<,<=,>,>=
113 template <>
114 class TypeConverter<String, std::string> {
115 public:
116 static String ConvertFrom(const std::string& input) {
117 return String(input);
119 static std::string ConvertTo(const String& input) {
120 return input;
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);
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);
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);
152 } // namespace mojo
154 #endif // MOJO_PUBLIC_CPP_BINDINGS_STRING_H_