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_
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"
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
) {
28 String(const char* chars
, size_t num_chars
)
29 : value_(chars
, num_chars
),
33 String(const char chars
[N
]) : value_(chars
, N
-1), is_null_(false) {}
36 static String
From(const U
& other
) {
37 return TypeConverter
<String
, U
>::ConvertFrom(other
);
42 return TypeConverter
<String
, U
>::ConvertTo(*this);
45 String
& operator=(const std::string
& str
) {
50 String
& operator=(const char* chars
) {
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
) {
88 typedef std::string
String::*Testable
;
91 operator Testable() const { return is_null_
? 0 : &String::value_
; }
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<,<=,>,>=
114 class TypeConverter
<String
, std::string
> {
116 static String
ConvertFrom(const std::string
& input
) {
117 return String(input
);
119 static std::string
ConvertTo(const String
& input
) {
125 class TypeConverter
<String
, char[N
]> {
127 static String
ConvertFrom(const char input
[N
]) {
129 return String(input
, N
-1);
135 class TypeConverter
<String
, const char[N
]> {
137 static String
ConvertFrom(const char input
[N
]) {
139 return String(input
, N
-1);
144 class TypeConverter
<String
, const char*> {
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
);
154 #endif // MOJO_PUBLIC_CPP_BINDINGS_STRING_H_